region_devs.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
  4. */
  5. #include <linux/scatterlist.h>
  6. #include <linux/memregion.h>
  7. #include <linux/highmem.h>
  8. #include <linux/kstrtox.h>
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/hash.h>
  12. #include <linux/sort.h>
  13. #include <linux/io.h>
  14. #include <linux/nd.h>
  15. #include "nd-core.h"
  16. #include "nd.h"
  17. /*
  18. * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
  19. * irrelevant.
  20. */
  21. #include <linux/io-64-nonatomic-hi-lo.h>
  22. static DEFINE_PER_CPU(int, flush_idx);
  23. static int nvdimm_map_flush(struct device *dev, struct nvdimm *nvdimm, int dimm,
  24. struct nd_region_data *ndrd)
  25. {
  26. int i, j;
  27. dev_dbg(dev, "%s: map %d flush address%s\n", nvdimm_name(nvdimm),
  28. nvdimm->num_flush, nvdimm->num_flush == 1 ? "" : "es");
  29. for (i = 0; i < (1 << ndrd->hints_shift); i++) {
  30. struct resource *res = &nvdimm->flush_wpq[i];
  31. unsigned long pfn = PHYS_PFN(res->start);
  32. void __iomem *flush_page;
  33. /* check if flush hints share a page */
  34. for (j = 0; j < i; j++) {
  35. struct resource *res_j = &nvdimm->flush_wpq[j];
  36. unsigned long pfn_j = PHYS_PFN(res_j->start);
  37. if (pfn == pfn_j)
  38. break;
  39. }
  40. if (j < i)
  41. flush_page = (void __iomem *) ((unsigned long)
  42. ndrd_get_flush_wpq(ndrd, dimm, j)
  43. & PAGE_MASK);
  44. else
  45. flush_page = devm_nvdimm_ioremap(dev,
  46. PFN_PHYS(pfn), PAGE_SIZE);
  47. if (!flush_page)
  48. return -ENXIO;
  49. ndrd_set_flush_wpq(ndrd, dimm, i, flush_page
  50. + (res->start & ~PAGE_MASK));
  51. }
  52. return 0;
  53. }
  54. static int nd_region_invalidate_memregion(struct nd_region *nd_region)
  55. {
  56. int i, incoherent = 0;
  57. for (i = 0; i < nd_region->ndr_mappings; i++) {
  58. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  59. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  60. if (test_bit(NDD_INCOHERENT, &nvdimm->flags)) {
  61. incoherent++;
  62. break;
  63. }
  64. }
  65. if (!incoherent)
  66. return 0;
  67. if (!cpu_cache_has_invalidate_memregion()) {
  68. if (IS_ENABLED(CONFIG_NVDIMM_SECURITY_TEST)) {
  69. dev_warn(
  70. &nd_region->dev,
  71. "Bypassing cpu_cache_invalidate_memergion() for testing!\n");
  72. goto out;
  73. } else {
  74. dev_err(&nd_region->dev,
  75. "Failed to synchronize CPU cache state\n");
  76. return -ENXIO;
  77. }
  78. }
  79. cpu_cache_invalidate_memregion(IORES_DESC_PERSISTENT_MEMORY);
  80. out:
  81. for (i = 0; i < nd_region->ndr_mappings; i++) {
  82. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  83. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  84. clear_bit(NDD_INCOHERENT, &nvdimm->flags);
  85. }
  86. return 0;
  87. }
  88. int nd_region_activate(struct nd_region *nd_region)
  89. {
  90. int i, j, rc, num_flush = 0;
  91. struct nd_region_data *ndrd;
  92. struct device *dev = &nd_region->dev;
  93. size_t flush_data_size = sizeof(void *);
  94. nvdimm_bus_lock(&nd_region->dev);
  95. for (i = 0; i < nd_region->ndr_mappings; i++) {
  96. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  97. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  98. if (test_bit(NDD_SECURITY_OVERWRITE, &nvdimm->flags)) {
  99. nvdimm_bus_unlock(&nd_region->dev);
  100. return -EBUSY;
  101. }
  102. /* at least one null hint slot per-dimm for the "no-hint" case */
  103. flush_data_size += sizeof(void *);
  104. num_flush = min_not_zero(num_flush, nvdimm->num_flush);
  105. if (!nvdimm->num_flush)
  106. continue;
  107. flush_data_size += nvdimm->num_flush * sizeof(void *);
  108. }
  109. nvdimm_bus_unlock(&nd_region->dev);
  110. rc = nd_region_invalidate_memregion(nd_region);
  111. if (rc)
  112. return rc;
  113. ndrd = devm_kzalloc(dev, sizeof(*ndrd) + flush_data_size, GFP_KERNEL);
  114. if (!ndrd)
  115. return -ENOMEM;
  116. dev_set_drvdata(dev, ndrd);
  117. if (!num_flush)
  118. return 0;
  119. ndrd->hints_shift = ilog2(num_flush);
  120. for (i = 0; i < nd_region->ndr_mappings; i++) {
  121. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  122. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  123. int rc = nvdimm_map_flush(&nd_region->dev, nvdimm, i, ndrd);
  124. if (rc)
  125. return rc;
  126. }
  127. /*
  128. * Clear out entries that are duplicates. This should prevent the
  129. * extra flushings.
  130. */
  131. for (i = 0; i < nd_region->ndr_mappings - 1; i++) {
  132. /* ignore if NULL already */
  133. if (!ndrd_get_flush_wpq(ndrd, i, 0))
  134. continue;
  135. for (j = i + 1; j < nd_region->ndr_mappings; j++)
  136. if (ndrd_get_flush_wpq(ndrd, i, 0) ==
  137. ndrd_get_flush_wpq(ndrd, j, 0))
  138. ndrd_set_flush_wpq(ndrd, j, 0, NULL);
  139. }
  140. return 0;
  141. }
  142. static void nd_region_release(struct device *dev)
  143. {
  144. struct nd_region *nd_region = to_nd_region(dev);
  145. u16 i;
  146. for (i = 0; i < nd_region->ndr_mappings; i++) {
  147. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  148. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  149. put_device(&nvdimm->dev);
  150. }
  151. free_percpu(nd_region->lane);
  152. if (!test_bit(ND_REGION_CXL, &nd_region->flags))
  153. memregion_free(nd_region->id);
  154. kfree(nd_region);
  155. }
  156. struct nd_region *to_nd_region(struct device *dev)
  157. {
  158. struct nd_region *nd_region = container_of(dev, struct nd_region, dev);
  159. WARN_ON(dev->type->release != nd_region_release);
  160. return nd_region;
  161. }
  162. EXPORT_SYMBOL_GPL(to_nd_region);
  163. struct device *nd_region_dev(struct nd_region *nd_region)
  164. {
  165. if (!nd_region)
  166. return NULL;
  167. return &nd_region->dev;
  168. }
  169. EXPORT_SYMBOL_GPL(nd_region_dev);
  170. void *nd_region_provider_data(struct nd_region *nd_region)
  171. {
  172. return nd_region->provider_data;
  173. }
  174. EXPORT_SYMBOL_GPL(nd_region_provider_data);
  175. /**
  176. * nd_region_to_nstype() - region to an integer namespace type
  177. * @nd_region: region-device to interrogate
  178. *
  179. * This is the 'nstype' attribute of a region as well, an input to the
  180. * MODALIAS for namespace devices, and bit number for a nvdimm_bus to match
  181. * namespace devices with namespace drivers.
  182. */
  183. int nd_region_to_nstype(struct nd_region *nd_region)
  184. {
  185. if (is_memory(&nd_region->dev)) {
  186. u16 i, label;
  187. for (i = 0, label = 0; i < nd_region->ndr_mappings; i++) {
  188. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  189. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  190. if (test_bit(NDD_LABELING, &nvdimm->flags))
  191. label++;
  192. }
  193. if (label)
  194. return ND_DEVICE_NAMESPACE_PMEM;
  195. else
  196. return ND_DEVICE_NAMESPACE_IO;
  197. }
  198. return 0;
  199. }
  200. EXPORT_SYMBOL(nd_region_to_nstype);
  201. static unsigned long long region_size(struct nd_region *nd_region)
  202. {
  203. if (is_memory(&nd_region->dev)) {
  204. return nd_region->ndr_size;
  205. } else if (nd_region->ndr_mappings == 1) {
  206. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  207. return nd_mapping->size;
  208. }
  209. return 0;
  210. }
  211. static ssize_t size_show(struct device *dev,
  212. struct device_attribute *attr, char *buf)
  213. {
  214. struct nd_region *nd_region = to_nd_region(dev);
  215. return sprintf(buf, "%llu\n", region_size(nd_region));
  216. }
  217. static DEVICE_ATTR_RO(size);
  218. static ssize_t deep_flush_show(struct device *dev,
  219. struct device_attribute *attr, char *buf)
  220. {
  221. struct nd_region *nd_region = to_nd_region(dev);
  222. /*
  223. * NOTE: in the nvdimm_has_flush() error case this attribute is
  224. * not visible.
  225. */
  226. return sprintf(buf, "%d\n", nvdimm_has_flush(nd_region));
  227. }
  228. static ssize_t deep_flush_store(struct device *dev, struct device_attribute *attr,
  229. const char *buf, size_t len)
  230. {
  231. bool flush;
  232. int rc = kstrtobool(buf, &flush);
  233. struct nd_region *nd_region = to_nd_region(dev);
  234. if (rc)
  235. return rc;
  236. if (!flush)
  237. return -EINVAL;
  238. rc = nvdimm_flush(nd_region, NULL);
  239. if (rc)
  240. return rc;
  241. return len;
  242. }
  243. static DEVICE_ATTR_RW(deep_flush);
  244. static ssize_t mappings_show(struct device *dev,
  245. struct device_attribute *attr, char *buf)
  246. {
  247. struct nd_region *nd_region = to_nd_region(dev);
  248. return sprintf(buf, "%d\n", nd_region->ndr_mappings);
  249. }
  250. static DEVICE_ATTR_RO(mappings);
  251. static ssize_t nstype_show(struct device *dev,
  252. struct device_attribute *attr, char *buf)
  253. {
  254. struct nd_region *nd_region = to_nd_region(dev);
  255. return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
  256. }
  257. static DEVICE_ATTR_RO(nstype);
  258. static ssize_t set_cookie_show(struct device *dev,
  259. struct device_attribute *attr, char *buf)
  260. {
  261. struct nd_region *nd_region = to_nd_region(dev);
  262. struct nd_interleave_set *nd_set = nd_region->nd_set;
  263. ssize_t rc = 0;
  264. if (is_memory(dev) && nd_set)
  265. /* pass, should be precluded by region_visible */;
  266. else
  267. return -ENXIO;
  268. /*
  269. * The cookie to show depends on which specification of the
  270. * labels we are using. If there are not labels then default to
  271. * the v1.1 namespace label cookie definition. To read all this
  272. * data we need to wait for probing to settle.
  273. */
  274. device_lock(dev);
  275. nvdimm_bus_lock(dev);
  276. wait_nvdimm_bus_probe_idle(dev);
  277. if (nd_region->ndr_mappings) {
  278. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  279. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  280. if (ndd) {
  281. struct nd_namespace_index *nsindex;
  282. nsindex = to_namespace_index(ndd, ndd->ns_current);
  283. rc = sprintf(buf, "%#llx\n",
  284. nd_region_interleave_set_cookie(nd_region,
  285. nsindex));
  286. }
  287. }
  288. nvdimm_bus_unlock(dev);
  289. device_unlock(dev);
  290. if (rc)
  291. return rc;
  292. return sprintf(buf, "%#llx\n", nd_set->cookie1);
  293. }
  294. static DEVICE_ATTR_RO(set_cookie);
  295. resource_size_t nd_region_available_dpa(struct nd_region *nd_region)
  296. {
  297. resource_size_t available;
  298. int i;
  299. WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
  300. available = 0;
  301. for (i = 0; i < nd_region->ndr_mappings; i++) {
  302. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  303. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  304. /* if a dimm is disabled the available capacity is zero */
  305. if (!ndd)
  306. return 0;
  307. available += nd_pmem_available_dpa(nd_region, nd_mapping);
  308. }
  309. return available;
  310. }
  311. resource_size_t nd_region_allocatable_dpa(struct nd_region *nd_region)
  312. {
  313. resource_size_t avail = 0;
  314. int i;
  315. WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
  316. for (i = 0; i < nd_region->ndr_mappings; i++) {
  317. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  318. avail = min_not_zero(avail, nd_pmem_max_contiguous_dpa(
  319. nd_region, nd_mapping));
  320. }
  321. return avail * nd_region->ndr_mappings;
  322. }
  323. static ssize_t available_size_show(struct device *dev,
  324. struct device_attribute *attr, char *buf)
  325. {
  326. struct nd_region *nd_region = to_nd_region(dev);
  327. unsigned long long available = 0;
  328. /*
  329. * Flush in-flight updates and grab a snapshot of the available
  330. * size. Of course, this value is potentially invalidated the
  331. * memory nvdimm_bus_lock() is dropped, but that's userspace's
  332. * problem to not race itself.
  333. */
  334. device_lock(dev);
  335. nvdimm_bus_lock(dev);
  336. wait_nvdimm_bus_probe_idle(dev);
  337. available = nd_region_available_dpa(nd_region);
  338. nvdimm_bus_unlock(dev);
  339. device_unlock(dev);
  340. return sprintf(buf, "%llu\n", available);
  341. }
  342. static DEVICE_ATTR_RO(available_size);
  343. static ssize_t max_available_extent_show(struct device *dev,
  344. struct device_attribute *attr, char *buf)
  345. {
  346. struct nd_region *nd_region = to_nd_region(dev);
  347. unsigned long long available = 0;
  348. device_lock(dev);
  349. nvdimm_bus_lock(dev);
  350. wait_nvdimm_bus_probe_idle(dev);
  351. available = nd_region_allocatable_dpa(nd_region);
  352. nvdimm_bus_unlock(dev);
  353. device_unlock(dev);
  354. return sprintf(buf, "%llu\n", available);
  355. }
  356. static DEVICE_ATTR_RO(max_available_extent);
  357. static ssize_t init_namespaces_show(struct device *dev,
  358. struct device_attribute *attr, char *buf)
  359. {
  360. struct nd_region_data *ndrd = dev_get_drvdata(dev);
  361. ssize_t rc;
  362. nvdimm_bus_lock(dev);
  363. if (ndrd)
  364. rc = sprintf(buf, "%d/%d\n", ndrd->ns_active, ndrd->ns_count);
  365. else
  366. rc = -ENXIO;
  367. nvdimm_bus_unlock(dev);
  368. return rc;
  369. }
  370. static DEVICE_ATTR_RO(init_namespaces);
  371. static ssize_t namespace_seed_show(struct device *dev,
  372. struct device_attribute *attr, char *buf)
  373. {
  374. struct nd_region *nd_region = to_nd_region(dev);
  375. ssize_t rc;
  376. nvdimm_bus_lock(dev);
  377. if (nd_region->ns_seed)
  378. rc = sprintf(buf, "%s\n", dev_name(nd_region->ns_seed));
  379. else
  380. rc = sprintf(buf, "\n");
  381. nvdimm_bus_unlock(dev);
  382. return rc;
  383. }
  384. static DEVICE_ATTR_RO(namespace_seed);
  385. static ssize_t btt_seed_show(struct device *dev,
  386. struct device_attribute *attr, char *buf)
  387. {
  388. struct nd_region *nd_region = to_nd_region(dev);
  389. ssize_t rc;
  390. nvdimm_bus_lock(dev);
  391. if (nd_region->btt_seed)
  392. rc = sprintf(buf, "%s\n", dev_name(nd_region->btt_seed));
  393. else
  394. rc = sprintf(buf, "\n");
  395. nvdimm_bus_unlock(dev);
  396. return rc;
  397. }
  398. static DEVICE_ATTR_RO(btt_seed);
  399. static ssize_t pfn_seed_show(struct device *dev,
  400. struct device_attribute *attr, char *buf)
  401. {
  402. struct nd_region *nd_region = to_nd_region(dev);
  403. ssize_t rc;
  404. nvdimm_bus_lock(dev);
  405. if (nd_region->pfn_seed)
  406. rc = sprintf(buf, "%s\n", dev_name(nd_region->pfn_seed));
  407. else
  408. rc = sprintf(buf, "\n");
  409. nvdimm_bus_unlock(dev);
  410. return rc;
  411. }
  412. static DEVICE_ATTR_RO(pfn_seed);
  413. static ssize_t dax_seed_show(struct device *dev,
  414. struct device_attribute *attr, char *buf)
  415. {
  416. struct nd_region *nd_region = to_nd_region(dev);
  417. ssize_t rc;
  418. nvdimm_bus_lock(dev);
  419. if (nd_region->dax_seed)
  420. rc = sprintf(buf, "%s\n", dev_name(nd_region->dax_seed));
  421. else
  422. rc = sprintf(buf, "\n");
  423. nvdimm_bus_unlock(dev);
  424. return rc;
  425. }
  426. static DEVICE_ATTR_RO(dax_seed);
  427. static ssize_t read_only_show(struct device *dev,
  428. struct device_attribute *attr, char *buf)
  429. {
  430. struct nd_region *nd_region = to_nd_region(dev);
  431. return sprintf(buf, "%d\n", nd_region->ro);
  432. }
  433. static int revalidate_read_only(struct device *dev, void *data)
  434. {
  435. nd_device_notify(dev, NVDIMM_REVALIDATE_REGION);
  436. return 0;
  437. }
  438. static ssize_t read_only_store(struct device *dev,
  439. struct device_attribute *attr, const char *buf, size_t len)
  440. {
  441. bool ro;
  442. int rc = kstrtobool(buf, &ro);
  443. struct nd_region *nd_region = to_nd_region(dev);
  444. if (rc)
  445. return rc;
  446. nd_region->ro = ro;
  447. device_for_each_child(dev, NULL, revalidate_read_only);
  448. return len;
  449. }
  450. static DEVICE_ATTR_RW(read_only);
  451. static ssize_t align_show(struct device *dev,
  452. struct device_attribute *attr, char *buf)
  453. {
  454. struct nd_region *nd_region = to_nd_region(dev);
  455. return sprintf(buf, "%#lx\n", nd_region->align);
  456. }
  457. static ssize_t align_store(struct device *dev,
  458. struct device_attribute *attr, const char *buf, size_t len)
  459. {
  460. struct nd_region *nd_region = to_nd_region(dev);
  461. unsigned long val, dpa;
  462. u32 mappings, remainder;
  463. int rc;
  464. rc = kstrtoul(buf, 0, &val);
  465. if (rc)
  466. return rc;
  467. /*
  468. * Ensure space-align is evenly divisible by the region
  469. * interleave-width because the kernel typically has no facility
  470. * to determine which DIMM(s), dimm-physical-addresses, would
  471. * contribute to the tail capacity in system-physical-address
  472. * space for the namespace.
  473. */
  474. mappings = max_t(u32, 1, nd_region->ndr_mappings);
  475. dpa = div_u64_rem(val, mappings, &remainder);
  476. if (!is_power_of_2(dpa) || dpa < PAGE_SIZE
  477. || val > region_size(nd_region) || remainder)
  478. return -EINVAL;
  479. /*
  480. * Given that space allocation consults this value multiple
  481. * times ensure it does not change for the duration of the
  482. * allocation.
  483. */
  484. nvdimm_bus_lock(dev);
  485. nd_region->align = val;
  486. nvdimm_bus_unlock(dev);
  487. return len;
  488. }
  489. static DEVICE_ATTR_RW(align);
  490. static ssize_t region_badblocks_show(struct device *dev,
  491. struct device_attribute *attr, char *buf)
  492. {
  493. struct nd_region *nd_region = to_nd_region(dev);
  494. ssize_t rc;
  495. device_lock(dev);
  496. if (dev->driver)
  497. rc = badblocks_show(&nd_region->bb, buf, 0);
  498. else
  499. rc = -ENXIO;
  500. device_unlock(dev);
  501. return rc;
  502. }
  503. static DEVICE_ATTR(badblocks, 0444, region_badblocks_show, NULL);
  504. static ssize_t resource_show(struct device *dev,
  505. struct device_attribute *attr, char *buf)
  506. {
  507. struct nd_region *nd_region = to_nd_region(dev);
  508. return sprintf(buf, "%#llx\n", nd_region->ndr_start);
  509. }
  510. static DEVICE_ATTR_ADMIN_RO(resource);
  511. static ssize_t persistence_domain_show(struct device *dev,
  512. struct device_attribute *attr, char *buf)
  513. {
  514. struct nd_region *nd_region = to_nd_region(dev);
  515. if (test_bit(ND_REGION_PERSIST_CACHE, &nd_region->flags))
  516. return sprintf(buf, "cpu_cache\n");
  517. else if (test_bit(ND_REGION_PERSIST_MEMCTRL, &nd_region->flags))
  518. return sprintf(buf, "memory_controller\n");
  519. else
  520. return sprintf(buf, "\n");
  521. }
  522. static DEVICE_ATTR_RO(persistence_domain);
  523. static struct attribute *nd_region_attributes[] = {
  524. &dev_attr_size.attr,
  525. &dev_attr_align.attr,
  526. &dev_attr_nstype.attr,
  527. &dev_attr_mappings.attr,
  528. &dev_attr_btt_seed.attr,
  529. &dev_attr_pfn_seed.attr,
  530. &dev_attr_dax_seed.attr,
  531. &dev_attr_deep_flush.attr,
  532. &dev_attr_read_only.attr,
  533. &dev_attr_set_cookie.attr,
  534. &dev_attr_available_size.attr,
  535. &dev_attr_max_available_extent.attr,
  536. &dev_attr_namespace_seed.attr,
  537. &dev_attr_init_namespaces.attr,
  538. &dev_attr_badblocks.attr,
  539. &dev_attr_resource.attr,
  540. &dev_attr_persistence_domain.attr,
  541. NULL,
  542. };
  543. static umode_t region_visible(struct kobject *kobj, struct attribute *a, int n)
  544. {
  545. struct device *dev = container_of(kobj, typeof(*dev), kobj);
  546. struct nd_region *nd_region = to_nd_region(dev);
  547. struct nd_interleave_set *nd_set = nd_region->nd_set;
  548. int type = nd_region_to_nstype(nd_region);
  549. if (!is_memory(dev) && a == &dev_attr_pfn_seed.attr)
  550. return 0;
  551. if (!is_memory(dev) && a == &dev_attr_dax_seed.attr)
  552. return 0;
  553. if (!is_memory(dev) && a == &dev_attr_badblocks.attr)
  554. return 0;
  555. if (a == &dev_attr_resource.attr && !is_memory(dev))
  556. return 0;
  557. if (a == &dev_attr_deep_flush.attr) {
  558. int has_flush = nvdimm_has_flush(nd_region);
  559. if (has_flush == 1)
  560. return a->mode;
  561. else if (has_flush == 0)
  562. return 0444;
  563. else
  564. return 0;
  565. }
  566. if (a == &dev_attr_persistence_domain.attr) {
  567. if ((nd_region->flags & (BIT(ND_REGION_PERSIST_CACHE)
  568. | BIT(ND_REGION_PERSIST_MEMCTRL))) == 0)
  569. return 0;
  570. return a->mode;
  571. }
  572. if (a == &dev_attr_align.attr)
  573. return a->mode;
  574. if (a != &dev_attr_set_cookie.attr
  575. && a != &dev_attr_available_size.attr)
  576. return a->mode;
  577. if (type == ND_DEVICE_NAMESPACE_PMEM &&
  578. a == &dev_attr_available_size.attr)
  579. return a->mode;
  580. else if (is_memory(dev) && nd_set)
  581. return a->mode;
  582. return 0;
  583. }
  584. static ssize_t mappingN(struct device *dev, char *buf, int n)
  585. {
  586. struct nd_region *nd_region = to_nd_region(dev);
  587. struct nd_mapping *nd_mapping;
  588. struct nvdimm *nvdimm;
  589. if (n >= nd_region->ndr_mappings)
  590. return -ENXIO;
  591. nd_mapping = &nd_region->mapping[n];
  592. nvdimm = nd_mapping->nvdimm;
  593. return sprintf(buf, "%s,%llu,%llu,%d\n", dev_name(&nvdimm->dev),
  594. nd_mapping->start, nd_mapping->size,
  595. nd_mapping->position);
  596. }
  597. #define REGION_MAPPING(idx) \
  598. static ssize_t mapping##idx##_show(struct device *dev, \
  599. struct device_attribute *attr, char *buf) \
  600. { \
  601. return mappingN(dev, buf, idx); \
  602. } \
  603. static DEVICE_ATTR_RO(mapping##idx)
  604. /*
  605. * 32 should be enough for a while, even in the presence of socket
  606. * interleave a 32-way interleave set is a degenerate case.
  607. */
  608. REGION_MAPPING(0);
  609. REGION_MAPPING(1);
  610. REGION_MAPPING(2);
  611. REGION_MAPPING(3);
  612. REGION_MAPPING(4);
  613. REGION_MAPPING(5);
  614. REGION_MAPPING(6);
  615. REGION_MAPPING(7);
  616. REGION_MAPPING(8);
  617. REGION_MAPPING(9);
  618. REGION_MAPPING(10);
  619. REGION_MAPPING(11);
  620. REGION_MAPPING(12);
  621. REGION_MAPPING(13);
  622. REGION_MAPPING(14);
  623. REGION_MAPPING(15);
  624. REGION_MAPPING(16);
  625. REGION_MAPPING(17);
  626. REGION_MAPPING(18);
  627. REGION_MAPPING(19);
  628. REGION_MAPPING(20);
  629. REGION_MAPPING(21);
  630. REGION_MAPPING(22);
  631. REGION_MAPPING(23);
  632. REGION_MAPPING(24);
  633. REGION_MAPPING(25);
  634. REGION_MAPPING(26);
  635. REGION_MAPPING(27);
  636. REGION_MAPPING(28);
  637. REGION_MAPPING(29);
  638. REGION_MAPPING(30);
  639. REGION_MAPPING(31);
  640. static umode_t mapping_visible(struct kobject *kobj, struct attribute *a, int n)
  641. {
  642. struct device *dev = container_of(kobj, struct device, kobj);
  643. struct nd_region *nd_region = to_nd_region(dev);
  644. if (n < nd_region->ndr_mappings)
  645. return a->mode;
  646. return 0;
  647. }
  648. static struct attribute *mapping_attributes[] = {
  649. &dev_attr_mapping0.attr,
  650. &dev_attr_mapping1.attr,
  651. &dev_attr_mapping2.attr,
  652. &dev_attr_mapping3.attr,
  653. &dev_attr_mapping4.attr,
  654. &dev_attr_mapping5.attr,
  655. &dev_attr_mapping6.attr,
  656. &dev_attr_mapping7.attr,
  657. &dev_attr_mapping8.attr,
  658. &dev_attr_mapping9.attr,
  659. &dev_attr_mapping10.attr,
  660. &dev_attr_mapping11.attr,
  661. &dev_attr_mapping12.attr,
  662. &dev_attr_mapping13.attr,
  663. &dev_attr_mapping14.attr,
  664. &dev_attr_mapping15.attr,
  665. &dev_attr_mapping16.attr,
  666. &dev_attr_mapping17.attr,
  667. &dev_attr_mapping18.attr,
  668. &dev_attr_mapping19.attr,
  669. &dev_attr_mapping20.attr,
  670. &dev_attr_mapping21.attr,
  671. &dev_attr_mapping22.attr,
  672. &dev_attr_mapping23.attr,
  673. &dev_attr_mapping24.attr,
  674. &dev_attr_mapping25.attr,
  675. &dev_attr_mapping26.attr,
  676. &dev_attr_mapping27.attr,
  677. &dev_attr_mapping28.attr,
  678. &dev_attr_mapping29.attr,
  679. &dev_attr_mapping30.attr,
  680. &dev_attr_mapping31.attr,
  681. NULL,
  682. };
  683. static const struct attribute_group nd_mapping_attribute_group = {
  684. .is_visible = mapping_visible,
  685. .attrs = mapping_attributes,
  686. };
  687. static const struct attribute_group nd_region_attribute_group = {
  688. .attrs = nd_region_attributes,
  689. .is_visible = region_visible,
  690. };
  691. static const struct attribute_group *nd_region_attribute_groups[] = {
  692. &nd_device_attribute_group,
  693. &nd_region_attribute_group,
  694. &nd_numa_attribute_group,
  695. &nd_mapping_attribute_group,
  696. NULL,
  697. };
  698. static const struct device_type nd_pmem_device_type = {
  699. .name = "nd_pmem",
  700. .release = nd_region_release,
  701. .groups = nd_region_attribute_groups,
  702. };
  703. static const struct device_type nd_volatile_device_type = {
  704. .name = "nd_volatile",
  705. .release = nd_region_release,
  706. .groups = nd_region_attribute_groups,
  707. };
  708. bool is_nd_pmem(const struct device *dev)
  709. {
  710. return dev ? dev->type == &nd_pmem_device_type : false;
  711. }
  712. bool is_nd_volatile(const struct device *dev)
  713. {
  714. return dev ? dev->type == &nd_volatile_device_type : false;
  715. }
  716. u64 nd_region_interleave_set_cookie(struct nd_region *nd_region,
  717. struct nd_namespace_index *nsindex)
  718. {
  719. struct nd_interleave_set *nd_set = nd_region->nd_set;
  720. if (!nd_set)
  721. return 0;
  722. if (nsindex && __le16_to_cpu(nsindex->major) == 1
  723. && __le16_to_cpu(nsindex->minor) == 1)
  724. return nd_set->cookie1;
  725. return nd_set->cookie2;
  726. }
  727. u64 nd_region_interleave_set_altcookie(struct nd_region *nd_region)
  728. {
  729. struct nd_interleave_set *nd_set = nd_region->nd_set;
  730. if (nd_set)
  731. return nd_set->altcookie;
  732. return 0;
  733. }
  734. void nd_mapping_free_labels(struct nd_mapping *nd_mapping)
  735. {
  736. struct nd_label_ent *label_ent, *e;
  737. lockdep_assert_held(&nd_mapping->lock);
  738. list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
  739. list_del(&label_ent->list);
  740. kfree(label_ent);
  741. }
  742. }
  743. /*
  744. * When a namespace is activated create new seeds for the next
  745. * namespace, or namespace-personality to be configured.
  746. */
  747. void nd_region_advance_seeds(struct nd_region *nd_region, struct device *dev)
  748. {
  749. nvdimm_bus_lock(dev);
  750. if (nd_region->ns_seed == dev) {
  751. nd_region_create_ns_seed(nd_region);
  752. } else if (is_nd_btt(dev)) {
  753. struct nd_btt *nd_btt = to_nd_btt(dev);
  754. if (nd_region->btt_seed == dev)
  755. nd_region_create_btt_seed(nd_region);
  756. if (nd_region->ns_seed == &nd_btt->ndns->dev)
  757. nd_region_create_ns_seed(nd_region);
  758. } else if (is_nd_pfn(dev)) {
  759. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  760. if (nd_region->pfn_seed == dev)
  761. nd_region_create_pfn_seed(nd_region);
  762. if (nd_region->ns_seed == &nd_pfn->ndns->dev)
  763. nd_region_create_ns_seed(nd_region);
  764. } else if (is_nd_dax(dev)) {
  765. struct nd_dax *nd_dax = to_nd_dax(dev);
  766. if (nd_region->dax_seed == dev)
  767. nd_region_create_dax_seed(nd_region);
  768. if (nd_region->ns_seed == &nd_dax->nd_pfn.ndns->dev)
  769. nd_region_create_ns_seed(nd_region);
  770. }
  771. nvdimm_bus_unlock(dev);
  772. }
  773. /**
  774. * nd_region_acquire_lane - allocate and lock a lane
  775. * @nd_region: region id and number of lanes possible
  776. *
  777. * A lane correlates to a BLK-data-window and/or a log slot in the BTT.
  778. * We optimize for the common case where there are 256 lanes, one
  779. * per-cpu. For larger systems we need to lock to share lanes. For now
  780. * this implementation assumes the cost of maintaining an allocator for
  781. * free lanes is on the order of the lock hold time, so it implements a
  782. * static lane = cpu % num_lanes mapping.
  783. *
  784. * In the case of a BTT instance on top of a BLK namespace a lane may be
  785. * acquired recursively. We lock on the first instance.
  786. *
  787. * In the case of a BTT instance on top of PMEM, we only acquire a lane
  788. * for the BTT metadata updates.
  789. */
  790. unsigned int nd_region_acquire_lane(struct nd_region *nd_region)
  791. {
  792. unsigned int cpu, lane;
  793. migrate_disable();
  794. cpu = smp_processor_id();
  795. if (nd_region->num_lanes < nr_cpu_ids) {
  796. struct nd_percpu_lane *ndl_lock, *ndl_count;
  797. lane = cpu % nd_region->num_lanes;
  798. ndl_count = per_cpu_ptr(nd_region->lane, cpu);
  799. ndl_lock = per_cpu_ptr(nd_region->lane, lane);
  800. if (ndl_count->count++ == 0)
  801. spin_lock(&ndl_lock->lock);
  802. } else
  803. lane = cpu;
  804. return lane;
  805. }
  806. EXPORT_SYMBOL(nd_region_acquire_lane);
  807. void nd_region_release_lane(struct nd_region *nd_region, unsigned int lane)
  808. {
  809. if (nd_region->num_lanes < nr_cpu_ids) {
  810. unsigned int cpu = smp_processor_id();
  811. struct nd_percpu_lane *ndl_lock, *ndl_count;
  812. ndl_count = per_cpu_ptr(nd_region->lane, cpu);
  813. ndl_lock = per_cpu_ptr(nd_region->lane, lane);
  814. if (--ndl_count->count == 0)
  815. spin_unlock(&ndl_lock->lock);
  816. }
  817. migrate_enable();
  818. }
  819. EXPORT_SYMBOL(nd_region_release_lane);
  820. /*
  821. * PowerPC requires this alignment for memremap_pages(). All other archs
  822. * should be ok with SUBSECTION_SIZE (see memremap_compat_align()).
  823. */
  824. #define MEMREMAP_COMPAT_ALIGN_MAX SZ_16M
  825. static unsigned long default_align(struct nd_region *nd_region)
  826. {
  827. unsigned long align;
  828. u32 remainder;
  829. int mappings;
  830. align = MEMREMAP_COMPAT_ALIGN_MAX;
  831. if (nd_region->ndr_size < MEMREMAP_COMPAT_ALIGN_MAX)
  832. align = PAGE_SIZE;
  833. mappings = max_t(u16, 1, nd_region->ndr_mappings);
  834. div_u64_rem(align, mappings, &remainder);
  835. if (remainder)
  836. align *= mappings;
  837. return align;
  838. }
  839. static struct lock_class_key nvdimm_region_key;
  840. static struct nd_region *nd_region_create(struct nvdimm_bus *nvdimm_bus,
  841. struct nd_region_desc *ndr_desc,
  842. const struct device_type *dev_type, const char *caller)
  843. {
  844. struct nd_region *nd_region;
  845. struct device *dev;
  846. unsigned int i;
  847. int ro = 0;
  848. for (i = 0; i < ndr_desc->num_mappings; i++) {
  849. struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
  850. struct nvdimm *nvdimm = mapping->nvdimm;
  851. if ((mapping->start | mapping->size) % PAGE_SIZE) {
  852. dev_err(&nvdimm_bus->dev,
  853. "%s: %s mapping%d is not %ld aligned\n",
  854. caller, dev_name(&nvdimm->dev), i, PAGE_SIZE);
  855. return NULL;
  856. }
  857. if (test_bit(NDD_UNARMED, &nvdimm->flags))
  858. ro = 1;
  859. }
  860. nd_region =
  861. kzalloc(struct_size(nd_region, mapping, ndr_desc->num_mappings),
  862. GFP_KERNEL);
  863. if (!nd_region)
  864. return NULL;
  865. nd_region->ndr_mappings = ndr_desc->num_mappings;
  866. /* CXL pre-assigns memregion ids before creating nvdimm regions */
  867. if (test_bit(ND_REGION_CXL, &ndr_desc->flags)) {
  868. nd_region->id = ndr_desc->memregion;
  869. } else {
  870. nd_region->id = memregion_alloc(GFP_KERNEL);
  871. if (nd_region->id < 0)
  872. goto err_id;
  873. }
  874. nd_region->lane = alloc_percpu(struct nd_percpu_lane);
  875. if (!nd_region->lane)
  876. goto err_percpu;
  877. for (i = 0; i < nr_cpu_ids; i++) {
  878. struct nd_percpu_lane *ndl;
  879. ndl = per_cpu_ptr(nd_region->lane, i);
  880. spin_lock_init(&ndl->lock);
  881. ndl->count = 0;
  882. }
  883. for (i = 0; i < ndr_desc->num_mappings; i++) {
  884. struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
  885. struct nvdimm *nvdimm = mapping->nvdimm;
  886. nd_region->mapping[i].nvdimm = nvdimm;
  887. nd_region->mapping[i].start = mapping->start;
  888. nd_region->mapping[i].size = mapping->size;
  889. nd_region->mapping[i].position = mapping->position;
  890. INIT_LIST_HEAD(&nd_region->mapping[i].labels);
  891. mutex_init(&nd_region->mapping[i].lock);
  892. get_device(&nvdimm->dev);
  893. }
  894. nd_region->provider_data = ndr_desc->provider_data;
  895. nd_region->nd_set = ndr_desc->nd_set;
  896. nd_region->num_lanes = ndr_desc->num_lanes;
  897. nd_region->flags = ndr_desc->flags;
  898. nd_region->ro = ro;
  899. nd_region->numa_node = ndr_desc->numa_node;
  900. nd_region->target_node = ndr_desc->target_node;
  901. ida_init(&nd_region->ns_ida);
  902. ida_init(&nd_region->btt_ida);
  903. ida_init(&nd_region->pfn_ida);
  904. ida_init(&nd_region->dax_ida);
  905. dev = &nd_region->dev;
  906. dev_set_name(dev, "region%d", nd_region->id);
  907. dev->parent = &nvdimm_bus->dev;
  908. dev->type = dev_type;
  909. dev->groups = ndr_desc->attr_groups;
  910. dev->of_node = ndr_desc->of_node;
  911. nd_region->ndr_size = resource_size(ndr_desc->res);
  912. nd_region->ndr_start = ndr_desc->res->start;
  913. nd_region->align = default_align(nd_region);
  914. if (ndr_desc->flush)
  915. nd_region->flush = ndr_desc->flush;
  916. else
  917. nd_region->flush = NULL;
  918. device_initialize(dev);
  919. lockdep_set_class(&dev->mutex, &nvdimm_region_key);
  920. nd_device_register(dev);
  921. return nd_region;
  922. err_percpu:
  923. if (!test_bit(ND_REGION_CXL, &ndr_desc->flags))
  924. memregion_free(nd_region->id);
  925. err_id:
  926. kfree(nd_region);
  927. return NULL;
  928. }
  929. struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus,
  930. struct nd_region_desc *ndr_desc)
  931. {
  932. ndr_desc->num_lanes = ND_MAX_LANES;
  933. return nd_region_create(nvdimm_bus, ndr_desc, &nd_pmem_device_type,
  934. __func__);
  935. }
  936. EXPORT_SYMBOL_GPL(nvdimm_pmem_region_create);
  937. struct nd_region *nvdimm_volatile_region_create(struct nvdimm_bus *nvdimm_bus,
  938. struct nd_region_desc *ndr_desc)
  939. {
  940. ndr_desc->num_lanes = ND_MAX_LANES;
  941. return nd_region_create(nvdimm_bus, ndr_desc, &nd_volatile_device_type,
  942. __func__);
  943. }
  944. EXPORT_SYMBOL_GPL(nvdimm_volatile_region_create);
  945. void nvdimm_region_delete(struct nd_region *nd_region)
  946. {
  947. if (nd_region)
  948. nd_device_unregister(&nd_region->dev, ND_SYNC);
  949. }
  950. EXPORT_SYMBOL_GPL(nvdimm_region_delete);
  951. int nvdimm_flush(struct nd_region *nd_region, struct bio *bio)
  952. {
  953. int rc = 0;
  954. if (!nd_region->flush)
  955. rc = generic_nvdimm_flush(nd_region);
  956. else {
  957. if (nd_region->flush(nd_region, bio))
  958. rc = -EIO;
  959. }
  960. return rc;
  961. }
  962. /**
  963. * generic_nvdimm_flush() - flush any posted write queues between the cpu and pmem media
  964. * @nd_region: interleaved pmem region
  965. */
  966. int generic_nvdimm_flush(struct nd_region *nd_region)
  967. {
  968. struct nd_region_data *ndrd = dev_get_drvdata(&nd_region->dev);
  969. int i, idx;
  970. /*
  971. * Try to encourage some diversity in flush hint addresses
  972. * across cpus assuming a limited number of flush hints.
  973. */
  974. idx = this_cpu_read(flush_idx);
  975. idx = this_cpu_add_return(flush_idx, hash_32(current->pid + idx, 8));
  976. /*
  977. * The pmem_wmb() is needed to 'sfence' all
  978. * previous writes such that they are architecturally visible for
  979. * the platform buffer flush. Note that we've already arranged for pmem
  980. * writes to avoid the cache via memcpy_flushcache(). The final
  981. * wmb() ensures ordering for the NVDIMM flush write.
  982. */
  983. pmem_wmb();
  984. for (i = 0; i < nd_region->ndr_mappings; i++)
  985. if (ndrd_get_flush_wpq(ndrd, i, 0))
  986. writeq(1, ndrd_get_flush_wpq(ndrd, i, idx));
  987. wmb();
  988. return 0;
  989. }
  990. EXPORT_SYMBOL_GPL(nvdimm_flush);
  991. /**
  992. * nvdimm_has_flush - determine write flushing requirements
  993. * @nd_region: interleaved pmem region
  994. *
  995. * Returns 1 if writes require flushing
  996. * Returns 0 if writes do not require flushing
  997. * Returns -ENXIO if flushing capability can not be determined
  998. */
  999. int nvdimm_has_flush(struct nd_region *nd_region)
  1000. {
  1001. int i;
  1002. /* no nvdimm or pmem api == flushing capability unknown */
  1003. if (nd_region->ndr_mappings == 0
  1004. || !IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API))
  1005. return -ENXIO;
  1006. /* Test if an explicit flush function is defined */
  1007. if (test_bit(ND_REGION_ASYNC, &nd_region->flags) && nd_region->flush)
  1008. return 1;
  1009. /* Test if any flush hints for the region are available */
  1010. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1011. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1012. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  1013. /* flush hints present / available */
  1014. if (nvdimm->num_flush)
  1015. return 1;
  1016. }
  1017. /*
  1018. * The platform defines dimm devices without hints nor explicit flush,
  1019. * assume platform persistence mechanism like ADR
  1020. */
  1021. return 0;
  1022. }
  1023. EXPORT_SYMBOL_GPL(nvdimm_has_flush);
  1024. int nvdimm_has_cache(struct nd_region *nd_region)
  1025. {
  1026. return is_nd_pmem(&nd_region->dev) &&
  1027. !test_bit(ND_REGION_PERSIST_CACHE, &nd_region->flags);
  1028. }
  1029. EXPORT_SYMBOL_GPL(nvdimm_has_cache);
  1030. bool is_nvdimm_sync(struct nd_region *nd_region)
  1031. {
  1032. if (is_nd_volatile(&nd_region->dev))
  1033. return true;
  1034. return is_nd_pmem(&nd_region->dev) &&
  1035. !test_bit(ND_REGION_ASYNC, &nd_region->flags);
  1036. }
  1037. EXPORT_SYMBOL_GPL(is_nvdimm_sync);
  1038. struct conflict_context {
  1039. struct nd_region *nd_region;
  1040. resource_size_t start, size;
  1041. };
  1042. static int region_conflict(struct device *dev, void *data)
  1043. {
  1044. struct nd_region *nd_region;
  1045. struct conflict_context *ctx = data;
  1046. resource_size_t res_end, region_end, region_start;
  1047. if (!is_memory(dev))
  1048. return 0;
  1049. nd_region = to_nd_region(dev);
  1050. if (nd_region == ctx->nd_region)
  1051. return 0;
  1052. res_end = ctx->start + ctx->size;
  1053. region_start = nd_region->ndr_start;
  1054. region_end = region_start + nd_region->ndr_size;
  1055. if (ctx->start >= region_start && ctx->start < region_end)
  1056. return -EBUSY;
  1057. if (res_end > region_start && res_end <= region_end)
  1058. return -EBUSY;
  1059. return 0;
  1060. }
  1061. int nd_region_conflict(struct nd_region *nd_region, resource_size_t start,
  1062. resource_size_t size)
  1063. {
  1064. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
  1065. struct conflict_context ctx = {
  1066. .nd_region = nd_region,
  1067. .start = start,
  1068. .size = size,
  1069. };
  1070. return device_for_each_child(&nvdimm_bus->dev, &ctx, region_conflict);
  1071. }
  1072. MODULE_IMPORT_NS(DEVMEM);