dax.c 40 KB

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