dax.c 44 KB

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