dax.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  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 (!radix_tree_exceptional_entry(pvec_ent))
  496. continue;
  497. xa_lock_irq(&mapping->i_pages);
  498. entry = get_unlocked_mapping_entry(mapping, index, NULL);
  499. if (entry)
  500. page = dax_busy_page(entry);
  501. put_unlocked_mapping_entry(mapping, index, entry);
  502. xa_unlock_irq(&mapping->i_pages);
  503. if (page)
  504. break;
  505. }
  506. pagevec_remove_exceptionals(&pvec);
  507. pagevec_release(&pvec);
  508. index++;
  509. if (page)
  510. break;
  511. }
  512. return page;
  513. }
  514. EXPORT_SYMBOL_GPL(dax_layout_busy_page);
  515. static int __dax_invalidate_mapping_entry(struct address_space *mapping,
  516. pgoff_t index, bool trunc)
  517. {
  518. int ret = 0;
  519. void *entry;
  520. struct radix_tree_root *pages = &mapping->i_pages;
  521. xa_lock_irq(pages);
  522. entry = get_unlocked_mapping_entry(mapping, index, NULL);
  523. if (!entry || WARN_ON_ONCE(!radix_tree_exceptional_entry(entry)))
  524. goto out;
  525. if (!trunc &&
  526. (radix_tree_tag_get(pages, index, PAGECACHE_TAG_DIRTY) ||
  527. radix_tree_tag_get(pages, index, PAGECACHE_TAG_TOWRITE)))
  528. goto out;
  529. dax_disassociate_entry(entry, mapping, trunc);
  530. radix_tree_delete(pages, index);
  531. mapping->nrexceptional--;
  532. ret = 1;
  533. out:
  534. put_unlocked_mapping_entry(mapping, index, entry);
  535. xa_unlock_irq(pages);
  536. return ret;
  537. }
  538. /*
  539. * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree
  540. * entry to get unlocked before deleting it.
  541. */
  542. int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
  543. {
  544. int ret = __dax_invalidate_mapping_entry(mapping, index, true);
  545. /*
  546. * This gets called from truncate / punch_hole path. As such, the caller
  547. * must hold locks protecting against concurrent modifications of the
  548. * radix tree (usually fs-private i_mmap_sem for writing). Since the
  549. * caller has seen exceptional entry for this index, we better find it
  550. * at that index as well...
  551. */
  552. WARN_ON_ONCE(!ret);
  553. return ret;
  554. }
  555. /*
  556. * Invalidate exceptional DAX entry if it is clean.
  557. */
  558. int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
  559. pgoff_t index)
  560. {
  561. return __dax_invalidate_mapping_entry(mapping, index, false);
  562. }
  563. static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev,
  564. sector_t sector, size_t size, struct page *to,
  565. unsigned long vaddr)
  566. {
  567. void *vto, *kaddr;
  568. pgoff_t pgoff;
  569. pfn_t pfn;
  570. long rc;
  571. int id;
  572. rc = bdev_dax_pgoff(bdev, sector, size, &pgoff);
  573. if (rc)
  574. return rc;
  575. id = dax_read_lock();
  576. rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, &pfn);
  577. if (rc < 0) {
  578. dax_read_unlock(id);
  579. return rc;
  580. }
  581. vto = kmap_atomic(to);
  582. copy_user_page(vto, (void __force *)kaddr, vaddr, to);
  583. kunmap_atomic(vto);
  584. dax_read_unlock(id);
  585. return 0;
  586. }
  587. /*
  588. * By this point grab_mapping_entry() has ensured that we have a locked entry
  589. * of the appropriate size so we don't have to worry about downgrading PMDs to
  590. * PTEs. If we happen to be trying to insert a PTE and there is a PMD
  591. * already in the tree, we will skip the insertion and just dirty the PMD as
  592. * appropriate.
  593. */
  594. static void *dax_insert_mapping_entry(struct address_space *mapping,
  595. struct vm_fault *vmf,
  596. void *entry, pfn_t pfn_t,
  597. unsigned long flags, bool dirty)
  598. {
  599. struct radix_tree_root *pages = &mapping->i_pages;
  600. unsigned long pfn = pfn_t_to_pfn(pfn_t);
  601. pgoff_t index = vmf->pgoff;
  602. void *new_entry;
  603. if (dirty)
  604. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  605. if (dax_is_zero_entry(entry) && !(flags & RADIX_DAX_ZERO_PAGE)) {
  606. /* we are replacing a zero page with block mapping */
  607. if (dax_is_pmd_entry(entry))
  608. unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR,
  609. PG_PMD_NR, false);
  610. else /* pte entry */
  611. unmap_mapping_pages(mapping, vmf->pgoff, 1, false);
  612. }
  613. xa_lock_irq(pages);
  614. new_entry = dax_radix_locked_entry(pfn, flags);
  615. if (dax_entry_size(entry) != dax_entry_size(new_entry)) {
  616. dax_disassociate_entry(entry, mapping, false);
  617. dax_associate_entry(new_entry, mapping);
  618. }
  619. if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) {
  620. /*
  621. * Only swap our new entry into the radix tree if the current
  622. * entry is a zero page or an empty entry. If a normal PTE or
  623. * PMD entry is already in the tree, we leave it alone. This
  624. * means that if we are trying to insert a PTE and the
  625. * existing entry is a PMD, we will just leave the PMD in the
  626. * tree and dirty it if necessary.
  627. */
  628. struct radix_tree_node *node;
  629. void **slot;
  630. void *ret;
  631. ret = __radix_tree_lookup(pages, index, &node, &slot);
  632. WARN_ON_ONCE(ret != entry);
  633. __radix_tree_replace(pages, node, slot,
  634. new_entry, NULL);
  635. entry = new_entry;
  636. }
  637. if (dirty)
  638. radix_tree_tag_set(pages, index, PAGECACHE_TAG_DIRTY);
  639. xa_unlock_irq(pages);
  640. return entry;
  641. }
  642. static inline unsigned long
  643. pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma)
  644. {
  645. unsigned long address;
  646. address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  647. VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
  648. return address;
  649. }
  650. /* Walk all mappings of a given index of a file and writeprotect them */
  651. static void dax_mapping_entry_mkclean(struct address_space *mapping,
  652. pgoff_t index, unsigned long pfn)
  653. {
  654. struct vm_area_struct *vma;
  655. pte_t pte, *ptep = NULL;
  656. pmd_t *pmdp = NULL;
  657. spinlock_t *ptl;
  658. i_mmap_lock_read(mapping);
  659. vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) {
  660. unsigned long address, start, end;
  661. cond_resched();
  662. if (!(vma->vm_flags & VM_SHARED))
  663. continue;
  664. address = pgoff_address(index, vma);
  665. /*
  666. * Note because we provide start/end to follow_pte_pmd it will
  667. * call mmu_notifier_invalidate_range_start() on our behalf
  668. * before taking any lock.
  669. */
  670. if (follow_pte_pmd(vma->vm_mm, address, &start, &end, &ptep, &pmdp, &ptl))
  671. continue;
  672. /*
  673. * No need to call mmu_notifier_invalidate_range() as we are
  674. * downgrading page table protection not changing it to point
  675. * to a new page.
  676. *
  677. * See Documentation/vm/mmu_notifier.rst
  678. */
  679. if (pmdp) {
  680. #ifdef CONFIG_FS_DAX_PMD
  681. pmd_t pmd;
  682. if (pfn != pmd_pfn(*pmdp))
  683. goto unlock_pmd;
  684. if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp))
  685. goto unlock_pmd;
  686. flush_cache_page(vma, address, pfn);
  687. pmd = pmdp_huge_clear_flush(vma, address, pmdp);
  688. pmd = pmd_wrprotect(pmd);
  689. pmd = pmd_mkclean(pmd);
  690. set_pmd_at(vma->vm_mm, address, pmdp, pmd);
  691. unlock_pmd:
  692. #endif
  693. spin_unlock(ptl);
  694. } else {
  695. if (pfn != pte_pfn(*ptep))
  696. goto unlock_pte;
  697. if (!pte_dirty(*ptep) && !pte_write(*ptep))
  698. goto unlock_pte;
  699. flush_cache_page(vma, address, pfn);
  700. pte = ptep_clear_flush(vma, address, ptep);
  701. pte = pte_wrprotect(pte);
  702. pte = pte_mkclean(pte);
  703. set_pte_at(vma->vm_mm, address, ptep, pte);
  704. unlock_pte:
  705. pte_unmap_unlock(ptep, ptl);
  706. }
  707. mmu_notifier_invalidate_range_end(vma->vm_mm, start, end);
  708. }
  709. i_mmap_unlock_read(mapping);
  710. }
  711. static int dax_writeback_one(struct dax_device *dax_dev,
  712. struct address_space *mapping, pgoff_t index, void *entry)
  713. {
  714. struct radix_tree_root *pages = &mapping->i_pages;
  715. void *entry2, **slot;
  716. unsigned long pfn;
  717. long ret = 0;
  718. size_t size;
  719. /*
  720. * A page got tagged dirty in DAX mapping? Something is seriously
  721. * wrong.
  722. */
  723. if (WARN_ON(!radix_tree_exceptional_entry(entry)))
  724. return -EIO;
  725. xa_lock_irq(pages);
  726. entry2 = get_unlocked_mapping_entry(mapping, index, &slot);
  727. /* Entry got punched out / reallocated? */
  728. if (!entry2 || WARN_ON_ONCE(!radix_tree_exceptional_entry(entry2)))
  729. goto put_unlocked;
  730. /*
  731. * Entry got reallocated elsewhere? No need to writeback. We have to
  732. * compare pfns as we must not bail out due to difference in lockbit
  733. * or entry type.
  734. */
  735. if (dax_radix_pfn(entry2) != dax_radix_pfn(entry))
  736. goto put_unlocked;
  737. if (WARN_ON_ONCE(dax_is_empty_entry(entry) ||
  738. dax_is_zero_entry(entry))) {
  739. ret = -EIO;
  740. goto put_unlocked;
  741. }
  742. /* Another fsync thread may have already written back this entry */
  743. if (!radix_tree_tag_get(pages, index, PAGECACHE_TAG_TOWRITE))
  744. goto put_unlocked;
  745. /* Lock the entry to serialize with page faults */
  746. entry = lock_slot(mapping, slot);
  747. /*
  748. * We can clear the tag now but we have to be careful so that concurrent
  749. * dax_writeback_one() calls for the same index cannot finish before we
  750. * actually flush the caches. This is achieved as the calls will look
  751. * at the entry only under the i_pages lock and once they do that
  752. * they will see the entry locked and wait for it to unlock.
  753. */
  754. radix_tree_tag_clear(pages, index, PAGECACHE_TAG_TOWRITE);
  755. xa_unlock_irq(pages);
  756. /*
  757. * Even if dax_writeback_mapping_range() was given a wbc->range_start
  758. * in the middle of a PMD, the 'index' we are given will be aligned to
  759. * the start index of the PMD, as will the pfn we pull from 'entry'.
  760. * This allows us to flush for PMD_SIZE and not have to worry about
  761. * partial PMD writebacks.
  762. */
  763. pfn = dax_radix_pfn(entry);
  764. size = PAGE_SIZE << dax_radix_order(entry);
  765. dax_mapping_entry_mkclean(mapping, index, pfn);
  766. dax_flush(dax_dev, page_address(pfn_to_page(pfn)), size);
  767. /*
  768. * After we have flushed the cache, we can clear the dirty tag. There
  769. * cannot be new dirty data in the pfn after the flush has completed as
  770. * the pfn mappings are writeprotected and fault waits for mapping
  771. * entry lock.
  772. */
  773. xa_lock_irq(pages);
  774. radix_tree_tag_clear(pages, index, PAGECACHE_TAG_DIRTY);
  775. xa_unlock_irq(pages);
  776. trace_dax_writeback_one(mapping->host, index, size >> PAGE_SHIFT);
  777. put_locked_mapping_entry(mapping, index);
  778. return ret;
  779. put_unlocked:
  780. put_unlocked_mapping_entry(mapping, index, entry2);
  781. xa_unlock_irq(pages);
  782. return ret;
  783. }
  784. /*
  785. * Flush the mapping to the persistent domain within the byte range of [start,
  786. * end]. This is required by data integrity operations to ensure file data is
  787. * on persistent storage prior to completion of the operation.
  788. */
  789. int dax_writeback_mapping_range(struct address_space *mapping,
  790. struct block_device *bdev, struct writeback_control *wbc)
  791. {
  792. struct inode *inode = mapping->host;
  793. pgoff_t start_index, end_index;
  794. pgoff_t indices[PAGEVEC_SIZE];
  795. struct dax_device *dax_dev;
  796. struct pagevec pvec;
  797. bool done = false;
  798. int i, ret = 0;
  799. if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
  800. return -EIO;
  801. if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
  802. return 0;
  803. dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
  804. if (!dax_dev)
  805. return -EIO;
  806. start_index = wbc->range_start >> PAGE_SHIFT;
  807. end_index = wbc->range_end >> PAGE_SHIFT;
  808. trace_dax_writeback_range(inode, start_index, end_index);
  809. tag_pages_for_writeback(mapping, start_index, end_index);
  810. pagevec_init(&pvec);
  811. while (!done) {
  812. pvec.nr = find_get_entries_tag(mapping, start_index,
  813. PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
  814. pvec.pages, indices);
  815. if (pvec.nr == 0)
  816. break;
  817. for (i = 0; i < pvec.nr; i++) {
  818. if (indices[i] > end_index) {
  819. done = true;
  820. break;
  821. }
  822. ret = dax_writeback_one(dax_dev, mapping, indices[i],
  823. pvec.pages[i]);
  824. if (ret < 0) {
  825. mapping_set_error(mapping, ret);
  826. goto out;
  827. }
  828. }
  829. start_index = indices[pvec.nr - 1] + 1;
  830. }
  831. out:
  832. put_dax(dax_dev);
  833. trace_dax_writeback_range_done(inode, start_index, end_index);
  834. return (ret < 0 ? ret : 0);
  835. }
  836. EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
  837. static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
  838. {
  839. return (iomap->addr + (pos & PAGE_MASK) - iomap->offset) >> 9;
  840. }
  841. static int dax_iomap_pfn(struct iomap *iomap, loff_t pos, size_t size,
  842. pfn_t *pfnp)
  843. {
  844. const sector_t sector = dax_iomap_sector(iomap, pos);
  845. pgoff_t pgoff;
  846. void *kaddr;
  847. int id, rc;
  848. long length;
  849. rc = bdev_dax_pgoff(iomap->bdev, sector, size, &pgoff);
  850. if (rc)
  851. return rc;
  852. id = dax_read_lock();
  853. length = dax_direct_access(iomap->dax_dev, pgoff, PHYS_PFN(size),
  854. &kaddr, pfnp);
  855. if (length < 0) {
  856. rc = length;
  857. goto out;
  858. }
  859. rc = -EINVAL;
  860. if (PFN_PHYS(length) < size)
  861. goto out;
  862. if (pfn_t_to_pfn(*pfnp) & (PHYS_PFN(size)-1))
  863. goto out;
  864. /* For larger pages we need devmap */
  865. if (length > 1 && !pfn_t_devmap(*pfnp))
  866. goto out;
  867. rc = 0;
  868. out:
  869. dax_read_unlock(id);
  870. return rc;
  871. }
  872. /*
  873. * The user has performed a load from a hole in the file. Allocating a new
  874. * page in the file would cause excessive storage usage for workloads with
  875. * sparse files. Instead we insert a read-only mapping of the 4k zero page.
  876. * If this page is ever written to we will re-fault and change the mapping to
  877. * point to real DAX storage instead.
  878. */
  879. static vm_fault_t dax_load_hole(struct address_space *mapping, void *entry,
  880. struct vm_fault *vmf)
  881. {
  882. struct inode *inode = mapping->host;
  883. unsigned long vaddr = vmf->address;
  884. vm_fault_t ret = VM_FAULT_NOPAGE;
  885. struct page *zero_page;
  886. pfn_t pfn;
  887. zero_page = ZERO_PAGE(0);
  888. if (unlikely(!zero_page)) {
  889. ret = VM_FAULT_OOM;
  890. goto out;
  891. }
  892. pfn = page_to_pfn_t(zero_page);
  893. dax_insert_mapping_entry(mapping, vmf, entry, pfn, RADIX_DAX_ZERO_PAGE,
  894. false);
  895. ret = vmf_insert_mixed(vmf->vma, vaddr, pfn);
  896. out:
  897. trace_dax_load_hole(inode, vmf, ret);
  898. return ret;
  899. }
  900. static bool dax_range_is_aligned(struct block_device *bdev,
  901. unsigned int offset, unsigned int length)
  902. {
  903. unsigned short sector_size = bdev_logical_block_size(bdev);
  904. if (!IS_ALIGNED(offset, sector_size))
  905. return false;
  906. if (!IS_ALIGNED(length, sector_size))
  907. return false;
  908. return true;
  909. }
  910. int __dax_zero_page_range(struct block_device *bdev,
  911. struct dax_device *dax_dev, sector_t sector,
  912. unsigned int offset, unsigned int size)
  913. {
  914. if (dax_range_is_aligned(bdev, offset, size)) {
  915. sector_t start_sector = sector + (offset >> 9);
  916. return blkdev_issue_zeroout(bdev, start_sector,
  917. size >> 9, GFP_NOFS, 0);
  918. } else {
  919. pgoff_t pgoff;
  920. long rc, id;
  921. void *kaddr;
  922. pfn_t pfn;
  923. rc = bdev_dax_pgoff(bdev, sector, PAGE_SIZE, &pgoff);
  924. if (rc)
  925. return rc;
  926. id = dax_read_lock();
  927. rc = dax_direct_access(dax_dev, pgoff, 1, &kaddr,
  928. &pfn);
  929. if (rc < 0) {
  930. dax_read_unlock(id);
  931. return rc;
  932. }
  933. memset(kaddr + offset, 0, size);
  934. dax_flush(dax_dev, kaddr + offset, size);
  935. dax_read_unlock(id);
  936. }
  937. return 0;
  938. }
  939. EXPORT_SYMBOL_GPL(__dax_zero_page_range);
  940. static loff_t
  941. dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
  942. struct iomap *iomap)
  943. {
  944. struct block_device *bdev = iomap->bdev;
  945. struct dax_device *dax_dev = iomap->dax_dev;
  946. struct iov_iter *iter = data;
  947. loff_t end = pos + length, done = 0;
  948. ssize_t ret = 0;
  949. size_t xfer;
  950. int id;
  951. if (iov_iter_rw(iter) == READ) {
  952. end = min(end, i_size_read(inode));
  953. if (pos >= end)
  954. return 0;
  955. if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
  956. return iov_iter_zero(min(length, end - pos), iter);
  957. }
  958. if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
  959. return -EIO;
  960. /*
  961. * Write can allocate block for an area which has a hole page mapped
  962. * into page tables. We have to tear down these mappings so that data
  963. * written by write(2) is visible in mmap.
  964. */
  965. if (iomap->flags & IOMAP_F_NEW) {
  966. invalidate_inode_pages2_range(inode->i_mapping,
  967. pos >> PAGE_SHIFT,
  968. (end - 1) >> PAGE_SHIFT);
  969. }
  970. id = dax_read_lock();
  971. while (pos < end) {
  972. unsigned offset = pos & (PAGE_SIZE - 1);
  973. const size_t size = ALIGN(length + offset, PAGE_SIZE);
  974. const sector_t sector = dax_iomap_sector(iomap, pos);
  975. ssize_t map_len;
  976. pgoff_t pgoff;
  977. void *kaddr;
  978. pfn_t pfn;
  979. if (fatal_signal_pending(current)) {
  980. ret = -EINTR;
  981. break;
  982. }
  983. ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
  984. if (ret)
  985. break;
  986. map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size),
  987. &kaddr, &pfn);
  988. if (map_len < 0) {
  989. ret = map_len;
  990. break;
  991. }
  992. map_len = PFN_PHYS(map_len);
  993. kaddr += offset;
  994. map_len -= offset;
  995. if (map_len > end - pos)
  996. map_len = end - pos;
  997. /*
  998. * The userspace address for the memory copy has already been
  999. * validated via access_ok() in either vfs_read() or
  1000. * vfs_write(), depending on which operation we are doing.
  1001. */
  1002. if (iov_iter_rw(iter) == WRITE)
  1003. xfer = dax_copy_from_iter(dax_dev, pgoff, kaddr,
  1004. map_len, iter);
  1005. else
  1006. xfer = dax_copy_to_iter(dax_dev, pgoff, kaddr,
  1007. map_len, iter);
  1008. pos += xfer;
  1009. length -= xfer;
  1010. done += xfer;
  1011. if (xfer == 0)
  1012. ret = -EFAULT;
  1013. if (xfer < map_len)
  1014. break;
  1015. }
  1016. dax_read_unlock(id);
  1017. return done ? done : ret;
  1018. }
  1019. /**
  1020. * dax_iomap_rw - Perform I/O to a DAX file
  1021. * @iocb: The control block for this I/O
  1022. * @iter: The addresses to do I/O from or to
  1023. * @ops: iomap ops passed from the file system
  1024. *
  1025. * This function performs read and write operations to directly mapped
  1026. * persistent memory. The callers needs to take care of read/write exclusion
  1027. * and evicting any page cache pages in the region under I/O.
  1028. */
  1029. ssize_t
  1030. dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
  1031. const struct iomap_ops *ops)
  1032. {
  1033. struct address_space *mapping = iocb->ki_filp->f_mapping;
  1034. struct inode *inode = mapping->host;
  1035. loff_t pos = iocb->ki_pos, ret = 0, done = 0;
  1036. unsigned flags = 0;
  1037. if (iov_iter_rw(iter) == WRITE) {
  1038. lockdep_assert_held_exclusive(&inode->i_rwsem);
  1039. flags |= IOMAP_WRITE;
  1040. } else {
  1041. lockdep_assert_held(&inode->i_rwsem);
  1042. }
  1043. while (iov_iter_count(iter)) {
  1044. ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
  1045. iter, dax_iomap_actor);
  1046. if (ret <= 0)
  1047. break;
  1048. pos += ret;
  1049. done += ret;
  1050. }
  1051. iocb->ki_pos += done;
  1052. return done ? done : ret;
  1053. }
  1054. EXPORT_SYMBOL_GPL(dax_iomap_rw);
  1055. static vm_fault_t dax_fault_return(int error)
  1056. {
  1057. if (error == 0)
  1058. return VM_FAULT_NOPAGE;
  1059. if (error == -ENOMEM)
  1060. return VM_FAULT_OOM;
  1061. return VM_FAULT_SIGBUS;
  1062. }
  1063. /*
  1064. * MAP_SYNC on a dax mapping guarantees dirty metadata is
  1065. * flushed on write-faults (non-cow), but not read-faults.
  1066. */
  1067. static bool dax_fault_is_synchronous(unsigned long flags,
  1068. struct vm_area_struct *vma, struct iomap *iomap)
  1069. {
  1070. return (flags & IOMAP_WRITE) && (vma->vm_flags & VM_SYNC)
  1071. && (iomap->flags & IOMAP_F_DIRTY);
  1072. }
  1073. static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp,
  1074. int *iomap_errp, const struct iomap_ops *ops)
  1075. {
  1076. struct vm_area_struct *vma = vmf->vma;
  1077. struct address_space *mapping = vma->vm_file->f_mapping;
  1078. struct inode *inode = mapping->host;
  1079. unsigned long vaddr = vmf->address;
  1080. loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
  1081. struct iomap iomap = { 0 };
  1082. unsigned flags = IOMAP_FAULT;
  1083. int error, major = 0;
  1084. bool write = vmf->flags & FAULT_FLAG_WRITE;
  1085. bool sync;
  1086. vm_fault_t ret = 0;
  1087. void *entry;
  1088. pfn_t pfn;
  1089. trace_dax_pte_fault(inode, vmf, ret);
  1090. /*
  1091. * Check whether offset isn't beyond end of file now. Caller is supposed
  1092. * to hold locks serializing us with truncate / punch hole so this is
  1093. * a reliable test.
  1094. */
  1095. if (pos >= i_size_read(inode)) {
  1096. ret = VM_FAULT_SIGBUS;
  1097. goto out;
  1098. }
  1099. if (write && !vmf->cow_page)
  1100. flags |= IOMAP_WRITE;
  1101. entry = grab_mapping_entry(mapping, vmf->pgoff, 0);
  1102. if (IS_ERR(entry)) {
  1103. ret = dax_fault_return(PTR_ERR(entry));
  1104. goto out;
  1105. }
  1106. /*
  1107. * It is possible, particularly with mixed reads & writes to private
  1108. * mappings, that we have raced with a PMD fault that overlaps with
  1109. * the PTE we need to set up. If so just return and the fault will be
  1110. * retried.
  1111. */
  1112. if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) {
  1113. ret = VM_FAULT_NOPAGE;
  1114. goto unlock_entry;
  1115. }
  1116. /*
  1117. * Note that we don't bother to use iomap_apply here: DAX required
  1118. * the file system block size to be equal the page size, which means
  1119. * that we never have to deal with more than a single extent here.
  1120. */
  1121. error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
  1122. if (iomap_errp)
  1123. *iomap_errp = error;
  1124. if (error) {
  1125. ret = dax_fault_return(error);
  1126. goto unlock_entry;
  1127. }
  1128. if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
  1129. error = -EIO; /* fs corruption? */
  1130. goto error_finish_iomap;
  1131. }
  1132. if (vmf->cow_page) {
  1133. sector_t sector = dax_iomap_sector(&iomap, pos);
  1134. switch (iomap.type) {
  1135. case IOMAP_HOLE:
  1136. case IOMAP_UNWRITTEN:
  1137. clear_user_highpage(vmf->cow_page, vaddr);
  1138. break;
  1139. case IOMAP_MAPPED:
  1140. error = copy_user_dax(iomap.bdev, iomap.dax_dev,
  1141. sector, PAGE_SIZE, vmf->cow_page, vaddr);
  1142. break;
  1143. default:
  1144. WARN_ON_ONCE(1);
  1145. error = -EIO;
  1146. break;
  1147. }
  1148. if (error)
  1149. goto error_finish_iomap;
  1150. __SetPageUptodate(vmf->cow_page);
  1151. ret = finish_fault(vmf);
  1152. if (!ret)
  1153. ret = VM_FAULT_DONE_COW;
  1154. goto finish_iomap;
  1155. }
  1156. sync = dax_fault_is_synchronous(flags, vma, &iomap);
  1157. switch (iomap.type) {
  1158. case IOMAP_MAPPED:
  1159. if (iomap.flags & IOMAP_F_NEW) {
  1160. count_vm_event(PGMAJFAULT);
  1161. count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
  1162. major = VM_FAULT_MAJOR;
  1163. }
  1164. error = dax_iomap_pfn(&iomap, pos, PAGE_SIZE, &pfn);
  1165. if (error < 0)
  1166. goto error_finish_iomap;
  1167. entry = dax_insert_mapping_entry(mapping, vmf, entry, pfn,
  1168. 0, write && !sync);
  1169. /*
  1170. * If we are doing synchronous page fault and inode needs fsync,
  1171. * we can insert PTE into page tables only after that happens.
  1172. * Skip insertion for now and return the pfn so that caller can
  1173. * insert it after fsync is done.
  1174. */
  1175. if (sync) {
  1176. if (WARN_ON_ONCE(!pfnp)) {
  1177. error = -EIO;
  1178. goto error_finish_iomap;
  1179. }
  1180. *pfnp = pfn;
  1181. ret = VM_FAULT_NEEDDSYNC | major;
  1182. goto finish_iomap;
  1183. }
  1184. trace_dax_insert_mapping(inode, vmf, entry);
  1185. if (write)
  1186. ret = vmf_insert_mixed_mkwrite(vma, vaddr, pfn);
  1187. else
  1188. ret = vmf_insert_mixed(vma, vaddr, pfn);
  1189. goto finish_iomap;
  1190. case IOMAP_UNWRITTEN:
  1191. case IOMAP_HOLE:
  1192. if (!write) {
  1193. ret = dax_load_hole(mapping, entry, vmf);
  1194. goto finish_iomap;
  1195. }
  1196. /*FALLTHRU*/
  1197. default:
  1198. WARN_ON_ONCE(1);
  1199. error = -EIO;
  1200. break;
  1201. }
  1202. error_finish_iomap:
  1203. ret = dax_fault_return(error);
  1204. finish_iomap:
  1205. if (ops->iomap_end) {
  1206. int copied = PAGE_SIZE;
  1207. if (ret & VM_FAULT_ERROR)
  1208. copied = 0;
  1209. /*
  1210. * The fault is done by now and there's no way back (other
  1211. * thread may be already happily using PTE we have installed).
  1212. * Just ignore error from ->iomap_end since we cannot do much
  1213. * with it.
  1214. */
  1215. ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap);
  1216. }
  1217. unlock_entry:
  1218. put_locked_mapping_entry(mapping, vmf->pgoff);
  1219. out:
  1220. trace_dax_pte_fault_done(inode, vmf, ret);
  1221. return ret | major;
  1222. }
  1223. #ifdef CONFIG_FS_DAX_PMD
  1224. static vm_fault_t dax_pmd_load_hole(struct vm_fault *vmf, struct iomap *iomap,
  1225. void *entry)
  1226. {
  1227. struct address_space *mapping = vmf->vma->vm_file->f_mapping;
  1228. unsigned long pmd_addr = vmf->address & PMD_MASK;
  1229. struct inode *inode = mapping->host;
  1230. struct page *zero_page;
  1231. void *ret = NULL;
  1232. spinlock_t *ptl;
  1233. pmd_t pmd_entry;
  1234. pfn_t pfn;
  1235. zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm);
  1236. if (unlikely(!zero_page))
  1237. goto fallback;
  1238. pfn = page_to_pfn_t(zero_page);
  1239. ret = dax_insert_mapping_entry(mapping, vmf, entry, pfn,
  1240. RADIX_DAX_PMD | RADIX_DAX_ZERO_PAGE, false);
  1241. ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
  1242. if (!pmd_none(*(vmf->pmd))) {
  1243. spin_unlock(ptl);
  1244. goto fallback;
  1245. }
  1246. pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot);
  1247. pmd_entry = pmd_mkhuge(pmd_entry);
  1248. set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry);
  1249. spin_unlock(ptl);
  1250. trace_dax_pmd_load_hole(inode, vmf, zero_page, ret);
  1251. return VM_FAULT_NOPAGE;
  1252. fallback:
  1253. trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, ret);
  1254. return VM_FAULT_FALLBACK;
  1255. }
  1256. static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
  1257. const struct iomap_ops *ops)
  1258. {
  1259. struct vm_area_struct *vma = vmf->vma;
  1260. struct address_space *mapping = vma->vm_file->f_mapping;
  1261. unsigned long pmd_addr = vmf->address & PMD_MASK;
  1262. bool write = vmf->flags & FAULT_FLAG_WRITE;
  1263. bool sync;
  1264. unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT;
  1265. struct inode *inode = mapping->host;
  1266. vm_fault_t result = VM_FAULT_FALLBACK;
  1267. struct iomap iomap = { 0 };
  1268. pgoff_t max_pgoff, pgoff;
  1269. void *entry;
  1270. loff_t pos;
  1271. int error;
  1272. pfn_t pfn;
  1273. /*
  1274. * Check whether offset isn't beyond end of file now. Caller is
  1275. * supposed to hold locks serializing us with truncate / punch hole so
  1276. * this is a reliable test.
  1277. */
  1278. pgoff = linear_page_index(vma, pmd_addr);
  1279. max_pgoff = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
  1280. trace_dax_pmd_fault(inode, vmf, max_pgoff, 0);
  1281. /*
  1282. * Make sure that the faulting address's PMD offset (color) matches
  1283. * the PMD offset from the start of the file. This is necessary so
  1284. * that a PMD range in the page table overlaps exactly with a PMD
  1285. * range in the radix tree.
  1286. */
  1287. if ((vmf->pgoff & PG_PMD_COLOUR) !=
  1288. ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR))
  1289. goto fallback;
  1290. /* Fall back to PTEs if we're going to COW */
  1291. if (write && !(vma->vm_flags & VM_SHARED))
  1292. goto fallback;
  1293. /* If the PMD would extend outside the VMA */
  1294. if (pmd_addr < vma->vm_start)
  1295. goto fallback;
  1296. if ((pmd_addr + PMD_SIZE) > vma->vm_end)
  1297. goto fallback;
  1298. if (pgoff >= max_pgoff) {
  1299. result = VM_FAULT_SIGBUS;
  1300. goto out;
  1301. }
  1302. /* If the PMD would extend beyond the file size */
  1303. if ((pgoff | PG_PMD_COLOUR) >= max_pgoff)
  1304. goto fallback;
  1305. /*
  1306. * grab_mapping_entry() will make sure we get a 2MiB empty entry, a
  1307. * 2MiB zero page entry or a DAX PMD. If it can't (because a 4k page
  1308. * is already in the tree, for instance), it will return -EEXIST and
  1309. * we just fall back to 4k entries.
  1310. */
  1311. entry = grab_mapping_entry(mapping, pgoff, RADIX_DAX_PMD);
  1312. if (IS_ERR(entry))
  1313. goto fallback;
  1314. /*
  1315. * It is possible, particularly with mixed reads & writes to private
  1316. * mappings, that we have raced with a PTE fault that overlaps with
  1317. * the PMD we need to set up. If so just return and the fault will be
  1318. * retried.
  1319. */
  1320. if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) &&
  1321. !pmd_devmap(*vmf->pmd)) {
  1322. result = 0;
  1323. goto unlock_entry;
  1324. }
  1325. /*
  1326. * Note that we don't use iomap_apply here. We aren't doing I/O, only
  1327. * setting up a mapping, so really we're using iomap_begin() as a way
  1328. * to look up our filesystem block.
  1329. */
  1330. pos = (loff_t)pgoff << PAGE_SHIFT;
  1331. error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap);
  1332. if (error)
  1333. goto unlock_entry;
  1334. if (iomap.offset + iomap.length < pos + PMD_SIZE)
  1335. goto finish_iomap;
  1336. sync = dax_fault_is_synchronous(iomap_flags, vma, &iomap);
  1337. switch (iomap.type) {
  1338. case IOMAP_MAPPED:
  1339. error = dax_iomap_pfn(&iomap, pos, PMD_SIZE, &pfn);
  1340. if (error < 0)
  1341. goto finish_iomap;
  1342. entry = dax_insert_mapping_entry(mapping, vmf, entry, pfn,
  1343. RADIX_DAX_PMD, write && !sync);
  1344. /*
  1345. * If we are doing synchronous page fault and inode needs fsync,
  1346. * we can insert PMD into page tables only after that happens.
  1347. * Skip insertion for now and return the pfn so that caller can
  1348. * insert it after fsync is done.
  1349. */
  1350. if (sync) {
  1351. if (WARN_ON_ONCE(!pfnp))
  1352. goto finish_iomap;
  1353. *pfnp = pfn;
  1354. result = VM_FAULT_NEEDDSYNC;
  1355. goto finish_iomap;
  1356. }
  1357. trace_dax_pmd_insert_mapping(inode, vmf, PMD_SIZE, pfn, entry);
  1358. result = vmf_insert_pfn_pmd(vma, vmf->address, vmf->pmd, pfn,
  1359. write);
  1360. break;
  1361. case IOMAP_UNWRITTEN:
  1362. case IOMAP_HOLE:
  1363. if (WARN_ON_ONCE(write))
  1364. break;
  1365. result = dax_pmd_load_hole(vmf, &iomap, entry);
  1366. break;
  1367. default:
  1368. WARN_ON_ONCE(1);
  1369. break;
  1370. }
  1371. finish_iomap:
  1372. if (ops->iomap_end) {
  1373. int copied = PMD_SIZE;
  1374. if (result == VM_FAULT_FALLBACK)
  1375. copied = 0;
  1376. /*
  1377. * The fault is done by now and there's no way back (other
  1378. * thread may be already happily using PMD we have installed).
  1379. * Just ignore error from ->iomap_end since we cannot do much
  1380. * with it.
  1381. */
  1382. ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags,
  1383. &iomap);
  1384. }
  1385. unlock_entry:
  1386. put_locked_mapping_entry(mapping, pgoff);
  1387. fallback:
  1388. if (result == VM_FAULT_FALLBACK) {
  1389. split_huge_pmd(vma, vmf->pmd, vmf->address);
  1390. count_vm_event(THP_FAULT_FALLBACK);
  1391. }
  1392. out:
  1393. trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result);
  1394. return result;
  1395. }
  1396. #else
  1397. static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
  1398. const struct iomap_ops *ops)
  1399. {
  1400. return VM_FAULT_FALLBACK;
  1401. }
  1402. #endif /* CONFIG_FS_DAX_PMD */
  1403. /**
  1404. * dax_iomap_fault - handle a page fault on a DAX file
  1405. * @vmf: The description of the fault
  1406. * @pe_size: Size of the page to fault in
  1407. * @pfnp: PFN to insert for synchronous faults if fsync is required
  1408. * @iomap_errp: Storage for detailed error code in case of error
  1409. * @ops: Iomap ops passed from the file system
  1410. *
  1411. * When a page fault occurs, filesystems may call this helper in
  1412. * their fault handler for DAX files. dax_iomap_fault() assumes the caller
  1413. * has done all the necessary locking for page fault to proceed
  1414. * successfully.
  1415. */
  1416. vm_fault_t dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
  1417. pfn_t *pfnp, int *iomap_errp, const struct iomap_ops *ops)
  1418. {
  1419. switch (pe_size) {
  1420. case PE_SIZE_PTE:
  1421. return dax_iomap_pte_fault(vmf, pfnp, iomap_errp, ops);
  1422. case PE_SIZE_PMD:
  1423. return dax_iomap_pmd_fault(vmf, pfnp, ops);
  1424. default:
  1425. return VM_FAULT_FALLBACK;
  1426. }
  1427. }
  1428. EXPORT_SYMBOL_GPL(dax_iomap_fault);
  1429. /**
  1430. * dax_insert_pfn_mkwrite - insert PTE or PMD entry into page tables
  1431. * @vmf: The description of the fault
  1432. * @pe_size: Size of entry to be inserted
  1433. * @pfn: PFN to insert
  1434. *
  1435. * This function inserts writeable PTE or PMD entry into page tables for mmaped
  1436. * DAX file. It takes care of marking corresponding radix tree entry as dirty
  1437. * as well.
  1438. */
  1439. static vm_fault_t dax_insert_pfn_mkwrite(struct vm_fault *vmf,
  1440. enum page_entry_size pe_size,
  1441. pfn_t pfn)
  1442. {
  1443. struct address_space *mapping = vmf->vma->vm_file->f_mapping;
  1444. void *entry, **slot;
  1445. pgoff_t index = vmf->pgoff;
  1446. vm_fault_t ret;
  1447. xa_lock_irq(&mapping->i_pages);
  1448. entry = get_unlocked_mapping_entry(mapping, index, &slot);
  1449. /* Did we race with someone splitting entry or so? */
  1450. if (!entry ||
  1451. (pe_size == PE_SIZE_PTE && !dax_is_pte_entry(entry)) ||
  1452. (pe_size == PE_SIZE_PMD && !dax_is_pmd_entry(entry))) {
  1453. put_unlocked_mapping_entry(mapping, index, entry);
  1454. xa_unlock_irq(&mapping->i_pages);
  1455. trace_dax_insert_pfn_mkwrite_no_entry(mapping->host, vmf,
  1456. VM_FAULT_NOPAGE);
  1457. return VM_FAULT_NOPAGE;
  1458. }
  1459. radix_tree_tag_set(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY);
  1460. entry = lock_slot(mapping, slot);
  1461. xa_unlock_irq(&mapping->i_pages);
  1462. switch (pe_size) {
  1463. case PE_SIZE_PTE:
  1464. ret = vmf_insert_mixed_mkwrite(vmf->vma, vmf->address, pfn);
  1465. break;
  1466. #ifdef CONFIG_FS_DAX_PMD
  1467. case PE_SIZE_PMD:
  1468. ret = vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd,
  1469. pfn, true);
  1470. break;
  1471. #endif
  1472. default:
  1473. ret = VM_FAULT_FALLBACK;
  1474. }
  1475. put_locked_mapping_entry(mapping, index);
  1476. trace_dax_insert_pfn_mkwrite(mapping->host, vmf, ret);
  1477. return ret;
  1478. }
  1479. /**
  1480. * dax_finish_sync_fault - finish synchronous page fault
  1481. * @vmf: The description of the fault
  1482. * @pe_size: Size of entry to be inserted
  1483. * @pfn: PFN to insert
  1484. *
  1485. * This function ensures that the file range touched by the page fault is
  1486. * stored persistently on the media and handles inserting of appropriate page
  1487. * table entry.
  1488. */
  1489. vm_fault_t dax_finish_sync_fault(struct vm_fault *vmf,
  1490. enum page_entry_size pe_size, pfn_t pfn)
  1491. {
  1492. int err;
  1493. loff_t start = ((loff_t)vmf->pgoff) << PAGE_SHIFT;
  1494. size_t len = 0;
  1495. if (pe_size == PE_SIZE_PTE)
  1496. len = PAGE_SIZE;
  1497. else if (pe_size == PE_SIZE_PMD)
  1498. len = PMD_SIZE;
  1499. else
  1500. WARN_ON_ONCE(1);
  1501. err = vfs_fsync_range(vmf->vma->vm_file, start, start + len - 1, 1);
  1502. if (err)
  1503. return VM_FAULT_SIGBUS;
  1504. return dax_insert_pfn_mkwrite(vmf, pe_size, pfn);
  1505. }
  1506. EXPORT_SYMBOL_GPL(dax_finish_sync_fault);