dax.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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_area_struct *vma,
  356. struct vm_fault *vmf)
  357. {
  358. struct device *dev = &dax_dev->dev;
  359. struct dax_region *dax_region;
  360. int rc = VM_FAULT_SIGBUS;
  361. phys_addr_t phys;
  362. pfn_t pfn;
  363. if (check_vma(dax_dev, vma, __func__))
  364. return VM_FAULT_SIGBUS;
  365. dax_region = dax_dev->region;
  366. if (dax_region->align > PAGE_SIZE) {
  367. dev_dbg(dev, "%s: alignment > fault size\n", __func__);
  368. return VM_FAULT_SIGBUS;
  369. }
  370. phys = pgoff_to_phys(dax_dev, vmf->pgoff, PAGE_SIZE);
  371. if (phys == -1) {
  372. dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__,
  373. vmf->pgoff);
  374. return VM_FAULT_SIGBUS;
  375. }
  376. pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
  377. rc = vm_insert_mixed(vma, vmf->address, pfn);
  378. if (rc == -ENOMEM)
  379. return VM_FAULT_OOM;
  380. if (rc < 0 && rc != -EBUSY)
  381. return VM_FAULT_SIGBUS;
  382. return VM_FAULT_NOPAGE;
  383. }
  384. static int dax_dev_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  385. {
  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, vma, vmf);
  394. rcu_read_unlock();
  395. return rc;
  396. }
  397. static int __dax_dev_pmd_fault(struct dax_dev *dax_dev,
  398. struct vm_area_struct *vma, unsigned long addr, pmd_t *pmd,
  399. unsigned int flags)
  400. {
  401. unsigned long pmd_addr = addr & PMD_MASK;
  402. struct device *dev = &dax_dev->dev;
  403. struct dax_region *dax_region;
  404. phys_addr_t phys;
  405. pgoff_t pgoff;
  406. pfn_t pfn;
  407. if (check_vma(dax_dev, vma, __func__))
  408. return VM_FAULT_SIGBUS;
  409. dax_region = dax_dev->region;
  410. if (dax_region->align > PMD_SIZE) {
  411. dev_dbg(dev, "%s: alignment > fault size\n", __func__);
  412. return VM_FAULT_SIGBUS;
  413. }
  414. /* dax pmd mappings require pfn_t_devmap() */
  415. if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) {
  416. dev_dbg(dev, "%s: alignment > fault size\n", __func__);
  417. return VM_FAULT_SIGBUS;
  418. }
  419. pgoff = linear_page_index(vma, pmd_addr);
  420. phys = pgoff_to_phys(dax_dev, pgoff, PMD_SIZE);
  421. if (phys == -1) {
  422. dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__,
  423. pgoff);
  424. return VM_FAULT_SIGBUS;
  425. }
  426. pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
  427. return vmf_insert_pfn_pmd(vma, addr, pmd, pfn,
  428. flags & FAULT_FLAG_WRITE);
  429. }
  430. static int dax_dev_pmd_fault(struct vm_area_struct *vma, unsigned long addr,
  431. pmd_t *pmd, unsigned int flags)
  432. {
  433. int rc;
  434. struct file *filp = vma->vm_file;
  435. struct dax_dev *dax_dev = filp->private_data;
  436. dev_dbg(&dax_dev->dev, "%s: %s: %s (%#lx - %#lx)\n", __func__,
  437. current->comm, (flags & FAULT_FLAG_WRITE)
  438. ? "write" : "read", vma->vm_start, vma->vm_end);
  439. rcu_read_lock();
  440. rc = __dax_dev_pmd_fault(dax_dev, vma, addr, pmd, flags);
  441. rcu_read_unlock();
  442. return rc;
  443. }
  444. static const struct vm_operations_struct dax_dev_vm_ops = {
  445. .fault = dax_dev_fault,
  446. .pmd_fault = dax_dev_pmd_fault,
  447. };
  448. static int dax_mmap(struct file *filp, struct vm_area_struct *vma)
  449. {
  450. struct dax_dev *dax_dev = filp->private_data;
  451. int rc;
  452. dev_dbg(&dax_dev->dev, "%s\n", __func__);
  453. rc = check_vma(dax_dev, vma, __func__);
  454. if (rc)
  455. return rc;
  456. vma->vm_ops = &dax_dev_vm_ops;
  457. vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
  458. return 0;
  459. }
  460. /* return an unmapped area aligned to the dax region specified alignment */
  461. static unsigned long dax_get_unmapped_area(struct file *filp,
  462. unsigned long addr, unsigned long len, unsigned long pgoff,
  463. unsigned long flags)
  464. {
  465. unsigned long off, off_end, off_align, len_align, addr_align, align;
  466. struct dax_dev *dax_dev = filp ? filp->private_data : NULL;
  467. struct dax_region *dax_region;
  468. if (!dax_dev || addr)
  469. goto out;
  470. dax_region = dax_dev->region;
  471. align = dax_region->align;
  472. off = pgoff << PAGE_SHIFT;
  473. off_end = off + len;
  474. off_align = round_up(off, align);
  475. if ((off_end <= off_align) || ((off_end - off_align) < align))
  476. goto out;
  477. len_align = len + align;
  478. if ((off + len_align) < off)
  479. goto out;
  480. addr_align = current->mm->get_unmapped_area(filp, addr, len_align,
  481. pgoff, flags);
  482. if (!IS_ERR_VALUE(addr_align)) {
  483. addr_align += (off - addr_align) & (align - 1);
  484. return addr_align;
  485. }
  486. out:
  487. return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
  488. }
  489. static int dax_open(struct inode *inode, struct file *filp)
  490. {
  491. struct dax_dev *dax_dev;
  492. dax_dev = container_of(inode->i_cdev, struct dax_dev, cdev);
  493. dev_dbg(&dax_dev->dev, "%s\n", __func__);
  494. inode->i_mapping = dax_dev->inode->i_mapping;
  495. inode->i_mapping->host = dax_dev->inode;
  496. filp->f_mapping = inode->i_mapping;
  497. filp->private_data = dax_dev;
  498. inode->i_flags = S_DAX;
  499. return 0;
  500. }
  501. static int dax_release(struct inode *inode, struct file *filp)
  502. {
  503. struct dax_dev *dax_dev = filp->private_data;
  504. dev_dbg(&dax_dev->dev, "%s\n", __func__);
  505. return 0;
  506. }
  507. static const struct file_operations dax_fops = {
  508. .llseek = noop_llseek,
  509. .owner = THIS_MODULE,
  510. .open = dax_open,
  511. .release = dax_release,
  512. .get_unmapped_area = dax_get_unmapped_area,
  513. .mmap = dax_mmap,
  514. };
  515. static void dax_dev_release(struct device *dev)
  516. {
  517. struct dax_dev *dax_dev = to_dax_dev(dev);
  518. struct dax_region *dax_region = dax_dev->region;
  519. ida_simple_remove(&dax_region->ida, dax_dev->id);
  520. ida_simple_remove(&dax_minor_ida, MINOR(dev->devt));
  521. dax_region_put(dax_region);
  522. iput(dax_dev->inode);
  523. kfree(dax_dev);
  524. }
  525. static void unregister_dax_dev(void *dev)
  526. {
  527. struct dax_dev *dax_dev = to_dax_dev(dev);
  528. struct cdev *cdev = &dax_dev->cdev;
  529. dev_dbg(dev, "%s\n", __func__);
  530. /*
  531. * Note, rcu is not protecting the liveness of dax_dev, rcu is
  532. * ensuring that any fault handlers that might have seen
  533. * dax_dev->alive == true, have completed. Any fault handlers
  534. * that start after synchronize_rcu() has started will abort
  535. * upon seeing dax_dev->alive == false.
  536. */
  537. dax_dev->alive = false;
  538. synchronize_rcu();
  539. unmap_mapping_range(dax_dev->inode->i_mapping, 0, 0, 1);
  540. cdev_del(cdev);
  541. device_unregister(dev);
  542. }
  543. struct dax_dev *devm_create_dax_dev(struct dax_region *dax_region,
  544. struct resource *res, int count)
  545. {
  546. struct device *parent = dax_region->dev;
  547. struct dax_dev *dax_dev;
  548. int rc = 0, minor, i;
  549. struct device *dev;
  550. struct cdev *cdev;
  551. dev_t dev_t;
  552. dax_dev = kzalloc(sizeof(*dax_dev) + sizeof(*res) * count, GFP_KERNEL);
  553. if (!dax_dev)
  554. return ERR_PTR(-ENOMEM);
  555. for (i = 0; i < count; i++) {
  556. if (!IS_ALIGNED(res[i].start, dax_region->align)
  557. || !IS_ALIGNED(resource_size(&res[i]),
  558. dax_region->align)) {
  559. rc = -EINVAL;
  560. break;
  561. }
  562. dax_dev->res[i].start = res[i].start;
  563. dax_dev->res[i].end = res[i].end;
  564. }
  565. if (i < count)
  566. goto err_id;
  567. dax_dev->id = ida_simple_get(&dax_region->ida, 0, 0, GFP_KERNEL);
  568. if (dax_dev->id < 0) {
  569. rc = dax_dev->id;
  570. goto err_id;
  571. }
  572. minor = ida_simple_get(&dax_minor_ida, 0, 0, GFP_KERNEL);
  573. if (minor < 0) {
  574. rc = minor;
  575. goto err_minor;
  576. }
  577. dev_t = MKDEV(MAJOR(dax_devt), minor);
  578. dev = &dax_dev->dev;
  579. dax_dev->inode = dax_inode_get(&dax_dev->cdev, dev_t);
  580. if (!dax_dev->inode) {
  581. rc = -ENOMEM;
  582. goto err_inode;
  583. }
  584. /* device_initialize() so cdev can reference kobj parent */
  585. device_initialize(dev);
  586. cdev = &dax_dev->cdev;
  587. cdev_init(cdev, &dax_fops);
  588. cdev->owner = parent->driver->owner;
  589. cdev->kobj.parent = &dev->kobj;
  590. rc = cdev_add(&dax_dev->cdev, dev_t, 1);
  591. if (rc)
  592. goto err_cdev;
  593. /* from here on we're committed to teardown via dax_dev_release() */
  594. dax_dev->num_resources = count;
  595. dax_dev->alive = true;
  596. dax_dev->region = dax_region;
  597. kref_get(&dax_region->kref);
  598. dev->devt = dev_t;
  599. dev->class = dax_class;
  600. dev->parent = parent;
  601. dev->groups = dax_attribute_groups;
  602. dev->release = dax_dev_release;
  603. dev_set_name(dev, "dax%d.%d", dax_region->id, dax_dev->id);
  604. rc = device_add(dev);
  605. if (rc) {
  606. put_device(dev);
  607. return ERR_PTR(rc);
  608. }
  609. rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_dev, dev);
  610. if (rc)
  611. return ERR_PTR(rc);
  612. return dax_dev;
  613. err_cdev:
  614. iput(dax_dev->inode);
  615. err_inode:
  616. ida_simple_remove(&dax_minor_ida, minor);
  617. err_minor:
  618. ida_simple_remove(&dax_region->ida, dax_dev->id);
  619. err_id:
  620. kfree(dax_dev);
  621. return ERR_PTR(rc);
  622. }
  623. EXPORT_SYMBOL_GPL(devm_create_dax_dev);
  624. static int __init dax_init(void)
  625. {
  626. int rc;
  627. rc = dax_inode_init();
  628. if (rc)
  629. return rc;
  630. nr_dax = max(nr_dax, 256);
  631. rc = alloc_chrdev_region(&dax_devt, 0, nr_dax, "dax");
  632. if (rc)
  633. goto err_chrdev;
  634. dax_class = class_create(THIS_MODULE, "dax");
  635. if (IS_ERR(dax_class)) {
  636. rc = PTR_ERR(dax_class);
  637. goto err_class;
  638. }
  639. return 0;
  640. err_class:
  641. unregister_chrdev_region(dax_devt, nr_dax);
  642. err_chrdev:
  643. dax_inode_exit();
  644. return rc;
  645. }
  646. static void __exit dax_exit(void)
  647. {
  648. class_destroy(dax_class);
  649. unregister_chrdev_region(dax_devt, nr_dax);
  650. ida_destroy(&dax_minor_ida);
  651. dax_inode_exit();
  652. }
  653. MODULE_AUTHOR("Intel Corporation");
  654. MODULE_LICENSE("GPL v2");
  655. subsys_initcall(dax_init);
  656. module_exit(dax_exit);