dax.c 44 KB

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