dax.c 40 KB

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