dax.c 41 KB

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