super.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright(c) 2017 Intel Corporation. All rights reserved.
  4. */
  5. #include <linux/pagemap.h>
  6. #include <linux/module.h>
  7. #include <linux/mount.h>
  8. #include <linux/pseudo_fs.h>
  9. #include <linux/magic.h>
  10. #include <linux/pfn_t.h>
  11. #include <linux/cdev.h>
  12. #include <linux/slab.h>
  13. #include <linux/uio.h>
  14. #include <linux/dax.h>
  15. #include <linux/fs.h>
  16. #include <linux/cacheinfo.h>
  17. #include "dax-private.h"
  18. /**
  19. * struct dax_device - anchor object for dax services
  20. * @inode: core vfs
  21. * @cdev: optional character interface for "device dax"
  22. * @private: dax driver private data
  23. * @flags: state and boolean properties
  24. * @ops: operations for this device
  25. * @holder_data: holder of a dax_device: could be filesystem or mapped device
  26. * @holder_ops: operations for the inner holder
  27. */
  28. struct dax_device {
  29. struct inode inode;
  30. struct cdev cdev;
  31. void *private;
  32. unsigned long flags;
  33. const struct dax_operations *ops;
  34. void *holder_data;
  35. const struct dax_holder_operations *holder_ops;
  36. };
  37. static dev_t dax_devt;
  38. DEFINE_STATIC_SRCU(dax_srcu);
  39. static struct vfsmount *dax_mnt;
  40. static DEFINE_IDA(dax_minor_ida);
  41. static struct kmem_cache *dax_cache __read_mostly;
  42. static struct super_block *dax_superblock __read_mostly;
  43. int dax_read_lock(void)
  44. {
  45. return srcu_read_lock(&dax_srcu);
  46. }
  47. EXPORT_SYMBOL_GPL(dax_read_lock);
  48. void dax_read_unlock(int id)
  49. {
  50. srcu_read_unlock(&dax_srcu, id);
  51. }
  52. EXPORT_SYMBOL_GPL(dax_read_unlock);
  53. #if defined(CONFIG_BLOCK) && defined(CONFIG_FS_DAX)
  54. #include <linux/blkdev.h>
  55. static DEFINE_XARRAY(dax_hosts);
  56. int dax_add_host(struct dax_device *dax_dev, struct gendisk *disk)
  57. {
  58. return xa_insert(&dax_hosts, (unsigned long)disk, dax_dev, GFP_KERNEL);
  59. }
  60. EXPORT_SYMBOL_GPL(dax_add_host);
  61. void dax_remove_host(struct gendisk *disk)
  62. {
  63. xa_erase(&dax_hosts, (unsigned long)disk);
  64. }
  65. EXPORT_SYMBOL_GPL(dax_remove_host);
  66. /**
  67. * fs_dax_get_by_bdev() - temporary lookup mechanism for filesystem-dax
  68. * @bdev: block device to find a dax_device for
  69. * @start_off: returns the byte offset into the dax_device that @bdev starts
  70. * @holder: filesystem or mapped device inside the dax_device
  71. * @ops: operations for the inner holder
  72. */
  73. struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev, u64 *start_off,
  74. void *holder, const struct dax_holder_operations *ops)
  75. {
  76. struct dax_device *dax_dev;
  77. u64 part_size;
  78. int id;
  79. if (!blk_queue_dax(bdev->bd_disk->queue))
  80. return NULL;
  81. *start_off = get_start_sect(bdev) * SECTOR_SIZE;
  82. part_size = bdev_nr_sectors(bdev) * SECTOR_SIZE;
  83. if (*start_off % PAGE_SIZE || part_size % PAGE_SIZE) {
  84. pr_info("%pg: error: unaligned partition for dax\n", bdev);
  85. return NULL;
  86. }
  87. id = dax_read_lock();
  88. dax_dev = xa_load(&dax_hosts, (unsigned long)bdev->bd_disk);
  89. if (!dax_dev || !dax_alive(dax_dev) || !igrab(&dax_dev->inode))
  90. dax_dev = NULL;
  91. else if (holder) {
  92. if (!cmpxchg(&dax_dev->holder_data, NULL, holder))
  93. dax_dev->holder_ops = ops;
  94. else
  95. dax_dev = NULL;
  96. }
  97. dax_read_unlock(id);
  98. return dax_dev;
  99. }
  100. EXPORT_SYMBOL_GPL(fs_dax_get_by_bdev);
  101. void fs_put_dax(struct dax_device *dax_dev, void *holder)
  102. {
  103. if (dax_dev && holder &&
  104. cmpxchg(&dax_dev->holder_data, holder, NULL) == holder)
  105. dax_dev->holder_ops = NULL;
  106. put_dax(dax_dev);
  107. }
  108. EXPORT_SYMBOL_GPL(fs_put_dax);
  109. #endif /* CONFIG_BLOCK && CONFIG_FS_DAX */
  110. enum dax_device_flags {
  111. /* !alive + rcu grace period == no new operations / mappings */
  112. DAXDEV_ALIVE,
  113. /* gate whether dax_flush() calls the low level flush routine */
  114. DAXDEV_WRITE_CACHE,
  115. /* flag to check if device supports synchronous flush */
  116. DAXDEV_SYNC,
  117. /* do not leave the caches dirty after writes */
  118. DAXDEV_NOCACHE,
  119. /* handle CPU fetch exceptions during reads */
  120. DAXDEV_NOMC,
  121. };
  122. /**
  123. * dax_direct_access() - translate a device pgoff to an absolute pfn
  124. * @dax_dev: a dax_device instance representing the logical memory range
  125. * @pgoff: offset in pages from the start of the device to translate
  126. * @nr_pages: number of consecutive pages caller can handle relative to @pfn
  127. * @mode: indicator on normal access or recovery write
  128. * @kaddr: output parameter that returns a virtual address mapping of pfn
  129. * @pfn: output parameter that returns an absolute pfn translation of @pgoff
  130. *
  131. * Return: negative errno if an error occurs, otherwise the number of
  132. * pages accessible at the device relative @pgoff.
  133. */
  134. long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
  135. enum dax_access_mode mode, void **kaddr, pfn_t *pfn)
  136. {
  137. long avail;
  138. if (!dax_dev)
  139. return -EOPNOTSUPP;
  140. if (!dax_alive(dax_dev))
  141. return -ENXIO;
  142. if (nr_pages < 0)
  143. return -EINVAL;
  144. avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages,
  145. mode, kaddr, pfn);
  146. if (!avail)
  147. return -ERANGE;
  148. return min(avail, nr_pages);
  149. }
  150. EXPORT_SYMBOL_GPL(dax_direct_access);
  151. size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
  152. size_t bytes, struct iov_iter *i)
  153. {
  154. if (!dax_alive(dax_dev))
  155. return 0;
  156. /*
  157. * The userspace address for the memory copy has already been validated
  158. * via access_ok() in vfs_write, so use the 'no check' version to bypass
  159. * the HARDENED_USERCOPY overhead.
  160. */
  161. if (test_bit(DAXDEV_NOCACHE, &dax_dev->flags))
  162. return _copy_from_iter_flushcache(addr, bytes, i);
  163. return _copy_from_iter(addr, bytes, i);
  164. }
  165. size_t dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
  166. size_t bytes, struct iov_iter *i)
  167. {
  168. if (!dax_alive(dax_dev))
  169. return 0;
  170. /*
  171. * The userspace address for the memory copy has already been validated
  172. * via access_ok() in vfs_red, so use the 'no check' version to bypass
  173. * the HARDENED_USERCOPY overhead.
  174. */
  175. if (test_bit(DAXDEV_NOMC, &dax_dev->flags))
  176. return _copy_mc_to_iter(addr, bytes, i);
  177. return _copy_to_iter(addr, bytes, i);
  178. }
  179. int dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
  180. size_t nr_pages)
  181. {
  182. int ret;
  183. if (!dax_alive(dax_dev))
  184. return -ENXIO;
  185. /*
  186. * There are no callers that want to zero more than one page as of now.
  187. * Once users are there, this check can be removed after the
  188. * device mapper code has been updated to split ranges across targets.
  189. */
  190. if (nr_pages != 1)
  191. return -EIO;
  192. ret = dax_dev->ops->zero_page_range(dax_dev, pgoff, nr_pages);
  193. return dax_mem2blk_err(ret);
  194. }
  195. EXPORT_SYMBOL_GPL(dax_zero_page_range);
  196. size_t dax_recovery_write(struct dax_device *dax_dev, pgoff_t pgoff,
  197. void *addr, size_t bytes, struct iov_iter *iter)
  198. {
  199. if (!dax_dev->ops->recovery_write)
  200. return 0;
  201. return dax_dev->ops->recovery_write(dax_dev, pgoff, addr, bytes, iter);
  202. }
  203. EXPORT_SYMBOL_GPL(dax_recovery_write);
  204. int dax_holder_notify_failure(struct dax_device *dax_dev, u64 off,
  205. u64 len, int mf_flags)
  206. {
  207. int rc, id;
  208. id = dax_read_lock();
  209. if (!dax_alive(dax_dev)) {
  210. rc = -ENXIO;
  211. goto out;
  212. }
  213. if (!dax_dev->holder_ops) {
  214. rc = -EOPNOTSUPP;
  215. goto out;
  216. }
  217. rc = dax_dev->holder_ops->notify_failure(dax_dev, off, len, mf_flags);
  218. out:
  219. dax_read_unlock(id);
  220. return rc;
  221. }
  222. EXPORT_SYMBOL_GPL(dax_holder_notify_failure);
  223. #ifdef CONFIG_ARCH_HAS_PMEM_API
  224. void arch_wb_cache_pmem(void *addr, size_t size);
  225. void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
  226. {
  227. if (unlikely(!dax_write_cache_enabled(dax_dev)))
  228. return;
  229. arch_wb_cache_pmem(addr, size);
  230. }
  231. #else
  232. void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
  233. {
  234. }
  235. #endif
  236. EXPORT_SYMBOL_GPL(dax_flush);
  237. void dax_write_cache(struct dax_device *dax_dev, bool wc)
  238. {
  239. if (wc)
  240. set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
  241. else
  242. clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
  243. }
  244. EXPORT_SYMBOL_GPL(dax_write_cache);
  245. bool dax_write_cache_enabled(struct dax_device *dax_dev)
  246. {
  247. return test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
  248. }
  249. EXPORT_SYMBOL_GPL(dax_write_cache_enabled);
  250. bool dax_synchronous(struct dax_device *dax_dev)
  251. {
  252. return test_bit(DAXDEV_SYNC, &dax_dev->flags);
  253. }
  254. EXPORT_SYMBOL_GPL(dax_synchronous);
  255. void set_dax_synchronous(struct dax_device *dax_dev)
  256. {
  257. set_bit(DAXDEV_SYNC, &dax_dev->flags);
  258. }
  259. EXPORT_SYMBOL_GPL(set_dax_synchronous);
  260. void set_dax_nocache(struct dax_device *dax_dev)
  261. {
  262. set_bit(DAXDEV_NOCACHE, &dax_dev->flags);
  263. }
  264. EXPORT_SYMBOL_GPL(set_dax_nocache);
  265. void set_dax_nomc(struct dax_device *dax_dev)
  266. {
  267. set_bit(DAXDEV_NOMC, &dax_dev->flags);
  268. }
  269. EXPORT_SYMBOL_GPL(set_dax_nomc);
  270. bool dax_alive(struct dax_device *dax_dev)
  271. {
  272. lockdep_assert_held(&dax_srcu);
  273. return test_bit(DAXDEV_ALIVE, &dax_dev->flags);
  274. }
  275. EXPORT_SYMBOL_GPL(dax_alive);
  276. /*
  277. * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
  278. * that any fault handlers or operations that might have seen
  279. * dax_alive(), have completed. Any operations that start after
  280. * synchronize_srcu() has run will abort upon seeing !dax_alive().
  281. *
  282. * Note, because alloc_dax() returns an ERR_PTR() on error, callers
  283. * typically store its result into a local variable in order to check
  284. * the result. Therefore, care must be taken to populate the struct
  285. * device dax_dev field make sure the dax_dev is not leaked.
  286. */
  287. void kill_dax(struct dax_device *dax_dev)
  288. {
  289. if (!dax_dev)
  290. return;
  291. if (dax_dev->holder_data != NULL)
  292. dax_holder_notify_failure(dax_dev, 0, U64_MAX,
  293. MF_MEM_PRE_REMOVE);
  294. clear_bit(DAXDEV_ALIVE, &dax_dev->flags);
  295. synchronize_srcu(&dax_srcu);
  296. /* clear holder data */
  297. dax_dev->holder_ops = NULL;
  298. dax_dev->holder_data = NULL;
  299. }
  300. EXPORT_SYMBOL_GPL(kill_dax);
  301. void run_dax(struct dax_device *dax_dev)
  302. {
  303. set_bit(DAXDEV_ALIVE, &dax_dev->flags);
  304. }
  305. EXPORT_SYMBOL_GPL(run_dax);
  306. static struct inode *dax_alloc_inode(struct super_block *sb)
  307. {
  308. struct dax_device *dax_dev;
  309. struct inode *inode;
  310. dax_dev = alloc_inode_sb(sb, dax_cache, GFP_KERNEL);
  311. if (!dax_dev)
  312. return NULL;
  313. inode = &dax_dev->inode;
  314. inode->i_rdev = 0;
  315. return inode;
  316. }
  317. static struct dax_device *to_dax_dev(struct inode *inode)
  318. {
  319. return container_of(inode, struct dax_device, inode);
  320. }
  321. static void dax_free_inode(struct inode *inode)
  322. {
  323. struct dax_device *dax_dev = to_dax_dev(inode);
  324. if (inode->i_rdev)
  325. ida_free(&dax_minor_ida, iminor(inode));
  326. kmem_cache_free(dax_cache, dax_dev);
  327. }
  328. static void dax_destroy_inode(struct inode *inode)
  329. {
  330. struct dax_device *dax_dev = to_dax_dev(inode);
  331. WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags),
  332. "kill_dax() must be called before final iput()\n");
  333. }
  334. static const struct super_operations dax_sops = {
  335. .statfs = simple_statfs,
  336. .alloc_inode = dax_alloc_inode,
  337. .destroy_inode = dax_destroy_inode,
  338. .free_inode = dax_free_inode,
  339. .drop_inode = generic_delete_inode,
  340. };
  341. static int dax_init_fs_context(struct fs_context *fc)
  342. {
  343. struct pseudo_fs_context *ctx = init_pseudo(fc, DAXFS_MAGIC);
  344. if (!ctx)
  345. return -ENOMEM;
  346. ctx->ops = &dax_sops;
  347. return 0;
  348. }
  349. static struct file_system_type dax_fs_type = {
  350. .name = "dax",
  351. .init_fs_context = dax_init_fs_context,
  352. .kill_sb = kill_anon_super,
  353. };
  354. static int dax_test(struct inode *inode, void *data)
  355. {
  356. dev_t devt = *(dev_t *) data;
  357. return inode->i_rdev == devt;
  358. }
  359. static int dax_set(struct inode *inode, void *data)
  360. {
  361. dev_t devt = *(dev_t *) data;
  362. inode->i_rdev = devt;
  363. return 0;
  364. }
  365. static struct dax_device *dax_dev_get(dev_t devt)
  366. {
  367. struct dax_device *dax_dev;
  368. struct inode *inode;
  369. inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
  370. dax_test, dax_set, &devt);
  371. if (!inode)
  372. return NULL;
  373. dax_dev = to_dax_dev(inode);
  374. if (inode->i_state & I_NEW) {
  375. set_bit(DAXDEV_ALIVE, &dax_dev->flags);
  376. inode->i_cdev = &dax_dev->cdev;
  377. inode->i_mode = S_IFCHR;
  378. inode->i_flags = S_DAX;
  379. mapping_set_gfp_mask(&inode->i_data, GFP_USER);
  380. unlock_new_inode(inode);
  381. }
  382. return dax_dev;
  383. }
  384. struct dax_device *alloc_dax(void *private, const struct dax_operations *ops)
  385. {
  386. struct dax_device *dax_dev;
  387. dev_t devt;
  388. int minor;
  389. /*
  390. * Unavailable on architectures with virtually aliased data caches,
  391. * except for device-dax (NULL operations pointer), which does
  392. * not use aliased mappings from the kernel.
  393. */
  394. if (ops && cpu_dcache_is_aliasing())
  395. return ERR_PTR(-EOPNOTSUPP);
  396. if (WARN_ON_ONCE(ops && !ops->zero_page_range))
  397. return ERR_PTR(-EINVAL);
  398. minor = ida_alloc_max(&dax_minor_ida, MINORMASK, GFP_KERNEL);
  399. if (minor < 0)
  400. return ERR_PTR(-ENOMEM);
  401. devt = MKDEV(MAJOR(dax_devt), minor);
  402. dax_dev = dax_dev_get(devt);
  403. if (!dax_dev)
  404. goto err_dev;
  405. dax_dev->ops = ops;
  406. dax_dev->private = private;
  407. return dax_dev;
  408. err_dev:
  409. ida_free(&dax_minor_ida, minor);
  410. return ERR_PTR(-ENOMEM);
  411. }
  412. EXPORT_SYMBOL_GPL(alloc_dax);
  413. void put_dax(struct dax_device *dax_dev)
  414. {
  415. if (!dax_dev)
  416. return;
  417. iput(&dax_dev->inode);
  418. }
  419. EXPORT_SYMBOL_GPL(put_dax);
  420. /**
  421. * dax_holder() - obtain the holder of a dax device
  422. * @dax_dev: a dax_device instance
  423. *
  424. * Return: the holder's data which represents the holder if registered,
  425. * otherwize NULL.
  426. */
  427. void *dax_holder(struct dax_device *dax_dev)
  428. {
  429. return dax_dev->holder_data;
  430. }
  431. EXPORT_SYMBOL_GPL(dax_holder);
  432. /**
  433. * inode_dax: convert a public inode into its dax_dev
  434. * @inode: An inode with i_cdev pointing to a dax_dev
  435. *
  436. * Note this is not equivalent to to_dax_dev() which is for private
  437. * internal use where we know the inode filesystem type == dax_fs_type.
  438. */
  439. struct dax_device *inode_dax(struct inode *inode)
  440. {
  441. struct cdev *cdev = inode->i_cdev;
  442. return container_of(cdev, struct dax_device, cdev);
  443. }
  444. EXPORT_SYMBOL_GPL(inode_dax);
  445. struct inode *dax_inode(struct dax_device *dax_dev)
  446. {
  447. return &dax_dev->inode;
  448. }
  449. EXPORT_SYMBOL_GPL(dax_inode);
  450. void *dax_get_private(struct dax_device *dax_dev)
  451. {
  452. if (!test_bit(DAXDEV_ALIVE, &dax_dev->flags))
  453. return NULL;
  454. return dax_dev->private;
  455. }
  456. EXPORT_SYMBOL_GPL(dax_get_private);
  457. static void init_once(void *_dax_dev)
  458. {
  459. struct dax_device *dax_dev = _dax_dev;
  460. struct inode *inode = &dax_dev->inode;
  461. memset(dax_dev, 0, sizeof(*dax_dev));
  462. inode_init_once(inode);
  463. }
  464. static int dax_fs_init(void)
  465. {
  466. int rc;
  467. dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
  468. SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
  469. init_once);
  470. if (!dax_cache)
  471. return -ENOMEM;
  472. dax_mnt = kern_mount(&dax_fs_type);
  473. if (IS_ERR(dax_mnt)) {
  474. rc = PTR_ERR(dax_mnt);
  475. goto err_mount;
  476. }
  477. dax_superblock = dax_mnt->mnt_sb;
  478. return 0;
  479. err_mount:
  480. kmem_cache_destroy(dax_cache);
  481. return rc;
  482. }
  483. static void dax_fs_exit(void)
  484. {
  485. kern_unmount(dax_mnt);
  486. rcu_barrier();
  487. kmem_cache_destroy(dax_cache);
  488. }
  489. static int __init dax_core_init(void)
  490. {
  491. int rc;
  492. rc = dax_fs_init();
  493. if (rc)
  494. return rc;
  495. rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax");
  496. if (rc)
  497. goto err_chrdev;
  498. rc = dax_bus_init();
  499. if (rc)
  500. goto err_bus;
  501. return 0;
  502. err_bus:
  503. unregister_chrdev_region(dax_devt, MINORMASK+1);
  504. err_chrdev:
  505. dax_fs_exit();
  506. return 0;
  507. }
  508. static void __exit dax_core_exit(void)
  509. {
  510. dax_bus_exit();
  511. unregister_chrdev_region(dax_devt, MINORMASK+1);
  512. ida_destroy(&dax_minor_ida);
  513. dax_fs_exit();
  514. }
  515. MODULE_AUTHOR("Intel Corporation");
  516. MODULE_DESCRIPTION("DAX: direct access to differentiated memory");
  517. MODULE_LICENSE("GPL v2");
  518. subsys_initcall(dax_core_init);
  519. module_exit(dax_core_exit);