pfn_devs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * Copyright(c) 2013-2015 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/blkdev.h>
  14. #include <linux/device.h>
  15. #include <linux/genhd.h>
  16. #include <linux/sizes.h>
  17. #include <linux/slab.h>
  18. #include <linux/fs.h>
  19. #include <linux/mm.h>
  20. #include "nd-core.h"
  21. #include "pfn.h"
  22. #include "nd.h"
  23. static void nd_pfn_release(struct device *dev)
  24. {
  25. struct nd_region *nd_region = to_nd_region(dev->parent);
  26. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  27. dev_dbg(dev, "%s\n", __func__);
  28. nd_detach_ndns(&nd_pfn->dev, &nd_pfn->ndns);
  29. ida_simple_remove(&nd_region->pfn_ida, nd_pfn->id);
  30. kfree(nd_pfn->uuid);
  31. kfree(nd_pfn);
  32. }
  33. static struct device_type nd_pfn_device_type = {
  34. .name = "nd_pfn",
  35. .release = nd_pfn_release,
  36. };
  37. bool is_nd_pfn(struct device *dev)
  38. {
  39. return dev ? dev->type == &nd_pfn_device_type : false;
  40. }
  41. EXPORT_SYMBOL(is_nd_pfn);
  42. struct nd_pfn *to_nd_pfn(struct device *dev)
  43. {
  44. struct nd_pfn *nd_pfn = container_of(dev, struct nd_pfn, dev);
  45. WARN_ON(!is_nd_pfn(dev));
  46. return nd_pfn;
  47. }
  48. EXPORT_SYMBOL(to_nd_pfn);
  49. static ssize_t mode_show(struct device *dev,
  50. struct device_attribute *attr, char *buf)
  51. {
  52. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  53. switch (nd_pfn->mode) {
  54. case PFN_MODE_RAM:
  55. return sprintf(buf, "ram\n");
  56. case PFN_MODE_PMEM:
  57. return sprintf(buf, "pmem\n");
  58. default:
  59. return sprintf(buf, "none\n");
  60. }
  61. }
  62. static ssize_t mode_store(struct device *dev,
  63. struct device_attribute *attr, const char *buf, size_t len)
  64. {
  65. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  66. ssize_t rc = 0;
  67. device_lock(dev);
  68. nvdimm_bus_lock(dev);
  69. if (dev->driver)
  70. rc = -EBUSY;
  71. else {
  72. size_t n = len - 1;
  73. if (strncmp(buf, "pmem\n", n) == 0
  74. || strncmp(buf, "pmem", n) == 0) {
  75. nd_pfn->mode = PFN_MODE_PMEM;
  76. } else if (strncmp(buf, "ram\n", n) == 0
  77. || strncmp(buf, "ram", n) == 0)
  78. nd_pfn->mode = PFN_MODE_RAM;
  79. else if (strncmp(buf, "none\n", n) == 0
  80. || strncmp(buf, "none", n) == 0)
  81. nd_pfn->mode = PFN_MODE_NONE;
  82. else
  83. rc = -EINVAL;
  84. }
  85. dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
  86. rc, buf, buf[len - 1] == '\n' ? "" : "\n");
  87. nvdimm_bus_unlock(dev);
  88. device_unlock(dev);
  89. return rc ? rc : len;
  90. }
  91. static DEVICE_ATTR_RW(mode);
  92. static ssize_t align_show(struct device *dev,
  93. struct device_attribute *attr, char *buf)
  94. {
  95. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  96. return sprintf(buf, "%lx\n", nd_pfn->align);
  97. }
  98. static ssize_t __align_store(struct nd_pfn *nd_pfn, const char *buf)
  99. {
  100. unsigned long val;
  101. int rc;
  102. rc = kstrtoul(buf, 0, &val);
  103. if (rc)
  104. return rc;
  105. if (!is_power_of_2(val) || val < PAGE_SIZE || val > SZ_1G)
  106. return -EINVAL;
  107. if (nd_pfn->dev.driver)
  108. return -EBUSY;
  109. else
  110. nd_pfn->align = val;
  111. return 0;
  112. }
  113. static ssize_t align_store(struct device *dev,
  114. struct device_attribute *attr, const char *buf, size_t len)
  115. {
  116. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  117. ssize_t rc;
  118. device_lock(dev);
  119. nvdimm_bus_lock(dev);
  120. rc = __align_store(nd_pfn, buf);
  121. dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
  122. rc, buf, buf[len - 1] == '\n' ? "" : "\n");
  123. nvdimm_bus_unlock(dev);
  124. device_unlock(dev);
  125. return rc ? rc : len;
  126. }
  127. static DEVICE_ATTR_RW(align);
  128. static ssize_t uuid_show(struct device *dev,
  129. struct device_attribute *attr, char *buf)
  130. {
  131. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  132. if (nd_pfn->uuid)
  133. return sprintf(buf, "%pUb\n", nd_pfn->uuid);
  134. return sprintf(buf, "\n");
  135. }
  136. static ssize_t uuid_store(struct device *dev,
  137. struct device_attribute *attr, const char *buf, size_t len)
  138. {
  139. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  140. ssize_t rc;
  141. device_lock(dev);
  142. rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len);
  143. dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
  144. rc, buf, buf[len - 1] == '\n' ? "" : "\n");
  145. device_unlock(dev);
  146. return rc ? rc : len;
  147. }
  148. static DEVICE_ATTR_RW(uuid);
  149. static ssize_t namespace_show(struct device *dev,
  150. struct device_attribute *attr, char *buf)
  151. {
  152. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  153. ssize_t rc;
  154. nvdimm_bus_lock(dev);
  155. rc = sprintf(buf, "%s\n", nd_pfn->ndns
  156. ? dev_name(&nd_pfn->ndns->dev) : "");
  157. nvdimm_bus_unlock(dev);
  158. return rc;
  159. }
  160. static ssize_t namespace_store(struct device *dev,
  161. struct device_attribute *attr, const char *buf, size_t len)
  162. {
  163. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  164. ssize_t rc;
  165. device_lock(dev);
  166. nvdimm_bus_lock(dev);
  167. rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len);
  168. dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
  169. rc, buf, buf[len - 1] == '\n' ? "" : "\n");
  170. nvdimm_bus_unlock(dev);
  171. device_unlock(dev);
  172. return rc;
  173. }
  174. static DEVICE_ATTR_RW(namespace);
  175. static ssize_t resource_show(struct device *dev,
  176. struct device_attribute *attr, char *buf)
  177. {
  178. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  179. ssize_t rc;
  180. device_lock(dev);
  181. if (dev->driver) {
  182. struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
  183. u64 offset = __le64_to_cpu(pfn_sb->dataoff);
  184. struct nd_namespace_common *ndns = nd_pfn->ndns;
  185. u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
  186. struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
  187. rc = sprintf(buf, "%#llx\n", (unsigned long long) nsio->res.start
  188. + start_pad + offset);
  189. } else {
  190. /* no address to convey if the pfn instance is disabled */
  191. rc = -ENXIO;
  192. }
  193. device_unlock(dev);
  194. return rc;
  195. }
  196. static DEVICE_ATTR_RO(resource);
  197. static ssize_t size_show(struct device *dev,
  198. struct device_attribute *attr, char *buf)
  199. {
  200. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  201. ssize_t rc;
  202. device_lock(dev);
  203. if (dev->driver) {
  204. struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
  205. u64 offset = __le64_to_cpu(pfn_sb->dataoff);
  206. struct nd_namespace_common *ndns = nd_pfn->ndns;
  207. u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
  208. u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
  209. struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
  210. rc = sprintf(buf, "%llu\n", (unsigned long long)
  211. resource_size(&nsio->res) - start_pad
  212. - end_trunc - offset);
  213. } else {
  214. /* no size to convey if the pfn instance is disabled */
  215. rc = -ENXIO;
  216. }
  217. device_unlock(dev);
  218. return rc;
  219. }
  220. static DEVICE_ATTR_RO(size);
  221. static struct attribute *nd_pfn_attributes[] = {
  222. &dev_attr_mode.attr,
  223. &dev_attr_namespace.attr,
  224. &dev_attr_uuid.attr,
  225. &dev_attr_align.attr,
  226. &dev_attr_resource.attr,
  227. &dev_attr_size.attr,
  228. NULL,
  229. };
  230. static struct attribute_group nd_pfn_attribute_group = {
  231. .attrs = nd_pfn_attributes,
  232. };
  233. static const struct attribute_group *nd_pfn_attribute_groups[] = {
  234. &nd_pfn_attribute_group,
  235. &nd_device_attribute_group,
  236. &nd_numa_attribute_group,
  237. NULL,
  238. };
  239. static struct device *__nd_pfn_create(struct nd_region *nd_region,
  240. struct nd_namespace_common *ndns)
  241. {
  242. struct nd_pfn *nd_pfn;
  243. struct device *dev;
  244. /* we can only create pages for contiguous ranged of pmem */
  245. if (!is_nd_pmem(&nd_region->dev))
  246. return NULL;
  247. nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL);
  248. if (!nd_pfn)
  249. return NULL;
  250. nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL);
  251. if (nd_pfn->id < 0) {
  252. kfree(nd_pfn);
  253. return NULL;
  254. }
  255. nd_pfn->mode = PFN_MODE_NONE;
  256. nd_pfn->align = HPAGE_SIZE;
  257. dev = &nd_pfn->dev;
  258. dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id);
  259. dev->parent = &nd_region->dev;
  260. dev->type = &nd_pfn_device_type;
  261. dev->groups = nd_pfn_attribute_groups;
  262. device_initialize(&nd_pfn->dev);
  263. if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
  264. dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n",
  265. __func__, dev_name(ndns->claim));
  266. put_device(dev);
  267. return NULL;
  268. }
  269. return dev;
  270. }
  271. struct device *nd_pfn_create(struct nd_region *nd_region)
  272. {
  273. struct device *dev = __nd_pfn_create(nd_region, NULL);
  274. if (dev)
  275. __nd_device_register(dev);
  276. return dev;
  277. }
  278. int nd_pfn_validate(struct nd_pfn *nd_pfn)
  279. {
  280. u64 checksum, offset;
  281. struct nd_namespace_io *nsio;
  282. struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
  283. struct nd_namespace_common *ndns = nd_pfn->ndns;
  284. const u8 *parent_uuid = nd_dev_to_uuid(&ndns->dev);
  285. if (!pfn_sb || !ndns)
  286. return -ENODEV;
  287. if (!is_nd_pmem(nd_pfn->dev.parent))
  288. return -ENODEV;
  289. if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb)))
  290. return -ENXIO;
  291. if (memcmp(pfn_sb->signature, PFN_SIG, PFN_SIG_LEN) != 0)
  292. return -ENODEV;
  293. checksum = le64_to_cpu(pfn_sb->checksum);
  294. pfn_sb->checksum = 0;
  295. if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb))
  296. return -ENODEV;
  297. pfn_sb->checksum = cpu_to_le64(checksum);
  298. if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0)
  299. return -ENODEV;
  300. if (__le16_to_cpu(pfn_sb->version_minor) < 1) {
  301. pfn_sb->start_pad = 0;
  302. pfn_sb->end_trunc = 0;
  303. }
  304. switch (le32_to_cpu(pfn_sb->mode)) {
  305. case PFN_MODE_RAM:
  306. case PFN_MODE_PMEM:
  307. break;
  308. default:
  309. return -ENXIO;
  310. }
  311. if (!nd_pfn->uuid) {
  312. /* from probe we allocate */
  313. nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL);
  314. if (!nd_pfn->uuid)
  315. return -ENOMEM;
  316. } else {
  317. /* from init we validate */
  318. if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0)
  319. return -EINVAL;
  320. }
  321. if (nd_pfn->align > nvdimm_namespace_capacity(ndns)) {
  322. dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n",
  323. nd_pfn->align, nvdimm_namespace_capacity(ndns));
  324. return -EINVAL;
  325. }
  326. /*
  327. * These warnings are verbose because they can only trigger in
  328. * the case where the physical address alignment of the
  329. * namespace has changed since the pfn superblock was
  330. * established.
  331. */
  332. offset = le64_to_cpu(pfn_sb->dataoff);
  333. nsio = to_nd_namespace_io(&ndns->dev);
  334. if (offset >= resource_size(&nsio->res)) {
  335. dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n",
  336. dev_name(&ndns->dev));
  337. return -EBUSY;
  338. }
  339. nd_pfn->align = 1UL << ilog2(offset);
  340. if (!is_power_of_2(offset) || offset < PAGE_SIZE) {
  341. dev_err(&nd_pfn->dev, "bad offset: %#llx dax disabled\n",
  342. offset);
  343. return -ENXIO;
  344. }
  345. return 0;
  346. }
  347. EXPORT_SYMBOL(nd_pfn_validate);
  348. int nd_pfn_probe(struct nd_namespace_common *ndns, void *drvdata)
  349. {
  350. int rc;
  351. struct device *dev;
  352. struct nd_pfn *nd_pfn;
  353. struct nd_pfn_sb *pfn_sb;
  354. struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
  355. if (ndns->force_raw)
  356. return -ENODEV;
  357. nvdimm_bus_lock(&ndns->dev);
  358. dev = __nd_pfn_create(nd_region, ndns);
  359. nvdimm_bus_unlock(&ndns->dev);
  360. if (!dev)
  361. return -ENOMEM;
  362. dev_set_drvdata(dev, drvdata);
  363. pfn_sb = kzalloc(sizeof(*pfn_sb), GFP_KERNEL);
  364. nd_pfn = to_nd_pfn(dev);
  365. nd_pfn->pfn_sb = pfn_sb;
  366. rc = nd_pfn_validate(nd_pfn);
  367. nd_pfn->pfn_sb = NULL;
  368. kfree(pfn_sb);
  369. dev_dbg(&ndns->dev, "%s: pfn: %s\n", __func__,
  370. rc == 0 ? dev_name(dev) : "<none>");
  371. if (rc < 0) {
  372. __nd_detach_ndns(dev, &nd_pfn->ndns);
  373. put_device(dev);
  374. } else
  375. __nd_device_register(&nd_pfn->dev);
  376. return rc;
  377. }
  378. EXPORT_SYMBOL(nd_pfn_probe);