device.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /*
  2. * Copyright(c) 2016 - 2017 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/pfn_t.h>
  17. #include <linux/cdev.h>
  18. #include <linux/slab.h>
  19. #include <linux/dax.h>
  20. #include <linux/fs.h>
  21. #include <linux/mm.h>
  22. #include <linux/mman.h>
  23. #include "dax-private.h"
  24. #include "dax.h"
  25. static struct class *dax_class;
  26. /*
  27. * Rely on the fact that drvdata is set before the attributes are
  28. * registered, and that the attributes are unregistered before drvdata
  29. * is cleared to assume that drvdata is always valid.
  30. */
  31. static ssize_t id_show(struct device *dev,
  32. struct device_attribute *attr, char *buf)
  33. {
  34. struct dax_region *dax_region = dev_get_drvdata(dev);
  35. return sprintf(buf, "%d\n", dax_region->id);
  36. }
  37. static DEVICE_ATTR_RO(id);
  38. static ssize_t region_size_show(struct device *dev,
  39. struct device_attribute *attr, char *buf)
  40. {
  41. struct dax_region *dax_region = dev_get_drvdata(dev);
  42. return sprintf(buf, "%llu\n", (unsigned long long)
  43. resource_size(&dax_region->res));
  44. }
  45. static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
  46. region_size_show, NULL);
  47. static ssize_t align_show(struct device *dev,
  48. struct device_attribute *attr, char *buf)
  49. {
  50. struct dax_region *dax_region = dev_get_drvdata(dev);
  51. return sprintf(buf, "%u\n", dax_region->align);
  52. }
  53. static DEVICE_ATTR_RO(align);
  54. static struct attribute *dax_region_attributes[] = {
  55. &dev_attr_region_size.attr,
  56. &dev_attr_align.attr,
  57. &dev_attr_id.attr,
  58. NULL,
  59. };
  60. static const struct attribute_group dax_region_attribute_group = {
  61. .name = "dax_region",
  62. .attrs = dax_region_attributes,
  63. };
  64. static const struct attribute_group *dax_region_attribute_groups[] = {
  65. &dax_region_attribute_group,
  66. NULL,
  67. };
  68. static void dax_region_free(struct kref *kref)
  69. {
  70. struct dax_region *dax_region;
  71. dax_region = container_of(kref, struct dax_region, kref);
  72. kfree(dax_region);
  73. }
  74. void dax_region_put(struct dax_region *dax_region)
  75. {
  76. kref_put(&dax_region->kref, dax_region_free);
  77. }
  78. EXPORT_SYMBOL_GPL(dax_region_put);
  79. static void dax_region_unregister(void *region)
  80. {
  81. struct dax_region *dax_region = region;
  82. sysfs_remove_groups(&dax_region->dev->kobj,
  83. dax_region_attribute_groups);
  84. dax_region_put(dax_region);
  85. }
  86. struct dax_region *alloc_dax_region(struct device *parent, int region_id,
  87. struct resource *res, unsigned int align, void *addr,
  88. unsigned long pfn_flags)
  89. {
  90. struct dax_region *dax_region;
  91. /*
  92. * The DAX core assumes that it can store its private data in
  93. * parent->driver_data. This WARN is a reminder / safeguard for
  94. * developers of device-dax drivers.
  95. */
  96. if (dev_get_drvdata(parent)) {
  97. dev_WARN(parent, "dax core failed to setup private data\n");
  98. return NULL;
  99. }
  100. if (!IS_ALIGNED(res->start, align)
  101. || !IS_ALIGNED(resource_size(res), align))
  102. return NULL;
  103. dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
  104. if (!dax_region)
  105. return NULL;
  106. dev_set_drvdata(parent, dax_region);
  107. memcpy(&dax_region->res, res, sizeof(*res));
  108. dax_region->pfn_flags = pfn_flags;
  109. kref_init(&dax_region->kref);
  110. dax_region->id = region_id;
  111. ida_init(&dax_region->ida);
  112. dax_region->align = align;
  113. dax_region->dev = parent;
  114. dax_region->base = addr;
  115. if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
  116. kfree(dax_region);
  117. return NULL;
  118. }
  119. kref_get(&dax_region->kref);
  120. if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
  121. return NULL;
  122. return dax_region;
  123. }
  124. EXPORT_SYMBOL_GPL(alloc_dax_region);
  125. static struct dev_dax *to_dev_dax(struct device *dev)
  126. {
  127. return container_of(dev, struct dev_dax, dev);
  128. }
  129. static ssize_t size_show(struct device *dev,
  130. struct device_attribute *attr, char *buf)
  131. {
  132. struct dev_dax *dev_dax = to_dev_dax(dev);
  133. unsigned long long size = 0;
  134. int i;
  135. for (i = 0; i < dev_dax->num_resources; i++)
  136. size += resource_size(&dev_dax->res[i]);
  137. return sprintf(buf, "%llu\n", size);
  138. }
  139. static DEVICE_ATTR_RO(size);
  140. static struct attribute *dev_dax_attributes[] = {
  141. &dev_attr_size.attr,
  142. NULL,
  143. };
  144. static const struct attribute_group dev_dax_attribute_group = {
  145. .attrs = dev_dax_attributes,
  146. };
  147. static const struct attribute_group *dax_attribute_groups[] = {
  148. &dev_dax_attribute_group,
  149. NULL,
  150. };
  151. static int check_vma(struct dev_dax *dev_dax, struct vm_area_struct *vma,
  152. const char *func)
  153. {
  154. struct dax_region *dax_region = dev_dax->region;
  155. struct device *dev = &dev_dax->dev;
  156. unsigned long mask;
  157. if (!dax_alive(dev_dax->dax_dev))
  158. return -ENXIO;
  159. /* prevent private mappings from being established */
  160. if ((vma->vm_flags & VM_MAYSHARE) != VM_MAYSHARE) {
  161. dev_info_ratelimited(dev,
  162. "%s: %s: fail, attempted private mapping\n",
  163. current->comm, func);
  164. return -EINVAL;
  165. }
  166. mask = dax_region->align - 1;
  167. if (vma->vm_start & mask || vma->vm_end & mask) {
  168. dev_info_ratelimited(dev,
  169. "%s: %s: fail, unaligned vma (%#lx - %#lx, %#lx)\n",
  170. current->comm, func, vma->vm_start, vma->vm_end,
  171. mask);
  172. return -EINVAL;
  173. }
  174. if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) == PFN_DEV
  175. && (vma->vm_flags & VM_DONTCOPY) == 0) {
  176. dev_info_ratelimited(dev,
  177. "%s: %s: fail, dax range requires MADV_DONTFORK\n",
  178. current->comm, func);
  179. return -EINVAL;
  180. }
  181. if (!vma_is_dax(vma)) {
  182. dev_info_ratelimited(dev,
  183. "%s: %s: fail, vma is not DAX capable\n",
  184. current->comm, func);
  185. return -EINVAL;
  186. }
  187. return 0;
  188. }
  189. /* see "strong" declaration in tools/testing/nvdimm/dax-dev.c */
  190. __weak phys_addr_t dax_pgoff_to_phys(struct dev_dax *dev_dax, pgoff_t pgoff,
  191. unsigned long size)
  192. {
  193. struct resource *res;
  194. /* gcc-4.6.3-nolibc for i386 complains that this is uninitialized */
  195. phys_addr_t uninitialized_var(phys);
  196. int i;
  197. for (i = 0; i < dev_dax->num_resources; i++) {
  198. res = &dev_dax->res[i];
  199. phys = pgoff * PAGE_SIZE + res->start;
  200. if (phys >= res->start && phys <= res->end)
  201. break;
  202. pgoff -= PHYS_PFN(resource_size(res));
  203. }
  204. if (i < dev_dax->num_resources) {
  205. res = &dev_dax->res[i];
  206. if (phys + size - 1 <= res->end)
  207. return phys;
  208. }
  209. return -1;
  210. }
  211. static vm_fault_t __dev_dax_pte_fault(struct dev_dax *dev_dax,
  212. struct vm_fault *vmf, pfn_t *pfn)
  213. {
  214. struct device *dev = &dev_dax->dev;
  215. struct dax_region *dax_region;
  216. phys_addr_t phys;
  217. unsigned int fault_size = PAGE_SIZE;
  218. if (check_vma(dev_dax, vmf->vma, __func__))
  219. return VM_FAULT_SIGBUS;
  220. dax_region = dev_dax->region;
  221. if (dax_region->align > PAGE_SIZE) {
  222. dev_dbg(dev, "alignment (%#x) > fault size (%#x)\n",
  223. dax_region->align, fault_size);
  224. return VM_FAULT_SIGBUS;
  225. }
  226. if (fault_size != dax_region->align)
  227. return VM_FAULT_SIGBUS;
  228. phys = dax_pgoff_to_phys(dev_dax, vmf->pgoff, PAGE_SIZE);
  229. if (phys == -1) {
  230. dev_dbg(dev, "pgoff_to_phys(%#lx) failed\n", vmf->pgoff);
  231. return VM_FAULT_SIGBUS;
  232. }
  233. *pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
  234. return vmf_insert_mixed(vmf->vma, vmf->address, *pfn);
  235. }
  236. static vm_fault_t __dev_dax_pmd_fault(struct dev_dax *dev_dax,
  237. struct vm_fault *vmf, pfn_t *pfn)
  238. {
  239. unsigned long pmd_addr = vmf->address & PMD_MASK;
  240. struct device *dev = &dev_dax->dev;
  241. struct dax_region *dax_region;
  242. phys_addr_t phys;
  243. pgoff_t pgoff;
  244. unsigned int fault_size = PMD_SIZE;
  245. if (check_vma(dev_dax, vmf->vma, __func__))
  246. return VM_FAULT_SIGBUS;
  247. dax_region = dev_dax->region;
  248. if (dax_region->align > PMD_SIZE) {
  249. dev_dbg(dev, "alignment (%#x) > fault size (%#x)\n",
  250. dax_region->align, fault_size);
  251. return VM_FAULT_SIGBUS;
  252. }
  253. /* dax pmd mappings require pfn_t_devmap() */
  254. if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) {
  255. dev_dbg(dev, "region lacks devmap flags\n");
  256. return VM_FAULT_SIGBUS;
  257. }
  258. if (fault_size < dax_region->align)
  259. return VM_FAULT_SIGBUS;
  260. else if (fault_size > dax_region->align)
  261. return VM_FAULT_FALLBACK;
  262. /* if we are outside of the VMA */
  263. if (pmd_addr < vmf->vma->vm_start ||
  264. (pmd_addr + PMD_SIZE) > vmf->vma->vm_end)
  265. return VM_FAULT_SIGBUS;
  266. pgoff = linear_page_index(vmf->vma, pmd_addr);
  267. phys = dax_pgoff_to_phys(dev_dax, pgoff, PMD_SIZE);
  268. if (phys == -1) {
  269. dev_dbg(dev, "pgoff_to_phys(%#lx) failed\n", pgoff);
  270. return VM_FAULT_SIGBUS;
  271. }
  272. *pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
  273. return vmf_insert_pfn_pmd(vmf, *pfn, vmf->flags & FAULT_FLAG_WRITE);
  274. }
  275. #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
  276. static vm_fault_t __dev_dax_pud_fault(struct dev_dax *dev_dax,
  277. struct vm_fault *vmf, pfn_t *pfn)
  278. {
  279. unsigned long pud_addr = vmf->address & PUD_MASK;
  280. struct device *dev = &dev_dax->dev;
  281. struct dax_region *dax_region;
  282. phys_addr_t phys;
  283. pgoff_t pgoff;
  284. unsigned int fault_size = PUD_SIZE;
  285. if (check_vma(dev_dax, vmf->vma, __func__))
  286. return VM_FAULT_SIGBUS;
  287. dax_region = dev_dax->region;
  288. if (dax_region->align > PUD_SIZE) {
  289. dev_dbg(dev, "alignment (%#x) > fault size (%#x)\n",
  290. dax_region->align, fault_size);
  291. return VM_FAULT_SIGBUS;
  292. }
  293. /* dax pud mappings require pfn_t_devmap() */
  294. if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) {
  295. dev_dbg(dev, "region lacks devmap flags\n");
  296. return VM_FAULT_SIGBUS;
  297. }
  298. if (fault_size < dax_region->align)
  299. return VM_FAULT_SIGBUS;
  300. else if (fault_size > dax_region->align)
  301. return VM_FAULT_FALLBACK;
  302. /* if we are outside of the VMA */
  303. if (pud_addr < vmf->vma->vm_start ||
  304. (pud_addr + PUD_SIZE) > vmf->vma->vm_end)
  305. return VM_FAULT_SIGBUS;
  306. pgoff = linear_page_index(vmf->vma, pud_addr);
  307. phys = dax_pgoff_to_phys(dev_dax, pgoff, PUD_SIZE);
  308. if (phys == -1) {
  309. dev_dbg(dev, "pgoff_to_phys(%#lx) failed\n", pgoff);
  310. return VM_FAULT_SIGBUS;
  311. }
  312. *pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
  313. return vmf_insert_pfn_pud(vmf, *pfn, vmf->flags & FAULT_FLAG_WRITE);
  314. }
  315. #else
  316. static vm_fault_t __dev_dax_pud_fault(struct dev_dax *dev_dax,
  317. struct vm_fault *vmf, pfn_t *pfn)
  318. {
  319. return VM_FAULT_FALLBACK;
  320. }
  321. #endif /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
  322. static vm_fault_t dev_dax_huge_fault(struct vm_fault *vmf,
  323. enum page_entry_size pe_size)
  324. {
  325. struct file *filp = vmf->vma->vm_file;
  326. unsigned long fault_size;
  327. vm_fault_t rc = VM_FAULT_SIGBUS;
  328. int id;
  329. pfn_t pfn;
  330. struct dev_dax *dev_dax = filp->private_data;
  331. dev_dbg(&dev_dax->dev, "%s: %s (%#lx - %#lx) size = %d\n", current->comm,
  332. (vmf->flags & FAULT_FLAG_WRITE) ? "write" : "read",
  333. vmf->vma->vm_start, vmf->vma->vm_end, pe_size);
  334. id = dax_read_lock();
  335. switch (pe_size) {
  336. case PE_SIZE_PTE:
  337. fault_size = PAGE_SIZE;
  338. rc = __dev_dax_pte_fault(dev_dax, vmf, &pfn);
  339. break;
  340. case PE_SIZE_PMD:
  341. fault_size = PMD_SIZE;
  342. rc = __dev_dax_pmd_fault(dev_dax, vmf, &pfn);
  343. break;
  344. case PE_SIZE_PUD:
  345. fault_size = PUD_SIZE;
  346. rc = __dev_dax_pud_fault(dev_dax, vmf, &pfn);
  347. break;
  348. default:
  349. rc = VM_FAULT_SIGBUS;
  350. }
  351. if (rc == VM_FAULT_NOPAGE) {
  352. unsigned long i;
  353. pgoff_t pgoff;
  354. /*
  355. * In the device-dax case the only possibility for a
  356. * VM_FAULT_NOPAGE result is when device-dax capacity is
  357. * mapped. No need to consider the zero page, or racing
  358. * conflicting mappings.
  359. */
  360. pgoff = linear_page_index(vmf->vma, vmf->address
  361. & ~(fault_size - 1));
  362. for (i = 0; i < fault_size / PAGE_SIZE; i++) {
  363. struct page *page;
  364. page = pfn_to_page(pfn_t_to_pfn(pfn) + i);
  365. if (page->mapping)
  366. continue;
  367. page->mapping = filp->f_mapping;
  368. page->index = pgoff + i;
  369. }
  370. }
  371. dax_read_unlock(id);
  372. return rc;
  373. }
  374. static vm_fault_t dev_dax_fault(struct vm_fault *vmf)
  375. {
  376. return dev_dax_huge_fault(vmf, PE_SIZE_PTE);
  377. }
  378. static int dev_dax_split(struct vm_area_struct *vma, unsigned long addr)
  379. {
  380. struct file *filp = vma->vm_file;
  381. struct dev_dax *dev_dax = filp->private_data;
  382. struct dax_region *dax_region = dev_dax->region;
  383. if (!IS_ALIGNED(addr, dax_region->align))
  384. return -EINVAL;
  385. return 0;
  386. }
  387. static unsigned long dev_dax_pagesize(struct vm_area_struct *vma)
  388. {
  389. struct file *filp = vma->vm_file;
  390. struct dev_dax *dev_dax = filp->private_data;
  391. struct dax_region *dax_region = dev_dax->region;
  392. return dax_region->align;
  393. }
  394. static const struct vm_operations_struct dax_vm_ops = {
  395. .fault = dev_dax_fault,
  396. .huge_fault = dev_dax_huge_fault,
  397. .split = dev_dax_split,
  398. .pagesize = dev_dax_pagesize,
  399. };
  400. static int dax_mmap(struct file *filp, struct vm_area_struct *vma)
  401. {
  402. struct dev_dax *dev_dax = filp->private_data;
  403. int rc, id;
  404. dev_dbg(&dev_dax->dev, "trace\n");
  405. /*
  406. * We lock to check dax_dev liveness and will re-check at
  407. * fault time.
  408. */
  409. id = dax_read_lock();
  410. rc = check_vma(dev_dax, vma, __func__);
  411. dax_read_unlock(id);
  412. if (rc)
  413. return rc;
  414. vma->vm_ops = &dax_vm_ops;
  415. vma->vm_flags |= VM_HUGEPAGE;
  416. return 0;
  417. }
  418. /* return an unmapped area aligned to the dax region specified alignment */
  419. static unsigned long dax_get_unmapped_area(struct file *filp,
  420. unsigned long addr, unsigned long len, unsigned long pgoff,
  421. unsigned long flags)
  422. {
  423. unsigned long off, off_end, off_align, len_align, addr_align, align;
  424. struct dev_dax *dev_dax = filp ? filp->private_data : NULL;
  425. struct dax_region *dax_region;
  426. if (!dev_dax || addr)
  427. goto out;
  428. dax_region = dev_dax->region;
  429. align = dax_region->align;
  430. off = pgoff << PAGE_SHIFT;
  431. off_end = off + len;
  432. off_align = round_up(off, align);
  433. if ((off_end <= off_align) || ((off_end - off_align) < align))
  434. goto out;
  435. len_align = len + align;
  436. if ((off + len_align) < off)
  437. goto out;
  438. addr_align = current->mm->get_unmapped_area(filp, addr, len_align,
  439. pgoff, flags);
  440. if (!IS_ERR_VALUE(addr_align)) {
  441. addr_align += (off - addr_align) & (align - 1);
  442. return addr_align;
  443. }
  444. out:
  445. return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
  446. }
  447. static const struct address_space_operations dev_dax_aops = {
  448. .set_page_dirty = noop_set_page_dirty,
  449. .invalidatepage = noop_invalidatepage,
  450. };
  451. static int dax_open(struct inode *inode, struct file *filp)
  452. {
  453. struct dax_device *dax_dev = inode_dax(inode);
  454. struct inode *__dax_inode = dax_inode(dax_dev);
  455. struct dev_dax *dev_dax = dax_get_private(dax_dev);
  456. dev_dbg(&dev_dax->dev, "trace\n");
  457. inode->i_mapping = __dax_inode->i_mapping;
  458. inode->i_mapping->host = __dax_inode;
  459. inode->i_mapping->a_ops = &dev_dax_aops;
  460. filp->f_mapping = inode->i_mapping;
  461. filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
  462. filp->private_data = dev_dax;
  463. inode->i_flags = S_DAX;
  464. return 0;
  465. }
  466. static int dax_release(struct inode *inode, struct file *filp)
  467. {
  468. struct dev_dax *dev_dax = filp->private_data;
  469. dev_dbg(&dev_dax->dev, "trace\n");
  470. return 0;
  471. }
  472. static const struct file_operations dax_fops = {
  473. .llseek = noop_llseek,
  474. .owner = THIS_MODULE,
  475. .open = dax_open,
  476. .release = dax_release,
  477. .get_unmapped_area = dax_get_unmapped_area,
  478. .mmap = dax_mmap,
  479. .mmap_supported_flags = MAP_SYNC,
  480. };
  481. static void dev_dax_release(struct device *dev)
  482. {
  483. struct dev_dax *dev_dax = to_dev_dax(dev);
  484. struct dax_region *dax_region = dev_dax->region;
  485. struct dax_device *dax_dev = dev_dax->dax_dev;
  486. if (dev_dax->id >= 0)
  487. ida_simple_remove(&dax_region->ida, dev_dax->id);
  488. dax_region_put(dax_region);
  489. put_dax(dax_dev);
  490. kfree(dev_dax);
  491. }
  492. static void kill_dev_dax(struct dev_dax *dev_dax)
  493. {
  494. struct dax_device *dax_dev = dev_dax->dax_dev;
  495. struct inode *inode = dax_inode(dax_dev);
  496. kill_dax(dax_dev);
  497. unmap_mapping_range(inode->i_mapping, 0, 0, 1);
  498. }
  499. static void unregister_dev_dax(void *dev)
  500. {
  501. struct dev_dax *dev_dax = to_dev_dax(dev);
  502. struct dax_device *dax_dev = dev_dax->dax_dev;
  503. struct inode *inode = dax_inode(dax_dev);
  504. struct cdev *cdev = inode->i_cdev;
  505. dev_dbg(dev, "trace\n");
  506. kill_dev_dax(dev_dax);
  507. cdev_device_del(cdev, dev);
  508. put_device(dev);
  509. }
  510. struct dev_dax *devm_create_dev_dax(struct dax_region *dax_region,
  511. int id, struct resource *res, int count)
  512. {
  513. struct device *parent = dax_region->dev;
  514. struct dax_device *dax_dev;
  515. struct dev_dax *dev_dax;
  516. struct inode *inode;
  517. struct device *dev;
  518. struct cdev *cdev;
  519. int rc, i;
  520. if (!count)
  521. return ERR_PTR(-EINVAL);
  522. dev_dax = kzalloc(struct_size(dev_dax, res, count), GFP_KERNEL);
  523. if (!dev_dax)
  524. return ERR_PTR(-ENOMEM);
  525. for (i = 0; i < count; i++) {
  526. if (!IS_ALIGNED(res[i].start, dax_region->align)
  527. || !IS_ALIGNED(resource_size(&res[i]),
  528. dax_region->align)) {
  529. rc = -EINVAL;
  530. break;
  531. }
  532. dev_dax->res[i].start = res[i].start;
  533. dev_dax->res[i].end = res[i].end;
  534. }
  535. if (i < count)
  536. goto err_id;
  537. if (id < 0) {
  538. id = ida_simple_get(&dax_region->ida, 0, 0, GFP_KERNEL);
  539. dev_dax->id = id;
  540. if (id < 0) {
  541. rc = id;
  542. goto err_id;
  543. }
  544. } else {
  545. /* region provider owns @id lifetime */
  546. dev_dax->id = -1;
  547. }
  548. /*
  549. * No 'host' or dax_operations since there is no access to this
  550. * device outside of mmap of the resulting character device.
  551. */
  552. dax_dev = alloc_dax(dev_dax, NULL, NULL);
  553. if (!dax_dev) {
  554. rc = -ENOMEM;
  555. goto err_dax;
  556. }
  557. /* from here on we're committed to teardown via dax_dev_release() */
  558. dev = &dev_dax->dev;
  559. device_initialize(dev);
  560. inode = dax_inode(dax_dev);
  561. cdev = inode->i_cdev;
  562. cdev_init(cdev, &dax_fops);
  563. cdev->owner = parent->driver->owner;
  564. dev_dax->num_resources = count;
  565. dev_dax->dax_dev = dax_dev;
  566. dev_dax->region = dax_region;
  567. kref_get(&dax_region->kref);
  568. dev->devt = inode->i_rdev;
  569. dev->class = dax_class;
  570. dev->parent = parent;
  571. dev->groups = dax_attribute_groups;
  572. dev->release = dev_dax_release;
  573. dev_set_name(dev, "dax%d.%d", dax_region->id, id);
  574. rc = cdev_device_add(cdev, dev);
  575. if (rc) {
  576. kill_dev_dax(dev_dax);
  577. put_device(dev);
  578. return ERR_PTR(rc);
  579. }
  580. rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev);
  581. if (rc)
  582. return ERR_PTR(rc);
  583. return dev_dax;
  584. err_dax:
  585. if (dev_dax->id >= 0)
  586. ida_simple_remove(&dax_region->ida, dev_dax->id);
  587. err_id:
  588. kfree(dev_dax);
  589. return ERR_PTR(rc);
  590. }
  591. EXPORT_SYMBOL_GPL(devm_create_dev_dax);
  592. static int __init dax_init(void)
  593. {
  594. dax_class = class_create(THIS_MODULE, "dax");
  595. return PTR_ERR_OR_ZERO(dax_class);
  596. }
  597. static void __exit dax_exit(void)
  598. {
  599. class_destroy(dax_class);
  600. }
  601. MODULE_AUTHOR("Intel Corporation");
  602. MODULE_LICENSE("GPL v2");
  603. subsys_initcall(dax_init);
  604. module_exit(dax_exit);