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