dax.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  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. static inline unsigned int pe_order(enum page_entry_size pe_size)
  39. {
  40. if (pe_size == PE_SIZE_PTE)
  41. return PAGE_SHIFT - PAGE_SHIFT;
  42. if (pe_size == PE_SIZE_PMD)
  43. return PMD_SHIFT - PAGE_SHIFT;
  44. if (pe_size == PE_SIZE_PUD)
  45. return PUD_SHIFT - PAGE_SHIFT;
  46. return ~0;
  47. }
  48. /* We choose 4096 entries - same as per-zone page wait tables */
  49. #define DAX_WAIT_TABLE_BITS 12
  50. #define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS)
  51. /* The 'colour' (ie low bits) within a PMD of a page offset. */
  52. #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
  53. #define PG_PMD_NR (PMD_SIZE >> PAGE_SHIFT)
  54. /* The order of a PMD entry */
  55. #define PMD_ORDER (PMD_SHIFT - PAGE_SHIFT)
  56. static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES];
  57. static int __init init_dax_wait_table(void)
  58. {
  59. int i;
  60. for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++)
  61. init_waitqueue_head(wait_table + i);
  62. return 0;
  63. }
  64. fs_initcall(init_dax_wait_table);
  65. /*
  66. * DAX pagecache entries use XArray value entries so they can't be mistaken
  67. * for pages. We use one bit for locking, one bit for the entry size (PMD)
  68. * and two more to tell us if the entry is a zero page or an empty entry that
  69. * is just used for locking. In total four special bits.
  70. *
  71. * If the PMD bit isn't set the entry has size PAGE_SIZE, and if the ZERO_PAGE
  72. * and EMPTY bits aren't set the entry is a normal DAX entry with a filesystem
  73. * block allocation.
  74. */
  75. #define DAX_SHIFT (4)
  76. #define DAX_LOCKED (1UL << 0)
  77. #define DAX_PMD (1UL << 1)
  78. #define DAX_ZERO_PAGE (1UL << 2)
  79. #define DAX_EMPTY (1UL << 3)
  80. static unsigned long dax_to_pfn(void *entry)
  81. {
  82. return xa_to_value(entry) >> DAX_SHIFT;
  83. }
  84. static void *dax_make_entry(pfn_t pfn, unsigned long flags)
  85. {
  86. return xa_mk_value(flags | (pfn_t_to_pfn(pfn) << DAX_SHIFT));
  87. }
  88. static bool dax_is_locked(void *entry)
  89. {
  90. return xa_to_value(entry) & DAX_LOCKED;
  91. }
  92. static unsigned int dax_entry_order(void *entry)
  93. {
  94. if (xa_to_value(entry) & DAX_PMD)
  95. return PMD_ORDER;
  96. return 0;
  97. }
  98. static unsigned long dax_is_pmd_entry(void *entry)
  99. {
  100. return xa_to_value(entry) & DAX_PMD;
  101. }
  102. static bool dax_is_pte_entry(void *entry)
  103. {
  104. return !(xa_to_value(entry) & DAX_PMD);
  105. }
  106. static int dax_is_zero_entry(void *entry)
  107. {
  108. return xa_to_value(entry) & DAX_ZERO_PAGE;
  109. }
  110. static int dax_is_empty_entry(void *entry)
  111. {
  112. return xa_to_value(entry) & DAX_EMPTY;
  113. }
  114. /*
  115. * DAX page cache entry locking
  116. */
  117. struct exceptional_entry_key {
  118. struct xarray *xa;
  119. pgoff_t entry_start;
  120. };
  121. struct wait_exceptional_entry_queue {
  122. wait_queue_entry_t wait;
  123. struct exceptional_entry_key key;
  124. };
  125. static wait_queue_head_t *dax_entry_waitqueue(struct xa_state *xas,
  126. void *entry, struct exceptional_entry_key *key)
  127. {
  128. unsigned long hash;
  129. unsigned long index = xas->xa_index;
  130. /*
  131. * If 'entry' is a PMD, align the 'index' that we use for the wait
  132. * queue to the start of that PMD. This ensures that all offsets in
  133. * the range covered by the PMD map to the same bit lock.
  134. */
  135. if (dax_is_pmd_entry(entry))
  136. index &= ~PG_PMD_COLOUR;
  137. key->xa = xas->xa;
  138. key->entry_start = index;
  139. hash = hash_long((unsigned long)xas->xa ^ index, DAX_WAIT_TABLE_BITS);
  140. return wait_table + hash;
  141. }
  142. static int wake_exceptional_entry_func(wait_queue_entry_t *wait,
  143. unsigned int mode, int sync, void *keyp)
  144. {
  145. struct exceptional_entry_key *key = keyp;
  146. struct wait_exceptional_entry_queue *ewait =
  147. container_of(wait, struct wait_exceptional_entry_queue, wait);
  148. if (key->xa != ewait->key.xa ||
  149. key->entry_start != ewait->key.entry_start)
  150. return 0;
  151. return autoremove_wake_function(wait, mode, sync, NULL);
  152. }
  153. /*
  154. * @entry may no longer be the entry at the index in the mapping.
  155. * The important information it's conveying is whether the entry at
  156. * this index used to be a PMD entry.
  157. */
  158. static void dax_wake_entry(struct xa_state *xas, void *entry, bool wake_all)
  159. {
  160. struct exceptional_entry_key key;
  161. wait_queue_head_t *wq;
  162. wq = dax_entry_waitqueue(xas, entry, &key);
  163. /*
  164. * Checking for locked entry and prepare_to_wait_exclusive() happens
  165. * under the i_pages lock, ditto for entry handling in our callers.
  166. * So at this point all tasks that could have seen our entry locked
  167. * must be in the waitqueue and the following check will see them.
  168. */
  169. if (waitqueue_active(wq))
  170. __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
  171. }
  172. /*
  173. * Look up entry in page cache, wait for it to become unlocked if it
  174. * is a DAX entry and return it. The caller must subsequently call
  175. * put_unlocked_entry() if it did not lock the entry or dax_unlock_entry()
  176. * if it did.
  177. *
  178. * Must be called with the i_pages lock held.
  179. */
  180. static void *get_unlocked_entry(struct xa_state *xas)
  181. {
  182. void *entry;
  183. struct wait_exceptional_entry_queue ewait;
  184. wait_queue_head_t *wq;
  185. init_wait(&ewait.wait);
  186. ewait.wait.func = wake_exceptional_entry_func;
  187. for (;;) {
  188. entry = xas_find_conflict(xas);
  189. if (!entry || WARN_ON_ONCE(!xa_is_value(entry)) ||
  190. !dax_is_locked(entry))
  191. return entry;
  192. wq = dax_entry_waitqueue(xas, entry, &ewait.key);
  193. prepare_to_wait_exclusive(wq, &ewait.wait,
  194. TASK_UNINTERRUPTIBLE);
  195. xas_unlock_irq(xas);
  196. xas_reset(xas);
  197. schedule();
  198. finish_wait(wq, &ewait.wait);
  199. xas_lock_irq(xas);
  200. }
  201. }
  202. /*
  203. * The only thing keeping the address space around is the i_pages lock
  204. * (it's cycled in clear_inode() after removing the entries from i_pages)
  205. * After we call xas_unlock_irq(), we cannot touch xas->xa.
  206. */
  207. static void wait_entry_unlocked(struct xa_state *xas, void *entry)
  208. {
  209. struct wait_exceptional_entry_queue ewait;
  210. wait_queue_head_t *wq;
  211. init_wait(&ewait.wait);
  212. ewait.wait.func = wake_exceptional_entry_func;
  213. wq = dax_entry_waitqueue(xas, entry, &ewait.key);
  214. prepare_to_wait_exclusive(wq, &ewait.wait, TASK_UNINTERRUPTIBLE);
  215. xas_unlock_irq(xas);
  216. schedule();
  217. finish_wait(wq, &ewait.wait);
  218. /*
  219. * Entry lock waits are exclusive. Wake up the next waiter since
  220. * we aren't sure we will acquire the entry lock and thus wake
  221. * the next waiter up on unlock.
  222. */
  223. if (waitqueue_active(wq))
  224. __wake_up(wq, TASK_NORMAL, 1, &ewait.key);
  225. }
  226. static void put_unlocked_entry(struct xa_state *xas, void *entry)
  227. {
  228. /* If we were the only waiter woken, wake the next one */
  229. if (entry)
  230. dax_wake_entry(xas, entry, false);
  231. }
  232. /*
  233. * We used the xa_state to get the entry, but then we locked the entry and
  234. * dropped the xa_lock, so we know the xa_state is stale and must be reset
  235. * before use.
  236. */
  237. static void dax_unlock_entry(struct xa_state *xas, void *entry)
  238. {
  239. void *old;
  240. BUG_ON(dax_is_locked(entry));
  241. xas_reset(xas);
  242. xas_lock_irq(xas);
  243. old = xas_store(xas, entry);
  244. xas_unlock_irq(xas);
  245. BUG_ON(!dax_is_locked(old));
  246. dax_wake_entry(xas, entry, false);
  247. }
  248. /*
  249. * Return: The entry stored at this location before it was locked.
  250. */
  251. static void *dax_lock_entry(struct xa_state *xas, void *entry)
  252. {
  253. unsigned long v = xa_to_value(entry);
  254. return xas_store(xas, xa_mk_value(v | DAX_LOCKED));
  255. }
  256. static unsigned long dax_entry_size(void *entry)
  257. {
  258. if (dax_is_zero_entry(entry))
  259. return 0;
  260. else if (dax_is_empty_entry(entry))
  261. return 0;
  262. else if (dax_is_pmd_entry(entry))
  263. return PMD_SIZE;
  264. else
  265. return PAGE_SIZE;
  266. }
  267. static unsigned long dax_end_pfn(void *entry)
  268. {
  269. return dax_to_pfn(entry) + dax_entry_size(entry) / PAGE_SIZE;
  270. }
  271. /*
  272. * Iterate through all mapped pfns represented by an entry, i.e. skip
  273. * 'empty' and 'zero' entries.
  274. */
  275. #define for_each_mapped_pfn(entry, pfn) \
  276. for (pfn = dax_to_pfn(entry); \
  277. pfn < dax_end_pfn(entry); pfn++)
  278. /*
  279. * TODO: for reflink+dax we need a way to associate a single page with
  280. * multiple address_space instances at different linear_page_index()
  281. * offsets.
  282. */
  283. static void dax_associate_entry(void *entry, struct address_space *mapping,
  284. struct vm_area_struct *vma, unsigned long address)
  285. {
  286. unsigned long size = dax_entry_size(entry), pfn, index;
  287. int i = 0;
  288. if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
  289. return;
  290. index = linear_page_index(vma, address & ~(size - 1));
  291. for_each_mapped_pfn(entry, pfn) {
  292. struct page *page = pfn_to_page(pfn);
  293. WARN_ON_ONCE(page->mapping);
  294. page->mapping = mapping;
  295. page->index = index + i++;
  296. }
  297. }
  298. static void dax_disassociate_entry(void *entry, struct address_space *mapping,
  299. bool trunc)
  300. {
  301. unsigned long pfn;
  302. if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
  303. return;
  304. for_each_mapped_pfn(entry, pfn) {
  305. struct page *page = pfn_to_page(pfn);
  306. WARN_ON_ONCE(trunc && page_ref_count(page) > 1);
  307. WARN_ON_ONCE(page->mapping && page->mapping != mapping);
  308. page->mapping = NULL;
  309. page->index = 0;
  310. }
  311. }
  312. static struct page *dax_busy_page(void *entry)
  313. {
  314. unsigned long pfn;
  315. for_each_mapped_pfn(entry, pfn) {
  316. struct page *page = pfn_to_page(pfn);
  317. if (page_ref_count(page) > 1)
  318. return page;
  319. }
  320. return NULL;
  321. }
  322. /*
  323. * dax_lock_mapping_entry - Lock the DAX entry corresponding to a page
  324. * @page: The page whose entry we want to lock
  325. *
  326. * Context: Process context.
  327. * Return: A cookie to pass to dax_unlock_page() or 0 if the entry could
  328. * not be locked.
  329. */
  330. dax_entry_t dax_lock_page(struct page *page)
  331. {
  332. XA_STATE(xas, NULL, 0);
  333. void *entry;
  334. /* Ensure page->mapping isn't freed while we look at it */
  335. rcu_read_lock();
  336. for (;;) {
  337. struct address_space *mapping = READ_ONCE(page->mapping);
  338. entry = NULL;
  339. if (!mapping || !dax_mapping(mapping))
  340. break;
  341. /*
  342. * In the device-dax case there's no need to lock, a
  343. * struct dev_pagemap pin is sufficient to keep the
  344. * inode alive, and we assume we have dev_pagemap pin
  345. * otherwise we would not have a valid pfn_to_page()
  346. * translation.
  347. */
  348. entry = (void *)~0UL;
  349. if (S_ISCHR(mapping->host->i_mode))
  350. break;
  351. xas.xa = &mapping->i_pages;
  352. xas_lock_irq(&xas);
  353. if (mapping != page->mapping) {
  354. xas_unlock_irq(&xas);
  355. continue;
  356. }
  357. xas_set(&xas, page->index);
  358. entry = xas_load(&xas);
  359. if (dax_is_locked(entry)) {
  360. rcu_read_unlock();
  361. wait_entry_unlocked(&xas, entry);
  362. rcu_read_lock();
  363. continue;
  364. }
  365. dax_lock_entry(&xas, entry);
  366. xas_unlock_irq(&xas);
  367. break;
  368. }
  369. rcu_read_unlock();
  370. return (dax_entry_t)entry;
  371. }
  372. void dax_unlock_page(struct page *page, dax_entry_t cookie)
  373. {
  374. struct address_space *mapping = page->mapping;
  375. XA_STATE(xas, &mapping->i_pages, page->index);
  376. if (S_ISCHR(mapping->host->i_mode))
  377. return;
  378. dax_unlock_entry(&xas, (void *)cookie);
  379. }
  380. /*
  381. * Find page cache entry at given index. If it is a DAX entry, return it
  382. * with the entry locked. If the page cache doesn't contain an entry at
  383. * that index, add a locked empty entry.
  384. *
  385. * When requesting an entry with size DAX_PMD, grab_mapping_entry() will
  386. * either return that locked entry or will return VM_FAULT_FALLBACK.
  387. * This will happen if there are any PTE entries within the PMD range
  388. * that we are requesting.
  389. *
  390. * We always favor PTE entries over PMD entries. There isn't a flow where we
  391. * evict PTE entries in order to 'upgrade' them to a PMD entry. A PMD
  392. * insertion will fail if it finds any PTE entries already in the tree, and a
  393. * PTE insertion will cause an existing PMD entry to be unmapped and
  394. * downgraded to PTE entries. This happens for both PMD zero pages as
  395. * well as PMD empty entries.
  396. *
  397. * The exception to this downgrade path is for PMD entries that have
  398. * real storage backing them. We will leave these real PMD entries in
  399. * the tree, and PTE writes will simply dirty the entire PMD entry.
  400. *
  401. * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
  402. * persistent memory the benefit is doubtful. We can add that later if we can
  403. * show it helps.
  404. *
  405. * On error, this function does not return an ERR_PTR. Instead it returns
  406. * a VM_FAULT code, encoded as an xarray internal entry. The ERR_PTR values
  407. * overlap with xarray value entries.
  408. */
  409. static void *grab_mapping_entry(struct xa_state *xas,
  410. struct address_space *mapping, unsigned long size_flag)
  411. {
  412. unsigned long index = xas->xa_index;
  413. bool pmd_downgrade = false; /* splitting PMD entry into PTE entries? */
  414. void *entry;
  415. retry:
  416. xas_lock_irq(xas);
  417. entry = get_unlocked_entry(xas);
  418. if (entry) {
  419. if (!xa_is_value(entry)) {
  420. xas_set_err(xas, EIO);
  421. goto out_unlock;
  422. }
  423. if (size_flag & DAX_PMD) {
  424. if (dax_is_pte_entry(entry)) {
  425. put_unlocked_entry(xas, entry);
  426. goto fallback;
  427. }
  428. } else { /* trying to grab a PTE entry */
  429. if (dax_is_pmd_entry(entry) &&
  430. (dax_is_zero_entry(entry) ||
  431. dax_is_empty_entry(entry))) {
  432. pmd_downgrade = true;
  433. }
  434. }
  435. }
  436. if (pmd_downgrade) {
  437. /*
  438. * Make sure 'entry' remains valid while we drop
  439. * the i_pages lock.
  440. */
  441. dax_lock_entry(xas, entry);
  442. /*
  443. * Besides huge zero pages the only other thing that gets
  444. * downgraded are empty entries which don't need to be
  445. * unmapped.
  446. */
  447. if (dax_is_zero_entry(entry)) {
  448. xas_unlock_irq(xas);
  449. unmap_mapping_pages(mapping,
  450. xas->xa_index & ~PG_PMD_COLOUR,
  451. PG_PMD_NR, false);
  452. xas_reset(xas);
  453. xas_lock_irq(xas);
  454. }
  455. dax_disassociate_entry(entry, mapping, false);
  456. xas_store(xas, NULL); /* undo the PMD join */
  457. dax_wake_entry(xas, entry, true);
  458. mapping->nrexceptional--;
  459. entry = NULL;
  460. xas_set(xas, index);
  461. }
  462. if (entry) {
  463. dax_lock_entry(xas, entry);
  464. } else {
  465. entry = dax_make_entry(pfn_to_pfn_t(0), size_flag | DAX_EMPTY);
  466. dax_lock_entry(xas, entry);
  467. if (xas_error(xas))
  468. goto out_unlock;
  469. mapping->nrexceptional++;
  470. }
  471. out_unlock:
  472. xas_unlock_irq(xas);
  473. if (xas_nomem(xas, mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM))
  474. goto retry;
  475. if (xas->xa_node == XA_ERROR(-ENOMEM))
  476. return xa_mk_internal(VM_FAULT_OOM);
  477. if (xas_error(xas))
  478. return xa_mk_internal(VM_FAULT_SIGBUS);
  479. return entry;
  480. fallback:
  481. xas_unlock_irq(xas);
  482. return xa_mk_internal(VM_FAULT_FALLBACK);
  483. }
  484. /**
  485. * dax_layout_busy_page - find first pinned page in @mapping
  486. * @mapping: address space to scan for a page with ref count > 1
  487. *
  488. * DAX requires ZONE_DEVICE mapped pages. These pages are never
  489. * 'onlined' to the page allocator so they are considered idle when
  490. * page->count == 1. A filesystem uses this interface to determine if
  491. * any page in the mapping is busy, i.e. for DMA, or other
  492. * get_user_pages() usages.
  493. *
  494. * It is expected that the filesystem is holding locks to block the
  495. * establishment of new mappings in this address_space. I.e. it expects
  496. * to be able to run unmap_mapping_range() and subsequently not race
  497. * mapping_mapped() becoming true.
  498. */
  499. struct page *dax_layout_busy_page(struct address_space *mapping)
  500. {
  501. XA_STATE(xas, &mapping->i_pages, 0);
  502. void *entry;
  503. unsigned int scanned = 0;
  504. struct page *page = NULL;
  505. /*
  506. * In the 'limited' case get_user_pages() for dax is disabled.
  507. */
  508. if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
  509. return NULL;
  510. if (!dax_mapping(mapping) || !mapping_mapped(mapping))
  511. return NULL;
  512. /*
  513. * If we race get_user_pages_fast() here either we'll see the
  514. * elevated page count in the iteration and wait, or
  515. * get_user_pages_fast() will see that the page it took a reference
  516. * against is no longer mapped in the page tables and bail to the
  517. * get_user_pages() slow path. The slow path is protected by
  518. * pte_lock() and pmd_lock(). New references are not taken without
  519. * holding those locks, and unmap_mapping_range() will not zero the
  520. * pte or pmd without holding the respective lock, so we are
  521. * guaranteed to either see new references or prevent new
  522. * references from being established.
  523. */
  524. unmap_mapping_range(mapping, 0, 0, 1);
  525. xas_lock_irq(&xas);
  526. xas_for_each(&xas, entry, ULONG_MAX) {
  527. if (WARN_ON_ONCE(!xa_is_value(entry)))
  528. continue;
  529. if (unlikely(dax_is_locked(entry)))
  530. entry = get_unlocked_entry(&xas);
  531. if (entry)
  532. page = dax_busy_page(entry);
  533. put_unlocked_entry(&xas, entry);
  534. if (page)
  535. break;
  536. if (++scanned % XA_CHECK_SCHED)
  537. continue;
  538. xas_pause(&xas);
  539. xas_unlock_irq(&xas);
  540. cond_resched();
  541. xas_lock_irq(&xas);
  542. }
  543. xas_unlock_irq(&xas);
  544. return page;
  545. }
  546. EXPORT_SYMBOL_GPL(dax_layout_busy_page);
  547. static int __dax_invalidate_entry(struct address_space *mapping,
  548. pgoff_t index, bool trunc)
  549. {
  550. XA_STATE(xas, &mapping->i_pages, index);
  551. int ret = 0;
  552. void *entry;
  553. xas_lock_irq(&xas);
  554. entry = get_unlocked_entry(&xas);
  555. if (!entry || WARN_ON_ONCE(!xa_is_value(entry)))
  556. goto out;
  557. if (!trunc &&
  558. (xas_get_mark(&xas, PAGECACHE_TAG_DIRTY) ||
  559. xas_get_mark(&xas, PAGECACHE_TAG_TOWRITE)))
  560. goto out;
  561. dax_disassociate_entry(entry, mapping, trunc);
  562. xas_store(&xas, NULL);
  563. mapping->nrexceptional--;
  564. ret = 1;
  565. out:
  566. put_unlocked_entry(&xas, entry);
  567. xas_unlock_irq(&xas);
  568. return ret;
  569. }
  570. /*
  571. * Delete DAX entry at @index from @mapping. Wait for it
  572. * to be unlocked before deleting it.
  573. */
  574. int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
  575. {
  576. int ret = __dax_invalidate_entry(mapping, index, true);
  577. /*
  578. * This gets called from truncate / punch_hole path. As such, the caller
  579. * must hold locks protecting against concurrent modifications of the
  580. * page cache (usually fs-private i_mmap_sem for writing). Since the
  581. * caller has seen a DAX entry for this index, we better find it
  582. * at that index as well...
  583. */
  584. WARN_ON_ONCE(!ret);
  585. return ret;
  586. }
  587. /*
  588. * Invalidate DAX entry if it is clean.
  589. */
  590. int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
  591. pgoff_t index)
  592. {
  593. return __dax_invalidate_entry(mapping, index, false);
  594. }
  595. static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev,
  596. sector_t sector, size_t size, struct page *to,
  597. unsigned long vaddr)
  598. {
  599. void *vto, *kaddr;
  600. pgoff_t pgoff;
  601. long rc;
  602. int id;
  603. rc = bdev_dax_pgoff(bdev, sector, size, &pgoff);
  604. if (rc)
  605. return rc;
  606. id = dax_read_lock();
  607. rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, NULL);
  608. if (rc < 0) {
  609. dax_read_unlock(id);
  610. return rc;
  611. }
  612. vto = kmap_atomic(to);
  613. copy_user_page(vto, (void __force *)kaddr, vaddr, to);
  614. kunmap_atomic(vto);
  615. dax_read_unlock(id);
  616. return 0;
  617. }
  618. /*
  619. * By this point grab_mapping_entry() has ensured that we have a locked entry
  620. * of the appropriate size so we don't have to worry about downgrading PMDs to
  621. * PTEs. If we happen to be trying to insert a PTE and there is a PMD
  622. * already in the tree, we will skip the insertion and just dirty the PMD as
  623. * appropriate.
  624. */
  625. static void *dax_insert_entry(struct xa_state *xas,
  626. struct address_space *mapping, struct vm_fault *vmf,
  627. void *entry, pfn_t pfn, unsigned long flags, bool dirty)
  628. {
  629. void *new_entry = dax_make_entry(pfn, flags);
  630. if (dirty)
  631. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  632. if (dax_is_zero_entry(entry) && !(flags & DAX_ZERO_PAGE)) {
  633. unsigned long index = xas->xa_index;
  634. /* we are replacing a zero page with block mapping */
  635. if (dax_is_pmd_entry(entry))
  636. unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR,
  637. PG_PMD_NR, false);
  638. else /* pte entry */
  639. unmap_mapping_pages(mapping, index, 1, false);
  640. }
  641. xas_reset(xas);
  642. xas_lock_irq(xas);
  643. if (dax_entry_size(entry) != dax_entry_size(new_entry)) {
  644. dax_disassociate_entry(entry, mapping, false);
  645. dax_associate_entry(new_entry, mapping, vmf->vma, vmf->address);
  646. }
  647. if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) {
  648. /*
  649. * Only swap our new entry into the page cache if the current
  650. * entry is a zero page or an empty entry. If a normal PTE or
  651. * PMD entry is already in the cache, we leave it alone. This
  652. * means that if we are trying to insert a PTE and the
  653. * existing entry is a PMD, we will just leave the PMD in the
  654. * tree and dirty it if necessary.
  655. */
  656. void *old = dax_lock_entry(xas, new_entry);
  657. WARN_ON_ONCE(old != xa_mk_value(xa_to_value(entry) |
  658. DAX_LOCKED));
  659. entry = new_entry;
  660. } else {
  661. xas_load(xas); /* Walk the xa_state */
  662. }
  663. if (dirty)
  664. xas_set_mark(xas, PAGECACHE_TAG_DIRTY);
  665. xas_unlock_irq(xas);
  666. return entry;
  667. }
  668. static inline
  669. unsigned long pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma)
  670. {
  671. unsigned long address;
  672. address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  673. VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
  674. return address;
  675. }
  676. /* Walk all mappings of a given index of a file and writeprotect them */
  677. static void dax_entry_mkclean(struct address_space *mapping, pgoff_t index,
  678. unsigned long pfn)
  679. {
  680. struct vm_area_struct *vma;
  681. pte_t pte, *ptep = NULL;
  682. pmd_t *pmdp = NULL;
  683. spinlock_t *ptl;
  684. i_mmap_lock_read(mapping);
  685. vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) {
  686. unsigned long address, start, end;
  687. cond_resched();
  688. if (!(vma->vm_flags & VM_SHARED))
  689. continue;
  690. address = pgoff_address(index, vma);
  691. /*
  692. * Note because we provide start/end to follow_pte_pmd it will
  693. * call mmu_notifier_invalidate_range_start() on our behalf
  694. * before taking any lock.
  695. */
  696. if (follow_pte_pmd(vma->vm_mm, address, &start, &end, &ptep, &pmdp, &ptl))
  697. continue;
  698. /*
  699. * No need to call mmu_notifier_invalidate_range() as we are
  700. * downgrading page table protection not changing it to point
  701. * to a new page.
  702. *
  703. * See Documentation/vm/mmu_notifier.rst
  704. */
  705. if (pmdp) {
  706. #ifdef CONFIG_FS_DAX_PMD
  707. pmd_t pmd;
  708. if (pfn != pmd_pfn(*pmdp))
  709. goto unlock_pmd;
  710. if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp))
  711. goto unlock_pmd;
  712. flush_cache_page(vma, address, pfn);
  713. pmd = pmdp_huge_clear_flush(vma, address, pmdp);
  714. pmd = pmd_wrprotect(pmd);
  715. pmd = pmd_mkclean(pmd);
  716. set_pmd_at(vma->vm_mm, address, pmdp, pmd);
  717. unlock_pmd:
  718. #endif
  719. spin_unlock(ptl);
  720. } else {
  721. if (pfn != pte_pfn(*ptep))
  722. goto unlock_pte;
  723. if (!pte_dirty(*ptep) && !pte_write(*ptep))
  724. goto unlock_pte;
  725. flush_cache_page(vma, address, pfn);
  726. pte = ptep_clear_flush(vma, address, ptep);
  727. pte = pte_wrprotect(pte);
  728. pte = pte_mkclean(pte);
  729. set_pte_at(vma->vm_mm, address, ptep, pte);
  730. unlock_pte:
  731. pte_unmap_unlock(ptep, ptl);
  732. }
  733. mmu_notifier_invalidate_range_end(vma->vm_mm, start, end);
  734. }
  735. i_mmap_unlock_read(mapping);
  736. }
  737. static int dax_writeback_one(struct xa_state *xas, struct dax_device *dax_dev,
  738. struct address_space *mapping, void *entry)
  739. {
  740. unsigned long pfn;
  741. long ret = 0;
  742. size_t size;
  743. /*
  744. * A page got tagged dirty in DAX mapping? Something is seriously
  745. * wrong.
  746. */
  747. if (WARN_ON(!xa_is_value(entry)))
  748. return -EIO;
  749. if (unlikely(dax_is_locked(entry))) {
  750. void *old_entry = entry;
  751. entry = get_unlocked_entry(xas);
  752. /* Entry got punched out / reallocated? */
  753. if (!entry || WARN_ON_ONCE(!xa_is_value(entry)))
  754. goto put_unlocked;
  755. /*
  756. * Entry got reallocated elsewhere? No need to writeback.
  757. * We have to compare pfns as we must not bail out due to
  758. * difference in lockbit or entry type.
  759. */
  760. if (dax_to_pfn(old_entry) != dax_to_pfn(entry))
  761. goto put_unlocked;
  762. if (WARN_ON_ONCE(dax_is_empty_entry(entry) ||
  763. dax_is_zero_entry(entry))) {
  764. ret = -EIO;
  765. goto put_unlocked;
  766. }
  767. /* Another fsync thread may have already done this entry */
  768. if (!xas_get_mark(xas, PAGECACHE_TAG_TOWRITE))
  769. goto put_unlocked;
  770. }
  771. /* Lock the entry to serialize with page faults */
  772. dax_lock_entry(xas, entry);
  773. /*
  774. * We can clear the tag now but we have to be careful so that concurrent
  775. * dax_writeback_one() calls for the same index cannot finish before we
  776. * actually flush the caches. This is achieved as the calls will look
  777. * at the entry only under the i_pages lock and once they do that
  778. * they will see the entry locked and wait for it to unlock.
  779. */
  780. xas_clear_mark(xas, PAGECACHE_TAG_TOWRITE);
  781. xas_unlock_irq(xas);
  782. /*
  783. * Even if dax_writeback_mapping_range() was given a wbc->range_start
  784. * in the middle of a PMD, the 'index' we are given will be aligned to
  785. * the start index of the PMD, as will the pfn we pull from 'entry'.
  786. * This allows us to flush for PMD_SIZE and not have to worry about
  787. * partial PMD writebacks.
  788. */
  789. pfn = dax_to_pfn(entry);
  790. size = PAGE_SIZE << dax_entry_order(entry);
  791. dax_entry_mkclean(mapping, xas->xa_index, pfn);
  792. dax_flush(dax_dev, page_address(pfn_to_page(pfn)), size);
  793. /*
  794. * After we have flushed the cache, we can clear the dirty tag. There
  795. * cannot be new dirty data in the pfn after the flush has completed as
  796. * the pfn mappings are writeprotected and fault waits for mapping
  797. * entry lock.
  798. */
  799. xas_reset(xas);
  800. xas_lock_irq(xas);
  801. xas_store(xas, entry);
  802. xas_clear_mark(xas, PAGECACHE_TAG_DIRTY);
  803. dax_wake_entry(xas, entry, false);
  804. trace_dax_writeback_one(mapping->host, xas->xa_index,
  805. size >> PAGE_SHIFT);
  806. return ret;
  807. put_unlocked:
  808. put_unlocked_entry(xas, entry);
  809. return ret;
  810. }
  811. /*
  812. * Flush the mapping to the persistent domain within the byte range of [start,
  813. * end]. This is required by data integrity operations to ensure file data is
  814. * on persistent storage prior to completion of the operation.
  815. */
  816. int dax_writeback_mapping_range(struct address_space *mapping,
  817. struct block_device *bdev, struct writeback_control *wbc)
  818. {
  819. XA_STATE(xas, &mapping->i_pages, wbc->range_start >> PAGE_SHIFT);
  820. struct inode *inode = mapping->host;
  821. pgoff_t end_index = wbc->range_end >> PAGE_SHIFT;
  822. struct dax_device *dax_dev;
  823. void *entry;
  824. int ret = 0;
  825. unsigned int scanned = 0;
  826. if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
  827. return -EIO;
  828. if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
  829. return 0;
  830. dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
  831. if (!dax_dev)
  832. return -EIO;
  833. trace_dax_writeback_range(inode, xas.xa_index, end_index);
  834. tag_pages_for_writeback(mapping, xas.xa_index, end_index);
  835. xas_lock_irq(&xas);
  836. xas_for_each_marked(&xas, entry, end_index, PAGECACHE_TAG_TOWRITE) {
  837. ret = dax_writeback_one(&xas, dax_dev, mapping, entry);
  838. if (ret < 0) {
  839. mapping_set_error(mapping, ret);
  840. break;
  841. }
  842. if (++scanned % XA_CHECK_SCHED)
  843. continue;
  844. xas_pause(&xas);
  845. xas_unlock_irq(&xas);
  846. cond_resched();
  847. xas_lock_irq(&xas);
  848. }
  849. xas_unlock_irq(&xas);
  850. put_dax(dax_dev);
  851. trace_dax_writeback_range_done(inode, xas.xa_index, end_index);
  852. return ret;
  853. }
  854. EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
  855. static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
  856. {
  857. return (iomap->addr + (pos & PAGE_MASK) - iomap->offset) >> 9;
  858. }
  859. static int dax_iomap_pfn(struct iomap *iomap, loff_t pos, size_t size,
  860. pfn_t *pfnp)
  861. {
  862. const sector_t sector = dax_iomap_sector(iomap, pos);
  863. pgoff_t pgoff;
  864. int id, rc;
  865. long length;
  866. rc = bdev_dax_pgoff(iomap->bdev, sector, size, &pgoff);
  867. if (rc)
  868. return rc;
  869. id = dax_read_lock();
  870. length = dax_direct_access(iomap->dax_dev, pgoff, PHYS_PFN(size),
  871. NULL, pfnp);
  872. if (length < 0) {
  873. rc = length;
  874. goto out;
  875. }
  876. rc = -EINVAL;
  877. if (PFN_PHYS(length) < size)
  878. goto out;
  879. if (pfn_t_to_pfn(*pfnp) & (PHYS_PFN(size)-1))
  880. goto out;
  881. /* For larger pages we need devmap */
  882. if (length > 1 && !pfn_t_devmap(*pfnp))
  883. goto out;
  884. rc = 0;
  885. out:
  886. dax_read_unlock(id);
  887. return rc;
  888. }
  889. /*
  890. * The user has performed a load from a hole in the file. Allocating a new
  891. * page in the file would cause excessive storage usage for workloads with
  892. * sparse files. Instead we insert a read-only mapping of the 4k zero page.
  893. * If this page is ever written to we will re-fault and change the mapping to
  894. * point to real DAX storage instead.
  895. */
  896. static vm_fault_t dax_load_hole(struct xa_state *xas,
  897. struct address_space *mapping, void **entry,
  898. struct vm_fault *vmf)
  899. {
  900. struct inode *inode = mapping->host;
  901. unsigned long vaddr = vmf->address;
  902. pfn_t pfn = pfn_to_pfn_t(my_zero_pfn(vaddr));
  903. vm_fault_t ret;
  904. *entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn,
  905. DAX_ZERO_PAGE, false);
  906. ret = vmf_insert_mixed(vmf->vma, vaddr, pfn);
  907. trace_dax_load_hole(inode, vmf, ret);
  908. return ret;
  909. }
  910. static bool dax_range_is_aligned(struct block_device *bdev,
  911. unsigned int offset, unsigned int length)
  912. {
  913. unsigned short sector_size = bdev_logical_block_size(bdev);
  914. if (!IS_ALIGNED(offset, sector_size))
  915. return false;
  916. if (!IS_ALIGNED(length, sector_size))
  917. return false;
  918. return true;
  919. }
  920. int __dax_zero_page_range(struct block_device *bdev,
  921. struct dax_device *dax_dev, sector_t sector,
  922. unsigned int offset, unsigned int size)
  923. {
  924. if (dax_range_is_aligned(bdev, offset, size)) {
  925. sector_t start_sector = sector + (offset >> 9);
  926. return blkdev_issue_zeroout(bdev, start_sector,
  927. size >> 9, GFP_NOFS, 0);
  928. } else {
  929. pgoff_t pgoff;
  930. long rc, id;
  931. void *kaddr;
  932. rc = bdev_dax_pgoff(bdev, sector, PAGE_SIZE, &pgoff);
  933. if (rc)
  934. return rc;
  935. id = dax_read_lock();
  936. rc = dax_direct_access(dax_dev, pgoff, 1, &kaddr, NULL);
  937. if (rc < 0) {
  938. dax_read_unlock(id);
  939. return rc;
  940. }
  941. memset(kaddr + offset, 0, size);
  942. dax_flush(dax_dev, kaddr + offset, size);
  943. dax_read_unlock(id);
  944. }
  945. return 0;
  946. }
  947. EXPORT_SYMBOL_GPL(__dax_zero_page_range);
  948. static loff_t
  949. dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
  950. struct iomap *iomap)
  951. {
  952. struct block_device *bdev = iomap->bdev;
  953. struct dax_device *dax_dev = iomap->dax_dev;
  954. struct iov_iter *iter = data;
  955. loff_t end = pos + length, done = 0;
  956. ssize_t ret = 0;
  957. size_t xfer;
  958. int id;
  959. if (iov_iter_rw(iter) == READ) {
  960. end = min(end, i_size_read(inode));
  961. if (pos >= end)
  962. return 0;
  963. if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
  964. return iov_iter_zero(min(length, end - pos), iter);
  965. }
  966. if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
  967. return -EIO;
  968. /*
  969. * Write can allocate block for an area which has a hole page mapped
  970. * into page tables. We have to tear down these mappings so that data
  971. * written by write(2) is visible in mmap.
  972. */
  973. if (iomap->flags & IOMAP_F_NEW) {
  974. invalidate_inode_pages2_range(inode->i_mapping,
  975. pos >> PAGE_SHIFT,
  976. (end - 1) >> PAGE_SHIFT);
  977. }
  978. id = dax_read_lock();
  979. while (pos < end) {
  980. unsigned offset = pos & (PAGE_SIZE - 1);
  981. const size_t size = ALIGN(length + offset, PAGE_SIZE);
  982. const sector_t sector = dax_iomap_sector(iomap, pos);
  983. ssize_t map_len;
  984. pgoff_t pgoff;
  985. void *kaddr;
  986. if (fatal_signal_pending(current)) {
  987. ret = -EINTR;
  988. break;
  989. }
  990. ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
  991. if (ret)
  992. break;
  993. map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size),
  994. &kaddr, NULL);
  995. if (map_len < 0) {
  996. ret = map_len;
  997. break;
  998. }
  999. map_len = PFN_PHYS(map_len);
  1000. kaddr += offset;
  1001. map_len -= offset;
  1002. if (map_len > end - pos)
  1003. map_len = end - pos;
  1004. /*
  1005. * The userspace address for the memory copy has already been
  1006. * validated via access_ok() in either vfs_read() or
  1007. * vfs_write(), depending on which operation we are doing.
  1008. */
  1009. if (iov_iter_rw(iter) == WRITE)
  1010. xfer = dax_copy_from_iter(dax_dev, pgoff, kaddr,
  1011. map_len, iter);
  1012. else
  1013. xfer = dax_copy_to_iter(dax_dev, pgoff, kaddr,
  1014. map_len, iter);
  1015. pos += xfer;
  1016. length -= xfer;
  1017. done += xfer;
  1018. if (xfer == 0)
  1019. ret = -EFAULT;
  1020. if (xfer < map_len)
  1021. break;
  1022. }
  1023. dax_read_unlock(id);
  1024. return done ? done : ret;
  1025. }
  1026. /**
  1027. * dax_iomap_rw - Perform I/O to a DAX file
  1028. * @iocb: The control block for this I/O
  1029. * @iter: The addresses to do I/O from or to
  1030. * @ops: iomap ops passed from the file system
  1031. *
  1032. * This function performs read and write operations to directly mapped
  1033. * persistent memory. The callers needs to take care of read/write exclusion
  1034. * and evicting any page cache pages in the region under I/O.
  1035. */
  1036. ssize_t
  1037. dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
  1038. const struct iomap_ops *ops)
  1039. {
  1040. struct address_space *mapping = iocb->ki_filp->f_mapping;
  1041. struct inode *inode = mapping->host;
  1042. loff_t pos = iocb->ki_pos, ret = 0, done = 0;
  1043. unsigned flags = 0;
  1044. if (iov_iter_rw(iter) == WRITE) {
  1045. lockdep_assert_held_exclusive(&inode->i_rwsem);
  1046. flags |= IOMAP_WRITE;
  1047. } else {
  1048. lockdep_assert_held(&inode->i_rwsem);
  1049. }
  1050. while (iov_iter_count(iter)) {
  1051. ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
  1052. iter, dax_iomap_actor);
  1053. if (ret <= 0)
  1054. break;
  1055. pos += ret;
  1056. done += ret;
  1057. }
  1058. iocb->ki_pos += done;
  1059. return done ? done : ret;
  1060. }
  1061. EXPORT_SYMBOL_GPL(dax_iomap_rw);
  1062. static vm_fault_t dax_fault_return(int error)
  1063. {
  1064. if (error == 0)
  1065. return VM_FAULT_NOPAGE;
  1066. if (error == -ENOMEM)
  1067. return VM_FAULT_OOM;
  1068. return VM_FAULT_SIGBUS;
  1069. }
  1070. /*
  1071. * MAP_SYNC on a dax mapping guarantees dirty metadata is
  1072. * flushed on write-faults (non-cow), but not read-faults.
  1073. */
  1074. static bool dax_fault_is_synchronous(unsigned long flags,
  1075. struct vm_area_struct *vma, struct iomap *iomap)
  1076. {
  1077. return (flags & IOMAP_WRITE) && (vma->vm_flags & VM_SYNC)
  1078. && (iomap->flags & IOMAP_F_DIRTY);
  1079. }
  1080. static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp,
  1081. int *iomap_errp, const struct iomap_ops *ops)
  1082. {
  1083. struct vm_area_struct *vma = vmf->vma;
  1084. struct address_space *mapping = vma->vm_file->f_mapping;
  1085. XA_STATE(xas, &mapping->i_pages, vmf->pgoff);
  1086. struct inode *inode = mapping->host;
  1087. unsigned long vaddr = vmf->address;
  1088. loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
  1089. struct iomap iomap = { 0 };
  1090. unsigned flags = IOMAP_FAULT;
  1091. int error, major = 0;
  1092. bool write = vmf->flags & FAULT_FLAG_WRITE;
  1093. bool sync;
  1094. vm_fault_t ret = 0;
  1095. void *entry;
  1096. pfn_t pfn;
  1097. trace_dax_pte_fault(inode, vmf, ret);
  1098. /*
  1099. * Check whether offset isn't beyond end of file now. Caller is supposed
  1100. * to hold locks serializing us with truncate / punch hole so this is
  1101. * a reliable test.
  1102. */
  1103. if (pos >= i_size_read(inode)) {
  1104. ret = VM_FAULT_SIGBUS;
  1105. goto out;
  1106. }
  1107. if (write && !vmf->cow_page)
  1108. flags |= IOMAP_WRITE;
  1109. entry = grab_mapping_entry(&xas, mapping, 0);
  1110. if (xa_is_internal(entry)) {
  1111. ret = xa_to_internal(entry);
  1112. goto out;
  1113. }
  1114. /*
  1115. * It is possible, particularly with mixed reads & writes to private
  1116. * mappings, that we have raced with a PMD fault that overlaps with
  1117. * the PTE we need to set up. If so just return and the fault will be
  1118. * retried.
  1119. */
  1120. if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) {
  1121. ret = VM_FAULT_NOPAGE;
  1122. goto unlock_entry;
  1123. }
  1124. /*
  1125. * Note that we don't bother to use iomap_apply here: DAX required
  1126. * the file system block size to be equal the page size, which means
  1127. * that we never have to deal with more than a single extent here.
  1128. */
  1129. error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
  1130. if (iomap_errp)
  1131. *iomap_errp = error;
  1132. if (error) {
  1133. ret = dax_fault_return(error);
  1134. goto unlock_entry;
  1135. }
  1136. if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
  1137. error = -EIO; /* fs corruption? */
  1138. goto error_finish_iomap;
  1139. }
  1140. if (vmf->cow_page) {
  1141. sector_t sector = dax_iomap_sector(&iomap, pos);
  1142. switch (iomap.type) {
  1143. case IOMAP_HOLE:
  1144. case IOMAP_UNWRITTEN:
  1145. clear_user_highpage(vmf->cow_page, vaddr);
  1146. break;
  1147. case IOMAP_MAPPED:
  1148. error = copy_user_dax(iomap.bdev, iomap.dax_dev,
  1149. sector, PAGE_SIZE, vmf->cow_page, vaddr);
  1150. break;
  1151. default:
  1152. WARN_ON_ONCE(1);
  1153. error = -EIO;
  1154. break;
  1155. }
  1156. if (error)
  1157. goto error_finish_iomap;
  1158. __SetPageUptodate(vmf->cow_page);
  1159. ret = finish_fault(vmf);
  1160. if (!ret)
  1161. ret = VM_FAULT_DONE_COW;
  1162. goto finish_iomap;
  1163. }
  1164. sync = dax_fault_is_synchronous(flags, vma, &iomap);
  1165. switch (iomap.type) {
  1166. case IOMAP_MAPPED:
  1167. if (iomap.flags & IOMAP_F_NEW) {
  1168. count_vm_event(PGMAJFAULT);
  1169. count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
  1170. major = VM_FAULT_MAJOR;
  1171. }
  1172. error = dax_iomap_pfn(&iomap, pos, PAGE_SIZE, &pfn);
  1173. if (error < 0)
  1174. goto error_finish_iomap;
  1175. entry = dax_insert_entry(&xas, mapping, vmf, entry, pfn,
  1176. 0, write && !sync);
  1177. /*
  1178. * If we are doing synchronous page fault and inode needs fsync,
  1179. * we can insert PTE into page tables only after that happens.
  1180. * Skip insertion for now and return the pfn so that caller can
  1181. * insert it after fsync is done.
  1182. */
  1183. if (sync) {
  1184. if (WARN_ON_ONCE(!pfnp)) {
  1185. error = -EIO;
  1186. goto error_finish_iomap;
  1187. }
  1188. *pfnp = pfn;
  1189. ret = VM_FAULT_NEEDDSYNC | major;
  1190. goto finish_iomap;
  1191. }
  1192. trace_dax_insert_mapping(inode, vmf, entry);
  1193. if (write)
  1194. ret = vmf_insert_mixed_mkwrite(vma, vaddr, pfn);
  1195. else
  1196. ret = vmf_insert_mixed(vma, vaddr, pfn);
  1197. goto finish_iomap;
  1198. case IOMAP_UNWRITTEN:
  1199. case IOMAP_HOLE:
  1200. if (!write) {
  1201. ret = dax_load_hole(&xas, mapping, &entry, vmf);
  1202. goto finish_iomap;
  1203. }
  1204. /*FALLTHRU*/
  1205. default:
  1206. WARN_ON_ONCE(1);
  1207. error = -EIO;
  1208. break;
  1209. }
  1210. error_finish_iomap:
  1211. ret = dax_fault_return(error);
  1212. finish_iomap:
  1213. if (ops->iomap_end) {
  1214. int copied = PAGE_SIZE;
  1215. if (ret & VM_FAULT_ERROR)
  1216. copied = 0;
  1217. /*
  1218. * The fault is done by now and there's no way back (other
  1219. * thread may be already happily using PTE we have installed).
  1220. * Just ignore error from ->iomap_end since we cannot do much
  1221. * with it.
  1222. */
  1223. ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap);
  1224. }
  1225. unlock_entry:
  1226. dax_unlock_entry(&xas, entry);
  1227. out:
  1228. trace_dax_pte_fault_done(inode, vmf, ret);
  1229. return ret | major;
  1230. }
  1231. #ifdef CONFIG_FS_DAX_PMD
  1232. static vm_fault_t dax_pmd_load_hole(struct xa_state *xas, struct vm_fault *vmf,
  1233. struct iomap *iomap, void **entry)
  1234. {
  1235. struct address_space *mapping = vmf->vma->vm_file->f_mapping;
  1236. unsigned long pmd_addr = vmf->address & PMD_MASK;
  1237. struct inode *inode = mapping->host;
  1238. struct page *zero_page;
  1239. spinlock_t *ptl;
  1240. pmd_t pmd_entry;
  1241. pfn_t pfn;
  1242. zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm);
  1243. if (unlikely(!zero_page))
  1244. goto fallback;
  1245. pfn = page_to_pfn_t(zero_page);
  1246. *entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn,
  1247. DAX_PMD | DAX_ZERO_PAGE, false);
  1248. ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
  1249. if (!pmd_none(*(vmf->pmd))) {
  1250. spin_unlock(ptl);
  1251. goto fallback;
  1252. }
  1253. pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot);
  1254. pmd_entry = pmd_mkhuge(pmd_entry);
  1255. set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry);
  1256. spin_unlock(ptl);
  1257. trace_dax_pmd_load_hole(inode, vmf, zero_page, *entry);
  1258. return VM_FAULT_NOPAGE;
  1259. fallback:
  1260. trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, *entry);
  1261. return VM_FAULT_FALLBACK;
  1262. }
  1263. static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
  1264. const struct iomap_ops *ops)
  1265. {
  1266. struct vm_area_struct *vma = vmf->vma;
  1267. struct address_space *mapping = vma->vm_file->f_mapping;
  1268. XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, PMD_ORDER);
  1269. unsigned long pmd_addr = vmf->address & PMD_MASK;
  1270. bool write = vmf->flags & FAULT_FLAG_WRITE;
  1271. bool sync;
  1272. unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT;
  1273. struct inode *inode = mapping->host;
  1274. vm_fault_t result = VM_FAULT_FALLBACK;
  1275. struct iomap iomap = { 0 };
  1276. pgoff_t max_pgoff;
  1277. void *entry;
  1278. loff_t pos;
  1279. int error;
  1280. pfn_t pfn;
  1281. /*
  1282. * Check whether offset isn't beyond end of file now. Caller is
  1283. * supposed to hold locks serializing us with truncate / punch hole so
  1284. * this is a reliable test.
  1285. */
  1286. max_pgoff = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
  1287. trace_dax_pmd_fault(inode, vmf, max_pgoff, 0);
  1288. /*
  1289. * Make sure that the faulting address's PMD offset (color) matches
  1290. * the PMD offset from the start of the file. This is necessary so
  1291. * that a PMD range in the page table overlaps exactly with a PMD
  1292. * range in the page cache.
  1293. */
  1294. if ((vmf->pgoff & PG_PMD_COLOUR) !=
  1295. ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR))
  1296. goto fallback;
  1297. /* Fall back to PTEs if we're going to COW */
  1298. if (write && !(vma->vm_flags & VM_SHARED))
  1299. goto fallback;
  1300. /* If the PMD would extend outside the VMA */
  1301. if (pmd_addr < vma->vm_start)
  1302. goto fallback;
  1303. if ((pmd_addr + PMD_SIZE) > vma->vm_end)
  1304. goto fallback;
  1305. if (xas.xa_index >= max_pgoff) {
  1306. result = VM_FAULT_SIGBUS;
  1307. goto out;
  1308. }
  1309. /* If the PMD would extend beyond the file size */
  1310. if ((xas.xa_index | PG_PMD_COLOUR) >= max_pgoff)
  1311. goto fallback;
  1312. /*
  1313. * grab_mapping_entry() will make sure we get an empty PMD entry,
  1314. * a zero PMD entry or a DAX PMD. If it can't (because a PTE
  1315. * entry is already in the array, for instance), it will return
  1316. * VM_FAULT_FALLBACK.
  1317. */
  1318. entry = grab_mapping_entry(&xas, mapping, DAX_PMD);
  1319. if (xa_is_internal(entry)) {
  1320. result = xa_to_internal(entry);
  1321. goto fallback;
  1322. }
  1323. /*
  1324. * It is possible, particularly with mixed reads & writes to private
  1325. * mappings, that we have raced with a PTE fault that overlaps with
  1326. * the PMD we need to set up. If so just return and the fault will be
  1327. * retried.
  1328. */
  1329. if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) &&
  1330. !pmd_devmap(*vmf->pmd)) {
  1331. result = 0;
  1332. goto unlock_entry;
  1333. }
  1334. /*
  1335. * Note that we don't use iomap_apply here. We aren't doing I/O, only
  1336. * setting up a mapping, so really we're using iomap_begin() as a way
  1337. * to look up our filesystem block.
  1338. */
  1339. pos = (loff_t)xas.xa_index << PAGE_SHIFT;
  1340. error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap);
  1341. if (error)
  1342. goto unlock_entry;
  1343. if (iomap.offset + iomap.length < pos + PMD_SIZE)
  1344. goto finish_iomap;
  1345. sync = dax_fault_is_synchronous(iomap_flags, vma, &iomap);
  1346. switch (iomap.type) {
  1347. case IOMAP_MAPPED:
  1348. error = dax_iomap_pfn(&iomap, pos, PMD_SIZE, &pfn);
  1349. if (error < 0)
  1350. goto finish_iomap;
  1351. entry = dax_insert_entry(&xas, mapping, vmf, entry, pfn,
  1352. DAX_PMD, write && !sync);
  1353. /*
  1354. * If we are doing synchronous page fault and inode needs fsync,
  1355. * we can insert PMD into page tables only after that happens.
  1356. * Skip insertion for now and return the pfn so that caller can
  1357. * insert it after fsync is done.
  1358. */
  1359. if (sync) {
  1360. if (WARN_ON_ONCE(!pfnp))
  1361. goto finish_iomap;
  1362. *pfnp = pfn;
  1363. result = VM_FAULT_NEEDDSYNC;
  1364. goto finish_iomap;
  1365. }
  1366. trace_dax_pmd_insert_mapping(inode, vmf, PMD_SIZE, pfn, entry);
  1367. result = vmf_insert_pfn_pmd(vma, vmf->address, vmf->pmd, pfn,
  1368. write);
  1369. break;
  1370. case IOMAP_UNWRITTEN:
  1371. case IOMAP_HOLE:
  1372. if (WARN_ON_ONCE(write))
  1373. break;
  1374. result = dax_pmd_load_hole(&xas, vmf, &iomap, &entry);
  1375. break;
  1376. default:
  1377. WARN_ON_ONCE(1);
  1378. break;
  1379. }
  1380. finish_iomap:
  1381. if (ops->iomap_end) {
  1382. int copied = PMD_SIZE;
  1383. if (result == VM_FAULT_FALLBACK)
  1384. copied = 0;
  1385. /*
  1386. * The fault is done by now and there's no way back (other
  1387. * thread may be already happily using PMD we have installed).
  1388. * Just ignore error from ->iomap_end since we cannot do much
  1389. * with it.
  1390. */
  1391. ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags,
  1392. &iomap);
  1393. }
  1394. unlock_entry:
  1395. dax_unlock_entry(&xas, entry);
  1396. fallback:
  1397. if (result == VM_FAULT_FALLBACK) {
  1398. split_huge_pmd(vma, vmf->pmd, vmf->address);
  1399. count_vm_event(THP_FAULT_FALLBACK);
  1400. }
  1401. out:
  1402. trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result);
  1403. return result;
  1404. }
  1405. #else
  1406. static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
  1407. const struct iomap_ops *ops)
  1408. {
  1409. return VM_FAULT_FALLBACK;
  1410. }
  1411. #endif /* CONFIG_FS_DAX_PMD */
  1412. /**
  1413. * dax_iomap_fault - handle a page fault on a DAX file
  1414. * @vmf: The description of the fault
  1415. * @pe_size: Size of the page to fault in
  1416. * @pfnp: PFN to insert for synchronous faults if fsync is required
  1417. * @iomap_errp: Storage for detailed error code in case of error
  1418. * @ops: Iomap ops passed from the file system
  1419. *
  1420. * When a page fault occurs, filesystems may call this helper in
  1421. * their fault handler for DAX files. dax_iomap_fault() assumes the caller
  1422. * has done all the necessary locking for page fault to proceed
  1423. * successfully.
  1424. */
  1425. vm_fault_t dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
  1426. pfn_t *pfnp, int *iomap_errp, const struct iomap_ops *ops)
  1427. {
  1428. switch (pe_size) {
  1429. case PE_SIZE_PTE:
  1430. return dax_iomap_pte_fault(vmf, pfnp, iomap_errp, ops);
  1431. case PE_SIZE_PMD:
  1432. return dax_iomap_pmd_fault(vmf, pfnp, ops);
  1433. default:
  1434. return VM_FAULT_FALLBACK;
  1435. }
  1436. }
  1437. EXPORT_SYMBOL_GPL(dax_iomap_fault);
  1438. /*
  1439. * dax_insert_pfn_mkwrite - insert PTE or PMD entry into page tables
  1440. * @vmf: The description of the fault
  1441. * @pfn: PFN to insert
  1442. * @order: Order of entry to insert.
  1443. *
  1444. * This function inserts a writeable PTE or PMD entry into the page tables
  1445. * for an mmaped DAX file. It also marks the page cache entry as dirty.
  1446. */
  1447. static vm_fault_t
  1448. dax_insert_pfn_mkwrite(struct vm_fault *vmf, pfn_t pfn, unsigned int order)
  1449. {
  1450. struct address_space *mapping = vmf->vma->vm_file->f_mapping;
  1451. XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, order);
  1452. void *entry;
  1453. vm_fault_t ret;
  1454. xas_lock_irq(&xas);
  1455. entry = get_unlocked_entry(&xas);
  1456. /* Did we race with someone splitting entry or so? */
  1457. if (!entry ||
  1458. (order == 0 && !dax_is_pte_entry(entry)) ||
  1459. (order == PMD_ORDER && !dax_is_pmd_entry(entry))) {
  1460. put_unlocked_entry(&xas, entry);
  1461. xas_unlock_irq(&xas);
  1462. trace_dax_insert_pfn_mkwrite_no_entry(mapping->host, vmf,
  1463. VM_FAULT_NOPAGE);
  1464. return VM_FAULT_NOPAGE;
  1465. }
  1466. xas_set_mark(&xas, PAGECACHE_TAG_DIRTY);
  1467. dax_lock_entry(&xas, entry);
  1468. xas_unlock_irq(&xas);
  1469. if (order == 0)
  1470. ret = vmf_insert_mixed_mkwrite(vmf->vma, vmf->address, pfn);
  1471. #ifdef CONFIG_FS_DAX_PMD
  1472. else if (order == PMD_ORDER)
  1473. ret = vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd,
  1474. pfn, true);
  1475. #endif
  1476. else
  1477. ret = VM_FAULT_FALLBACK;
  1478. dax_unlock_entry(&xas, entry);
  1479. trace_dax_insert_pfn_mkwrite(mapping->host, vmf, ret);
  1480. return ret;
  1481. }
  1482. /**
  1483. * dax_finish_sync_fault - finish synchronous page fault
  1484. * @vmf: The description of the fault
  1485. * @pe_size: Size of entry to be inserted
  1486. * @pfn: PFN to insert
  1487. *
  1488. * This function ensures that the file range touched by the page fault is
  1489. * stored persistently on the media and handles inserting of appropriate page
  1490. * table entry.
  1491. */
  1492. vm_fault_t dax_finish_sync_fault(struct vm_fault *vmf,
  1493. enum page_entry_size pe_size, pfn_t pfn)
  1494. {
  1495. int err;
  1496. loff_t start = ((loff_t)vmf->pgoff) << PAGE_SHIFT;
  1497. unsigned int order = pe_order(pe_size);
  1498. size_t len = PAGE_SIZE << order;
  1499. err = vfs_fsync_range(vmf->vma->vm_file, start, start + len - 1, 1);
  1500. if (err)
  1501. return VM_FAULT_SIGBUS;
  1502. return dax_insert_pfn_mkwrite(vmf, pfn, order);
  1503. }
  1504. EXPORT_SYMBOL_GPL(dax_finish_sync_fault);