dax.c 49 KB

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