dax.c 18 KB

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