dax.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  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 easily possible. This handles DAX
  414. * entries for invalidate_inode_pages() so we evict the entry only if we can
  415. * do so without blocking.
  416. */
  417. int dax_invalidate_mapping_entry(struct address_space *mapping, pgoff_t index)
  418. {
  419. int ret = 0;
  420. void *entry, **slot;
  421. struct radix_tree_root *page_tree = &mapping->page_tree;
  422. spin_lock_irq(&mapping->tree_lock);
  423. entry = __radix_tree_lookup(page_tree, index, NULL, &slot);
  424. if (!entry || !radix_tree_exceptional_entry(entry) ||
  425. slot_locked(mapping, slot))
  426. goto out;
  427. if (radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_DIRTY) ||
  428. radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
  429. goto out;
  430. radix_tree_delete(page_tree, index);
  431. mapping->nrexceptional--;
  432. ret = 1;
  433. out:
  434. spin_unlock_irq(&mapping->tree_lock);
  435. if (ret)
  436. dax_wake_mapping_entry_waiter(mapping, index, entry, true);
  437. return ret;
  438. }
  439. /*
  440. * Invalidate exceptional DAX entry if it is clean.
  441. */
  442. int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
  443. pgoff_t index)
  444. {
  445. return __dax_invalidate_mapping_entry(mapping, index, false);
  446. }
  447. /*
  448. * The user has performed a load from a hole in the file. Allocating
  449. * a new page in the file would cause excessive storage usage for
  450. * workloads with sparse files. We allocate a page cache page instead.
  451. * We'll kick it out of the page cache if it's ever written to,
  452. * otherwise it will simply fall out of the page cache under memory
  453. * pressure without ever having been dirtied.
  454. */
  455. static int dax_load_hole(struct address_space *mapping, void **entry,
  456. struct vm_fault *vmf)
  457. {
  458. struct page *page;
  459. int ret;
  460. /* Hole page already exists? Return it... */
  461. if (!radix_tree_exceptional_entry(*entry)) {
  462. page = *entry;
  463. goto out;
  464. }
  465. /* This will replace locked radix tree entry with a hole page */
  466. page = find_or_create_page(mapping, vmf->pgoff,
  467. vmf->gfp_mask | __GFP_ZERO);
  468. if (!page)
  469. return VM_FAULT_OOM;
  470. out:
  471. vmf->page = page;
  472. ret = finish_fault(vmf);
  473. vmf->page = NULL;
  474. *entry = page;
  475. if (!ret) {
  476. /* Grab reference for PTE that is now referencing the page */
  477. get_page(page);
  478. return VM_FAULT_NOPAGE;
  479. }
  480. return ret;
  481. }
  482. static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev,
  483. sector_t sector, size_t size, struct page *to,
  484. unsigned long vaddr)
  485. {
  486. void *vto, *kaddr;
  487. pgoff_t pgoff;
  488. pfn_t pfn;
  489. long rc;
  490. int id;
  491. rc = bdev_dax_pgoff(bdev, sector, size, &pgoff);
  492. if (rc)
  493. return rc;
  494. id = dax_read_lock();
  495. rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, &pfn);
  496. if (rc < 0) {
  497. dax_read_unlock(id);
  498. return rc;
  499. }
  500. vto = kmap_atomic(to);
  501. copy_user_page(vto, (void __force *)kaddr, vaddr, to);
  502. kunmap_atomic(vto);
  503. dax_read_unlock(id);
  504. return 0;
  505. }
  506. /*
  507. * By this point grab_mapping_entry() has ensured that we have a locked entry
  508. * of the appropriate size so we don't have to worry about downgrading PMDs to
  509. * PTEs. If we happen to be trying to insert a PTE and there is a PMD
  510. * already in the tree, we will skip the insertion and just dirty the PMD as
  511. * appropriate.
  512. */
  513. static void *dax_insert_mapping_entry(struct address_space *mapping,
  514. struct vm_fault *vmf,
  515. void *entry, sector_t sector,
  516. unsigned long flags)
  517. {
  518. struct radix_tree_root *page_tree = &mapping->page_tree;
  519. int error = 0;
  520. bool hole_fill = false;
  521. void *new_entry;
  522. pgoff_t index = vmf->pgoff;
  523. if (vmf->flags & FAULT_FLAG_WRITE)
  524. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  525. /* Replacing hole page with block mapping? */
  526. if (!radix_tree_exceptional_entry(entry)) {
  527. hole_fill = true;
  528. /*
  529. * Unmap the page now before we remove it from page cache below.
  530. * The page is locked so it cannot be faulted in again.
  531. */
  532. unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
  533. PAGE_SIZE, 0);
  534. error = radix_tree_preload(vmf->gfp_mask & ~__GFP_HIGHMEM);
  535. if (error)
  536. return ERR_PTR(error);
  537. } else if (dax_is_zero_entry(entry) && !(flags & RADIX_DAX_HZP)) {
  538. /* replacing huge zero page with PMD block mapping */
  539. unmap_mapping_range(mapping,
  540. (vmf->pgoff << PAGE_SHIFT) & PMD_MASK, PMD_SIZE, 0);
  541. }
  542. spin_lock_irq(&mapping->tree_lock);
  543. new_entry = dax_radix_locked_entry(sector, flags);
  544. if (hole_fill) {
  545. __delete_from_page_cache(entry, NULL);
  546. /* Drop pagecache reference */
  547. put_page(entry);
  548. error = __radix_tree_insert(page_tree, index,
  549. dax_radix_order(new_entry), new_entry);
  550. if (error) {
  551. new_entry = ERR_PTR(error);
  552. goto unlock;
  553. }
  554. mapping->nrexceptional++;
  555. } else if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) {
  556. /*
  557. * Only swap our new entry into the radix tree if the current
  558. * entry is a zero page or an empty entry. If a normal PTE or
  559. * PMD entry is already in the tree, we leave it alone. This
  560. * means that if we are trying to insert a PTE and the
  561. * existing entry is a PMD, we will just leave the PMD in the
  562. * tree and dirty it if necessary.
  563. */
  564. struct radix_tree_node *node;
  565. void **slot;
  566. void *ret;
  567. ret = __radix_tree_lookup(page_tree, index, &node, &slot);
  568. WARN_ON_ONCE(ret != entry);
  569. __radix_tree_replace(page_tree, node, slot,
  570. new_entry, NULL, NULL);
  571. }
  572. if (vmf->flags & FAULT_FLAG_WRITE)
  573. radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
  574. unlock:
  575. spin_unlock_irq(&mapping->tree_lock);
  576. if (hole_fill) {
  577. radix_tree_preload_end();
  578. /*
  579. * We don't need hole page anymore, it has been replaced with
  580. * locked radix tree entry now.
  581. */
  582. if (mapping->a_ops->freepage)
  583. mapping->a_ops->freepage(entry);
  584. unlock_page(entry);
  585. put_page(entry);
  586. }
  587. return new_entry;
  588. }
  589. static inline unsigned long
  590. pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma)
  591. {
  592. unsigned long address;
  593. address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  594. VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
  595. return address;
  596. }
  597. /* Walk all mappings of a given index of a file and writeprotect them */
  598. static void dax_mapping_entry_mkclean(struct address_space *mapping,
  599. pgoff_t index, unsigned long pfn)
  600. {
  601. struct vm_area_struct *vma;
  602. pte_t pte, *ptep = NULL;
  603. pmd_t *pmdp = NULL;
  604. spinlock_t *ptl;
  605. bool changed;
  606. i_mmap_lock_read(mapping);
  607. vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) {
  608. unsigned long address;
  609. cond_resched();
  610. if (!(vma->vm_flags & VM_SHARED))
  611. continue;
  612. address = pgoff_address(index, vma);
  613. changed = false;
  614. if (follow_pte_pmd(vma->vm_mm, address, &ptep, &pmdp, &ptl))
  615. continue;
  616. if (pmdp) {
  617. #ifdef CONFIG_FS_DAX_PMD
  618. pmd_t pmd;
  619. if (pfn != pmd_pfn(*pmdp))
  620. goto unlock_pmd;
  621. if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp))
  622. goto unlock_pmd;
  623. flush_cache_page(vma, address, pfn);
  624. pmd = pmdp_huge_clear_flush(vma, address, pmdp);
  625. pmd = pmd_wrprotect(pmd);
  626. pmd = pmd_mkclean(pmd);
  627. set_pmd_at(vma->vm_mm, address, pmdp, pmd);
  628. changed = true;
  629. unlock_pmd:
  630. spin_unlock(ptl);
  631. #endif
  632. } else {
  633. if (pfn != pte_pfn(*ptep))
  634. goto unlock_pte;
  635. if (!pte_dirty(*ptep) && !pte_write(*ptep))
  636. goto unlock_pte;
  637. flush_cache_page(vma, address, pfn);
  638. pte = ptep_clear_flush(vma, address, ptep);
  639. pte = pte_wrprotect(pte);
  640. pte = pte_mkclean(pte);
  641. set_pte_at(vma->vm_mm, address, ptep, pte);
  642. changed = true;
  643. unlock_pte:
  644. pte_unmap_unlock(ptep, ptl);
  645. }
  646. if (changed)
  647. mmu_notifier_invalidate_page(vma->vm_mm, address);
  648. }
  649. i_mmap_unlock_read(mapping);
  650. }
  651. static int dax_writeback_one(struct block_device *bdev,
  652. struct dax_device *dax_dev, struct address_space *mapping,
  653. pgoff_t index, void *entry)
  654. {
  655. struct radix_tree_root *page_tree = &mapping->page_tree;
  656. void *entry2, **slot, *kaddr;
  657. long ret = 0, id;
  658. sector_t sector;
  659. pgoff_t pgoff;
  660. size_t size;
  661. pfn_t pfn;
  662. /*
  663. * A page got tagged dirty in DAX mapping? Something is seriously
  664. * wrong.
  665. */
  666. if (WARN_ON(!radix_tree_exceptional_entry(entry)))
  667. return -EIO;
  668. spin_lock_irq(&mapping->tree_lock);
  669. entry2 = get_unlocked_mapping_entry(mapping, index, &slot);
  670. /* Entry got punched out / reallocated? */
  671. if (!entry2 || !radix_tree_exceptional_entry(entry2))
  672. goto put_unlocked;
  673. /*
  674. * Entry got reallocated elsewhere? No need to writeback. We have to
  675. * compare sectors as we must not bail out due to difference in lockbit
  676. * or entry type.
  677. */
  678. if (dax_radix_sector(entry2) != dax_radix_sector(entry))
  679. goto put_unlocked;
  680. if (WARN_ON_ONCE(dax_is_empty_entry(entry) ||
  681. dax_is_zero_entry(entry))) {
  682. ret = -EIO;
  683. goto put_unlocked;
  684. }
  685. /* Another fsync thread may have already written back this entry */
  686. if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
  687. goto put_unlocked;
  688. /* Lock the entry to serialize with page faults */
  689. entry = lock_slot(mapping, slot);
  690. /*
  691. * We can clear the tag now but we have to be careful so that concurrent
  692. * dax_writeback_one() calls for the same index cannot finish before we
  693. * actually flush the caches. This is achieved as the calls will look
  694. * at the entry only under tree_lock and once they do that they will
  695. * see the entry locked and wait for it to unlock.
  696. */
  697. radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE);
  698. spin_unlock_irq(&mapping->tree_lock);
  699. /*
  700. * Even if dax_writeback_mapping_range() was given a wbc->range_start
  701. * in the middle of a PMD, the 'index' we are given will be aligned to
  702. * the start index of the PMD, as will the sector we pull from
  703. * 'entry'. This allows us to flush for PMD_SIZE and not have to
  704. * worry about partial PMD writebacks.
  705. */
  706. sector = dax_radix_sector(entry);
  707. size = PAGE_SIZE << dax_radix_order(entry);
  708. id = dax_read_lock();
  709. ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
  710. if (ret)
  711. goto dax_unlock;
  712. /*
  713. * dax_direct_access() may sleep, so cannot hold tree_lock over
  714. * its invocation.
  715. */
  716. ret = dax_direct_access(dax_dev, pgoff, size / PAGE_SIZE, &kaddr, &pfn);
  717. if (ret < 0)
  718. goto dax_unlock;
  719. if (WARN_ON_ONCE(ret < size / PAGE_SIZE)) {
  720. ret = -EIO;
  721. goto dax_unlock;
  722. }
  723. dax_mapping_entry_mkclean(mapping, index, pfn_t_to_pfn(pfn));
  724. wb_cache_pmem(kaddr, size);
  725. /*
  726. * After we have flushed the cache, we can clear the dirty tag. There
  727. * cannot be new dirty data in the pfn after the flush has completed as
  728. * the pfn mappings are writeprotected and fault waits for mapping
  729. * entry lock.
  730. */
  731. spin_lock_irq(&mapping->tree_lock);
  732. radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_DIRTY);
  733. spin_unlock_irq(&mapping->tree_lock);
  734. dax_unlock:
  735. dax_read_unlock(id);
  736. put_locked_mapping_entry(mapping, index, entry);
  737. return ret;
  738. put_unlocked:
  739. put_unlocked_mapping_entry(mapping, index, entry2);
  740. spin_unlock_irq(&mapping->tree_lock);
  741. return ret;
  742. }
  743. /*
  744. * Flush the mapping to the persistent domain within the byte range of [start,
  745. * end]. This is required by data integrity operations to ensure file data is
  746. * on persistent storage prior to completion of the operation.
  747. */
  748. int dax_writeback_mapping_range(struct address_space *mapping,
  749. struct block_device *bdev, struct writeback_control *wbc)
  750. {
  751. struct inode *inode = mapping->host;
  752. pgoff_t start_index, end_index;
  753. pgoff_t indices[PAGEVEC_SIZE];
  754. struct dax_device *dax_dev;
  755. struct pagevec pvec;
  756. bool done = false;
  757. int i, ret = 0;
  758. if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
  759. return -EIO;
  760. if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
  761. return 0;
  762. dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
  763. if (!dax_dev)
  764. return -EIO;
  765. start_index = wbc->range_start >> PAGE_SHIFT;
  766. end_index = wbc->range_end >> PAGE_SHIFT;
  767. tag_pages_for_writeback(mapping, start_index, end_index);
  768. pagevec_init(&pvec, 0);
  769. while (!done) {
  770. pvec.nr = find_get_entries_tag(mapping, start_index,
  771. PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
  772. pvec.pages, indices);
  773. if (pvec.nr == 0)
  774. break;
  775. for (i = 0; i < pvec.nr; i++) {
  776. if (indices[i] > end_index) {
  777. done = true;
  778. break;
  779. }
  780. ret = dax_writeback_one(bdev, dax_dev, mapping,
  781. indices[i], pvec.pages[i]);
  782. if (ret < 0) {
  783. put_dax(dax_dev);
  784. return ret;
  785. }
  786. }
  787. }
  788. put_dax(dax_dev);
  789. return 0;
  790. }
  791. EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
  792. static int dax_insert_mapping(struct address_space *mapping,
  793. struct block_device *bdev, struct dax_device *dax_dev,
  794. sector_t sector, size_t size, void **entryp,
  795. struct vm_area_struct *vma, struct vm_fault *vmf)
  796. {
  797. unsigned long vaddr = vmf->address;
  798. void *entry = *entryp;
  799. void *ret, *kaddr;
  800. pgoff_t pgoff;
  801. int id, rc;
  802. pfn_t pfn;
  803. rc = bdev_dax_pgoff(bdev, sector, size, &pgoff);
  804. if (rc)
  805. return rc;
  806. id = dax_read_lock();
  807. rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, &pfn);
  808. if (rc < 0) {
  809. dax_read_unlock(id);
  810. return rc;
  811. }
  812. dax_read_unlock(id);
  813. ret = dax_insert_mapping_entry(mapping, vmf, entry, sector, 0);
  814. if (IS_ERR(ret))
  815. return PTR_ERR(ret);
  816. *entryp = ret;
  817. return vm_insert_mixed(vma, vaddr, pfn);
  818. }
  819. /**
  820. * dax_pfn_mkwrite - handle first write to DAX page
  821. * @vmf: The description of the fault
  822. */
  823. int dax_pfn_mkwrite(struct vm_fault *vmf)
  824. {
  825. struct file *file = vmf->vma->vm_file;
  826. struct address_space *mapping = file->f_mapping;
  827. struct inode *inode = mapping->host;
  828. void *entry, **slot;
  829. pgoff_t index = vmf->pgoff;
  830. spin_lock_irq(&mapping->tree_lock);
  831. entry = get_unlocked_mapping_entry(mapping, index, &slot);
  832. if (!entry || !radix_tree_exceptional_entry(entry)) {
  833. if (entry)
  834. put_unlocked_mapping_entry(mapping, index, entry);
  835. spin_unlock_irq(&mapping->tree_lock);
  836. trace_dax_pfn_mkwrite_no_entry(inode, vmf, VM_FAULT_NOPAGE);
  837. return VM_FAULT_NOPAGE;
  838. }
  839. radix_tree_tag_set(&mapping->page_tree, index, PAGECACHE_TAG_DIRTY);
  840. entry = lock_slot(mapping, slot);
  841. spin_unlock_irq(&mapping->tree_lock);
  842. /*
  843. * If we race with somebody updating the PTE and finish_mkwrite_fault()
  844. * fails, we don't care. We need to return VM_FAULT_NOPAGE and retry
  845. * the fault in either case.
  846. */
  847. finish_mkwrite_fault(vmf);
  848. put_locked_mapping_entry(mapping, index, entry);
  849. trace_dax_pfn_mkwrite(inode, vmf, VM_FAULT_NOPAGE);
  850. return VM_FAULT_NOPAGE;
  851. }
  852. EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
  853. static bool dax_range_is_aligned(struct block_device *bdev,
  854. unsigned int offset, unsigned int length)
  855. {
  856. unsigned short sector_size = bdev_logical_block_size(bdev);
  857. if (!IS_ALIGNED(offset, sector_size))
  858. return false;
  859. if (!IS_ALIGNED(length, sector_size))
  860. return false;
  861. return true;
  862. }
  863. int __dax_zero_page_range(struct block_device *bdev,
  864. struct dax_device *dax_dev, sector_t sector,
  865. unsigned int offset, unsigned int size)
  866. {
  867. if (dax_range_is_aligned(bdev, offset, size)) {
  868. sector_t start_sector = sector + (offset >> 9);
  869. return blkdev_issue_zeroout(bdev, start_sector,
  870. size >> 9, GFP_NOFS, 0);
  871. } else {
  872. pgoff_t pgoff;
  873. long rc, id;
  874. void *kaddr;
  875. pfn_t pfn;
  876. rc = bdev_dax_pgoff(bdev, sector, size, &pgoff);
  877. if (rc)
  878. return rc;
  879. id = dax_read_lock();
  880. rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr,
  881. &pfn);
  882. if (rc < 0) {
  883. dax_read_unlock(id);
  884. return rc;
  885. }
  886. clear_pmem(kaddr + offset, size);
  887. dax_read_unlock(id);
  888. }
  889. return 0;
  890. }
  891. EXPORT_SYMBOL_GPL(__dax_zero_page_range);
  892. static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
  893. {
  894. return iomap->blkno + (((pos & PAGE_MASK) - iomap->offset) >> 9);
  895. }
  896. static loff_t
  897. dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
  898. struct iomap *iomap)
  899. {
  900. struct block_device *bdev = iomap->bdev;
  901. struct dax_device *dax_dev = iomap->dax_dev;
  902. struct iov_iter *iter = data;
  903. loff_t end = pos + length, done = 0;
  904. ssize_t ret = 0;
  905. int id;
  906. if (iov_iter_rw(iter) == READ) {
  907. end = min(end, i_size_read(inode));
  908. if (pos >= end)
  909. return 0;
  910. if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
  911. return iov_iter_zero(min(length, end - pos), iter);
  912. }
  913. if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
  914. return -EIO;
  915. /*
  916. * Write can allocate block for an area which has a hole page mapped
  917. * into page tables. We have to tear down these mappings so that data
  918. * written by write(2) is visible in mmap.
  919. */
  920. if ((iomap->flags & IOMAP_F_NEW) && inode->i_mapping->nrpages) {
  921. invalidate_inode_pages2_range(inode->i_mapping,
  922. pos >> PAGE_SHIFT,
  923. (end - 1) >> PAGE_SHIFT);
  924. }
  925. id = dax_read_lock();
  926. while (pos < end) {
  927. unsigned offset = pos & (PAGE_SIZE - 1);
  928. const size_t size = ALIGN(length + offset, PAGE_SIZE);
  929. const sector_t sector = dax_iomap_sector(iomap, pos);
  930. ssize_t map_len;
  931. pgoff_t pgoff;
  932. void *kaddr;
  933. pfn_t pfn;
  934. if (fatal_signal_pending(current)) {
  935. ret = -EINTR;
  936. break;
  937. }
  938. ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
  939. if (ret)
  940. break;
  941. map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size),
  942. &kaddr, &pfn);
  943. if (map_len < 0) {
  944. ret = map_len;
  945. break;
  946. }
  947. map_len = PFN_PHYS(map_len);
  948. kaddr += offset;
  949. map_len -= offset;
  950. if (map_len > end - pos)
  951. map_len = end - pos;
  952. if (iov_iter_rw(iter) == WRITE)
  953. map_len = copy_from_iter_pmem(kaddr, map_len, iter);
  954. else
  955. map_len = copy_to_iter(kaddr, map_len, iter);
  956. if (map_len <= 0) {
  957. ret = map_len ? map_len : -EFAULT;
  958. break;
  959. }
  960. pos += map_len;
  961. length -= map_len;
  962. done += map_len;
  963. }
  964. dax_read_unlock(id);
  965. return done ? done : ret;
  966. }
  967. /**
  968. * dax_iomap_rw - Perform I/O to a DAX file
  969. * @iocb: The control block for this I/O
  970. * @iter: The addresses to do I/O from or to
  971. * @ops: iomap ops passed from the file system
  972. *
  973. * This function performs read and write operations to directly mapped
  974. * persistent memory. The callers needs to take care of read/write exclusion
  975. * and evicting any page cache pages in the region under I/O.
  976. */
  977. ssize_t
  978. dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
  979. const struct iomap_ops *ops)
  980. {
  981. struct address_space *mapping = iocb->ki_filp->f_mapping;
  982. struct inode *inode = mapping->host;
  983. loff_t pos = iocb->ki_pos, ret = 0, done = 0;
  984. unsigned flags = 0;
  985. if (iov_iter_rw(iter) == WRITE) {
  986. lockdep_assert_held_exclusive(&inode->i_rwsem);
  987. flags |= IOMAP_WRITE;
  988. } else {
  989. lockdep_assert_held(&inode->i_rwsem);
  990. }
  991. while (iov_iter_count(iter)) {
  992. ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
  993. iter, dax_iomap_actor);
  994. if (ret <= 0)
  995. break;
  996. pos += ret;
  997. done += ret;
  998. }
  999. iocb->ki_pos += done;
  1000. return done ? done : ret;
  1001. }
  1002. EXPORT_SYMBOL_GPL(dax_iomap_rw);
  1003. static int dax_fault_return(int error)
  1004. {
  1005. if (error == 0)
  1006. return VM_FAULT_NOPAGE;
  1007. if (error == -ENOMEM)
  1008. return VM_FAULT_OOM;
  1009. return VM_FAULT_SIGBUS;
  1010. }
  1011. static int dax_iomap_pte_fault(struct vm_fault *vmf,
  1012. const struct iomap_ops *ops)
  1013. {
  1014. struct address_space *mapping = vmf->vma->vm_file->f_mapping;
  1015. struct inode *inode = mapping->host;
  1016. unsigned long vaddr = vmf->address;
  1017. loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
  1018. sector_t sector;
  1019. struct iomap iomap = { 0 };
  1020. unsigned flags = IOMAP_FAULT;
  1021. int error, major = 0;
  1022. int vmf_ret = 0;
  1023. void *entry;
  1024. trace_dax_pte_fault(inode, vmf, vmf_ret);
  1025. /*
  1026. * Check whether offset isn't beyond end of file now. Caller is supposed
  1027. * to hold locks serializing us with truncate / punch hole so this is
  1028. * a reliable test.
  1029. */
  1030. if (pos >= i_size_read(inode)) {
  1031. vmf_ret = VM_FAULT_SIGBUS;
  1032. goto out;
  1033. }
  1034. if ((vmf->flags & FAULT_FLAG_WRITE) && !vmf->cow_page)
  1035. flags |= IOMAP_WRITE;
  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 out;
  1045. }
  1046. if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
  1047. vmf_ret = dax_fault_return(-EIO); /* fs corruption? */
  1048. goto finish_iomap;
  1049. }
  1050. entry = grab_mapping_entry(mapping, vmf->pgoff, 0);
  1051. if (IS_ERR(entry)) {
  1052. vmf_ret = dax_fault_return(PTR_ERR(entry));
  1053. goto finish_iomap;
  1054. }
  1055. sector = dax_iomap_sector(&iomap, pos);
  1056. if (vmf->cow_page) {
  1057. switch (iomap.type) {
  1058. case IOMAP_HOLE:
  1059. case IOMAP_UNWRITTEN:
  1060. clear_user_highpage(vmf->cow_page, vaddr);
  1061. break;
  1062. case IOMAP_MAPPED:
  1063. error = copy_user_dax(iomap.bdev, iomap.dax_dev,
  1064. sector, PAGE_SIZE, vmf->cow_page, vaddr);
  1065. break;
  1066. default:
  1067. WARN_ON_ONCE(1);
  1068. error = -EIO;
  1069. break;
  1070. }
  1071. if (error)
  1072. goto error_unlock_entry;
  1073. __SetPageUptodate(vmf->cow_page);
  1074. vmf_ret = finish_fault(vmf);
  1075. if (!vmf_ret)
  1076. vmf_ret = VM_FAULT_DONE_COW;
  1077. goto unlock_entry;
  1078. }
  1079. switch (iomap.type) {
  1080. case IOMAP_MAPPED:
  1081. if (iomap.flags & IOMAP_F_NEW) {
  1082. count_vm_event(PGMAJFAULT);
  1083. mem_cgroup_count_vm_event(vmf->vma->vm_mm, PGMAJFAULT);
  1084. major = VM_FAULT_MAJOR;
  1085. }
  1086. error = dax_insert_mapping(mapping, iomap.bdev, iomap.dax_dev,
  1087. sector, PAGE_SIZE, &entry, vmf->vma, vmf);
  1088. /* -EBUSY is fine, somebody else faulted on the same PTE */
  1089. if (error == -EBUSY)
  1090. error = 0;
  1091. break;
  1092. case IOMAP_UNWRITTEN:
  1093. case IOMAP_HOLE:
  1094. if (!(vmf->flags & FAULT_FLAG_WRITE)) {
  1095. vmf_ret = dax_load_hole(mapping, &entry, vmf);
  1096. goto unlock_entry;
  1097. }
  1098. /*FALLTHRU*/
  1099. default:
  1100. WARN_ON_ONCE(1);
  1101. error = -EIO;
  1102. break;
  1103. }
  1104. error_unlock_entry:
  1105. vmf_ret = dax_fault_return(error) | major;
  1106. unlock_entry:
  1107. put_locked_mapping_entry(mapping, vmf->pgoff, entry);
  1108. finish_iomap:
  1109. if (ops->iomap_end) {
  1110. int copied = PAGE_SIZE;
  1111. if (vmf_ret & VM_FAULT_ERROR)
  1112. copied = 0;
  1113. /*
  1114. * The fault is done by now and there's no way back (other
  1115. * thread may be already happily using PTE we have installed).
  1116. * Just ignore error from ->iomap_end since we cannot do much
  1117. * with it.
  1118. */
  1119. ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap);
  1120. }
  1121. out:
  1122. trace_dax_pte_fault_done(inode, vmf, vmf_ret);
  1123. return vmf_ret;
  1124. }
  1125. #ifdef CONFIG_FS_DAX_PMD
  1126. /*
  1127. * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
  1128. * more often than one might expect in the below functions.
  1129. */
  1130. #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
  1131. static int dax_pmd_insert_mapping(struct vm_fault *vmf, struct iomap *iomap,
  1132. loff_t pos, void **entryp)
  1133. {
  1134. struct address_space *mapping = vmf->vma->vm_file->f_mapping;
  1135. const sector_t sector = dax_iomap_sector(iomap, pos);
  1136. struct dax_device *dax_dev = iomap->dax_dev;
  1137. struct block_device *bdev = iomap->bdev;
  1138. struct inode *inode = mapping->host;
  1139. const size_t size = PMD_SIZE;
  1140. void *ret = NULL, *kaddr;
  1141. long length = 0;
  1142. pgoff_t pgoff;
  1143. pfn_t pfn;
  1144. int id;
  1145. if (bdev_dax_pgoff(bdev, sector, size, &pgoff) != 0)
  1146. goto fallback;
  1147. id = dax_read_lock();
  1148. length = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, &pfn);
  1149. if (length < 0)
  1150. goto unlock_fallback;
  1151. length = PFN_PHYS(length);
  1152. if (length < size)
  1153. goto unlock_fallback;
  1154. if (pfn_t_to_pfn(pfn) & PG_PMD_COLOUR)
  1155. goto unlock_fallback;
  1156. if (!pfn_t_devmap(pfn))
  1157. goto unlock_fallback;
  1158. dax_read_unlock(id);
  1159. ret = dax_insert_mapping_entry(mapping, vmf, *entryp, sector,
  1160. RADIX_DAX_PMD);
  1161. if (IS_ERR(ret))
  1162. goto fallback;
  1163. *entryp = ret;
  1164. trace_dax_pmd_insert_mapping(inode, vmf, length, pfn, ret);
  1165. return vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd,
  1166. pfn, vmf->flags & FAULT_FLAG_WRITE);
  1167. unlock_fallback:
  1168. dax_read_unlock(id);
  1169. fallback:
  1170. trace_dax_pmd_insert_mapping_fallback(inode, vmf, length, pfn, ret);
  1171. return VM_FAULT_FALLBACK;
  1172. }
  1173. static int dax_pmd_load_hole(struct vm_fault *vmf, struct iomap *iomap,
  1174. void **entryp)
  1175. {
  1176. struct address_space *mapping = vmf->vma->vm_file->f_mapping;
  1177. unsigned long pmd_addr = vmf->address & PMD_MASK;
  1178. struct inode *inode = mapping->host;
  1179. struct page *zero_page;
  1180. void *ret = NULL;
  1181. spinlock_t *ptl;
  1182. pmd_t pmd_entry;
  1183. zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm);
  1184. if (unlikely(!zero_page))
  1185. goto fallback;
  1186. ret = dax_insert_mapping_entry(mapping, vmf, *entryp, 0,
  1187. RADIX_DAX_PMD | RADIX_DAX_HZP);
  1188. if (IS_ERR(ret))
  1189. goto fallback;
  1190. *entryp = ret;
  1191. ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
  1192. if (!pmd_none(*(vmf->pmd))) {
  1193. spin_unlock(ptl);
  1194. goto fallback;
  1195. }
  1196. pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot);
  1197. pmd_entry = pmd_mkhuge(pmd_entry);
  1198. set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry);
  1199. spin_unlock(ptl);
  1200. trace_dax_pmd_load_hole(inode, vmf, zero_page, ret);
  1201. return VM_FAULT_NOPAGE;
  1202. fallback:
  1203. trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, ret);
  1204. return VM_FAULT_FALLBACK;
  1205. }
  1206. static int dax_iomap_pmd_fault(struct vm_fault *vmf,
  1207. const struct iomap_ops *ops)
  1208. {
  1209. struct vm_area_struct *vma = vmf->vma;
  1210. struct address_space *mapping = vma->vm_file->f_mapping;
  1211. unsigned long pmd_addr = vmf->address & PMD_MASK;
  1212. bool write = vmf->flags & FAULT_FLAG_WRITE;
  1213. unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT;
  1214. struct inode *inode = mapping->host;
  1215. int result = VM_FAULT_FALLBACK;
  1216. struct iomap iomap = { 0 };
  1217. pgoff_t max_pgoff, pgoff;
  1218. void *entry;
  1219. loff_t pos;
  1220. int error;
  1221. /*
  1222. * Check whether offset isn't beyond end of file now. Caller is
  1223. * supposed to hold locks serializing us with truncate / punch hole so
  1224. * this is a reliable test.
  1225. */
  1226. pgoff = linear_page_index(vma, pmd_addr);
  1227. max_pgoff = (i_size_read(inode) - 1) >> PAGE_SHIFT;
  1228. trace_dax_pmd_fault(inode, vmf, max_pgoff, 0);
  1229. /* Fall back to PTEs if we're going to COW */
  1230. if (write && !(vma->vm_flags & VM_SHARED))
  1231. goto fallback;
  1232. /* If the PMD would extend outside the VMA */
  1233. if (pmd_addr < vma->vm_start)
  1234. goto fallback;
  1235. if ((pmd_addr + PMD_SIZE) > vma->vm_end)
  1236. goto fallback;
  1237. if (pgoff > max_pgoff) {
  1238. result = VM_FAULT_SIGBUS;
  1239. goto out;
  1240. }
  1241. /* If the PMD would extend beyond the file size */
  1242. if ((pgoff | PG_PMD_COLOUR) > max_pgoff)
  1243. goto fallback;
  1244. /*
  1245. * Note that we don't use iomap_apply here. We aren't doing I/O, only
  1246. * setting up a mapping, so really we're using iomap_begin() as a way
  1247. * to look up our filesystem block.
  1248. */
  1249. pos = (loff_t)pgoff << PAGE_SHIFT;
  1250. error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap);
  1251. if (error)
  1252. goto fallback;
  1253. if (iomap.offset + iomap.length < pos + PMD_SIZE)
  1254. goto finish_iomap;
  1255. /*
  1256. * grab_mapping_entry() will make sure we get a 2M empty entry, a DAX
  1257. * PMD or a HZP entry. If it can't (because a 4k page is already in
  1258. * the tree, for instance), it will return -EEXIST and we just fall
  1259. * back to 4k entries.
  1260. */
  1261. entry = grab_mapping_entry(mapping, pgoff, RADIX_DAX_PMD);
  1262. if (IS_ERR(entry))
  1263. goto finish_iomap;
  1264. switch (iomap.type) {
  1265. case IOMAP_MAPPED:
  1266. result = dax_pmd_insert_mapping(vmf, &iomap, pos, &entry);
  1267. break;
  1268. case IOMAP_UNWRITTEN:
  1269. case IOMAP_HOLE:
  1270. if (WARN_ON_ONCE(write))
  1271. goto unlock_entry;
  1272. result = dax_pmd_load_hole(vmf, &iomap, &entry);
  1273. break;
  1274. default:
  1275. WARN_ON_ONCE(1);
  1276. break;
  1277. }
  1278. unlock_entry:
  1279. put_locked_mapping_entry(mapping, pgoff, entry);
  1280. finish_iomap:
  1281. if (ops->iomap_end) {
  1282. int copied = PMD_SIZE;
  1283. if (result == VM_FAULT_FALLBACK)
  1284. copied = 0;
  1285. /*
  1286. * The fault is done by now and there's no way back (other
  1287. * thread may be already happily using PMD we have installed).
  1288. * Just ignore error from ->iomap_end since we cannot do much
  1289. * with it.
  1290. */
  1291. ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags,
  1292. &iomap);
  1293. }
  1294. fallback:
  1295. if (result == VM_FAULT_FALLBACK) {
  1296. split_huge_pmd(vma, vmf->pmd, vmf->address);
  1297. count_vm_event(THP_FAULT_FALLBACK);
  1298. }
  1299. out:
  1300. trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result);
  1301. return result;
  1302. }
  1303. #else
  1304. static int dax_iomap_pmd_fault(struct vm_fault *vmf,
  1305. const struct iomap_ops *ops)
  1306. {
  1307. return VM_FAULT_FALLBACK;
  1308. }
  1309. #endif /* CONFIG_FS_DAX_PMD */
  1310. /**
  1311. * dax_iomap_fault - handle a page fault on a DAX file
  1312. * @vmf: The description of the fault
  1313. * @ops: iomap ops passed from the file system
  1314. *
  1315. * When a page fault occurs, filesystems may call this helper in
  1316. * their fault handler for DAX files. dax_iomap_fault() assumes the caller
  1317. * has done all the necessary locking for page fault to proceed
  1318. * successfully.
  1319. */
  1320. int dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
  1321. const struct iomap_ops *ops)
  1322. {
  1323. switch (pe_size) {
  1324. case PE_SIZE_PTE:
  1325. return dax_iomap_pte_fault(vmf, ops);
  1326. case PE_SIZE_PMD:
  1327. return dax_iomap_pmd_fault(vmf, ops);
  1328. default:
  1329. return VM_FAULT_FALLBACK;
  1330. }
  1331. }
  1332. EXPORT_SYMBOL_GPL(dax_iomap_fault);