pfn_devs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright(c) 2013-2016 Intel Corporation. All rights reserved.
  4. */
  5. #include <linux/memremap.h>
  6. #include <linux/blkdev.h>
  7. #include <linux/device.h>
  8. #include <linux/sizes.h>
  9. #include <linux/slab.h>
  10. #include <linux/fs.h>
  11. #include <linux/mm.h>
  12. #include "nd-core.h"
  13. #include "pfn.h"
  14. #include "nd.h"
  15. static const bool page_struct_override = IS_ENABLED(CONFIG_NVDIMM_KMSAN);
  16. static void nd_pfn_release(struct device *dev)
  17. {
  18. struct nd_region *nd_region = to_nd_region(dev->parent);
  19. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  20. dev_dbg(dev, "trace\n");
  21. nd_detach_ndns(&nd_pfn->dev, &nd_pfn->ndns);
  22. ida_simple_remove(&nd_region->pfn_ida, nd_pfn->id);
  23. kfree(nd_pfn->uuid);
  24. kfree(nd_pfn);
  25. }
  26. struct nd_pfn *to_nd_pfn(struct device *dev)
  27. {
  28. struct nd_pfn *nd_pfn = container_of(dev, struct nd_pfn, dev);
  29. WARN_ON(!is_nd_pfn(dev));
  30. return nd_pfn;
  31. }
  32. EXPORT_SYMBOL(to_nd_pfn);
  33. static ssize_t mode_show(struct device *dev,
  34. struct device_attribute *attr, char *buf)
  35. {
  36. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  37. switch (nd_pfn->mode) {
  38. case PFN_MODE_RAM:
  39. return sprintf(buf, "ram\n");
  40. case PFN_MODE_PMEM:
  41. return sprintf(buf, "pmem\n");
  42. default:
  43. return sprintf(buf, "none\n");
  44. }
  45. }
  46. static ssize_t mode_store(struct device *dev,
  47. struct device_attribute *attr, const char *buf, size_t len)
  48. {
  49. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  50. ssize_t rc = 0;
  51. device_lock(dev);
  52. nvdimm_bus_lock(dev);
  53. if (dev->driver)
  54. rc = -EBUSY;
  55. else {
  56. size_t n = len - 1;
  57. if (strncmp(buf, "pmem\n", n) == 0
  58. || strncmp(buf, "pmem", n) == 0) {
  59. nd_pfn->mode = PFN_MODE_PMEM;
  60. } else if (strncmp(buf, "ram\n", n) == 0
  61. || strncmp(buf, "ram", n) == 0)
  62. nd_pfn->mode = PFN_MODE_RAM;
  63. else if (strncmp(buf, "none\n", n) == 0
  64. || strncmp(buf, "none", n) == 0)
  65. nd_pfn->mode = PFN_MODE_NONE;
  66. else
  67. rc = -EINVAL;
  68. }
  69. dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
  70. buf[len - 1] == '\n' ? "" : "\n");
  71. nvdimm_bus_unlock(dev);
  72. device_unlock(dev);
  73. return rc ? rc : len;
  74. }
  75. static DEVICE_ATTR_RW(mode);
  76. static ssize_t align_show(struct device *dev,
  77. struct device_attribute *attr, char *buf)
  78. {
  79. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  80. return sprintf(buf, "%ld\n", nd_pfn->align);
  81. }
  82. static unsigned long *nd_pfn_supported_alignments(unsigned long *alignments)
  83. {
  84. alignments[0] = PAGE_SIZE;
  85. if (has_transparent_hugepage()) {
  86. alignments[1] = HPAGE_PMD_SIZE;
  87. if (IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD))
  88. alignments[2] = HPAGE_PUD_SIZE;
  89. }
  90. return alignments;
  91. }
  92. /*
  93. * Use pmd mapping if supported as default alignment
  94. */
  95. static unsigned long nd_pfn_default_alignment(void)
  96. {
  97. if (has_transparent_hugepage())
  98. return HPAGE_PMD_SIZE;
  99. return PAGE_SIZE;
  100. }
  101. static ssize_t align_store(struct device *dev,
  102. struct device_attribute *attr, const char *buf, size_t len)
  103. {
  104. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  105. unsigned long aligns[MAX_NVDIMM_ALIGN] = { [0] = 0, };
  106. ssize_t rc;
  107. device_lock(dev);
  108. nvdimm_bus_lock(dev);
  109. rc = nd_size_select_store(dev, buf, &nd_pfn->align,
  110. nd_pfn_supported_alignments(aligns));
  111. dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
  112. buf[len - 1] == '\n' ? "" : "\n");
  113. nvdimm_bus_unlock(dev);
  114. device_unlock(dev);
  115. return rc ? rc : len;
  116. }
  117. static DEVICE_ATTR_RW(align);
  118. static ssize_t uuid_show(struct device *dev,
  119. struct device_attribute *attr, char *buf)
  120. {
  121. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  122. if (nd_pfn->uuid)
  123. return sprintf(buf, "%pUb\n", nd_pfn->uuid);
  124. return sprintf(buf, "\n");
  125. }
  126. static ssize_t uuid_store(struct device *dev,
  127. struct device_attribute *attr, const char *buf, size_t len)
  128. {
  129. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  130. ssize_t rc;
  131. device_lock(dev);
  132. rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len);
  133. dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
  134. buf[len - 1] == '\n' ? "" : "\n");
  135. device_unlock(dev);
  136. return rc ? rc : len;
  137. }
  138. static DEVICE_ATTR_RW(uuid);
  139. static ssize_t namespace_show(struct device *dev,
  140. struct device_attribute *attr, char *buf)
  141. {
  142. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  143. ssize_t rc;
  144. nvdimm_bus_lock(dev);
  145. rc = sprintf(buf, "%s\n", nd_pfn->ndns
  146. ? dev_name(&nd_pfn->ndns->dev) : "");
  147. nvdimm_bus_unlock(dev);
  148. return rc;
  149. }
  150. static ssize_t namespace_store(struct device *dev,
  151. struct device_attribute *attr, const char *buf, size_t len)
  152. {
  153. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  154. ssize_t rc;
  155. device_lock(dev);
  156. nvdimm_bus_lock(dev);
  157. rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len);
  158. dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
  159. buf[len - 1] == '\n' ? "" : "\n");
  160. nvdimm_bus_unlock(dev);
  161. device_unlock(dev);
  162. return rc;
  163. }
  164. static DEVICE_ATTR_RW(namespace);
  165. static ssize_t resource_show(struct device *dev,
  166. struct device_attribute *attr, char *buf)
  167. {
  168. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  169. ssize_t rc;
  170. device_lock(dev);
  171. if (dev->driver) {
  172. struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
  173. u64 offset = __le64_to_cpu(pfn_sb->dataoff);
  174. struct nd_namespace_common *ndns = nd_pfn->ndns;
  175. u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
  176. struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
  177. rc = sprintf(buf, "%#llx\n", (unsigned long long) nsio->res.start
  178. + start_pad + offset);
  179. } else {
  180. /* no address to convey if the pfn instance is disabled */
  181. rc = -ENXIO;
  182. }
  183. device_unlock(dev);
  184. return rc;
  185. }
  186. static DEVICE_ATTR_ADMIN_RO(resource);
  187. static ssize_t size_show(struct device *dev,
  188. struct device_attribute *attr, char *buf)
  189. {
  190. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  191. ssize_t rc;
  192. device_lock(dev);
  193. if (dev->driver) {
  194. struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
  195. u64 offset = __le64_to_cpu(pfn_sb->dataoff);
  196. struct nd_namespace_common *ndns = nd_pfn->ndns;
  197. u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
  198. u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
  199. struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
  200. rc = sprintf(buf, "%llu\n", (unsigned long long)
  201. resource_size(&nsio->res) - start_pad
  202. - end_trunc - offset);
  203. } else {
  204. /* no size to convey if the pfn instance is disabled */
  205. rc = -ENXIO;
  206. }
  207. device_unlock(dev);
  208. return rc;
  209. }
  210. static DEVICE_ATTR_RO(size);
  211. static ssize_t supported_alignments_show(struct device *dev,
  212. struct device_attribute *attr, char *buf)
  213. {
  214. unsigned long aligns[MAX_NVDIMM_ALIGN] = { [0] = 0, };
  215. return nd_size_select_show(0,
  216. nd_pfn_supported_alignments(aligns), buf);
  217. }
  218. static DEVICE_ATTR_RO(supported_alignments);
  219. static struct attribute *nd_pfn_attributes[] = {
  220. &dev_attr_mode.attr,
  221. &dev_attr_namespace.attr,
  222. &dev_attr_uuid.attr,
  223. &dev_attr_align.attr,
  224. &dev_attr_resource.attr,
  225. &dev_attr_size.attr,
  226. &dev_attr_supported_alignments.attr,
  227. NULL,
  228. };
  229. static struct attribute_group nd_pfn_attribute_group = {
  230. .attrs = nd_pfn_attributes,
  231. };
  232. const struct attribute_group *nd_pfn_attribute_groups[] = {
  233. &nd_pfn_attribute_group,
  234. &nd_device_attribute_group,
  235. &nd_numa_attribute_group,
  236. NULL,
  237. };
  238. static const struct device_type nd_pfn_device_type = {
  239. .name = "nd_pfn",
  240. .release = nd_pfn_release,
  241. .groups = nd_pfn_attribute_groups,
  242. };
  243. bool is_nd_pfn(struct device *dev)
  244. {
  245. return dev ? dev->type == &nd_pfn_device_type : false;
  246. }
  247. EXPORT_SYMBOL(is_nd_pfn);
  248. static struct lock_class_key nvdimm_pfn_key;
  249. struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn,
  250. struct nd_namespace_common *ndns)
  251. {
  252. struct device *dev;
  253. if (!nd_pfn)
  254. return NULL;
  255. nd_pfn->mode = PFN_MODE_NONE;
  256. nd_pfn->align = nd_pfn_default_alignment();
  257. dev = &nd_pfn->dev;
  258. device_initialize(&nd_pfn->dev);
  259. lockdep_set_class(&nd_pfn->dev.mutex, &nvdimm_pfn_key);
  260. if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
  261. dev_dbg(&ndns->dev, "failed, already claimed by %s\n",
  262. dev_name(ndns->claim));
  263. put_device(dev);
  264. return NULL;
  265. }
  266. return dev;
  267. }
  268. static struct nd_pfn *nd_pfn_alloc(struct nd_region *nd_region)
  269. {
  270. struct nd_pfn *nd_pfn;
  271. struct device *dev;
  272. nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL);
  273. if (!nd_pfn)
  274. return NULL;
  275. nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL);
  276. if (nd_pfn->id < 0) {
  277. kfree(nd_pfn);
  278. return NULL;
  279. }
  280. dev = &nd_pfn->dev;
  281. dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id);
  282. dev->type = &nd_pfn_device_type;
  283. dev->parent = &nd_region->dev;
  284. return nd_pfn;
  285. }
  286. struct device *nd_pfn_create(struct nd_region *nd_region)
  287. {
  288. struct nd_pfn *nd_pfn;
  289. struct device *dev;
  290. if (!is_memory(&nd_region->dev))
  291. return NULL;
  292. nd_pfn = nd_pfn_alloc(nd_region);
  293. dev = nd_pfn_devinit(nd_pfn, NULL);
  294. nd_device_register(dev);
  295. return dev;
  296. }
  297. /*
  298. * nd_pfn_clear_memmap_errors() clears any errors in the volatile memmap
  299. * space associated with the namespace. If the memmap is set to DRAM, then
  300. * this is a no-op. Since the memmap area is freshly initialized during
  301. * probe, we have an opportunity to clear any badblocks in this area.
  302. */
  303. static int nd_pfn_clear_memmap_errors(struct nd_pfn *nd_pfn)
  304. {
  305. struct nd_region *nd_region = to_nd_region(nd_pfn->dev.parent);
  306. struct nd_namespace_common *ndns = nd_pfn->ndns;
  307. void *zero_page = page_address(ZERO_PAGE(0));
  308. struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
  309. int num_bad, meta_num, rc, bb_present;
  310. sector_t first_bad, meta_start;
  311. struct nd_namespace_io *nsio;
  312. if (nd_pfn->mode != PFN_MODE_PMEM)
  313. return 0;
  314. nsio = to_nd_namespace_io(&ndns->dev);
  315. meta_start = (SZ_4K + sizeof(*pfn_sb)) >> 9;
  316. meta_num = (le64_to_cpu(pfn_sb->dataoff) >> 9) - meta_start;
  317. /*
  318. * re-enable the namespace with correct size so that we can access
  319. * the device memmap area.
  320. */
  321. devm_namespace_disable(&nd_pfn->dev, ndns);
  322. rc = devm_namespace_enable(&nd_pfn->dev, ndns, le64_to_cpu(pfn_sb->dataoff));
  323. if (rc)
  324. return rc;
  325. do {
  326. unsigned long zero_len;
  327. u64 nsoff;
  328. bb_present = badblocks_check(&nd_region->bb, meta_start,
  329. meta_num, &first_bad, &num_bad);
  330. if (bb_present) {
  331. dev_dbg(&nd_pfn->dev, "meta: %x badblocks at %llx\n",
  332. num_bad, first_bad);
  333. nsoff = ALIGN_DOWN((nd_region->ndr_start
  334. + (first_bad << 9)) - nsio->res.start,
  335. PAGE_SIZE);
  336. zero_len = ALIGN(num_bad << 9, PAGE_SIZE);
  337. while (zero_len) {
  338. unsigned long chunk = min(zero_len, PAGE_SIZE);
  339. rc = nvdimm_write_bytes(ndns, nsoff, zero_page,
  340. chunk, 0);
  341. if (rc)
  342. break;
  343. zero_len -= chunk;
  344. nsoff += chunk;
  345. }
  346. if (rc) {
  347. dev_err(&nd_pfn->dev,
  348. "error clearing %x badblocks at %llx\n",
  349. num_bad, first_bad);
  350. return rc;
  351. }
  352. }
  353. } while (bb_present);
  354. return 0;
  355. }
  356. static bool nd_supported_alignment(unsigned long align)
  357. {
  358. int i;
  359. unsigned long supported[MAX_NVDIMM_ALIGN] = { [0] = 0, };
  360. if (align == 0)
  361. return false;
  362. nd_pfn_supported_alignments(supported);
  363. for (i = 0; supported[i]; i++)
  364. if (align == supported[i])
  365. return true;
  366. return false;
  367. }
  368. /**
  369. * nd_pfn_validate - read and validate info-block
  370. * @nd_pfn: fsdax namespace runtime state / properties
  371. * @sig: 'devdax' or 'fsdax' signature
  372. *
  373. * Upon return the info-block buffer contents (->pfn_sb) are
  374. * indeterminate when validation fails, and a coherent info-block
  375. * otherwise.
  376. */
  377. int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
  378. {
  379. u64 checksum, offset;
  380. struct resource *res;
  381. enum nd_pfn_mode mode;
  382. struct nd_namespace_io *nsio;
  383. unsigned long align, start_pad;
  384. struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
  385. struct nd_namespace_common *ndns = nd_pfn->ndns;
  386. const uuid_t *parent_uuid = nd_dev_to_uuid(&ndns->dev);
  387. if (!pfn_sb || !ndns)
  388. return -ENODEV;
  389. if (!is_memory(nd_pfn->dev.parent))
  390. return -ENODEV;
  391. if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0))
  392. return -ENXIO;
  393. if (memcmp(pfn_sb->signature, sig, PFN_SIG_LEN) != 0)
  394. return -ENODEV;
  395. checksum = le64_to_cpu(pfn_sb->checksum);
  396. pfn_sb->checksum = 0;
  397. if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb))
  398. return -ENODEV;
  399. pfn_sb->checksum = cpu_to_le64(checksum);
  400. if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0)
  401. return -ENODEV;
  402. if (__le16_to_cpu(pfn_sb->version_minor) < 1) {
  403. pfn_sb->start_pad = 0;
  404. pfn_sb->end_trunc = 0;
  405. }
  406. if (__le16_to_cpu(pfn_sb->version_minor) < 2)
  407. pfn_sb->align = 0;
  408. if (__le16_to_cpu(pfn_sb->version_minor) < 4) {
  409. pfn_sb->page_struct_size = cpu_to_le16(64);
  410. pfn_sb->page_size = cpu_to_le32(PAGE_SIZE);
  411. }
  412. switch (le32_to_cpu(pfn_sb->mode)) {
  413. case PFN_MODE_RAM:
  414. case PFN_MODE_PMEM:
  415. break;
  416. default:
  417. return -ENXIO;
  418. }
  419. align = le32_to_cpu(pfn_sb->align);
  420. offset = le64_to_cpu(pfn_sb->dataoff);
  421. start_pad = le32_to_cpu(pfn_sb->start_pad);
  422. if (align == 0)
  423. align = 1UL << ilog2(offset);
  424. mode = le32_to_cpu(pfn_sb->mode);
  425. if ((le32_to_cpu(pfn_sb->page_size) > PAGE_SIZE) &&
  426. (mode == PFN_MODE_PMEM)) {
  427. dev_err(&nd_pfn->dev,
  428. "init failed, page size mismatch %d\n",
  429. le32_to_cpu(pfn_sb->page_size));
  430. return -EOPNOTSUPP;
  431. }
  432. if ((le16_to_cpu(pfn_sb->page_struct_size) < sizeof(struct page)) &&
  433. (mode == PFN_MODE_PMEM)) {
  434. dev_err(&nd_pfn->dev,
  435. "init failed, struct page size mismatch %d\n",
  436. le16_to_cpu(pfn_sb->page_struct_size));
  437. return -EOPNOTSUPP;
  438. }
  439. /*
  440. * Check whether the we support the alignment. For Dax if the
  441. * superblock alignment is not matching, we won't initialize
  442. * the device.
  443. */
  444. if (!nd_supported_alignment(align) &&
  445. !memcmp(pfn_sb->signature, DAX_SIG, PFN_SIG_LEN)) {
  446. dev_err(&nd_pfn->dev, "init failed, alignment mismatch: "
  447. "%ld:%ld\n", nd_pfn->align, align);
  448. return -EOPNOTSUPP;
  449. }
  450. if (!nd_pfn->uuid) {
  451. /*
  452. * When probing a namepace via nd_pfn_probe() the uuid
  453. * is NULL (see: nd_pfn_devinit()) we init settings from
  454. * pfn_sb
  455. */
  456. nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL);
  457. if (!nd_pfn->uuid)
  458. return -ENOMEM;
  459. nd_pfn->align = align;
  460. nd_pfn->mode = mode;
  461. } else {
  462. /*
  463. * When probing a pfn / dax instance we validate the
  464. * live settings against the pfn_sb
  465. */
  466. if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0)
  467. return -ENODEV;
  468. /*
  469. * If the uuid validates, but other settings mismatch
  470. * return EINVAL because userspace has managed to change
  471. * the configuration without specifying new
  472. * identification.
  473. */
  474. if (nd_pfn->align != align || nd_pfn->mode != mode) {
  475. dev_err(&nd_pfn->dev,
  476. "init failed, settings mismatch\n");
  477. dev_dbg(&nd_pfn->dev, "align: %lx:%lx mode: %d:%d\n",
  478. nd_pfn->align, align, nd_pfn->mode,
  479. mode);
  480. return -EOPNOTSUPP;
  481. }
  482. }
  483. if (align > nvdimm_namespace_capacity(ndns)) {
  484. dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n",
  485. align, nvdimm_namespace_capacity(ndns));
  486. return -EOPNOTSUPP;
  487. }
  488. /*
  489. * These warnings are verbose because they can only trigger in
  490. * the case where the physical address alignment of the
  491. * namespace has changed since the pfn superblock was
  492. * established.
  493. */
  494. nsio = to_nd_namespace_io(&ndns->dev);
  495. res = &nsio->res;
  496. if (offset >= resource_size(res)) {
  497. dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n",
  498. dev_name(&ndns->dev));
  499. return -EOPNOTSUPP;
  500. }
  501. if ((align && !IS_ALIGNED(res->start + offset + start_pad, align))
  502. || !IS_ALIGNED(offset, PAGE_SIZE)) {
  503. dev_err(&nd_pfn->dev,
  504. "bad offset: %#llx dax disabled align: %#lx\n",
  505. offset, align);
  506. return -EOPNOTSUPP;
  507. }
  508. if (!IS_ALIGNED(res->start + le32_to_cpu(pfn_sb->start_pad),
  509. memremap_compat_align())) {
  510. dev_err(&nd_pfn->dev, "resource start misaligned\n");
  511. return -EOPNOTSUPP;
  512. }
  513. if (!IS_ALIGNED(res->end + 1 - le32_to_cpu(pfn_sb->end_trunc),
  514. memremap_compat_align())) {
  515. dev_err(&nd_pfn->dev, "resource end misaligned\n");
  516. return -EOPNOTSUPP;
  517. }
  518. return 0;
  519. }
  520. EXPORT_SYMBOL(nd_pfn_validate);
  521. int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns)
  522. {
  523. int rc;
  524. struct nd_pfn *nd_pfn;
  525. struct device *pfn_dev;
  526. struct nd_pfn_sb *pfn_sb;
  527. struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
  528. if (ndns->force_raw)
  529. return -ENODEV;
  530. switch (ndns->claim_class) {
  531. case NVDIMM_CCLASS_NONE:
  532. case NVDIMM_CCLASS_PFN:
  533. break;
  534. default:
  535. return -ENODEV;
  536. }
  537. nvdimm_bus_lock(&ndns->dev);
  538. nd_pfn = nd_pfn_alloc(nd_region);
  539. pfn_dev = nd_pfn_devinit(nd_pfn, ndns);
  540. nvdimm_bus_unlock(&ndns->dev);
  541. if (!pfn_dev)
  542. return -ENOMEM;
  543. pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
  544. nd_pfn = to_nd_pfn(pfn_dev);
  545. nd_pfn->pfn_sb = pfn_sb;
  546. rc = nd_pfn_validate(nd_pfn, PFN_SIG);
  547. dev_dbg(dev, "pfn: %s\n", rc == 0 ? dev_name(pfn_dev) : "<none>");
  548. if (rc < 0) {
  549. nd_detach_ndns(pfn_dev, &nd_pfn->ndns);
  550. put_device(pfn_dev);
  551. } else
  552. nd_device_register(pfn_dev);
  553. return rc;
  554. }
  555. EXPORT_SYMBOL(nd_pfn_probe);
  556. /*
  557. * We hotplug memory at sub-section granularity, pad the reserved area
  558. * from the previous section base to the namespace base address.
  559. */
  560. static unsigned long init_altmap_base(resource_size_t base)
  561. {
  562. unsigned long base_pfn = PHYS_PFN(base);
  563. return SUBSECTION_ALIGN_DOWN(base_pfn);
  564. }
  565. static unsigned long init_altmap_reserve(resource_size_t base)
  566. {
  567. unsigned long reserve = nd_info_block_reserve() >> PAGE_SHIFT;
  568. unsigned long base_pfn = PHYS_PFN(base);
  569. reserve += base_pfn - SUBSECTION_ALIGN_DOWN(base_pfn);
  570. return reserve;
  571. }
  572. static int __nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap)
  573. {
  574. struct range *range = &pgmap->range;
  575. struct vmem_altmap *altmap = &pgmap->altmap;
  576. struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
  577. u64 offset = le64_to_cpu(pfn_sb->dataoff);
  578. u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
  579. u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
  580. u32 reserve = nd_info_block_reserve();
  581. struct nd_namespace_common *ndns = nd_pfn->ndns;
  582. struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
  583. resource_size_t base = nsio->res.start + start_pad;
  584. resource_size_t end = nsio->res.end - end_trunc;
  585. struct vmem_altmap __altmap = {
  586. .base_pfn = init_altmap_base(base),
  587. .reserve = init_altmap_reserve(base),
  588. .end_pfn = PHYS_PFN(end),
  589. };
  590. *range = (struct range) {
  591. .start = nsio->res.start + start_pad,
  592. .end = nsio->res.end - end_trunc,
  593. };
  594. pgmap->nr_range = 1;
  595. if (nd_pfn->mode == PFN_MODE_RAM) {
  596. if (offset < reserve)
  597. return -EINVAL;
  598. nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns);
  599. } else if (nd_pfn->mode == PFN_MODE_PMEM) {
  600. nd_pfn->npfns = PHYS_PFN((range_len(range) - offset));
  601. if (le64_to_cpu(nd_pfn->pfn_sb->npfns) > nd_pfn->npfns)
  602. dev_info(&nd_pfn->dev,
  603. "number of pfns truncated from %lld to %ld\n",
  604. le64_to_cpu(nd_pfn->pfn_sb->npfns),
  605. nd_pfn->npfns);
  606. memcpy(altmap, &__altmap, sizeof(*altmap));
  607. altmap->free = PHYS_PFN(offset - reserve);
  608. altmap->alloc = 0;
  609. pgmap->flags |= PGMAP_ALTMAP_VALID;
  610. } else
  611. return -ENXIO;
  612. return 0;
  613. }
  614. static int nd_pfn_init(struct nd_pfn *nd_pfn)
  615. {
  616. struct nd_namespace_common *ndns = nd_pfn->ndns;
  617. struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
  618. resource_size_t start, size;
  619. struct nd_region *nd_region;
  620. unsigned long npfns, align;
  621. u32 end_trunc;
  622. struct nd_pfn_sb *pfn_sb;
  623. phys_addr_t offset;
  624. const char *sig;
  625. u64 checksum;
  626. int rc;
  627. pfn_sb = devm_kmalloc(&nd_pfn->dev, sizeof(*pfn_sb), GFP_KERNEL);
  628. if (!pfn_sb)
  629. return -ENOMEM;
  630. nd_pfn->pfn_sb = pfn_sb;
  631. if (is_nd_dax(&nd_pfn->dev))
  632. sig = DAX_SIG;
  633. else
  634. sig = PFN_SIG;
  635. rc = nd_pfn_validate(nd_pfn, sig);
  636. if (rc == 0)
  637. return nd_pfn_clear_memmap_errors(nd_pfn);
  638. if (rc != -ENODEV)
  639. return rc;
  640. /* no info block, do init */;
  641. memset(pfn_sb, 0, sizeof(*pfn_sb));
  642. nd_region = to_nd_region(nd_pfn->dev.parent);
  643. if (nd_region->ro) {
  644. dev_info(&nd_pfn->dev,
  645. "%s is read-only, unable to init metadata\n",
  646. dev_name(&nd_region->dev));
  647. return -ENXIO;
  648. }
  649. start = nsio->res.start;
  650. size = resource_size(&nsio->res);
  651. npfns = PHYS_PFN(size - SZ_8K);
  652. align = max(nd_pfn->align, memremap_compat_align());
  653. /*
  654. * When @start is misaligned fail namespace creation. See
  655. * the 'struct nd_pfn_sb' commentary on why ->start_pad is not
  656. * an option.
  657. */
  658. if (!IS_ALIGNED(start, memremap_compat_align())) {
  659. dev_err(&nd_pfn->dev, "%s: start %pa misaligned to %#lx\n",
  660. dev_name(&ndns->dev), &start,
  661. memremap_compat_align());
  662. return -EINVAL;
  663. }
  664. end_trunc = start + size - ALIGN_DOWN(start + size, align);
  665. if (nd_pfn->mode == PFN_MODE_PMEM) {
  666. unsigned long page_map_size = MAX_STRUCT_PAGE_SIZE * npfns;
  667. /*
  668. * The altmap should be padded out to the block size used
  669. * when populating the vmemmap. This *should* be equal to
  670. * PMD_SIZE for most architectures.
  671. *
  672. * Also make sure size of struct page is less than
  673. * MAX_STRUCT_PAGE_SIZE. The goal here is compatibility in the
  674. * face of production kernel configurations that reduce the
  675. * 'struct page' size below MAX_STRUCT_PAGE_SIZE. For debug
  676. * kernel configurations that increase the 'struct page' size
  677. * above MAX_STRUCT_PAGE_SIZE, the page_struct_override allows
  678. * for continuing with the capacity that will be wasted when
  679. * reverting to a production kernel configuration. Otherwise,
  680. * those configurations are blocked by default.
  681. */
  682. if (sizeof(struct page) > MAX_STRUCT_PAGE_SIZE) {
  683. if (page_struct_override)
  684. page_map_size = sizeof(struct page) * npfns;
  685. else {
  686. dev_err(&nd_pfn->dev,
  687. "Memory debug options prevent using pmem for the page map\n");
  688. return -EINVAL;
  689. }
  690. }
  691. offset = ALIGN(start + SZ_8K + page_map_size, align) - start;
  692. } else if (nd_pfn->mode == PFN_MODE_RAM)
  693. offset = ALIGN(start + SZ_8K, align) - start;
  694. else
  695. return -ENXIO;
  696. if (offset >= size) {
  697. dev_err(&nd_pfn->dev, "%s unable to satisfy requested alignment\n",
  698. dev_name(&ndns->dev));
  699. return -ENXIO;
  700. }
  701. npfns = PHYS_PFN(size - offset - end_trunc);
  702. pfn_sb->mode = cpu_to_le32(nd_pfn->mode);
  703. pfn_sb->dataoff = cpu_to_le64(offset);
  704. pfn_sb->npfns = cpu_to_le64(npfns);
  705. memcpy(pfn_sb->signature, sig, PFN_SIG_LEN);
  706. memcpy(pfn_sb->uuid, nd_pfn->uuid, 16);
  707. memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16);
  708. pfn_sb->version_major = cpu_to_le16(1);
  709. pfn_sb->version_minor = cpu_to_le16(4);
  710. pfn_sb->end_trunc = cpu_to_le32(end_trunc);
  711. pfn_sb->align = cpu_to_le32(nd_pfn->align);
  712. if (sizeof(struct page) > MAX_STRUCT_PAGE_SIZE && page_struct_override)
  713. pfn_sb->page_struct_size = cpu_to_le16(sizeof(struct page));
  714. else
  715. pfn_sb->page_struct_size = cpu_to_le16(MAX_STRUCT_PAGE_SIZE);
  716. pfn_sb->page_size = cpu_to_le32(PAGE_SIZE);
  717. checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb);
  718. pfn_sb->checksum = cpu_to_le64(checksum);
  719. rc = nd_pfn_clear_memmap_errors(nd_pfn);
  720. if (rc)
  721. return rc;
  722. return nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0);
  723. }
  724. /*
  725. * Determine the effective resource range and vmem_altmap from an nd_pfn
  726. * instance.
  727. */
  728. int nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap)
  729. {
  730. int rc;
  731. if (!nd_pfn->uuid || !nd_pfn->ndns)
  732. return -ENODEV;
  733. rc = nd_pfn_init(nd_pfn);
  734. if (rc)
  735. return rc;
  736. /* we need a valid pfn_sb before we can init a dev_pagemap */
  737. return __nvdimm_setup_pfn(nd_pfn, pgmap);
  738. }
  739. EXPORT_SYMBOL_GPL(nvdimm_setup_pfn);