pfn_devs.c 22 KB

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