dax.c 47 KB

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