dax.c 41 KB

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