dax.c 41 KB

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