dax.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. /*
  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. #include <linux/atomic.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/buffer_head.h>
  19. #include <linux/dax.h>
  20. #include <linux/fs.h>
  21. #include <linux/genhd.h>
  22. #include <linux/highmem.h>
  23. #include <linux/memcontrol.h>
  24. #include <linux/mm.h>
  25. #include <linux/mutex.h>
  26. #include <linux/pagevec.h>
  27. #include <linux/pmem.h>
  28. #include <linux/sched.h>
  29. #include <linux/uio.h>
  30. #include <linux/vmstat.h>
  31. #include <linux/pfn_t.h>
  32. #include <linux/sizes.h>
  33. #include <linux/mmu_notifier.h>
  34. #include <linux/iomap.h>
  35. #include "internal.h"
  36. /* We choose 4096 entries - same as per-zone page wait tables */
  37. #define DAX_WAIT_TABLE_BITS 12
  38. #define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS)
  39. static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES];
  40. static int __init init_dax_wait_table(void)
  41. {
  42. int i;
  43. for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++)
  44. init_waitqueue_head(wait_table + i);
  45. return 0;
  46. }
  47. fs_initcall(init_dax_wait_table);
  48. static long dax_map_atomic(struct block_device *bdev, struct blk_dax_ctl *dax)
  49. {
  50. struct request_queue *q = bdev->bd_queue;
  51. long rc = -EIO;
  52. dax->addr = ERR_PTR(-EIO);
  53. if (blk_queue_enter(q, true) != 0)
  54. return rc;
  55. rc = bdev_direct_access(bdev, dax);
  56. if (rc < 0) {
  57. dax->addr = ERR_PTR(rc);
  58. blk_queue_exit(q);
  59. return rc;
  60. }
  61. return rc;
  62. }
  63. static void dax_unmap_atomic(struct block_device *bdev,
  64. const struct blk_dax_ctl *dax)
  65. {
  66. if (IS_ERR(dax->addr))
  67. return;
  68. blk_queue_exit(bdev->bd_queue);
  69. }
  70. static int dax_is_pmd_entry(void *entry)
  71. {
  72. return (unsigned long)entry & RADIX_DAX_PMD;
  73. }
  74. static int dax_is_pte_entry(void *entry)
  75. {
  76. return !((unsigned long)entry & RADIX_DAX_PMD);
  77. }
  78. static int dax_is_zero_entry(void *entry)
  79. {
  80. return (unsigned long)entry & RADIX_DAX_HZP;
  81. }
  82. static int dax_is_empty_entry(void *entry)
  83. {
  84. return (unsigned long)entry & RADIX_DAX_EMPTY;
  85. }
  86. struct page *read_dax_sector(struct block_device *bdev, sector_t n)
  87. {
  88. struct page *page = alloc_pages(GFP_KERNEL, 0);
  89. struct blk_dax_ctl dax = {
  90. .size = PAGE_SIZE,
  91. .sector = n & ~((((int) PAGE_SIZE) / 512) - 1),
  92. };
  93. long rc;
  94. if (!page)
  95. return ERR_PTR(-ENOMEM);
  96. rc = dax_map_atomic(bdev, &dax);
  97. if (rc < 0)
  98. return ERR_PTR(rc);
  99. memcpy_from_pmem(page_address(page), dax.addr, PAGE_SIZE);
  100. dax_unmap_atomic(bdev, &dax);
  101. return page;
  102. }
  103. /*
  104. * DAX radix tree locking
  105. */
  106. struct exceptional_entry_key {
  107. struct address_space *mapping;
  108. pgoff_t entry_start;
  109. };
  110. struct wait_exceptional_entry_queue {
  111. wait_queue_t wait;
  112. struct exceptional_entry_key key;
  113. };
  114. static wait_queue_head_t *dax_entry_waitqueue(struct address_space *mapping,
  115. pgoff_t index, void *entry, struct exceptional_entry_key *key)
  116. {
  117. unsigned long hash;
  118. /*
  119. * If 'entry' is a PMD, align the 'index' that we use for the wait
  120. * queue to the start of that PMD. This ensures that all offsets in
  121. * the range covered by the PMD map to the same bit lock.
  122. */
  123. if (dax_is_pmd_entry(entry))
  124. index &= ~((1UL << (PMD_SHIFT - PAGE_SHIFT)) - 1);
  125. key->mapping = mapping;
  126. key->entry_start = index;
  127. hash = hash_long((unsigned long)mapping ^ index, DAX_WAIT_TABLE_BITS);
  128. return wait_table + hash;
  129. }
  130. static int wake_exceptional_entry_func(wait_queue_t *wait, unsigned int mode,
  131. int sync, void *keyp)
  132. {
  133. struct exceptional_entry_key *key = keyp;
  134. struct wait_exceptional_entry_queue *ewait =
  135. container_of(wait, struct wait_exceptional_entry_queue, wait);
  136. if (key->mapping != ewait->key.mapping ||
  137. key->entry_start != ewait->key.entry_start)
  138. return 0;
  139. return autoremove_wake_function(wait, mode, sync, NULL);
  140. }
  141. /*
  142. * Check whether the given slot is locked. The function must be called with
  143. * mapping->tree_lock held
  144. */
  145. static inline int slot_locked(struct address_space *mapping, void **slot)
  146. {
  147. unsigned long entry = (unsigned long)
  148. radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
  149. return entry & RADIX_DAX_ENTRY_LOCK;
  150. }
  151. /*
  152. * Mark the given slot is locked. The function must be called with
  153. * mapping->tree_lock held
  154. */
  155. static inline void *lock_slot(struct address_space *mapping, void **slot)
  156. {
  157. unsigned long entry = (unsigned long)
  158. radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
  159. entry |= RADIX_DAX_ENTRY_LOCK;
  160. radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry);
  161. return (void *)entry;
  162. }
  163. /*
  164. * Mark the given slot is unlocked. The function must be called with
  165. * mapping->tree_lock held
  166. */
  167. static inline void *unlock_slot(struct address_space *mapping, void **slot)
  168. {
  169. unsigned long entry = (unsigned long)
  170. radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
  171. entry &= ~(unsigned long)RADIX_DAX_ENTRY_LOCK;
  172. radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry);
  173. return (void *)entry;
  174. }
  175. /*
  176. * Lookup entry in radix tree, wait for it to become unlocked if it is
  177. * exceptional entry and return it. The caller must call
  178. * put_unlocked_mapping_entry() when he decided not to lock the entry or
  179. * put_locked_mapping_entry() when he locked the entry and now wants to
  180. * unlock it.
  181. *
  182. * The function must be called with mapping->tree_lock held.
  183. */
  184. static void *get_unlocked_mapping_entry(struct address_space *mapping,
  185. pgoff_t index, void ***slotp)
  186. {
  187. void *entry, **slot;
  188. struct wait_exceptional_entry_queue ewait;
  189. wait_queue_head_t *wq;
  190. init_wait(&ewait.wait);
  191. ewait.wait.func = wake_exceptional_entry_func;
  192. for (;;) {
  193. entry = __radix_tree_lookup(&mapping->page_tree, index, NULL,
  194. &slot);
  195. if (!entry || !radix_tree_exceptional_entry(entry) ||
  196. !slot_locked(mapping, slot)) {
  197. if (slotp)
  198. *slotp = slot;
  199. return entry;
  200. }
  201. wq = dax_entry_waitqueue(mapping, index, entry, &ewait.key);
  202. prepare_to_wait_exclusive(wq, &ewait.wait,
  203. TASK_UNINTERRUPTIBLE);
  204. spin_unlock_irq(&mapping->tree_lock);
  205. schedule();
  206. finish_wait(wq, &ewait.wait);
  207. spin_lock_irq(&mapping->tree_lock);
  208. }
  209. }
  210. static void dax_unlock_mapping_entry(struct address_space *mapping,
  211. pgoff_t index)
  212. {
  213. void *entry, **slot;
  214. spin_lock_irq(&mapping->tree_lock);
  215. entry = __radix_tree_lookup(&mapping->page_tree, index, NULL, &slot);
  216. if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry) ||
  217. !slot_locked(mapping, slot))) {
  218. spin_unlock_irq(&mapping->tree_lock);
  219. return;
  220. }
  221. unlock_slot(mapping, slot);
  222. spin_unlock_irq(&mapping->tree_lock);
  223. dax_wake_mapping_entry_waiter(mapping, index, entry, false);
  224. }
  225. static void put_locked_mapping_entry(struct address_space *mapping,
  226. pgoff_t index, void *entry)
  227. {
  228. if (!radix_tree_exceptional_entry(entry)) {
  229. unlock_page(entry);
  230. put_page(entry);
  231. } else {
  232. dax_unlock_mapping_entry(mapping, index);
  233. }
  234. }
  235. /*
  236. * Called when we are done with radix tree entry we looked up via
  237. * get_unlocked_mapping_entry() and which we didn't lock in the end.
  238. */
  239. static void put_unlocked_mapping_entry(struct address_space *mapping,
  240. pgoff_t index, void *entry)
  241. {
  242. if (!radix_tree_exceptional_entry(entry))
  243. return;
  244. /* We have to wake up next waiter for the radix tree entry lock */
  245. dax_wake_mapping_entry_waiter(mapping, index, entry, false);
  246. }
  247. /*
  248. * Find radix tree entry at given index. If it points to a page, return with
  249. * the page locked. If it points to the exceptional entry, return with the
  250. * radix tree entry locked. If the radix tree doesn't contain given index,
  251. * create empty exceptional entry for the index and return with it locked.
  252. *
  253. * When requesting an entry with size RADIX_DAX_PMD, grab_mapping_entry() will
  254. * either return that locked entry or will return an error. This error will
  255. * happen if there are any 4k entries (either zero pages or DAX entries)
  256. * within the 2MiB range that we are requesting.
  257. *
  258. * We always favor 4k entries over 2MiB entries. There isn't a flow where we
  259. * evict 4k entries in order to 'upgrade' them to a 2MiB entry. A 2MiB
  260. * insertion will fail if it finds any 4k entries already in the tree, and a
  261. * 4k insertion will cause an existing 2MiB entry to be unmapped and
  262. * downgraded to 4k entries. This happens for both 2MiB huge zero pages as
  263. * well as 2MiB empty entries.
  264. *
  265. * The exception to this downgrade path is for 2MiB DAX PMD entries that have
  266. * real storage backing them. We will leave these real 2MiB DAX entries in
  267. * the tree, and PTE writes will simply dirty the entire 2MiB DAX entry.
  268. *
  269. * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
  270. * persistent memory the benefit is doubtful. We can add that later if we can
  271. * show it helps.
  272. */
  273. static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index,
  274. unsigned long size_flag)
  275. {
  276. bool pmd_downgrade = false; /* splitting 2MiB entry into 4k entries? */
  277. void *entry, **slot;
  278. restart:
  279. spin_lock_irq(&mapping->tree_lock);
  280. entry = get_unlocked_mapping_entry(mapping, index, &slot);
  281. if (entry) {
  282. if (size_flag & RADIX_DAX_PMD) {
  283. if (!radix_tree_exceptional_entry(entry) ||
  284. dax_is_pte_entry(entry)) {
  285. put_unlocked_mapping_entry(mapping, index,
  286. entry);
  287. entry = ERR_PTR(-EEXIST);
  288. goto out_unlock;
  289. }
  290. } else { /* trying to grab a PTE entry */
  291. if (radix_tree_exceptional_entry(entry) &&
  292. dax_is_pmd_entry(entry) &&
  293. (dax_is_zero_entry(entry) ||
  294. dax_is_empty_entry(entry))) {
  295. pmd_downgrade = true;
  296. }
  297. }
  298. }
  299. /* No entry for given index? Make sure radix tree is big enough. */
  300. if (!entry || pmd_downgrade) {
  301. int err;
  302. if (pmd_downgrade) {
  303. /*
  304. * Make sure 'entry' remains valid while we drop
  305. * mapping->tree_lock.
  306. */
  307. entry = lock_slot(mapping, slot);
  308. }
  309. spin_unlock_irq(&mapping->tree_lock);
  310. /*
  311. * Besides huge zero pages the only other thing that gets
  312. * downgraded are empty entries which don't need to be
  313. * unmapped.
  314. */
  315. if (pmd_downgrade && dax_is_zero_entry(entry))
  316. unmap_mapping_range(mapping,
  317. (index << PAGE_SHIFT) & PMD_MASK, PMD_SIZE, 0);
  318. err = radix_tree_preload(
  319. mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM);
  320. if (err) {
  321. if (pmd_downgrade)
  322. put_locked_mapping_entry(mapping, index, entry);
  323. return ERR_PTR(err);
  324. }
  325. spin_lock_irq(&mapping->tree_lock);
  326. if (pmd_downgrade) {
  327. radix_tree_delete(&mapping->page_tree, index);
  328. mapping->nrexceptional--;
  329. dax_wake_mapping_entry_waiter(mapping, index, entry,
  330. true);
  331. }
  332. entry = dax_radix_locked_entry(0, size_flag | RADIX_DAX_EMPTY);
  333. err = __radix_tree_insert(&mapping->page_tree, index,
  334. dax_radix_order(entry), entry);
  335. radix_tree_preload_end();
  336. if (err) {
  337. spin_unlock_irq(&mapping->tree_lock);
  338. /*
  339. * Someone already created the entry? This is a
  340. * normal failure when inserting PMDs in a range
  341. * that already contains PTEs. In that case we want
  342. * to return -EEXIST immediately.
  343. */
  344. if (err == -EEXIST && !(size_flag & RADIX_DAX_PMD))
  345. goto restart;
  346. /*
  347. * Our insertion of a DAX PMD entry failed, most
  348. * likely because it collided with a PTE sized entry
  349. * at a different index in the PMD range. We haven't
  350. * inserted anything into the radix tree and have no
  351. * waiters to wake.
  352. */
  353. return ERR_PTR(err);
  354. }
  355. /* Good, we have inserted empty locked entry into the tree. */
  356. mapping->nrexceptional++;
  357. spin_unlock_irq(&mapping->tree_lock);
  358. return entry;
  359. }
  360. /* Normal page in radix tree? */
  361. if (!radix_tree_exceptional_entry(entry)) {
  362. struct page *page = entry;
  363. get_page(page);
  364. spin_unlock_irq(&mapping->tree_lock);
  365. lock_page(page);
  366. /* Page got truncated? Retry... */
  367. if (unlikely(page->mapping != mapping)) {
  368. unlock_page(page);
  369. put_page(page);
  370. goto restart;
  371. }
  372. return page;
  373. }
  374. entry = lock_slot(mapping, slot);
  375. out_unlock:
  376. spin_unlock_irq(&mapping->tree_lock);
  377. return entry;
  378. }
  379. /*
  380. * We do not necessarily hold the mapping->tree_lock when we call this
  381. * function so it is possible that 'entry' is no longer a valid item in the
  382. * radix tree. This is okay because all we really need to do is to find the
  383. * correct waitqueue where tasks might be waiting for that old 'entry' and
  384. * wake them.
  385. */
  386. void dax_wake_mapping_entry_waiter(struct address_space *mapping,
  387. pgoff_t index, void *entry, bool wake_all)
  388. {
  389. struct exceptional_entry_key key;
  390. wait_queue_head_t *wq;
  391. wq = dax_entry_waitqueue(mapping, index, entry, &key);
  392. /*
  393. * Checking for locked entry and prepare_to_wait_exclusive() happens
  394. * under mapping->tree_lock, ditto for entry handling in our callers.
  395. * So at this point all tasks that could have seen our entry locked
  396. * must be in the waitqueue and the following check will see them.
  397. */
  398. if (waitqueue_active(wq))
  399. __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
  400. }
  401. static int __dax_invalidate_mapping_entry(struct address_space *mapping,
  402. pgoff_t index, bool trunc)
  403. {
  404. int ret = 0;
  405. void *entry;
  406. struct radix_tree_root *page_tree = &mapping->page_tree;
  407. spin_lock_irq(&mapping->tree_lock);
  408. entry = get_unlocked_mapping_entry(mapping, index, NULL);
  409. if (!entry || !radix_tree_exceptional_entry(entry))
  410. goto out;
  411. if (!trunc &&
  412. (radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_DIRTY) ||
  413. radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE)))
  414. goto out;
  415. radix_tree_delete(page_tree, index);
  416. mapping->nrexceptional--;
  417. ret = 1;
  418. out:
  419. put_unlocked_mapping_entry(mapping, index, entry);
  420. spin_unlock_irq(&mapping->tree_lock);
  421. return ret;
  422. }
  423. /*
  424. * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree
  425. * entry to get unlocked before deleting it.
  426. */
  427. int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
  428. {
  429. int ret = __dax_invalidate_mapping_entry(mapping, index, true);
  430. /*
  431. * This gets called from truncate / punch_hole path. As such, the caller
  432. * must hold locks protecting against concurrent modifications of the
  433. * radix tree (usually fs-private i_mmap_sem for writing). Since the
  434. * caller has seen exceptional entry for this index, we better find it
  435. * at that index as well...
  436. */
  437. WARN_ON_ONCE(!ret);
  438. return ret;
  439. }
  440. /*
  441. * Invalidate exceptional DAX entry if easily possible. This handles DAX
  442. * entries for invalidate_inode_pages() so we evict the entry only if we can
  443. * do so without blocking.
  444. */
  445. int dax_invalidate_mapping_entry(struct address_space *mapping, pgoff_t index)
  446. {
  447. int ret = 0;
  448. void *entry, **slot;
  449. struct radix_tree_root *page_tree = &mapping->page_tree;
  450. spin_lock_irq(&mapping->tree_lock);
  451. entry = __radix_tree_lookup(page_tree, index, NULL, &slot);
  452. if (!entry || !radix_tree_exceptional_entry(entry) ||
  453. slot_locked(mapping, slot))
  454. goto out;
  455. if (radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_DIRTY) ||
  456. radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
  457. goto out;
  458. radix_tree_delete(page_tree, index);
  459. mapping->nrexceptional--;
  460. ret = 1;
  461. out:
  462. spin_unlock_irq(&mapping->tree_lock);
  463. if (ret)
  464. dax_wake_mapping_entry_waiter(mapping, index, entry, true);
  465. return ret;
  466. }
  467. /*
  468. * Invalidate exceptional DAX entry if it is clean.
  469. */
  470. int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
  471. pgoff_t index)
  472. {
  473. return __dax_invalidate_mapping_entry(mapping, index, false);
  474. }
  475. /*
  476. * The user has performed a load from a hole in the file. Allocating
  477. * a new page in the file would cause excessive storage usage for
  478. * workloads with sparse files. We allocate a page cache page instead.
  479. * We'll kick it out of the page cache if it's ever written to,
  480. * otherwise it will simply fall out of the page cache under memory
  481. * pressure without ever having been dirtied.
  482. */
  483. static int dax_load_hole(struct address_space *mapping, void **entry,
  484. struct vm_fault *vmf)
  485. {
  486. struct page *page;
  487. int ret;
  488. /* Hole page already exists? Return it... */
  489. if (!radix_tree_exceptional_entry(*entry)) {
  490. page = *entry;
  491. goto out;
  492. }
  493. /* This will replace locked radix tree entry with a hole page */
  494. page = find_or_create_page(mapping, vmf->pgoff,
  495. vmf->gfp_mask | __GFP_ZERO);
  496. if (!page)
  497. return VM_FAULT_OOM;
  498. out:
  499. vmf->page = page;
  500. ret = finish_fault(vmf);
  501. vmf->page = NULL;
  502. *entry = page;
  503. if (!ret) {
  504. /* Grab reference for PTE that is now referencing the page */
  505. get_page(page);
  506. return VM_FAULT_NOPAGE;
  507. }
  508. return ret;
  509. }
  510. static int copy_user_dax(struct block_device *bdev, sector_t sector, size_t size,
  511. struct page *to, unsigned long vaddr)
  512. {
  513. struct blk_dax_ctl dax = {
  514. .sector = sector,
  515. .size = size,
  516. };
  517. void *vto;
  518. if (dax_map_atomic(bdev, &dax) < 0)
  519. return PTR_ERR(dax.addr);
  520. vto = kmap_atomic(to);
  521. copy_user_page(vto, (void __force *)dax.addr, vaddr, to);
  522. kunmap_atomic(vto);
  523. dax_unmap_atomic(bdev, &dax);
  524. return 0;
  525. }
  526. /*
  527. * By this point grab_mapping_entry() has ensured that we have a locked entry
  528. * of the appropriate size so we don't have to worry about downgrading PMDs to
  529. * PTEs. If we happen to be trying to insert a PTE and there is a PMD
  530. * already in the tree, we will skip the insertion and just dirty the PMD as
  531. * appropriate.
  532. */
  533. static void *dax_insert_mapping_entry(struct address_space *mapping,
  534. struct vm_fault *vmf,
  535. void *entry, sector_t sector,
  536. unsigned long flags)
  537. {
  538. struct radix_tree_root *page_tree = &mapping->page_tree;
  539. int error = 0;
  540. bool hole_fill = false;
  541. void *new_entry;
  542. pgoff_t index = vmf->pgoff;
  543. if (vmf->flags & FAULT_FLAG_WRITE)
  544. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  545. /* Replacing hole page with block mapping? */
  546. if (!radix_tree_exceptional_entry(entry)) {
  547. hole_fill = true;
  548. /*
  549. * Unmap the page now before we remove it from page cache below.
  550. * The page is locked so it cannot be faulted in again.
  551. */
  552. unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
  553. PAGE_SIZE, 0);
  554. error = radix_tree_preload(vmf->gfp_mask & ~__GFP_HIGHMEM);
  555. if (error)
  556. return ERR_PTR(error);
  557. } else if (dax_is_zero_entry(entry) && !(flags & RADIX_DAX_HZP)) {
  558. /* replacing huge zero page with PMD block mapping */
  559. unmap_mapping_range(mapping,
  560. (vmf->pgoff << PAGE_SHIFT) & PMD_MASK, PMD_SIZE, 0);
  561. }
  562. spin_lock_irq(&mapping->tree_lock);
  563. new_entry = dax_radix_locked_entry(sector, flags);
  564. if (hole_fill) {
  565. __delete_from_page_cache(entry, NULL);
  566. /* Drop pagecache reference */
  567. put_page(entry);
  568. error = __radix_tree_insert(page_tree, index,
  569. dax_radix_order(new_entry), new_entry);
  570. if (error) {
  571. new_entry = ERR_PTR(error);
  572. goto unlock;
  573. }
  574. mapping->nrexceptional++;
  575. } else if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) {
  576. /*
  577. * Only swap our new entry into the radix tree if the current
  578. * entry is a zero page or an empty entry. If a normal PTE or
  579. * PMD entry is already in the tree, we leave it alone. This
  580. * means that if we are trying to insert a PTE and the
  581. * existing entry is a PMD, we will just leave the PMD in the
  582. * tree and dirty it if necessary.
  583. */
  584. struct radix_tree_node *node;
  585. void **slot;
  586. void *ret;
  587. ret = __radix_tree_lookup(page_tree, index, &node, &slot);
  588. WARN_ON_ONCE(ret != entry);
  589. __radix_tree_replace(page_tree, node, slot,
  590. new_entry, NULL, NULL);
  591. }
  592. if (vmf->flags & FAULT_FLAG_WRITE)
  593. radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
  594. unlock:
  595. spin_unlock_irq(&mapping->tree_lock);
  596. if (hole_fill) {
  597. radix_tree_preload_end();
  598. /*
  599. * We don't need hole page anymore, it has been replaced with
  600. * locked radix tree entry now.
  601. */
  602. if (mapping->a_ops->freepage)
  603. mapping->a_ops->freepage(entry);
  604. unlock_page(entry);
  605. put_page(entry);
  606. }
  607. return new_entry;
  608. }
  609. static inline unsigned long
  610. pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma)
  611. {
  612. unsigned long address;
  613. address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  614. VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
  615. return address;
  616. }
  617. /* Walk all mappings of a given index of a file and writeprotect them */
  618. static void dax_mapping_entry_mkclean(struct address_space *mapping,
  619. pgoff_t index, unsigned long pfn)
  620. {
  621. struct vm_area_struct *vma;
  622. pte_t pte, *ptep = NULL;
  623. pmd_t *pmdp = NULL;
  624. spinlock_t *ptl;
  625. bool changed;
  626. i_mmap_lock_read(mapping);
  627. vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) {
  628. unsigned long address;
  629. cond_resched();
  630. if (!(vma->vm_flags & VM_SHARED))
  631. continue;
  632. address = pgoff_address(index, vma);
  633. changed = false;
  634. if (follow_pte_pmd(vma->vm_mm, address, &ptep, &pmdp, &ptl))
  635. continue;
  636. if (pmdp) {
  637. #ifdef CONFIG_FS_DAX_PMD
  638. pmd_t pmd;
  639. if (pfn != pmd_pfn(*pmdp))
  640. goto unlock_pmd;
  641. if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp))
  642. goto unlock_pmd;
  643. flush_cache_page(vma, address, pfn);
  644. pmd = pmdp_huge_clear_flush(vma, address, pmdp);
  645. pmd = pmd_wrprotect(pmd);
  646. pmd = pmd_mkclean(pmd);
  647. set_pmd_at(vma->vm_mm, address, pmdp, pmd);
  648. changed = true;
  649. unlock_pmd:
  650. spin_unlock(ptl);
  651. #endif
  652. } else {
  653. if (pfn != pte_pfn(*ptep))
  654. goto unlock_pte;
  655. if (!pte_dirty(*ptep) && !pte_write(*ptep))
  656. goto unlock_pte;
  657. flush_cache_page(vma, address, pfn);
  658. pte = ptep_clear_flush(vma, address, ptep);
  659. pte = pte_wrprotect(pte);
  660. pte = pte_mkclean(pte);
  661. set_pte_at(vma->vm_mm, address, ptep, pte);
  662. changed = true;
  663. unlock_pte:
  664. pte_unmap_unlock(ptep, ptl);
  665. }
  666. if (changed)
  667. mmu_notifier_invalidate_page(vma->vm_mm, address);
  668. }
  669. i_mmap_unlock_read(mapping);
  670. }
  671. static int dax_writeback_one(struct block_device *bdev,
  672. struct address_space *mapping, pgoff_t index, void *entry)
  673. {
  674. struct radix_tree_root *page_tree = &mapping->page_tree;
  675. struct blk_dax_ctl dax;
  676. void *entry2, **slot;
  677. int ret = 0;
  678. /*
  679. * A page got tagged dirty in DAX mapping? Something is seriously
  680. * wrong.
  681. */
  682. if (WARN_ON(!radix_tree_exceptional_entry(entry)))
  683. return -EIO;
  684. spin_lock_irq(&mapping->tree_lock);
  685. entry2 = get_unlocked_mapping_entry(mapping, index, &slot);
  686. /* Entry got punched out / reallocated? */
  687. if (!entry2 || !radix_tree_exceptional_entry(entry2))
  688. goto put_unlocked;
  689. /*
  690. * Entry got reallocated elsewhere? No need to writeback. We have to
  691. * compare sectors as we must not bail out due to difference in lockbit
  692. * or entry type.
  693. */
  694. if (dax_radix_sector(entry2) != dax_radix_sector(entry))
  695. goto put_unlocked;
  696. if (WARN_ON_ONCE(dax_is_empty_entry(entry) ||
  697. dax_is_zero_entry(entry))) {
  698. ret = -EIO;
  699. goto put_unlocked;
  700. }
  701. /* Another fsync thread may have already written back this entry */
  702. if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
  703. goto put_unlocked;
  704. /* Lock the entry to serialize with page faults */
  705. entry = lock_slot(mapping, slot);
  706. /*
  707. * We can clear the tag now but we have to be careful so that concurrent
  708. * dax_writeback_one() calls for the same index cannot finish before we
  709. * actually flush the caches. This is achieved as the calls will look
  710. * at the entry only under tree_lock and once they do that they will
  711. * see the entry locked and wait for it to unlock.
  712. */
  713. radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE);
  714. spin_unlock_irq(&mapping->tree_lock);
  715. /*
  716. * Even if dax_writeback_mapping_range() was given a wbc->range_start
  717. * in the middle of a PMD, the 'index' we are given will be aligned to
  718. * the start index of the PMD, as will the sector we pull from
  719. * 'entry'. This allows us to flush for PMD_SIZE and not have to
  720. * worry about partial PMD writebacks.
  721. */
  722. dax.sector = dax_radix_sector(entry);
  723. dax.size = PAGE_SIZE << dax_radix_order(entry);
  724. /*
  725. * We cannot hold tree_lock while calling dax_map_atomic() because it
  726. * eventually calls cond_resched().
  727. */
  728. ret = dax_map_atomic(bdev, &dax);
  729. if (ret < 0) {
  730. put_locked_mapping_entry(mapping, index, entry);
  731. return ret;
  732. }
  733. if (WARN_ON_ONCE(ret < dax.size)) {
  734. ret = -EIO;
  735. goto unmap;
  736. }
  737. dax_mapping_entry_mkclean(mapping, index, pfn_t_to_pfn(dax.pfn));
  738. wb_cache_pmem(dax.addr, dax.size);
  739. /*
  740. * After we have flushed the cache, we can clear the dirty tag. There
  741. * cannot be new dirty data in the pfn after the flush has completed as
  742. * the pfn mappings are writeprotected and fault waits for mapping
  743. * entry lock.
  744. */
  745. spin_lock_irq(&mapping->tree_lock);
  746. radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_DIRTY);
  747. spin_unlock_irq(&mapping->tree_lock);
  748. unmap:
  749. dax_unmap_atomic(bdev, &dax);
  750. put_locked_mapping_entry(mapping, index, entry);
  751. return ret;
  752. put_unlocked:
  753. put_unlocked_mapping_entry(mapping, index, entry2);
  754. spin_unlock_irq(&mapping->tree_lock);
  755. return ret;
  756. }
  757. /*
  758. * Flush the mapping to the persistent domain within the byte range of [start,
  759. * end]. This is required by data integrity operations to ensure file data is
  760. * on persistent storage prior to completion of the operation.
  761. */
  762. int dax_writeback_mapping_range(struct address_space *mapping,
  763. struct block_device *bdev, struct writeback_control *wbc)
  764. {
  765. struct inode *inode = mapping->host;
  766. pgoff_t start_index, end_index;
  767. pgoff_t indices[PAGEVEC_SIZE];
  768. struct pagevec pvec;
  769. bool done = false;
  770. int i, ret = 0;
  771. if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
  772. return -EIO;
  773. if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
  774. return 0;
  775. start_index = wbc->range_start >> PAGE_SHIFT;
  776. end_index = wbc->range_end >> PAGE_SHIFT;
  777. tag_pages_for_writeback(mapping, start_index, end_index);
  778. pagevec_init(&pvec, 0);
  779. while (!done) {
  780. pvec.nr = find_get_entries_tag(mapping, start_index,
  781. PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
  782. pvec.pages, indices);
  783. if (pvec.nr == 0)
  784. break;
  785. for (i = 0; i < pvec.nr; i++) {
  786. if (indices[i] > end_index) {
  787. done = true;
  788. break;
  789. }
  790. ret = dax_writeback_one(bdev, mapping, indices[i],
  791. pvec.pages[i]);
  792. if (ret < 0)
  793. return ret;
  794. }
  795. }
  796. return 0;
  797. }
  798. EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
  799. static int dax_insert_mapping(struct address_space *mapping,
  800. struct block_device *bdev, sector_t sector, size_t size,
  801. void **entryp, struct vm_area_struct *vma, struct vm_fault *vmf)
  802. {
  803. unsigned long vaddr = vmf->address;
  804. struct blk_dax_ctl dax = {
  805. .sector = sector,
  806. .size = size,
  807. };
  808. void *ret;
  809. void *entry = *entryp;
  810. if (dax_map_atomic(bdev, &dax) < 0)
  811. return PTR_ERR(dax.addr);
  812. dax_unmap_atomic(bdev, &dax);
  813. ret = dax_insert_mapping_entry(mapping, vmf, entry, dax.sector, 0);
  814. if (IS_ERR(ret))
  815. return PTR_ERR(ret);
  816. *entryp = ret;
  817. return vm_insert_mixed(vma, vaddr, dax.pfn);
  818. }
  819. /**
  820. * dax_pfn_mkwrite - handle first write to DAX page
  821. * @vma: The virtual memory area where the fault occurred
  822. * @vmf: The description of the fault
  823. */
  824. int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  825. {
  826. struct file *file = vma->vm_file;
  827. struct address_space *mapping = file->f_mapping;
  828. void *entry, **slot;
  829. pgoff_t index = vmf->pgoff;
  830. spin_lock_irq(&mapping->tree_lock);
  831. entry = get_unlocked_mapping_entry(mapping, index, &slot);
  832. if (!entry || !radix_tree_exceptional_entry(entry)) {
  833. if (entry)
  834. put_unlocked_mapping_entry(mapping, index, entry);
  835. spin_unlock_irq(&mapping->tree_lock);
  836. return VM_FAULT_NOPAGE;
  837. }
  838. radix_tree_tag_set(&mapping->page_tree, index, PAGECACHE_TAG_DIRTY);
  839. entry = lock_slot(mapping, slot);
  840. spin_unlock_irq(&mapping->tree_lock);
  841. /*
  842. * If we race with somebody updating the PTE and finish_mkwrite_fault()
  843. * fails, we don't care. We need to return VM_FAULT_NOPAGE and retry
  844. * the fault in either case.
  845. */
  846. finish_mkwrite_fault(vmf);
  847. put_locked_mapping_entry(mapping, index, entry);
  848. return VM_FAULT_NOPAGE;
  849. }
  850. EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
  851. static bool dax_range_is_aligned(struct block_device *bdev,
  852. unsigned int offset, unsigned int length)
  853. {
  854. unsigned short sector_size = bdev_logical_block_size(bdev);
  855. if (!IS_ALIGNED(offset, sector_size))
  856. return false;
  857. if (!IS_ALIGNED(length, sector_size))
  858. return false;
  859. return true;
  860. }
  861. int __dax_zero_page_range(struct block_device *bdev, sector_t sector,
  862. unsigned int offset, unsigned int length)
  863. {
  864. struct blk_dax_ctl dax = {
  865. .sector = sector,
  866. .size = PAGE_SIZE,
  867. };
  868. if (dax_range_is_aligned(bdev, offset, length)) {
  869. sector_t start_sector = dax.sector + (offset >> 9);
  870. return blkdev_issue_zeroout(bdev, start_sector,
  871. length >> 9, GFP_NOFS, true);
  872. } else {
  873. if (dax_map_atomic(bdev, &dax) < 0)
  874. return PTR_ERR(dax.addr);
  875. clear_pmem(dax.addr + offset, length);
  876. dax_unmap_atomic(bdev, &dax);
  877. }
  878. return 0;
  879. }
  880. EXPORT_SYMBOL_GPL(__dax_zero_page_range);
  881. #ifdef CONFIG_FS_IOMAP
  882. static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
  883. {
  884. return iomap->blkno + (((pos & PAGE_MASK) - iomap->offset) >> 9);
  885. }
  886. static loff_t
  887. dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
  888. struct iomap *iomap)
  889. {
  890. struct iov_iter *iter = data;
  891. loff_t end = pos + length, done = 0;
  892. ssize_t ret = 0;
  893. if (iov_iter_rw(iter) == READ) {
  894. end = min(end, i_size_read(inode));
  895. if (pos >= end)
  896. return 0;
  897. if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
  898. return iov_iter_zero(min(length, end - pos), iter);
  899. }
  900. if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
  901. return -EIO;
  902. /*
  903. * Write can allocate block for an area which has a hole page mapped
  904. * into page tables. We have to tear down these mappings so that data
  905. * written by write(2) is visible in mmap.
  906. */
  907. if ((iomap->flags & IOMAP_F_NEW) && inode->i_mapping->nrpages) {
  908. invalidate_inode_pages2_range(inode->i_mapping,
  909. pos >> PAGE_SHIFT,
  910. (end - 1) >> PAGE_SHIFT);
  911. }
  912. while (pos < end) {
  913. unsigned offset = pos & (PAGE_SIZE - 1);
  914. struct blk_dax_ctl dax = { 0 };
  915. ssize_t map_len;
  916. dax.sector = dax_iomap_sector(iomap, pos);
  917. dax.size = (length + offset + PAGE_SIZE - 1) & PAGE_MASK;
  918. map_len = dax_map_atomic(iomap->bdev, &dax);
  919. if (map_len < 0) {
  920. ret = map_len;
  921. break;
  922. }
  923. dax.addr += offset;
  924. map_len -= offset;
  925. if (map_len > end - pos)
  926. map_len = end - pos;
  927. if (iov_iter_rw(iter) == WRITE)
  928. map_len = copy_from_iter_pmem(dax.addr, map_len, iter);
  929. else
  930. map_len = copy_to_iter(dax.addr, map_len, iter);
  931. dax_unmap_atomic(iomap->bdev, &dax);
  932. if (map_len <= 0) {
  933. ret = map_len ? map_len : -EFAULT;
  934. break;
  935. }
  936. pos += map_len;
  937. length -= map_len;
  938. done += map_len;
  939. }
  940. return done ? done : ret;
  941. }
  942. /**
  943. * dax_iomap_rw - Perform I/O to a DAX file
  944. * @iocb: The control block for this I/O
  945. * @iter: The addresses to do I/O from or to
  946. * @ops: iomap ops passed from the file system
  947. *
  948. * This function performs read and write operations to directly mapped
  949. * persistent memory. The callers needs to take care of read/write exclusion
  950. * and evicting any page cache pages in the region under I/O.
  951. */
  952. ssize_t
  953. dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
  954. struct iomap_ops *ops)
  955. {
  956. struct address_space *mapping = iocb->ki_filp->f_mapping;
  957. struct inode *inode = mapping->host;
  958. loff_t pos = iocb->ki_pos, ret = 0, done = 0;
  959. unsigned flags = 0;
  960. if (iov_iter_rw(iter) == WRITE)
  961. flags |= IOMAP_WRITE;
  962. while (iov_iter_count(iter)) {
  963. ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
  964. iter, dax_iomap_actor);
  965. if (ret <= 0)
  966. break;
  967. pos += ret;
  968. done += ret;
  969. }
  970. iocb->ki_pos += done;
  971. return done ? done : ret;
  972. }
  973. EXPORT_SYMBOL_GPL(dax_iomap_rw);
  974. static int dax_fault_return(int error)
  975. {
  976. if (error == 0)
  977. return VM_FAULT_NOPAGE;
  978. if (error == -ENOMEM)
  979. return VM_FAULT_OOM;
  980. return VM_FAULT_SIGBUS;
  981. }
  982. /**
  983. * dax_iomap_fault - handle a page fault on a DAX file
  984. * @vma: The virtual memory area where the fault occurred
  985. * @vmf: The description of the fault
  986. * @ops: iomap ops passed from the file system
  987. *
  988. * When a page fault occurs, filesystems may call this helper in their fault
  989. * or mkwrite handler for DAX files. Assumes the caller has done all the
  990. * necessary locking for the page fault to proceed successfully.
  991. */
  992. int dax_iomap_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  993. struct iomap_ops *ops)
  994. {
  995. struct address_space *mapping = vma->vm_file->f_mapping;
  996. struct inode *inode = mapping->host;
  997. unsigned long vaddr = vmf->address;
  998. loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
  999. sector_t sector;
  1000. struct iomap iomap = { 0 };
  1001. unsigned flags = IOMAP_FAULT;
  1002. int error, major = 0;
  1003. int vmf_ret = 0;
  1004. void *entry;
  1005. /*
  1006. * Check whether offset isn't beyond end of file now. Caller is supposed
  1007. * to hold locks serializing us with truncate / punch hole so this is
  1008. * a reliable test.
  1009. */
  1010. if (pos >= i_size_read(inode))
  1011. return VM_FAULT_SIGBUS;
  1012. if ((vmf->flags & FAULT_FLAG_WRITE) && !vmf->cow_page)
  1013. flags |= IOMAP_WRITE;
  1014. /*
  1015. * Note that we don't bother to use iomap_apply here: DAX required
  1016. * the file system block size to be equal the page size, which means
  1017. * that we never have to deal with more than a single extent here.
  1018. */
  1019. error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
  1020. if (error)
  1021. return dax_fault_return(error);
  1022. if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
  1023. vmf_ret = dax_fault_return(-EIO); /* fs corruption? */
  1024. goto finish_iomap;
  1025. }
  1026. entry = grab_mapping_entry(mapping, vmf->pgoff, 0);
  1027. if (IS_ERR(entry)) {
  1028. vmf_ret = dax_fault_return(PTR_ERR(entry));
  1029. goto finish_iomap;
  1030. }
  1031. sector = dax_iomap_sector(&iomap, pos);
  1032. if (vmf->cow_page) {
  1033. switch (iomap.type) {
  1034. case IOMAP_HOLE:
  1035. case IOMAP_UNWRITTEN:
  1036. clear_user_highpage(vmf->cow_page, vaddr);
  1037. break;
  1038. case IOMAP_MAPPED:
  1039. error = copy_user_dax(iomap.bdev, sector, PAGE_SIZE,
  1040. vmf->cow_page, vaddr);
  1041. break;
  1042. default:
  1043. WARN_ON_ONCE(1);
  1044. error = -EIO;
  1045. break;
  1046. }
  1047. if (error)
  1048. goto error_unlock_entry;
  1049. __SetPageUptodate(vmf->cow_page);
  1050. vmf_ret = finish_fault(vmf);
  1051. if (!vmf_ret)
  1052. vmf_ret = VM_FAULT_DONE_COW;
  1053. goto unlock_entry;
  1054. }
  1055. switch (iomap.type) {
  1056. case IOMAP_MAPPED:
  1057. if (iomap.flags & IOMAP_F_NEW) {
  1058. count_vm_event(PGMAJFAULT);
  1059. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  1060. major = VM_FAULT_MAJOR;
  1061. }
  1062. error = dax_insert_mapping(mapping, iomap.bdev, sector,
  1063. PAGE_SIZE, &entry, vma, vmf);
  1064. /* -EBUSY is fine, somebody else faulted on the same PTE */
  1065. if (error == -EBUSY)
  1066. error = 0;
  1067. break;
  1068. case IOMAP_UNWRITTEN:
  1069. case IOMAP_HOLE:
  1070. if (!(vmf->flags & FAULT_FLAG_WRITE)) {
  1071. vmf_ret = dax_load_hole(mapping, &entry, vmf);
  1072. goto unlock_entry;
  1073. }
  1074. /*FALLTHRU*/
  1075. default:
  1076. WARN_ON_ONCE(1);
  1077. error = -EIO;
  1078. break;
  1079. }
  1080. error_unlock_entry:
  1081. vmf_ret = dax_fault_return(error) | major;
  1082. unlock_entry:
  1083. put_locked_mapping_entry(mapping, vmf->pgoff, entry);
  1084. finish_iomap:
  1085. if (ops->iomap_end) {
  1086. int copied = PAGE_SIZE;
  1087. if (vmf_ret & VM_FAULT_ERROR)
  1088. copied = 0;
  1089. /*
  1090. * The fault is done by now and there's no way back (other
  1091. * thread may be already happily using PTE we have installed).
  1092. * Just ignore error from ->iomap_end since we cannot do much
  1093. * with it.
  1094. */
  1095. ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap);
  1096. }
  1097. return vmf_ret;
  1098. }
  1099. EXPORT_SYMBOL_GPL(dax_iomap_fault);
  1100. #ifdef CONFIG_FS_DAX_PMD
  1101. /*
  1102. * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
  1103. * more often than one might expect in the below functions.
  1104. */
  1105. #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
  1106. static int dax_pmd_insert_mapping(struct vm_area_struct *vma, pmd_t *pmd,
  1107. struct vm_fault *vmf, unsigned long address,
  1108. struct iomap *iomap, loff_t pos, bool write, void **entryp)
  1109. {
  1110. struct address_space *mapping = vma->vm_file->f_mapping;
  1111. struct block_device *bdev = iomap->bdev;
  1112. struct blk_dax_ctl dax = {
  1113. .sector = dax_iomap_sector(iomap, pos),
  1114. .size = PMD_SIZE,
  1115. };
  1116. long length = dax_map_atomic(bdev, &dax);
  1117. void *ret;
  1118. if (length < 0) /* dax_map_atomic() failed */
  1119. return VM_FAULT_FALLBACK;
  1120. if (length < PMD_SIZE)
  1121. goto unmap_fallback;
  1122. if (pfn_t_to_pfn(dax.pfn) & PG_PMD_COLOUR)
  1123. goto unmap_fallback;
  1124. if (!pfn_t_devmap(dax.pfn))
  1125. goto unmap_fallback;
  1126. dax_unmap_atomic(bdev, &dax);
  1127. ret = dax_insert_mapping_entry(mapping, vmf, *entryp, dax.sector,
  1128. RADIX_DAX_PMD);
  1129. if (IS_ERR(ret))
  1130. return VM_FAULT_FALLBACK;
  1131. *entryp = ret;
  1132. return vmf_insert_pfn_pmd(vma, address, pmd, dax.pfn, write);
  1133. unmap_fallback:
  1134. dax_unmap_atomic(bdev, &dax);
  1135. return VM_FAULT_FALLBACK;
  1136. }
  1137. static int dax_pmd_load_hole(struct vm_area_struct *vma, pmd_t *pmd,
  1138. struct vm_fault *vmf, unsigned long address,
  1139. struct iomap *iomap, void **entryp)
  1140. {
  1141. struct address_space *mapping = vma->vm_file->f_mapping;
  1142. unsigned long pmd_addr = address & PMD_MASK;
  1143. struct page *zero_page;
  1144. spinlock_t *ptl;
  1145. pmd_t pmd_entry;
  1146. void *ret;
  1147. zero_page = mm_get_huge_zero_page(vma->vm_mm);
  1148. if (unlikely(!zero_page))
  1149. return VM_FAULT_FALLBACK;
  1150. ret = dax_insert_mapping_entry(mapping, vmf, *entryp, 0,
  1151. RADIX_DAX_PMD | RADIX_DAX_HZP);
  1152. if (IS_ERR(ret))
  1153. return VM_FAULT_FALLBACK;
  1154. *entryp = ret;
  1155. ptl = pmd_lock(vma->vm_mm, pmd);
  1156. if (!pmd_none(*pmd)) {
  1157. spin_unlock(ptl);
  1158. return VM_FAULT_FALLBACK;
  1159. }
  1160. pmd_entry = mk_pmd(zero_page, vma->vm_page_prot);
  1161. pmd_entry = pmd_mkhuge(pmd_entry);
  1162. set_pmd_at(vma->vm_mm, pmd_addr, pmd, pmd_entry);
  1163. spin_unlock(ptl);
  1164. return VM_FAULT_NOPAGE;
  1165. }
  1166. int dax_iomap_pmd_fault(struct vm_area_struct *vma, unsigned long address,
  1167. pmd_t *pmd, unsigned int flags, struct iomap_ops *ops)
  1168. {
  1169. struct address_space *mapping = vma->vm_file->f_mapping;
  1170. unsigned long pmd_addr = address & PMD_MASK;
  1171. bool write = flags & FAULT_FLAG_WRITE;
  1172. unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT;
  1173. struct inode *inode = mapping->host;
  1174. int result = VM_FAULT_FALLBACK;
  1175. struct iomap iomap = { 0 };
  1176. pgoff_t max_pgoff, pgoff;
  1177. struct vm_fault vmf;
  1178. void *entry;
  1179. loff_t pos;
  1180. int error;
  1181. /* Fall back to PTEs if we're going to COW */
  1182. if (write && !(vma->vm_flags & VM_SHARED))
  1183. goto fallback;
  1184. /* If the PMD would extend outside the VMA */
  1185. if (pmd_addr < vma->vm_start)
  1186. goto fallback;
  1187. if ((pmd_addr + PMD_SIZE) > vma->vm_end)
  1188. goto fallback;
  1189. /*
  1190. * Check whether offset isn't beyond end of file now. Caller is
  1191. * supposed to hold locks serializing us with truncate / punch hole so
  1192. * this is a reliable test.
  1193. */
  1194. pgoff = linear_page_index(vma, pmd_addr);
  1195. max_pgoff = (i_size_read(inode) - 1) >> PAGE_SHIFT;
  1196. if (pgoff > max_pgoff)
  1197. return VM_FAULT_SIGBUS;
  1198. /* If the PMD would extend beyond the file size */
  1199. if ((pgoff | PG_PMD_COLOUR) > max_pgoff)
  1200. goto fallback;
  1201. /*
  1202. * Note that we don't use iomap_apply here. We aren't doing I/O, only
  1203. * setting up a mapping, so really we're using iomap_begin() as a way
  1204. * to look up our filesystem block.
  1205. */
  1206. pos = (loff_t)pgoff << PAGE_SHIFT;
  1207. error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap);
  1208. if (error)
  1209. goto fallback;
  1210. if (iomap.offset + iomap.length < pos + PMD_SIZE)
  1211. goto finish_iomap;
  1212. /*
  1213. * grab_mapping_entry() will make sure we get a 2M empty entry, a DAX
  1214. * PMD or a HZP entry. If it can't (because a 4k page is already in
  1215. * the tree, for instance), it will return -EEXIST and we just fall
  1216. * back to 4k entries.
  1217. */
  1218. entry = grab_mapping_entry(mapping, pgoff, RADIX_DAX_PMD);
  1219. if (IS_ERR(entry))
  1220. goto finish_iomap;
  1221. vmf.pgoff = pgoff;
  1222. vmf.flags = flags;
  1223. vmf.gfp_mask = mapping_gfp_mask(mapping) | __GFP_IO;
  1224. switch (iomap.type) {
  1225. case IOMAP_MAPPED:
  1226. result = dax_pmd_insert_mapping(vma, pmd, &vmf, address,
  1227. &iomap, pos, write, &entry);
  1228. break;
  1229. case IOMAP_UNWRITTEN:
  1230. case IOMAP_HOLE:
  1231. if (WARN_ON_ONCE(write))
  1232. goto unlock_entry;
  1233. result = dax_pmd_load_hole(vma, pmd, &vmf, address, &iomap,
  1234. &entry);
  1235. break;
  1236. default:
  1237. WARN_ON_ONCE(1);
  1238. break;
  1239. }
  1240. unlock_entry:
  1241. put_locked_mapping_entry(mapping, pgoff, entry);
  1242. finish_iomap:
  1243. if (ops->iomap_end) {
  1244. int copied = PMD_SIZE;
  1245. if (result == VM_FAULT_FALLBACK)
  1246. copied = 0;
  1247. /*
  1248. * The fault is done by now and there's no way back (other
  1249. * thread may be already happily using PMD we have installed).
  1250. * Just ignore error from ->iomap_end since we cannot do much
  1251. * with it.
  1252. */
  1253. ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags,
  1254. &iomap);
  1255. }
  1256. fallback:
  1257. if (result == VM_FAULT_FALLBACK) {
  1258. split_huge_pmd(vma, pmd, address);
  1259. count_vm_event(THP_FAULT_FALLBACK);
  1260. }
  1261. return result;
  1262. }
  1263. EXPORT_SYMBOL_GPL(dax_iomap_pmd_fault);
  1264. #endif /* CONFIG_FS_DAX_PMD */
  1265. #endif /* CONFIG_FS_IOMAP */