dax.c 50 KB

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