dax.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  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. static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
  882. {
  883. return iomap->blkno + (((pos & PAGE_MASK) - iomap->offset) >> 9);
  884. }
  885. static loff_t
  886. dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
  887. struct iomap *iomap)
  888. {
  889. struct iov_iter *iter = data;
  890. loff_t end = pos + length, done = 0;
  891. ssize_t ret = 0;
  892. if (iov_iter_rw(iter) == READ) {
  893. end = min(end, i_size_read(inode));
  894. if (pos >= end)
  895. return 0;
  896. if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
  897. return iov_iter_zero(min(length, end - pos), iter);
  898. }
  899. if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
  900. return -EIO;
  901. /*
  902. * Write can allocate block for an area which has a hole page mapped
  903. * into page tables. We have to tear down these mappings so that data
  904. * written by write(2) is visible in mmap.
  905. */
  906. if ((iomap->flags & IOMAP_F_NEW) && inode->i_mapping->nrpages) {
  907. invalidate_inode_pages2_range(inode->i_mapping,
  908. pos >> PAGE_SHIFT,
  909. (end - 1) >> PAGE_SHIFT);
  910. }
  911. while (pos < end) {
  912. unsigned offset = pos & (PAGE_SIZE - 1);
  913. struct blk_dax_ctl dax = { 0 };
  914. ssize_t map_len;
  915. dax.sector = dax_iomap_sector(iomap, pos);
  916. dax.size = (length + offset + PAGE_SIZE - 1) & PAGE_MASK;
  917. map_len = dax_map_atomic(iomap->bdev, &dax);
  918. if (map_len < 0) {
  919. ret = map_len;
  920. break;
  921. }
  922. dax.addr += offset;
  923. map_len -= offset;
  924. if (map_len > end - pos)
  925. map_len = end - pos;
  926. if (iov_iter_rw(iter) == WRITE)
  927. map_len = copy_from_iter_pmem(dax.addr, map_len, iter);
  928. else
  929. map_len = copy_to_iter(dax.addr, map_len, iter);
  930. dax_unmap_atomic(iomap->bdev, &dax);
  931. if (map_len <= 0) {
  932. ret = map_len ? map_len : -EFAULT;
  933. break;
  934. }
  935. pos += map_len;
  936. length -= map_len;
  937. done += map_len;
  938. }
  939. return done ? done : ret;
  940. }
  941. /**
  942. * dax_iomap_rw - Perform I/O to a DAX file
  943. * @iocb: The control block for this I/O
  944. * @iter: The addresses to do I/O from or to
  945. * @ops: iomap ops passed from the file system
  946. *
  947. * This function performs read and write operations to directly mapped
  948. * persistent memory. The callers needs to take care of read/write exclusion
  949. * and evicting any page cache pages in the region under I/O.
  950. */
  951. ssize_t
  952. dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
  953. struct iomap_ops *ops)
  954. {
  955. struct address_space *mapping = iocb->ki_filp->f_mapping;
  956. struct inode *inode = mapping->host;
  957. loff_t pos = iocb->ki_pos, ret = 0, done = 0;
  958. unsigned flags = 0;
  959. if (iov_iter_rw(iter) == WRITE)
  960. flags |= IOMAP_WRITE;
  961. while (iov_iter_count(iter)) {
  962. ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
  963. iter, dax_iomap_actor);
  964. if (ret <= 0)
  965. break;
  966. pos += ret;
  967. done += ret;
  968. }
  969. iocb->ki_pos += done;
  970. return done ? done : ret;
  971. }
  972. EXPORT_SYMBOL_GPL(dax_iomap_rw);
  973. static int dax_fault_return(int error)
  974. {
  975. if (error == 0)
  976. return VM_FAULT_NOPAGE;
  977. if (error == -ENOMEM)
  978. return VM_FAULT_OOM;
  979. return VM_FAULT_SIGBUS;
  980. }
  981. /**
  982. * dax_iomap_fault - handle a page fault on a DAX file
  983. * @vma: The virtual memory area where the fault occurred
  984. * @vmf: The description of the fault
  985. * @ops: iomap ops passed from the file system
  986. *
  987. * When a page fault occurs, filesystems may call this helper in their fault
  988. * or mkwrite handler for DAX files. Assumes the caller has done all the
  989. * necessary locking for the page fault to proceed successfully.
  990. */
  991. int dax_iomap_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  992. struct iomap_ops *ops)
  993. {
  994. struct address_space *mapping = vma->vm_file->f_mapping;
  995. struct inode *inode = mapping->host;
  996. unsigned long vaddr = vmf->address;
  997. loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
  998. sector_t sector;
  999. struct iomap iomap = { 0 };
  1000. unsigned flags = IOMAP_FAULT;
  1001. int error, major = 0;
  1002. int vmf_ret = 0;
  1003. void *entry;
  1004. /*
  1005. * Check whether offset isn't beyond end of file now. Caller is supposed
  1006. * to hold locks serializing us with truncate / punch hole so this is
  1007. * a reliable test.
  1008. */
  1009. if (pos >= i_size_read(inode))
  1010. return VM_FAULT_SIGBUS;
  1011. if ((vmf->flags & FAULT_FLAG_WRITE) && !vmf->cow_page)
  1012. flags |= IOMAP_WRITE;
  1013. /*
  1014. * Note that we don't bother to use iomap_apply here: DAX required
  1015. * the file system block size to be equal the page size, which means
  1016. * that we never have to deal with more than a single extent here.
  1017. */
  1018. error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
  1019. if (error)
  1020. return dax_fault_return(error);
  1021. if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
  1022. vmf_ret = dax_fault_return(-EIO); /* fs corruption? */
  1023. goto finish_iomap;
  1024. }
  1025. entry = grab_mapping_entry(mapping, vmf->pgoff, 0);
  1026. if (IS_ERR(entry)) {
  1027. vmf_ret = dax_fault_return(PTR_ERR(entry));
  1028. goto finish_iomap;
  1029. }
  1030. sector = dax_iomap_sector(&iomap, pos);
  1031. if (vmf->cow_page) {
  1032. switch (iomap.type) {
  1033. case IOMAP_HOLE:
  1034. case IOMAP_UNWRITTEN:
  1035. clear_user_highpage(vmf->cow_page, vaddr);
  1036. break;
  1037. case IOMAP_MAPPED:
  1038. error = copy_user_dax(iomap.bdev, sector, PAGE_SIZE,
  1039. vmf->cow_page, vaddr);
  1040. break;
  1041. default:
  1042. WARN_ON_ONCE(1);
  1043. error = -EIO;
  1044. break;
  1045. }
  1046. if (error)
  1047. goto error_unlock_entry;
  1048. __SetPageUptodate(vmf->cow_page);
  1049. vmf_ret = finish_fault(vmf);
  1050. if (!vmf_ret)
  1051. vmf_ret = VM_FAULT_DONE_COW;
  1052. goto unlock_entry;
  1053. }
  1054. switch (iomap.type) {
  1055. case IOMAP_MAPPED:
  1056. if (iomap.flags & IOMAP_F_NEW) {
  1057. count_vm_event(PGMAJFAULT);
  1058. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  1059. major = VM_FAULT_MAJOR;
  1060. }
  1061. error = dax_insert_mapping(mapping, iomap.bdev, sector,
  1062. PAGE_SIZE, &entry, vma, vmf);
  1063. /* -EBUSY is fine, somebody else faulted on the same PTE */
  1064. if (error == -EBUSY)
  1065. error = 0;
  1066. break;
  1067. case IOMAP_UNWRITTEN:
  1068. case IOMAP_HOLE:
  1069. if (!(vmf->flags & FAULT_FLAG_WRITE)) {
  1070. vmf_ret = dax_load_hole(mapping, &entry, vmf);
  1071. goto unlock_entry;
  1072. }
  1073. /*FALLTHRU*/
  1074. default:
  1075. WARN_ON_ONCE(1);
  1076. error = -EIO;
  1077. break;
  1078. }
  1079. error_unlock_entry:
  1080. vmf_ret = dax_fault_return(error) | major;
  1081. unlock_entry:
  1082. put_locked_mapping_entry(mapping, vmf->pgoff, entry);
  1083. finish_iomap:
  1084. if (ops->iomap_end) {
  1085. int copied = PAGE_SIZE;
  1086. if (vmf_ret & VM_FAULT_ERROR)
  1087. copied = 0;
  1088. /*
  1089. * The fault is done by now and there's no way back (other
  1090. * thread may be already happily using PTE we have installed).
  1091. * Just ignore error from ->iomap_end since we cannot do much
  1092. * with it.
  1093. */
  1094. ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap);
  1095. }
  1096. return vmf_ret;
  1097. }
  1098. EXPORT_SYMBOL_GPL(dax_iomap_fault);
  1099. #ifdef CONFIG_FS_DAX_PMD
  1100. /*
  1101. * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
  1102. * more often than one might expect in the below functions.
  1103. */
  1104. #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
  1105. static int dax_pmd_insert_mapping(struct vm_area_struct *vma, pmd_t *pmd,
  1106. struct vm_fault *vmf, unsigned long address,
  1107. struct iomap *iomap, loff_t pos, bool write, void **entryp)
  1108. {
  1109. struct address_space *mapping = vma->vm_file->f_mapping;
  1110. struct block_device *bdev = iomap->bdev;
  1111. struct blk_dax_ctl dax = {
  1112. .sector = dax_iomap_sector(iomap, pos),
  1113. .size = PMD_SIZE,
  1114. };
  1115. long length = dax_map_atomic(bdev, &dax);
  1116. void *ret;
  1117. if (length < 0) /* dax_map_atomic() failed */
  1118. return VM_FAULT_FALLBACK;
  1119. if (length < PMD_SIZE)
  1120. goto unmap_fallback;
  1121. if (pfn_t_to_pfn(dax.pfn) & PG_PMD_COLOUR)
  1122. goto unmap_fallback;
  1123. if (!pfn_t_devmap(dax.pfn))
  1124. goto unmap_fallback;
  1125. dax_unmap_atomic(bdev, &dax);
  1126. ret = dax_insert_mapping_entry(mapping, vmf, *entryp, dax.sector,
  1127. RADIX_DAX_PMD);
  1128. if (IS_ERR(ret))
  1129. return VM_FAULT_FALLBACK;
  1130. *entryp = ret;
  1131. return vmf_insert_pfn_pmd(vma, address, pmd, dax.pfn, write);
  1132. unmap_fallback:
  1133. dax_unmap_atomic(bdev, &dax);
  1134. return VM_FAULT_FALLBACK;
  1135. }
  1136. static int dax_pmd_load_hole(struct vm_area_struct *vma, pmd_t *pmd,
  1137. struct vm_fault *vmf, unsigned long address,
  1138. struct iomap *iomap, void **entryp)
  1139. {
  1140. struct address_space *mapping = vma->vm_file->f_mapping;
  1141. unsigned long pmd_addr = address & PMD_MASK;
  1142. struct page *zero_page;
  1143. spinlock_t *ptl;
  1144. pmd_t pmd_entry;
  1145. void *ret;
  1146. zero_page = mm_get_huge_zero_page(vma->vm_mm);
  1147. if (unlikely(!zero_page))
  1148. return VM_FAULT_FALLBACK;
  1149. ret = dax_insert_mapping_entry(mapping, vmf, *entryp, 0,
  1150. RADIX_DAX_PMD | RADIX_DAX_HZP);
  1151. if (IS_ERR(ret))
  1152. return VM_FAULT_FALLBACK;
  1153. *entryp = ret;
  1154. ptl = pmd_lock(vma->vm_mm, pmd);
  1155. if (!pmd_none(*pmd)) {
  1156. spin_unlock(ptl);
  1157. return VM_FAULT_FALLBACK;
  1158. }
  1159. pmd_entry = mk_pmd(zero_page, vma->vm_page_prot);
  1160. pmd_entry = pmd_mkhuge(pmd_entry);
  1161. set_pmd_at(vma->vm_mm, pmd_addr, pmd, pmd_entry);
  1162. spin_unlock(ptl);
  1163. return VM_FAULT_NOPAGE;
  1164. }
  1165. int dax_iomap_pmd_fault(struct vm_area_struct *vma, unsigned long address,
  1166. pmd_t *pmd, unsigned int flags, struct iomap_ops *ops)
  1167. {
  1168. struct address_space *mapping = vma->vm_file->f_mapping;
  1169. unsigned long pmd_addr = address & PMD_MASK;
  1170. bool write = flags & FAULT_FLAG_WRITE;
  1171. unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT;
  1172. struct inode *inode = mapping->host;
  1173. int result = VM_FAULT_FALLBACK;
  1174. struct iomap iomap = { 0 };
  1175. pgoff_t max_pgoff, pgoff;
  1176. struct vm_fault vmf;
  1177. void *entry;
  1178. loff_t pos;
  1179. int error;
  1180. /* Fall back to PTEs if we're going to COW */
  1181. if (write && !(vma->vm_flags & VM_SHARED))
  1182. goto fallback;
  1183. /* If the PMD would extend outside the VMA */
  1184. if (pmd_addr < vma->vm_start)
  1185. goto fallback;
  1186. if ((pmd_addr + PMD_SIZE) > vma->vm_end)
  1187. goto fallback;
  1188. /*
  1189. * Check whether offset isn't beyond end of file now. Caller is
  1190. * supposed to hold locks serializing us with truncate / punch hole so
  1191. * this is a reliable test.
  1192. */
  1193. pgoff = linear_page_index(vma, pmd_addr);
  1194. max_pgoff = (i_size_read(inode) - 1) >> PAGE_SHIFT;
  1195. if (pgoff > max_pgoff)
  1196. return VM_FAULT_SIGBUS;
  1197. /* If the PMD would extend beyond the file size */
  1198. if ((pgoff | PG_PMD_COLOUR) > max_pgoff)
  1199. goto fallback;
  1200. /*
  1201. * Note that we don't use iomap_apply here. We aren't doing I/O, only
  1202. * setting up a mapping, so really we're using iomap_begin() as a way
  1203. * to look up our filesystem block.
  1204. */
  1205. pos = (loff_t)pgoff << PAGE_SHIFT;
  1206. error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap);
  1207. if (error)
  1208. goto fallback;
  1209. if (iomap.offset + iomap.length < pos + PMD_SIZE)
  1210. goto finish_iomap;
  1211. /*
  1212. * grab_mapping_entry() will make sure we get a 2M empty entry, a DAX
  1213. * PMD or a HZP entry. If it can't (because a 4k page is already in
  1214. * the tree, for instance), it will return -EEXIST and we just fall
  1215. * back to 4k entries.
  1216. */
  1217. entry = grab_mapping_entry(mapping, pgoff, RADIX_DAX_PMD);
  1218. if (IS_ERR(entry))
  1219. goto finish_iomap;
  1220. vmf.pgoff = pgoff;
  1221. vmf.flags = flags;
  1222. vmf.gfp_mask = mapping_gfp_mask(mapping) | __GFP_IO;
  1223. switch (iomap.type) {
  1224. case IOMAP_MAPPED:
  1225. result = dax_pmd_insert_mapping(vma, pmd, &vmf, address,
  1226. &iomap, pos, write, &entry);
  1227. break;
  1228. case IOMAP_UNWRITTEN:
  1229. case IOMAP_HOLE:
  1230. if (WARN_ON_ONCE(write))
  1231. goto unlock_entry;
  1232. result = dax_pmd_load_hole(vma, pmd, &vmf, address, &iomap,
  1233. &entry);
  1234. break;
  1235. default:
  1236. WARN_ON_ONCE(1);
  1237. break;
  1238. }
  1239. unlock_entry:
  1240. put_locked_mapping_entry(mapping, pgoff, entry);
  1241. finish_iomap:
  1242. if (ops->iomap_end) {
  1243. int copied = PMD_SIZE;
  1244. if (result == VM_FAULT_FALLBACK)
  1245. copied = 0;
  1246. /*
  1247. * The fault is done by now and there's no way back (other
  1248. * thread may be already happily using PMD we have installed).
  1249. * Just ignore error from ->iomap_end since we cannot do much
  1250. * with it.
  1251. */
  1252. ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags,
  1253. &iomap);
  1254. }
  1255. fallback:
  1256. if (result == VM_FAULT_FALLBACK) {
  1257. split_huge_pmd(vma, pmd, address);
  1258. count_vm_event(THP_FAULT_FALLBACK);
  1259. }
  1260. return result;
  1261. }
  1262. EXPORT_SYMBOL_GPL(dax_iomap_pmd_fault);
  1263. #endif /* CONFIG_FS_DAX_PMD */