super.c 15 KB

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