dax.c 47 KB

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