dax.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /*
  2. * Copyright(c) 2016 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. #include <linux/pagemap.h>
  14. #include <linux/module.h>
  15. #include <linux/device.h>
  16. #include <linux/mount.h>
  17. #include <linux/pfn_t.h>
  18. #include <linux/hash.h>
  19. #include <linux/cdev.h>
  20. #include <linux/slab.h>
  21. #include <linux/dax.h>
  22. #include <linux/fs.h>
  23. #include <linux/mm.h>
  24. #include "dax.h"
  25. static dev_t dax_devt;
  26. DEFINE_STATIC_SRCU(dax_srcu);
  27. static struct class *dax_class;
  28. static DEFINE_IDA(dax_minor_ida);
  29. static int nr_dax = CONFIG_NR_DEV_DAX;
  30. module_param(nr_dax, int, S_IRUGO);
  31. static struct vfsmount *dax_mnt;
  32. static struct kmem_cache *dax_cache __read_mostly;
  33. static struct super_block *dax_superblock __read_mostly;
  34. MODULE_PARM_DESC(nr_dax, "max number of device-dax instances");
  35. /**
  36. * struct dax_region - mapping infrastructure for dax devices
  37. * @id: kernel-wide unique region for a memory range
  38. * @base: linear address corresponding to @res
  39. * @kref: to pin while other agents have a need to do lookups
  40. * @dev: parent device backing this region
  41. * @align: allocation and mapping alignment for child dax devices
  42. * @res: physical address range of the region
  43. * @pfn_flags: identify whether the pfns are paged back or not
  44. */
  45. struct dax_region {
  46. int id;
  47. struct ida ida;
  48. void *base;
  49. struct kref kref;
  50. struct device *dev;
  51. unsigned int align;
  52. struct resource res;
  53. unsigned long pfn_flags;
  54. };
  55. /**
  56. * struct dax_dev - subdivision of a dax region
  57. * @region - parent region
  58. * @dev - device backing the character device
  59. * @cdev - core chardev data
  60. * @alive - !alive + srcu grace period == no new mappings can be established
  61. * @id - child id in the region
  62. * @num_resources - number of physical address extents in this device
  63. * @res - array of physical address ranges
  64. */
  65. struct dax_dev {
  66. struct dax_region *region;
  67. struct inode *inode;
  68. struct device dev;
  69. struct cdev cdev;
  70. bool alive;
  71. int id;
  72. int num_resources;
  73. struct resource res[0];
  74. };
  75. static struct inode *dax_alloc_inode(struct super_block *sb)
  76. {
  77. return kmem_cache_alloc(dax_cache, GFP_KERNEL);
  78. }
  79. static void dax_i_callback(struct rcu_head *head)
  80. {
  81. struct inode *inode = container_of(head, struct inode, i_rcu);
  82. kmem_cache_free(dax_cache, inode);
  83. }
  84. static void dax_destroy_inode(struct inode *inode)
  85. {
  86. call_rcu(&inode->i_rcu, dax_i_callback);
  87. }
  88. static const struct super_operations dax_sops = {
  89. .statfs = simple_statfs,
  90. .alloc_inode = dax_alloc_inode,
  91. .destroy_inode = dax_destroy_inode,
  92. .drop_inode = generic_delete_inode,
  93. };
  94. static struct dentry *dax_mount(struct file_system_type *fs_type,
  95. int flags, const char *dev_name, void *data)
  96. {
  97. return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC);
  98. }
  99. static struct file_system_type dax_type = {
  100. .name = "dax",
  101. .mount = dax_mount,
  102. .kill_sb = kill_anon_super,
  103. };
  104. static int dax_test(struct inode *inode, void *data)
  105. {
  106. return inode->i_cdev == data;
  107. }
  108. static int dax_set(struct inode *inode, void *data)
  109. {
  110. inode->i_cdev = data;
  111. return 0;
  112. }
  113. static struct inode *dax_inode_get(struct cdev *cdev, dev_t devt)
  114. {
  115. struct inode *inode;
  116. inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
  117. dax_test, dax_set, cdev);
  118. if (!inode)
  119. return NULL;
  120. if (inode->i_state & I_NEW) {
  121. inode->i_mode = S_IFCHR;
  122. inode->i_flags = S_DAX;
  123. inode->i_rdev = devt;
  124. mapping_set_gfp_mask(&inode->i_data, GFP_USER);
  125. unlock_new_inode(inode);
  126. }
  127. return inode;
  128. }
  129. static void init_once(void *inode)
  130. {
  131. inode_init_once(inode);
  132. }
  133. static int dax_inode_init(void)
  134. {
  135. int rc;
  136. dax_cache = kmem_cache_create("dax_cache", sizeof(struct inode), 0,
  137. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  138. SLAB_MEM_SPREAD|SLAB_ACCOUNT),
  139. init_once);
  140. if (!dax_cache)
  141. return -ENOMEM;
  142. rc = register_filesystem(&dax_type);
  143. if (rc)
  144. goto err_register_fs;
  145. dax_mnt = kern_mount(&dax_type);
  146. if (IS_ERR(dax_mnt)) {
  147. rc = PTR_ERR(dax_mnt);
  148. goto err_mount;
  149. }
  150. dax_superblock = dax_mnt->mnt_sb;
  151. return 0;
  152. err_mount:
  153. unregister_filesystem(&dax_type);
  154. err_register_fs:
  155. kmem_cache_destroy(dax_cache);
  156. return rc;
  157. }
  158. static void dax_inode_exit(void)
  159. {
  160. kern_unmount(dax_mnt);
  161. unregister_filesystem(&dax_type);
  162. kmem_cache_destroy(dax_cache);
  163. }
  164. static void dax_region_free(struct kref *kref)
  165. {
  166. struct dax_region *dax_region;
  167. dax_region = container_of(kref, struct dax_region, kref);
  168. kfree(dax_region);
  169. }
  170. void dax_region_put(struct dax_region *dax_region)
  171. {
  172. kref_put(&dax_region->kref, dax_region_free);
  173. }
  174. EXPORT_SYMBOL_GPL(dax_region_put);
  175. struct dax_region *alloc_dax_region(struct device *parent, int region_id,
  176. struct resource *res, unsigned int align, void *addr,
  177. unsigned long pfn_flags)
  178. {
  179. struct dax_region *dax_region;
  180. if (!IS_ALIGNED(res->start, align)
  181. || !IS_ALIGNED(resource_size(res), align))
  182. return NULL;
  183. dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
  184. if (!dax_region)
  185. return NULL;
  186. memcpy(&dax_region->res, res, sizeof(*res));
  187. dax_region->pfn_flags = pfn_flags;
  188. kref_init(&dax_region->kref);
  189. dax_region->id = region_id;
  190. ida_init(&dax_region->ida);
  191. dax_region->align = align;
  192. dax_region->dev = parent;
  193. dax_region->base = addr;
  194. return dax_region;
  195. }
  196. EXPORT_SYMBOL_GPL(alloc_dax_region);
  197. static struct dax_dev *to_dax_dev(struct device *dev)
  198. {
  199. return container_of(dev, struct dax_dev, dev);
  200. }
  201. static ssize_t size_show(struct device *dev,
  202. struct device_attribute *attr, char *buf)
  203. {
  204. struct dax_dev *dax_dev = to_dax_dev(dev);
  205. unsigned long long size = 0;
  206. int i;
  207. for (i = 0; i < dax_dev->num_resources; i++)
  208. size += resource_size(&dax_dev->res[i]);
  209. return sprintf(buf, "%llu\n", size);
  210. }
  211. static DEVICE_ATTR_RO(size);
  212. static struct attribute *dax_device_attributes[] = {
  213. &dev_attr_size.attr,
  214. NULL,
  215. };
  216. static const struct attribute_group dax_device_attribute_group = {
  217. .attrs = dax_device_attributes,
  218. };
  219. static const struct attribute_group *dax_attribute_groups[] = {
  220. &dax_device_attribute_group,
  221. NULL,
  222. };
  223. static int check_vma(struct dax_dev *dax_dev, struct vm_area_struct *vma,
  224. const char *func)
  225. {
  226. struct dax_region *dax_region = dax_dev->region;
  227. struct device *dev = &dax_dev->dev;
  228. unsigned long mask;
  229. if (!dax_dev->alive)
  230. return -ENXIO;
  231. /* prevent private mappings from being established */
  232. if ((vma->vm_flags & VM_MAYSHARE) != VM_MAYSHARE) {
  233. dev_info(dev, "%s: %s: fail, attempted private mapping\n",
  234. current->comm, func);
  235. return -EINVAL;
  236. }
  237. mask = dax_region->align - 1;
  238. if (vma->vm_start & mask || vma->vm_end & mask) {
  239. dev_info(dev, "%s: %s: fail, unaligned vma (%#lx - %#lx, %#lx)\n",
  240. current->comm, func, vma->vm_start, vma->vm_end,
  241. mask);
  242. return -EINVAL;
  243. }
  244. if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) == PFN_DEV
  245. && (vma->vm_flags & VM_DONTCOPY) == 0) {
  246. dev_info(dev, "%s: %s: fail, dax range requires MADV_DONTFORK\n",
  247. current->comm, func);
  248. return -EINVAL;
  249. }
  250. if (!vma_is_dax(vma)) {
  251. dev_info(dev, "%s: %s: fail, vma is not DAX capable\n",
  252. current->comm, func);
  253. return -EINVAL;
  254. }
  255. return 0;
  256. }
  257. static phys_addr_t pgoff_to_phys(struct dax_dev *dax_dev, pgoff_t pgoff,
  258. unsigned long size)
  259. {
  260. struct resource *res;
  261. phys_addr_t phys;
  262. int i;
  263. for (i = 0; i < dax_dev->num_resources; i++) {
  264. res = &dax_dev->res[i];
  265. phys = pgoff * PAGE_SIZE + res->start;
  266. if (phys >= res->start && phys <= res->end)
  267. break;
  268. pgoff -= PHYS_PFN(resource_size(res));
  269. }
  270. if (i < dax_dev->num_resources) {
  271. res = &dax_dev->res[i];
  272. if (phys + size - 1 <= res->end)
  273. return phys;
  274. }
  275. return -1;
  276. }
  277. static int __dax_dev_fault(struct dax_dev *dax_dev, struct vm_area_struct *vma,
  278. struct vm_fault *vmf)
  279. {
  280. unsigned long vaddr = (unsigned long) vmf->virtual_address;
  281. struct device *dev = &dax_dev->dev;
  282. struct dax_region *dax_region;
  283. int rc = VM_FAULT_SIGBUS;
  284. phys_addr_t phys;
  285. pfn_t pfn;
  286. unsigned int fault_size = PAGE_SIZE;
  287. if (check_vma(dax_dev, vma, __func__))
  288. return VM_FAULT_SIGBUS;
  289. dax_region = dax_dev->region;
  290. if (dax_region->align > PAGE_SIZE) {
  291. dev_dbg(dev, "%s: alignment > fault size\n", __func__);
  292. return VM_FAULT_SIGBUS;
  293. }
  294. if (fault_size != dax_region->align)
  295. return VM_FAULT_SIGBUS;
  296. phys = pgoff_to_phys(dax_dev, vmf->pgoff, PAGE_SIZE);
  297. if (phys == -1) {
  298. dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__,
  299. vmf->pgoff);
  300. return VM_FAULT_SIGBUS;
  301. }
  302. pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
  303. rc = vm_insert_mixed(vma, vaddr, pfn);
  304. if (rc == -ENOMEM)
  305. return VM_FAULT_OOM;
  306. if (rc < 0 && rc != -EBUSY)
  307. return VM_FAULT_SIGBUS;
  308. return VM_FAULT_NOPAGE;
  309. }
  310. static int dax_dev_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  311. {
  312. int rc;
  313. struct file *filp = vma->vm_file;
  314. struct dax_dev *dax_dev = filp->private_data;
  315. dev_dbg(&dax_dev->dev, "%s: %s: %s (%#lx - %#lx)\n", __func__,
  316. current->comm, (vmf->flags & FAULT_FLAG_WRITE)
  317. ? "write" : "read", vma->vm_start, vma->vm_end);
  318. rcu_read_lock();
  319. rc = __dax_dev_fault(dax_dev, vma, vmf);
  320. rcu_read_unlock();
  321. return rc;
  322. }
  323. static int __dax_dev_pmd_fault(struct dax_dev *dax_dev,
  324. struct vm_area_struct *vma, unsigned long addr, pmd_t *pmd,
  325. unsigned int flags)
  326. {
  327. unsigned long pmd_addr = addr & PMD_MASK;
  328. struct device *dev = &dax_dev->dev;
  329. struct dax_region *dax_region;
  330. phys_addr_t phys;
  331. pgoff_t pgoff;
  332. pfn_t pfn;
  333. unsigned int fault_size = PMD_SIZE;
  334. if (check_vma(dax_dev, vma, __func__))
  335. return VM_FAULT_SIGBUS;
  336. dax_region = dax_dev->region;
  337. if (dax_region->align > PMD_SIZE) {
  338. dev_dbg(dev, "%s: alignment > fault size\n", __func__);
  339. return VM_FAULT_SIGBUS;
  340. }
  341. /* dax pmd mappings require pfn_t_devmap() */
  342. if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) {
  343. dev_dbg(dev, "%s: alignment > fault size\n", __func__);
  344. return VM_FAULT_SIGBUS;
  345. }
  346. if (fault_size < dax_region->align)
  347. return VM_FAULT_SIGBUS;
  348. else if (fault_size > dax_region->align)
  349. return VM_FAULT_FALLBACK;
  350. /* if we are outside of the VMA */
  351. if (pmd_addr < vma->vm_start ||
  352. (pmd_addr + PMD_SIZE) > vma->vm_end)
  353. return VM_FAULT_SIGBUS;
  354. pgoff = linear_page_index(vma, pmd_addr);
  355. phys = pgoff_to_phys(dax_dev, pgoff, PMD_SIZE);
  356. if (phys == -1) {
  357. dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__,
  358. pgoff);
  359. return VM_FAULT_SIGBUS;
  360. }
  361. pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
  362. return vmf_insert_pfn_pmd(vma, addr, pmd, pfn,
  363. flags & FAULT_FLAG_WRITE);
  364. }
  365. static int dax_dev_pmd_fault(struct vm_area_struct *vma, unsigned long addr,
  366. pmd_t *pmd, unsigned int flags)
  367. {
  368. int rc, id;
  369. struct file *filp = vma->vm_file;
  370. struct dax_dev *dax_dev = filp->private_data;
  371. dev_dbg(&dax_dev->dev, "%s: %s: %s (%#lx - %#lx)\n", __func__,
  372. current->comm, (flags & FAULT_FLAG_WRITE)
  373. ? "write" : "read", vma->vm_start, vma->vm_end);
  374. id = srcu_read_lock(&dax_srcu);
  375. rc = __dax_dev_pmd_fault(dax_dev, vma, addr, pmd, flags);
  376. srcu_read_unlock(&dax_srcu, id);
  377. return rc;
  378. }
  379. static int dax_dev_split(struct vm_area_struct *vma, unsigned long addr)
  380. {
  381. struct file *filp = vma->vm_file;
  382. struct dax_dev *dax_dev = filp->private_data;
  383. struct dax_region *dax_region = dax_dev->region;
  384. if (!IS_ALIGNED(addr, dax_region->align))
  385. return -EINVAL;
  386. return 0;
  387. }
  388. static const struct vm_operations_struct dax_dev_vm_ops = {
  389. .fault = dax_dev_fault,
  390. .pmd_fault = dax_dev_pmd_fault,
  391. .split = dax_dev_split,
  392. };
  393. static int dax_mmap(struct file *filp, struct vm_area_struct *vma)
  394. {
  395. struct dax_dev *dax_dev = filp->private_data;
  396. int rc;
  397. dev_dbg(&dax_dev->dev, "%s\n", __func__);
  398. rc = check_vma(dax_dev, vma, __func__);
  399. if (rc)
  400. return rc;
  401. vma->vm_ops = &dax_dev_vm_ops;
  402. vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
  403. return 0;
  404. }
  405. /* return an unmapped area aligned to the dax region specified alignment */
  406. static unsigned long dax_get_unmapped_area(struct file *filp,
  407. unsigned long addr, unsigned long len, unsigned long pgoff,
  408. unsigned long flags)
  409. {
  410. unsigned long off, off_end, off_align, len_align, addr_align, align;
  411. struct dax_dev *dax_dev = filp ? filp->private_data : NULL;
  412. struct dax_region *dax_region;
  413. if (!dax_dev || addr)
  414. goto out;
  415. dax_region = dax_dev->region;
  416. align = dax_region->align;
  417. off = pgoff << PAGE_SHIFT;
  418. off_end = off + len;
  419. off_align = round_up(off, align);
  420. if ((off_end <= off_align) || ((off_end - off_align) < align))
  421. goto out;
  422. len_align = len + align;
  423. if ((off + len_align) < off)
  424. goto out;
  425. addr_align = current->mm->get_unmapped_area(filp, addr, len_align,
  426. pgoff, flags);
  427. if (!IS_ERR_VALUE(addr_align)) {
  428. addr_align += (off - addr_align) & (align - 1);
  429. return addr_align;
  430. }
  431. out:
  432. return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
  433. }
  434. static int dax_open(struct inode *inode, struct file *filp)
  435. {
  436. struct dax_dev *dax_dev;
  437. dax_dev = container_of(inode->i_cdev, struct dax_dev, cdev);
  438. dev_dbg(&dax_dev->dev, "%s\n", __func__);
  439. inode->i_mapping = dax_dev->inode->i_mapping;
  440. inode->i_mapping->host = dax_dev->inode;
  441. filp->f_mapping = inode->i_mapping;
  442. filp->private_data = dax_dev;
  443. inode->i_flags = S_DAX;
  444. return 0;
  445. }
  446. static int dax_release(struct inode *inode, struct file *filp)
  447. {
  448. struct dax_dev *dax_dev = filp->private_data;
  449. dev_dbg(&dax_dev->dev, "%s\n", __func__);
  450. return 0;
  451. }
  452. static const struct file_operations dax_fops = {
  453. .llseek = noop_llseek,
  454. .owner = THIS_MODULE,
  455. .open = dax_open,
  456. .release = dax_release,
  457. .get_unmapped_area = dax_get_unmapped_area,
  458. .mmap = dax_mmap,
  459. };
  460. static void dax_dev_release(struct device *dev)
  461. {
  462. struct dax_dev *dax_dev = to_dax_dev(dev);
  463. struct dax_region *dax_region = dax_dev->region;
  464. if (dax_dev->id >= 0)
  465. ida_simple_remove(&dax_region->ida, dax_dev->id);
  466. ida_simple_remove(&dax_minor_ida, MINOR(dev->devt));
  467. dax_region_put(dax_region);
  468. iput(dax_dev->inode);
  469. kfree(dax_dev);
  470. }
  471. static void kill_dax_dev(struct dax_dev *dax_dev)
  472. {
  473. struct cdev *cdev = &dax_dev->cdev;
  474. /*
  475. * Note, rcu is not protecting the liveness of dax_dev, rcu is
  476. * ensuring that any fault handlers that might have seen
  477. * dax_dev->alive == true, have completed. Any fault handlers
  478. * that start after synchronize_srcu() has started will abort
  479. * upon seeing dax_dev->alive == false.
  480. */
  481. dax_dev->alive = false;
  482. synchronize_srcu(&dax_srcu);
  483. unmap_mapping_range(dax_dev->inode->i_mapping, 0, 0, 1);
  484. cdev_del(cdev);
  485. }
  486. static void unregister_dax_dev(void *dev)
  487. {
  488. struct dax_dev *dax_dev = to_dax_dev(dev);
  489. dev_dbg(dev, "%s\n", __func__);
  490. kill_dax_dev(dax_dev);
  491. device_unregister(dev);
  492. }
  493. struct dax_dev *devm_create_dax_dev(struct dax_region *dax_region,
  494. int id, struct resource *res, int count)
  495. {
  496. struct device *parent = dax_region->dev;
  497. struct dax_dev *dax_dev;
  498. int rc = 0, minor, i;
  499. struct device *dev;
  500. struct cdev *cdev;
  501. dev_t dev_t;
  502. dax_dev = kzalloc(sizeof(*dax_dev) + sizeof(*res) * count, GFP_KERNEL);
  503. if (!dax_dev)
  504. return ERR_PTR(-ENOMEM);
  505. for (i = 0; i < count; i++) {
  506. if (!IS_ALIGNED(res[i].start, dax_region->align)
  507. || !IS_ALIGNED(resource_size(&res[i]),
  508. dax_region->align)) {
  509. rc = -EINVAL;
  510. break;
  511. }
  512. dax_dev->res[i].start = res[i].start;
  513. dax_dev->res[i].end = res[i].end;
  514. }
  515. if (i < count)
  516. goto err_id;
  517. if (id < 0) {
  518. id = ida_simple_get(&dax_region->ida, 0, 0, GFP_KERNEL);
  519. dax_dev->id = id;
  520. if (id < 0) {
  521. rc = id;
  522. goto err_id;
  523. }
  524. } else {
  525. /* region provider owns @id lifetime */
  526. dax_dev->id = -1;
  527. }
  528. minor = ida_simple_get(&dax_minor_ida, 0, 0, GFP_KERNEL);
  529. if (minor < 0) {
  530. rc = minor;
  531. goto err_minor;
  532. }
  533. dev_t = MKDEV(MAJOR(dax_devt), minor);
  534. dev = &dax_dev->dev;
  535. dax_dev->inode = dax_inode_get(&dax_dev->cdev, dev_t);
  536. if (!dax_dev->inode) {
  537. rc = -ENOMEM;
  538. goto err_inode;
  539. }
  540. /* device_initialize() so cdev can reference kobj parent */
  541. device_initialize(dev);
  542. cdev = &dax_dev->cdev;
  543. cdev_init(cdev, &dax_fops);
  544. cdev->owner = parent->driver->owner;
  545. cdev->kobj.parent = &dev->kobj;
  546. rc = cdev_add(&dax_dev->cdev, dev_t, 1);
  547. if (rc)
  548. goto err_cdev;
  549. /* from here on we're committed to teardown via dax_dev_release() */
  550. dax_dev->num_resources = count;
  551. dax_dev->alive = true;
  552. dax_dev->region = dax_region;
  553. kref_get(&dax_region->kref);
  554. dev->devt = dev_t;
  555. dev->class = dax_class;
  556. dev->parent = parent;
  557. dev->groups = dax_attribute_groups;
  558. dev->release = dax_dev_release;
  559. dev_set_name(dev, "dax%d.%d", dax_region->id, id);
  560. rc = device_add(dev);
  561. if (rc) {
  562. kill_dax_dev(dax_dev);
  563. put_device(dev);
  564. return ERR_PTR(rc);
  565. }
  566. rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_dev, dev);
  567. if (rc)
  568. return ERR_PTR(rc);
  569. return dax_dev;
  570. err_cdev:
  571. iput(dax_dev->inode);
  572. err_inode:
  573. ida_simple_remove(&dax_minor_ida, minor);
  574. err_minor:
  575. if (dax_dev->id >= 0)
  576. ida_simple_remove(&dax_region->ida, dax_dev->id);
  577. err_id:
  578. kfree(dax_dev);
  579. return ERR_PTR(rc);
  580. }
  581. EXPORT_SYMBOL_GPL(devm_create_dax_dev);
  582. static int __init dax_init(void)
  583. {
  584. int rc;
  585. rc = dax_inode_init();
  586. if (rc)
  587. return rc;
  588. nr_dax = max(nr_dax, 256);
  589. rc = alloc_chrdev_region(&dax_devt, 0, nr_dax, "dax");
  590. if (rc)
  591. goto err_chrdev;
  592. dax_class = class_create(THIS_MODULE, "dax");
  593. if (IS_ERR(dax_class)) {
  594. rc = PTR_ERR(dax_class);
  595. goto err_class;
  596. }
  597. return 0;
  598. err_class:
  599. unregister_chrdev_region(dax_devt, nr_dax);
  600. err_chrdev:
  601. dax_inode_exit();
  602. return rc;
  603. }
  604. static void __exit dax_exit(void)
  605. {
  606. class_destroy(dax_class);
  607. unregister_chrdev_region(dax_devt, nr_dax);
  608. ida_destroy(&dax_minor_ida);
  609. dax_inode_exit();
  610. }
  611. MODULE_AUTHOR("Intel Corporation");
  612. MODULE_LICENSE("GPL v2");
  613. subsys_initcall(dax_init);
  614. module_exit(dax_exit);