dax.c 41 KB

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