dax.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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/pfn_t.h>
  17. #include <linux/slab.h>
  18. #include <linux/dax.h>
  19. #include <linux/fs.h>
  20. #include <linux/mm.h>
  21. static int dax_major;
  22. static struct class *dax_class;
  23. static DEFINE_IDA(dax_minor_ida);
  24. /**
  25. * struct dax_region - mapping infrastructure for dax devices
  26. * @id: kernel-wide unique region for a memory range
  27. * @base: linear address corresponding to @res
  28. * @kref: to pin while other agents have a need to do lookups
  29. * @dev: parent device backing this region
  30. * @align: allocation and mapping alignment for child dax devices
  31. * @res: physical address range of the region
  32. * @pfn_flags: identify whether the pfns are paged back or not
  33. */
  34. struct dax_region {
  35. int id;
  36. struct ida ida;
  37. void *base;
  38. struct kref kref;
  39. struct device *dev;
  40. unsigned int align;
  41. struct resource res;
  42. unsigned long pfn_flags;
  43. };
  44. /**
  45. * struct dax_dev - subdivision of a dax region
  46. * @region - parent region
  47. * @dev - device backing the character device
  48. * @kref - enable this data to be tracked in filp->private_data
  49. * @alive - !alive + rcu grace period == no new mappings can be established
  50. * @id - child id in the region
  51. * @num_resources - number of physical address extents in this device
  52. * @res - array of physical address ranges
  53. */
  54. struct dax_dev {
  55. struct dax_region *region;
  56. struct device *dev;
  57. struct kref kref;
  58. bool alive;
  59. int id;
  60. int num_resources;
  61. struct resource res[0];
  62. };
  63. static void dax_region_free(struct kref *kref)
  64. {
  65. struct dax_region *dax_region;
  66. dax_region = container_of(kref, struct dax_region, kref);
  67. kfree(dax_region);
  68. }
  69. void dax_region_put(struct dax_region *dax_region)
  70. {
  71. kref_put(&dax_region->kref, dax_region_free);
  72. }
  73. EXPORT_SYMBOL_GPL(dax_region_put);
  74. static void dax_dev_free(struct kref *kref)
  75. {
  76. struct dax_dev *dax_dev;
  77. dax_dev = container_of(kref, struct dax_dev, kref);
  78. dax_region_put(dax_dev->region);
  79. kfree(dax_dev);
  80. }
  81. static void dax_dev_put(struct dax_dev *dax_dev)
  82. {
  83. kref_put(&dax_dev->kref, dax_dev_free);
  84. }
  85. struct dax_region *alloc_dax_region(struct device *parent, int region_id,
  86. struct resource *res, unsigned int align, void *addr,
  87. unsigned long pfn_flags)
  88. {
  89. struct dax_region *dax_region;
  90. dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
  91. if (!dax_region)
  92. return NULL;
  93. memcpy(&dax_region->res, res, sizeof(*res));
  94. dax_region->pfn_flags = pfn_flags;
  95. kref_init(&dax_region->kref);
  96. dax_region->id = region_id;
  97. ida_init(&dax_region->ida);
  98. dax_region->align = align;
  99. dax_region->dev = parent;
  100. dax_region->base = addr;
  101. return dax_region;
  102. }
  103. EXPORT_SYMBOL_GPL(alloc_dax_region);
  104. static ssize_t size_show(struct device *dev,
  105. struct device_attribute *attr, char *buf)
  106. {
  107. struct dax_dev *dax_dev = dev_get_drvdata(dev);
  108. unsigned long long size = 0;
  109. int i;
  110. for (i = 0; i < dax_dev->num_resources; i++)
  111. size += resource_size(&dax_dev->res[i]);
  112. return sprintf(buf, "%llu\n", size);
  113. }
  114. static DEVICE_ATTR_RO(size);
  115. static struct attribute *dax_device_attributes[] = {
  116. &dev_attr_size.attr,
  117. NULL,
  118. };
  119. static const struct attribute_group dax_device_attribute_group = {
  120. .attrs = dax_device_attributes,
  121. };
  122. static const struct attribute_group *dax_attribute_groups[] = {
  123. &dax_device_attribute_group,
  124. NULL,
  125. };
  126. static void unregister_dax_dev(void *_dev)
  127. {
  128. struct device *dev = _dev;
  129. struct dax_dev *dax_dev = dev_get_drvdata(dev);
  130. struct dax_region *dax_region = dax_dev->region;
  131. dev_dbg(dev, "%s\n", __func__);
  132. /*
  133. * Note, rcu is not protecting the liveness of dax_dev, rcu is
  134. * ensuring that any fault handlers that might have seen
  135. * dax_dev->alive == true, have completed. Any fault handlers
  136. * that start after synchronize_rcu() has started will abort
  137. * upon seeing dax_dev->alive == false.
  138. */
  139. dax_dev->alive = false;
  140. synchronize_rcu();
  141. get_device(dev);
  142. device_unregister(dev);
  143. ida_simple_remove(&dax_region->ida, dax_dev->id);
  144. ida_simple_remove(&dax_minor_ida, MINOR(dev->devt));
  145. put_device(dev);
  146. dax_dev_put(dax_dev);
  147. }
  148. int devm_create_dax_dev(struct dax_region *dax_region, struct resource *res,
  149. int count)
  150. {
  151. struct device *parent = dax_region->dev;
  152. struct dax_dev *dax_dev;
  153. struct device *dev;
  154. int rc, minor;
  155. dev_t dev_t;
  156. dax_dev = kzalloc(sizeof(*dax_dev) + sizeof(*res) * count, GFP_KERNEL);
  157. if (!dax_dev)
  158. return -ENOMEM;
  159. memcpy(dax_dev->res, res, sizeof(*res) * count);
  160. dax_dev->num_resources = count;
  161. kref_init(&dax_dev->kref);
  162. dax_dev->alive = true;
  163. dax_dev->region = dax_region;
  164. kref_get(&dax_region->kref);
  165. dax_dev->id = ida_simple_get(&dax_region->ida, 0, 0, GFP_KERNEL);
  166. if (dax_dev->id < 0) {
  167. rc = dax_dev->id;
  168. goto err_id;
  169. }
  170. minor = ida_simple_get(&dax_minor_ida, 0, 0, GFP_KERNEL);
  171. if (minor < 0) {
  172. rc = minor;
  173. goto err_minor;
  174. }
  175. dev_t = MKDEV(dax_major, minor);
  176. dev = device_create_with_groups(dax_class, parent, dev_t, dax_dev,
  177. dax_attribute_groups, "dax%d.%d", dax_region->id,
  178. dax_dev->id);
  179. if (IS_ERR(dev)) {
  180. rc = PTR_ERR(dev);
  181. goto err_create;
  182. }
  183. dax_dev->dev = dev;
  184. rc = devm_add_action(dax_region->dev, unregister_dax_dev, dev);
  185. if (rc) {
  186. unregister_dax_dev(dev);
  187. return rc;
  188. }
  189. return 0;
  190. err_create:
  191. ida_simple_remove(&dax_minor_ida, minor);
  192. err_minor:
  193. ida_simple_remove(&dax_region->ida, dax_dev->id);
  194. err_id:
  195. dax_dev_put(dax_dev);
  196. return rc;
  197. }
  198. EXPORT_SYMBOL_GPL(devm_create_dax_dev);
  199. /* return an unmapped area aligned to the dax region specified alignment */
  200. static unsigned long dax_dev_get_unmapped_area(struct file *filp,
  201. unsigned long addr, unsigned long len, unsigned long pgoff,
  202. unsigned long flags)
  203. {
  204. unsigned long off, off_end, off_align, len_align, addr_align, align;
  205. struct dax_dev *dax_dev = filp ? filp->private_data : NULL;
  206. struct dax_region *dax_region;
  207. if (!dax_dev || addr)
  208. goto out;
  209. dax_region = dax_dev->region;
  210. align = dax_region->align;
  211. off = pgoff << PAGE_SHIFT;
  212. off_end = off + len;
  213. off_align = round_up(off, align);
  214. if ((off_end <= off_align) || ((off_end - off_align) < align))
  215. goto out;
  216. len_align = len + align;
  217. if ((off + len_align) < off)
  218. goto out;
  219. addr_align = current->mm->get_unmapped_area(filp, addr, len_align,
  220. pgoff, flags);
  221. if (!IS_ERR_VALUE(addr_align)) {
  222. addr_align += (off - addr_align) & (align - 1);
  223. return addr_align;
  224. }
  225. out:
  226. return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
  227. }
  228. static int __match_devt(struct device *dev, const void *data)
  229. {
  230. const dev_t *devt = data;
  231. return dev->devt == *devt;
  232. }
  233. static struct device *dax_dev_find(dev_t dev_t)
  234. {
  235. return class_find_device(dax_class, NULL, &dev_t, __match_devt);
  236. }
  237. static int dax_dev_open(struct inode *inode, struct file *filp)
  238. {
  239. struct dax_dev *dax_dev = NULL;
  240. struct device *dev;
  241. dev = dax_dev_find(inode->i_rdev);
  242. if (!dev)
  243. return -ENXIO;
  244. device_lock(dev);
  245. dax_dev = dev_get_drvdata(dev);
  246. if (dax_dev) {
  247. dev_dbg(dev, "%s\n", __func__);
  248. filp->private_data = dax_dev;
  249. kref_get(&dax_dev->kref);
  250. inode->i_flags = S_DAX;
  251. }
  252. device_unlock(dev);
  253. if (!dax_dev) {
  254. put_device(dev);
  255. return -ENXIO;
  256. }
  257. return 0;
  258. }
  259. static int dax_dev_release(struct inode *inode, struct file *filp)
  260. {
  261. struct dax_dev *dax_dev = filp->private_data;
  262. struct device *dev = dax_dev->dev;
  263. dev_dbg(dax_dev->dev, "%s\n", __func__);
  264. dax_dev_put(dax_dev);
  265. put_device(dev);
  266. return 0;
  267. }
  268. static int check_vma(struct dax_dev *dax_dev, struct vm_area_struct *vma,
  269. const char *func)
  270. {
  271. struct dax_region *dax_region = dax_dev->region;
  272. struct device *dev = dax_dev->dev;
  273. unsigned long mask;
  274. if (!dax_dev->alive)
  275. return -ENXIO;
  276. /* prevent private / writable mappings from being established */
  277. if ((vma->vm_flags & (VM_NORESERVE|VM_SHARED|VM_WRITE)) == VM_WRITE) {
  278. dev_info(dev, "%s: %s: fail, attempted private mapping\n",
  279. current->comm, func);
  280. return -EINVAL;
  281. }
  282. mask = dax_region->align - 1;
  283. if (vma->vm_start & mask || vma->vm_end & mask) {
  284. dev_info(dev, "%s: %s: fail, unaligned vma (%#lx - %#lx, %#lx)\n",
  285. current->comm, func, vma->vm_start, vma->vm_end,
  286. mask);
  287. return -EINVAL;
  288. }
  289. if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) == PFN_DEV
  290. && (vma->vm_flags & VM_DONTCOPY) == 0) {
  291. dev_info(dev, "%s: %s: fail, dax range requires MADV_DONTFORK\n",
  292. current->comm, func);
  293. return -EINVAL;
  294. }
  295. if (!vma_is_dax(vma)) {
  296. dev_info(dev, "%s: %s: fail, vma is not DAX capable\n",
  297. current->comm, func);
  298. return -EINVAL;
  299. }
  300. return 0;
  301. }
  302. static phys_addr_t pgoff_to_phys(struct dax_dev *dax_dev, pgoff_t pgoff,
  303. unsigned long size)
  304. {
  305. struct resource *res;
  306. phys_addr_t phys;
  307. int i;
  308. for (i = 0; i < dax_dev->num_resources; i++) {
  309. res = &dax_dev->res[i];
  310. phys = pgoff * PAGE_SIZE + res->start;
  311. if (phys >= res->start && phys <= res->end)
  312. break;
  313. pgoff -= PHYS_PFN(resource_size(res));
  314. }
  315. if (i < dax_dev->num_resources) {
  316. res = &dax_dev->res[i];
  317. if (phys + size - 1 <= res->end)
  318. return phys;
  319. }
  320. return -1;
  321. }
  322. static int __dax_dev_fault(struct dax_dev *dax_dev, struct vm_area_struct *vma,
  323. struct vm_fault *vmf)
  324. {
  325. unsigned long vaddr = (unsigned long) vmf->virtual_address;
  326. struct device *dev = dax_dev->dev;
  327. struct dax_region *dax_region;
  328. int rc = VM_FAULT_SIGBUS;
  329. phys_addr_t phys;
  330. pfn_t pfn;
  331. if (check_vma(dax_dev, vma, __func__))
  332. return VM_FAULT_SIGBUS;
  333. dax_region = dax_dev->region;
  334. if (dax_region->align > PAGE_SIZE) {
  335. dev_dbg(dev, "%s: alignment > fault size\n", __func__);
  336. return VM_FAULT_SIGBUS;
  337. }
  338. phys = pgoff_to_phys(dax_dev, vmf->pgoff, PAGE_SIZE);
  339. if (phys == -1) {
  340. dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__,
  341. vmf->pgoff);
  342. return VM_FAULT_SIGBUS;
  343. }
  344. pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
  345. rc = vm_insert_mixed(vma, vaddr, pfn);
  346. if (rc == -ENOMEM)
  347. return VM_FAULT_OOM;
  348. if (rc < 0 && rc != -EBUSY)
  349. return VM_FAULT_SIGBUS;
  350. return VM_FAULT_NOPAGE;
  351. }
  352. static int dax_dev_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  353. {
  354. int rc;
  355. struct file *filp = vma->vm_file;
  356. struct dax_dev *dax_dev = filp->private_data;
  357. dev_dbg(dax_dev->dev, "%s: %s: %s (%#lx - %#lx)\n", __func__,
  358. current->comm, (vmf->flags & FAULT_FLAG_WRITE)
  359. ? "write" : "read", vma->vm_start, vma->vm_end);
  360. rcu_read_lock();
  361. rc = __dax_dev_fault(dax_dev, vma, vmf);
  362. rcu_read_unlock();
  363. return rc;
  364. }
  365. static int __dax_dev_pmd_fault(struct dax_dev *dax_dev,
  366. struct vm_area_struct *vma, unsigned long addr, pmd_t *pmd,
  367. unsigned int flags)
  368. {
  369. unsigned long pmd_addr = addr & PMD_MASK;
  370. struct device *dev = dax_dev->dev;
  371. struct dax_region *dax_region;
  372. phys_addr_t phys;
  373. pgoff_t pgoff;
  374. pfn_t pfn;
  375. if (check_vma(dax_dev, vma, __func__))
  376. return VM_FAULT_SIGBUS;
  377. dax_region = dax_dev->region;
  378. if (dax_region->align > PMD_SIZE) {
  379. dev_dbg(dev, "%s: alignment > fault size\n", __func__);
  380. return VM_FAULT_SIGBUS;
  381. }
  382. /* dax pmd mappings require pfn_t_devmap() */
  383. if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) {
  384. dev_dbg(dev, "%s: alignment > fault size\n", __func__);
  385. return VM_FAULT_SIGBUS;
  386. }
  387. pgoff = linear_page_index(vma, pmd_addr);
  388. phys = pgoff_to_phys(dax_dev, pgoff, PAGE_SIZE);
  389. if (phys == -1) {
  390. dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__,
  391. pgoff);
  392. return VM_FAULT_SIGBUS;
  393. }
  394. pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
  395. return vmf_insert_pfn_pmd(vma, addr, pmd, pfn,
  396. flags & FAULT_FLAG_WRITE);
  397. }
  398. static int dax_dev_pmd_fault(struct vm_area_struct *vma, unsigned long addr,
  399. pmd_t *pmd, unsigned int flags)
  400. {
  401. int rc;
  402. struct file *filp = vma->vm_file;
  403. struct dax_dev *dax_dev = filp->private_data;
  404. dev_dbg(dax_dev->dev, "%s: %s: %s (%#lx - %#lx)\n", __func__,
  405. current->comm, (flags & FAULT_FLAG_WRITE)
  406. ? "write" : "read", vma->vm_start, vma->vm_end);
  407. rcu_read_lock();
  408. rc = __dax_dev_pmd_fault(dax_dev, vma, addr, pmd, flags);
  409. rcu_read_unlock();
  410. return rc;
  411. }
  412. static void dax_dev_vm_open(struct vm_area_struct *vma)
  413. {
  414. struct file *filp = vma->vm_file;
  415. struct dax_dev *dax_dev = filp->private_data;
  416. dev_dbg(dax_dev->dev, "%s\n", __func__);
  417. kref_get(&dax_dev->kref);
  418. }
  419. static void dax_dev_vm_close(struct vm_area_struct *vma)
  420. {
  421. struct file *filp = vma->vm_file;
  422. struct dax_dev *dax_dev = filp->private_data;
  423. dev_dbg(dax_dev->dev, "%s\n", __func__);
  424. dax_dev_put(dax_dev);
  425. }
  426. static const struct vm_operations_struct dax_dev_vm_ops = {
  427. .fault = dax_dev_fault,
  428. .pmd_fault = dax_dev_pmd_fault,
  429. .open = dax_dev_vm_open,
  430. .close = dax_dev_vm_close,
  431. };
  432. static int dax_dev_mmap(struct file *filp, struct vm_area_struct *vma)
  433. {
  434. struct dax_dev *dax_dev = filp->private_data;
  435. int rc;
  436. dev_dbg(dax_dev->dev, "%s\n", __func__);
  437. rc = check_vma(dax_dev, vma, __func__);
  438. if (rc)
  439. return rc;
  440. kref_get(&dax_dev->kref);
  441. vma->vm_ops = &dax_dev_vm_ops;
  442. vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
  443. return 0;
  444. }
  445. static const struct file_operations dax_fops = {
  446. .llseek = noop_llseek,
  447. .owner = THIS_MODULE,
  448. .open = dax_dev_open,
  449. .release = dax_dev_release,
  450. .get_unmapped_area = dax_dev_get_unmapped_area,
  451. .mmap = dax_dev_mmap,
  452. };
  453. static int __init dax_init(void)
  454. {
  455. int rc;
  456. rc = register_chrdev(0, "dax", &dax_fops);
  457. if (rc < 0)
  458. return rc;
  459. dax_major = rc;
  460. dax_class = class_create(THIS_MODULE, "dax");
  461. if (IS_ERR(dax_class)) {
  462. unregister_chrdev(dax_major, "dax");
  463. return PTR_ERR(dax_class);
  464. }
  465. return 0;
  466. }
  467. static void __exit dax_exit(void)
  468. {
  469. class_destroy(dax_class);
  470. unregister_chrdev(dax_major, "dax");
  471. ida_destroy(&dax_minor_ida);
  472. }
  473. MODULE_AUTHOR("Intel Corporation");
  474. MODULE_LICENSE("GPL v2");
  475. subsys_initcall(dax_init);
  476. module_exit(dax_exit);