nvdimm.txt 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. LIBNVDIMM: Non-Volatile Devices
  2. libnvdimm - kernel / libndctl - userspace helper library
  3. linux-nvdimm@lists.01.org
  4. v13
  5. Glossary
  6. Overview
  7. Supporting Documents
  8. Git Trees
  9. LIBNVDIMM PMEM and BLK
  10. Why BLK?
  11. PMEM vs BLK
  12. BLK-REGIONs, PMEM-REGIONs, Atomic Sectors, and DAX
  13. Example NVDIMM Platform
  14. LIBNVDIMM Kernel Device Model and LIBNDCTL Userspace API
  15. LIBNDCTL: Context
  16. libndctl: instantiate a new library context example
  17. LIBNVDIMM/LIBNDCTL: Bus
  18. libnvdimm: control class device in /sys/class
  19. libnvdimm: bus
  20. libndctl: bus enumeration example
  21. LIBNVDIMM/LIBNDCTL: DIMM (NMEM)
  22. libnvdimm: DIMM (NMEM)
  23. libndctl: DIMM enumeration example
  24. LIBNVDIMM/LIBNDCTL: Region
  25. libnvdimm: region
  26. libndctl: region enumeration example
  27. Why Not Encode the Region Type into the Region Name?
  28. How Do I Determine the Major Type of a Region?
  29. LIBNVDIMM/LIBNDCTL: Namespace
  30. libnvdimm: namespace
  31. libndctl: namespace enumeration example
  32. libndctl: namespace creation example
  33. Why the Term "namespace"?
  34. LIBNVDIMM/LIBNDCTL: Block Translation Table "btt"
  35. libnvdimm: btt layout
  36. libndctl: btt creation example
  37. Summary LIBNDCTL Diagram
  38. Glossary
  39. --------
  40. PMEM: A system-physical-address range where writes are persistent. A
  41. block device composed of PMEM is capable of DAX. A PMEM address range
  42. may span an interleave of several DIMMs.
  43. BLK: A set of one or more programmable memory mapped apertures provided
  44. by a DIMM to access its media. This indirection precludes the
  45. performance benefit of interleaving, but enables DIMM-bounded failure
  46. modes.
  47. DPA: DIMM Physical Address, is a DIMM-relative offset. With one DIMM in
  48. the system there would be a 1:1 system-physical-address:DPA association.
  49. Once more DIMMs are added a memory controller interleave must be
  50. decoded to determine the DPA associated with a given
  51. system-physical-address. BLK capacity always has a 1:1 relationship
  52. with a single-DIMM's DPA range.
  53. DAX: File system extensions to bypass the page cache and block layer to
  54. mmap persistent memory, from a PMEM block device, directly into a
  55. process address space.
  56. BTT: Block Translation Table: Persistent memory is byte addressable.
  57. Existing software may have an expectation that the power-fail-atomicity
  58. of writes is at least one sector, 512 bytes. The BTT is an indirection
  59. table with atomic update semantics to front a PMEM/BLK block device
  60. driver and present arbitrary atomic sector sizes.
  61. LABEL: Metadata stored on a DIMM device that partitions and identifies
  62. (persistently names) storage between PMEM and BLK. It also partitions
  63. BLK storage to host BTTs with different parameters per BLK-partition.
  64. Note that traditional partition tables, GPT/MBR, are layered on top of a
  65. BLK or PMEM device.
  66. Overview
  67. --------
  68. The LIBNVDIMM subsystem provides support for three types of NVDIMMs, namely,
  69. PMEM, BLK, and NVDIMM devices that can simultaneously support both PMEM
  70. and BLK mode access. These three modes of operation are described by
  71. the "NVDIMM Firmware Interface Table" (NFIT) in ACPI 6. While the LIBNVDIMM
  72. implementation is generic and supports pre-NFIT platforms, it was guided
  73. by the superset of capabilities need to support this ACPI 6 definition
  74. for NVDIMM resources. The bulk of the kernel implementation is in place
  75. to handle the case where DPA accessible via PMEM is aliased with DPA
  76. accessible via BLK. When that occurs a LABEL is needed to reserve DPA
  77. for exclusive access via one mode a time.
  78. Supporting Documents
  79. ACPI 6: http://www.uefi.org/sites/default/files/resources/ACPI_6.0.pdf
  80. NVDIMM Namespace: http://pmem.io/documents/NVDIMM_Namespace_Spec.pdf
  81. DSM Interface Example: http://pmem.io/documents/NVDIMM_DSM_Interface_Example.pdf
  82. Driver Writer's Guide: http://pmem.io/documents/NVDIMM_Driver_Writers_Guide.pdf
  83. Git Trees
  84. LIBNVDIMM: https://git.kernel.org/cgit/linux/kernel/git/djbw/nvdimm.git
  85. LIBNDCTL: https://github.com/pmem/ndctl.git
  86. PMEM: https://github.com/01org/prd
  87. LIBNVDIMM PMEM and BLK
  88. ------------------
  89. Prior to the arrival of the NFIT, non-volatile memory was described to a
  90. system in various ad-hoc ways. Usually only the bare minimum was
  91. provided, namely, a single system-physical-address range where writes
  92. are expected to be durable after a system power loss. Now, the NFIT
  93. specification standardizes not only the description of PMEM, but also
  94. BLK and platform message-passing entry points for control and
  95. configuration.
  96. For each NVDIMM access method (PMEM, BLK), LIBNVDIMM provides a block
  97. device driver:
  98. 1. PMEM (nd_pmem.ko): Drives a system-physical-address range. This
  99. range is contiguous in system memory and may be interleaved (hardware
  100. memory controller striped) across multiple DIMMs. When interleaved the
  101. platform may optionally provide details of which DIMMs are participating
  102. in the interleave.
  103. Note that while LIBNVDIMM describes system-physical-address ranges that may
  104. alias with BLK access as ND_NAMESPACE_PMEM ranges and those without
  105. alias as ND_NAMESPACE_IO ranges, to the nd_pmem driver there is no
  106. distinction. The different device-types are an implementation detail
  107. that userspace can exploit to implement policies like "only interface
  108. with address ranges from certain DIMMs". It is worth noting that when
  109. aliasing is present and a DIMM lacks a label, then no block device can
  110. be created by default as userspace needs to do at least one allocation
  111. of DPA to the PMEM range. In contrast ND_NAMESPACE_IO ranges, once
  112. registered, can be immediately attached to nd_pmem.
  113. 2. BLK (nd_blk.ko): This driver performs I/O using a set of platform
  114. defined apertures. A set of apertures will all access just one DIMM.
  115. Multiple windows allow multiple concurrent accesses, much like
  116. tagged-command-queuing, and would likely be used by different threads or
  117. different CPUs.
  118. The NFIT specification defines a standard format for a BLK-aperture, but
  119. the spec also allows for vendor specific layouts, and non-NFIT BLK
  120. implementations may other designs for BLK I/O. For this reason "nd_blk"
  121. calls back into platform-specific code to perform the I/O. One such
  122. implementation is defined in the "Driver Writer's Guide" and "DSM
  123. Interface Example".
  124. Why BLK?
  125. --------
  126. While PMEM provides direct byte-addressable CPU-load/store access to
  127. NVDIMM storage, it does not provide the best system RAS (recovery,
  128. availability, and serviceability) model. An access to a corrupted
  129. system-physical-address address causes a cpu exception while an access
  130. to a corrupted address through an BLK-aperture causes that block window
  131. to raise an error status in a register. The latter is more aligned with
  132. the standard error model that host-bus-adapter attached disks present.
  133. Also, if an administrator ever wants to replace a memory it is easier to
  134. service a system at DIMM module boundaries. Compare this to PMEM where
  135. data could be interleaved in an opaque hardware specific manner across
  136. several DIMMs.
  137. PMEM vs BLK
  138. BLK-apertures solve this RAS problem, but their presence is also the
  139. major contributing factor to the complexity of the ND subsystem. They
  140. complicate the implementation because PMEM and BLK alias in DPA space.
  141. Any given DIMM's DPA-range may contribute to one or more
  142. system-physical-address sets of interleaved DIMMs, *and* may also be
  143. accessed in its entirety through its BLK-aperture. Accessing a DPA
  144. through a system-physical-address while simultaneously accessing the
  145. same DPA through a BLK-aperture has undefined results. For this reason,
  146. DIMMs with this dual interface configuration include a DSM function to
  147. store/retrieve a LABEL. The LABEL effectively partitions the DPA-space
  148. into exclusive system-physical-address and BLK-aperture accessible
  149. regions. For simplicity a DIMM is allowed a PMEM "region" per each
  150. interleave set in which it is a member. The remaining DPA space can be
  151. carved into an arbitrary number of BLK devices with discontiguous
  152. extents.
  153. BLK-REGIONs, PMEM-REGIONs, Atomic Sectors, and DAX
  154. --------------------------------------------------
  155. One of the few
  156. reasons to allow multiple BLK namespaces per REGION is so that each
  157. BLK-namespace can be configured with a BTT with unique atomic sector
  158. sizes. While a PMEM device can host a BTT the LABEL specification does
  159. not provide for a sector size to be specified for a PMEM namespace.
  160. This is due to the expectation that the primary usage model for PMEM is
  161. via DAX, and the BTT is incompatible with DAX. However, for the cases
  162. where an application or filesystem still needs atomic sector update
  163. guarantees it can register a BTT on a PMEM device or partition. See
  164. LIBNVDIMM/NDCTL: Block Translation Table "btt"
  165. Example NVDIMM Platform
  166. -----------------------
  167. For the remainder of this document the following diagram will be
  168. referenced for any example sysfs layouts.
  169. (a) (b) DIMM BLK-REGION
  170. +-------------------+--------+--------+--------+
  171. +------+ | pm0.0 | blk2.0 | pm1.0 | blk2.1 | 0 region2
  172. | imc0 +--+- - - region0- - - +--------+ +--------+
  173. +--+---+ | pm0.0 | blk3.0 | pm1.0 | blk3.1 | 1 region3
  174. | +-------------------+--------v v--------+
  175. +--+---+ | |
  176. | cpu0 | region1
  177. +--+---+ | |
  178. | +----------------------------^ ^--------+
  179. +--+---+ | blk4.0 | pm1.0 | blk4.0 | 2 region4
  180. | imc1 +--+----------------------------| +--------+
  181. +------+ | blk5.0 | pm1.0 | blk5.0 | 3 region5
  182. +----------------------------+--------+--------+
  183. In this platform we have four DIMMs and two memory controllers in one
  184. socket. Each unique interface (BLK or PMEM) to DPA space is identified
  185. by a region device with a dynamically assigned id (REGION0 - REGION5).
  186. 1. The first portion of DIMM0 and DIMM1 are interleaved as REGION0. A
  187. single PMEM namespace is created in the REGION0-SPA-range that spans
  188. DIMM0 and DIMM1 with a user-specified name of "pm0.0". Some of that
  189. interleaved system-physical-address range is reclaimed as BLK-aperture
  190. accessed space starting at DPA-offset (a) into each DIMM. In that
  191. reclaimed space we create two BLK-aperture "namespaces" from REGION2 and
  192. REGION3 where "blk2.0" and "blk3.0" are just human readable names that
  193. could be set to any user-desired name in the LABEL.
  194. 2. In the last portion of DIMM0 and DIMM1 we have an interleaved
  195. system-physical-address range, REGION1, that spans those two DIMMs as
  196. well as DIMM2 and DIMM3. Some of REGION1 allocated to a PMEM namespace
  197. named "pm1.0" the rest is reclaimed in 4 BLK-aperture namespaces (for
  198. each DIMM in the interleave set), "blk2.1", "blk3.1", "blk4.0", and
  199. "blk5.0".
  200. 3. The portion of DIMM2 and DIMM3 that do not participate in the REGION1
  201. interleaved system-physical-address range (i.e. the DPA address below
  202. offset (b) are also included in the "blk4.0" and "blk5.0" namespaces.
  203. Note, that this example shows that BLK-aperture namespaces don't need to
  204. be contiguous in DPA-space.
  205. This bus is provided by the kernel under the device
  206. /sys/devices/platform/nfit_test.0 when CONFIG_NFIT_TEST is enabled and
  207. the nfit_test.ko module is loaded. This not only test LIBNVDIMM but the
  208. acpi_nfit.ko driver as well.
  209. LIBNVDIMM Kernel Device Model and LIBNDCTL Userspace API
  210. ----------------------------------------------------
  211. What follows is a description of the LIBNVDIMM sysfs layout and a
  212. corresponding object hierarchy diagram as viewed through the LIBNDCTL
  213. api. The example sysfs paths and diagrams are relative to the Example
  214. NVDIMM Platform which is also the LIBNVDIMM bus used in the LIBNDCTL unit
  215. test.
  216. LIBNDCTL: Context
  217. Every api call in the LIBNDCTL library requires a context that holds the
  218. logging parameters and other library instance state. The library is
  219. based on the libabc template:
  220. https://git.kernel.org/cgit/linux/kernel/git/kay/libabc.git/
  221. LIBNDCTL: instantiate a new library context example
  222. struct ndctl_ctx *ctx;
  223. if (ndctl_new(&ctx) == 0)
  224. return ctx;
  225. else
  226. return NULL;
  227. LIBNVDIMM/LIBNDCTL: Bus
  228. -------------------
  229. A bus has a 1:1 relationship with an NFIT. The current expectation for
  230. ACPI based systems is that there is only ever one platform-global NFIT.
  231. That said, it is trivial to register multiple NFITs, the specification
  232. does not preclude it. The infrastructure supports multiple busses and
  233. we we use this capability to test multiple NFIT configurations in the
  234. unit test.
  235. LIBNVDIMM: control class device in /sys/class
  236. This character device accepts DSM messages to be passed to DIMM
  237. identified by its NFIT handle.
  238. /sys/class/nd/ndctl0
  239. |-- dev
  240. |-- device -> ../../../ndbus0
  241. |-- subsystem -> ../../../../../../../class/nd
  242. LIBNVDIMM: bus
  243. struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
  244. struct nvdimm_bus_descriptor *nfit_desc);
  245. /sys/devices/platform/nfit_test.0/ndbus0
  246. |-- commands
  247. |-- nd
  248. |-- nfit
  249. |-- nmem0
  250. |-- nmem1
  251. |-- nmem2
  252. |-- nmem3
  253. |-- power
  254. |-- provider
  255. |-- region0
  256. |-- region1
  257. |-- region2
  258. |-- region3
  259. |-- region4
  260. |-- region5
  261. |-- uevent
  262. `-- wait_probe
  263. LIBNDCTL: bus enumeration example
  264. Find the bus handle that describes the bus from Example NVDIMM Platform
  265. static struct ndctl_bus *get_bus_by_provider(struct ndctl_ctx *ctx,
  266. const char *provider)
  267. {
  268. struct ndctl_bus *bus;
  269. ndctl_bus_foreach(ctx, bus)
  270. if (strcmp(provider, ndctl_bus_get_provider(bus)) == 0)
  271. return bus;
  272. return NULL;
  273. }
  274. bus = get_bus_by_provider(ctx, "nfit_test.0");
  275. LIBNVDIMM/LIBNDCTL: DIMM (NMEM)
  276. ---------------------------
  277. The DIMM device provides a character device for sending commands to
  278. hardware, and it is a container for LABELs. If the DIMM is defined by
  279. NFIT then an optional 'nfit' attribute sub-directory is available to add
  280. NFIT-specifics.
  281. Note that the kernel device name for "DIMMs" is "nmemX". The NFIT
  282. describes these devices via "Memory Device to System Physical Address
  283. Range Mapping Structure", and there is no requirement that they actually
  284. be physical DIMMs, so we use a more generic name.
  285. LIBNVDIMM: DIMM (NMEM)
  286. struct nvdimm *nvdimm_create(struct nvdimm_bus *nvdimm_bus, void *provider_data,
  287. const struct attribute_group **groups, unsigned long flags,
  288. unsigned long *dsm_mask);
  289. /sys/devices/platform/nfit_test.0/ndbus0
  290. |-- nmem0
  291. | |-- available_slots
  292. | |-- commands
  293. | |-- dev
  294. | |-- devtype
  295. | |-- driver -> ../../../../../bus/nd/drivers/nvdimm
  296. | |-- modalias
  297. | |-- nfit
  298. | | |-- device
  299. | | |-- format
  300. | | |-- handle
  301. | | |-- phys_id
  302. | | |-- rev_id
  303. | | |-- serial
  304. | | `-- vendor
  305. | |-- state
  306. | |-- subsystem -> ../../../../../bus/nd
  307. | `-- uevent
  308. |-- nmem1
  309. [..]
  310. LIBNDCTL: DIMM enumeration example
  311. Note, in this example we are assuming NFIT-defined DIMMs which are
  312. identified by an "nfit_handle" a 32-bit value where:
  313. Bit 3:0 DIMM number within the memory channel
  314. Bit 7:4 memory channel number
  315. Bit 11:8 memory controller ID
  316. Bit 15:12 socket ID (within scope of a Node controller if node controller is present)
  317. Bit 27:16 Node Controller ID
  318. Bit 31:28 Reserved
  319. static struct ndctl_dimm *get_dimm_by_handle(struct ndctl_bus *bus,
  320. unsigned int handle)
  321. {
  322. struct ndctl_dimm *dimm;
  323. ndctl_dimm_foreach(bus, dimm)
  324. if (ndctl_dimm_get_handle(dimm) == handle)
  325. return dimm;
  326. return NULL;
  327. }
  328. #define DIMM_HANDLE(n, s, i, c, d) \
  329. (((n & 0xfff) << 16) | ((s & 0xf) << 12) | ((i & 0xf) << 8) \
  330. | ((c & 0xf) << 4) | (d & 0xf))
  331. dimm = get_dimm_by_handle(bus, DIMM_HANDLE(0, 0, 0, 0, 0));
  332. LIBNVDIMM/LIBNDCTL: Region
  333. ----------------------
  334. A generic REGION device is registered for each PMEM range orBLK-aperture
  335. set. Per the example there are 6 regions: 2 PMEM and 4 BLK-aperture
  336. sets on the "nfit_test.0" bus. The primary role of regions are to be a
  337. container of "mappings". A mapping is a tuple of <DIMM,
  338. DPA-start-offset, length>.
  339. LIBNVDIMM provides a built-in driver for these REGION devices. This driver
  340. is responsible for reconciling the aliased DPA mappings across all
  341. regions, parsing the LABEL, if present, and then emitting NAMESPACE
  342. devices with the resolved/exclusive DPA-boundaries for the nd_pmem or
  343. nd_blk device driver to consume.
  344. In addition to the generic attributes of "mapping"s, "interleave_ways"
  345. and "size" the REGION device also exports some convenience attributes.
  346. "nstype" indicates the integer type of namespace-device this region
  347. emits, "devtype" duplicates the DEVTYPE variable stored by udev at the
  348. 'add' event, "modalias" duplicates the MODALIAS variable stored by udev
  349. at the 'add' event, and finally, the optional "spa_index" is provided in
  350. the case where the region is defined by a SPA.
  351. LIBNVDIMM: region
  352. struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus,
  353. struct nd_region_desc *ndr_desc);
  354. struct nd_region *nvdimm_blk_region_create(struct nvdimm_bus *nvdimm_bus,
  355. struct nd_region_desc *ndr_desc);
  356. /sys/devices/platform/nfit_test.0/ndbus0
  357. |-- region0
  358. | |-- available_size
  359. | |-- btt0
  360. | |-- btt_seed
  361. | |-- devtype
  362. | |-- driver -> ../../../../../bus/nd/drivers/nd_region
  363. | |-- init_namespaces
  364. | |-- mapping0
  365. | |-- mapping1
  366. | |-- mappings
  367. | |-- modalias
  368. | |-- namespace0.0
  369. | |-- namespace_seed
  370. | |-- numa_node
  371. | |-- nfit
  372. | | `-- spa_index
  373. | |-- nstype
  374. | |-- set_cookie
  375. | |-- size
  376. | |-- subsystem -> ../../../../../bus/nd
  377. | `-- uevent
  378. |-- region1
  379. [..]
  380. LIBNDCTL: region enumeration example
  381. Sample region retrieval routines based on NFIT-unique data like
  382. "spa_index" (interleave set id) for PMEM and "nfit_handle" (dimm id) for
  383. BLK.
  384. static struct ndctl_region *get_pmem_region_by_spa_index(struct ndctl_bus *bus,
  385. unsigned int spa_index)
  386. {
  387. struct ndctl_region *region;
  388. ndctl_region_foreach(bus, region) {
  389. if (ndctl_region_get_type(region) != ND_DEVICE_REGION_PMEM)
  390. continue;
  391. if (ndctl_region_get_spa_index(region) == spa_index)
  392. return region;
  393. }
  394. return NULL;
  395. }
  396. static struct ndctl_region *get_blk_region_by_dimm_handle(struct ndctl_bus *bus,
  397. unsigned int handle)
  398. {
  399. struct ndctl_region *region;
  400. ndctl_region_foreach(bus, region) {
  401. struct ndctl_mapping *map;
  402. if (ndctl_region_get_type(region) != ND_DEVICE_REGION_BLOCK)
  403. continue;
  404. ndctl_mapping_foreach(region, map) {
  405. struct ndctl_dimm *dimm = ndctl_mapping_get_dimm(map);
  406. if (ndctl_dimm_get_handle(dimm) == handle)
  407. return region;
  408. }
  409. }
  410. return NULL;
  411. }
  412. Why Not Encode the Region Type into the Region Name?
  413. ----------------------------------------------------
  414. At first glance it seems since NFIT defines just PMEM and BLK interface
  415. types that we should simply name REGION devices with something derived
  416. from those type names. However, the ND subsystem explicitly keeps the
  417. REGION name generic and expects userspace to always consider the
  418. region-attributes for 4 reasons:
  419. 1. There are already more than two REGION and "namespace" types. For
  420. PMEM there are two subtypes. As mentioned previously we have PMEM where
  421. the constituent DIMM devices are known and anonymous PMEM. For BLK
  422. regions the NFIT specification already anticipates vendor specific
  423. implementations. The exact distinction of what a region contains is in
  424. the region-attributes not the region-name or the region-devtype.
  425. 2. A region with zero child-namespaces is a possible configuration. For
  426. example, the NFIT allows for a DCR to be published without a
  427. corresponding BLK-aperture. This equates to a DIMM that can only accept
  428. control/configuration messages, but no i/o through a descendant block
  429. device. Again, this "type" is advertised in the attributes ('mappings'
  430. == 0) and the name does not tell you much.
  431. 3. What if a third major interface type arises in the future? Outside
  432. of vendor specific implementations, it's not difficult to envision a
  433. third class of interface type beyond BLK and PMEM. With a generic name
  434. for the REGION level of the device-hierarchy old userspace
  435. implementations can still make sense of new kernel advertised
  436. region-types. Userspace can always rely on the generic region
  437. attributes like "mappings", "size", etc and the expected child devices
  438. named "namespace". This generic format of the device-model hierarchy
  439. allows the LIBNVDIMM and LIBNDCTL implementations to be more uniform and
  440. future-proof.
  441. 4. There are more robust mechanisms for determining the major type of a
  442. region than a device name. See the next section, How Do I Determine the
  443. Major Type of a Region?
  444. How Do I Determine the Major Type of a Region?
  445. ----------------------------------------------
  446. Outside of the blanket recommendation of "use libndctl", or simply
  447. looking at the kernel header (/usr/include/linux/ndctl.h) to decode the
  448. "nstype" integer attribute, here are some other options.
  449. 1. module alias lookup:
  450. The whole point of region/namespace device type differentiation is to
  451. decide which block-device driver will attach to a given LIBNVDIMM namespace.
  452. One can simply use the modalias to lookup the resulting module. It's
  453. important to note that this method is robust in the presence of a
  454. vendor-specific driver down the road. If a vendor-specific
  455. implementation wants to supplant the standard nd_blk driver it can with
  456. minimal impact to the rest of LIBNVDIMM.
  457. In fact, a vendor may also want to have a vendor-specific region-driver
  458. (outside of nd_region). For example, if a vendor defined its own LABEL
  459. format it would need its own region driver to parse that LABEL and emit
  460. the resulting namespaces. The output from module resolution is more
  461. accurate than a region-name or region-devtype.
  462. 2. udev:
  463. The kernel "devtype" is registered in the udev database
  464. # udevadm info --path=/devices/platform/nfit_test.0/ndbus0/region0
  465. P: /devices/platform/nfit_test.0/ndbus0/region0
  466. E: DEVPATH=/devices/platform/nfit_test.0/ndbus0/region0
  467. E: DEVTYPE=nd_pmem
  468. E: MODALIAS=nd:t2
  469. E: SUBSYSTEM=nd
  470. # udevadm info --path=/devices/platform/nfit_test.0/ndbus0/region4
  471. P: /devices/platform/nfit_test.0/ndbus0/region4
  472. E: DEVPATH=/devices/platform/nfit_test.0/ndbus0/region4
  473. E: DEVTYPE=nd_blk
  474. E: MODALIAS=nd:t3
  475. E: SUBSYSTEM=nd
  476. ...and is available as a region attribute, but keep in mind that the
  477. "devtype" does not indicate sub-type variations and scripts should
  478. really be understanding the other attributes.
  479. 3. type specific attributes:
  480. As it currently stands a BLK-aperture region will never have a
  481. "nfit/spa_index" attribute, but neither will a non-NFIT PMEM region. A
  482. BLK region with a "mappings" value of 0 is, as mentioned above, a DIMM
  483. that does not allow I/O. A PMEM region with a "mappings" value of zero
  484. is a simple system-physical-address range.
  485. LIBNVDIMM/LIBNDCTL: Namespace
  486. -------------------------
  487. A REGION, after resolving DPA aliasing and LABEL specified boundaries,
  488. surfaces one or more "namespace" devices. The arrival of a "namespace"
  489. device currently triggers either the nd_blk or nd_pmem driver to load
  490. and register a disk/block device.
  491. LIBNVDIMM: namespace
  492. Here is a sample layout from the three major types of NAMESPACE where
  493. namespace0.0 represents DIMM-info-backed PMEM (note that it has a 'uuid'
  494. attribute), namespace2.0 represents a BLK namespace (note it has a
  495. 'sector_size' attribute) that, and namespace6.0 represents an anonymous
  496. PMEM namespace (note that has no 'uuid' attribute due to not support a
  497. LABEL).
  498. /sys/devices/platform/nfit_test.0/ndbus0/region0/namespace0.0
  499. |-- alt_name
  500. |-- devtype
  501. |-- dpa_extents
  502. |-- force_raw
  503. |-- modalias
  504. |-- numa_node
  505. |-- resource
  506. |-- size
  507. |-- subsystem -> ../../../../../../bus/nd
  508. |-- type
  509. |-- uevent
  510. `-- uuid
  511. /sys/devices/platform/nfit_test.0/ndbus0/region2/namespace2.0
  512. |-- alt_name
  513. |-- devtype
  514. |-- dpa_extents
  515. |-- force_raw
  516. |-- modalias
  517. |-- numa_node
  518. |-- sector_size
  519. |-- size
  520. |-- subsystem -> ../../../../../../bus/nd
  521. |-- type
  522. |-- uevent
  523. `-- uuid
  524. /sys/devices/platform/nfit_test.1/ndbus1/region6/namespace6.0
  525. |-- block
  526. | `-- pmem0
  527. |-- devtype
  528. |-- driver -> ../../../../../../bus/nd/drivers/pmem
  529. |-- force_raw
  530. |-- modalias
  531. |-- numa_node
  532. |-- resource
  533. |-- size
  534. |-- subsystem -> ../../../../../../bus/nd
  535. |-- type
  536. `-- uevent
  537. LIBNDCTL: namespace enumeration example
  538. Namespaces are indexed relative to their parent region, example below.
  539. These indexes are mostly static from boot to boot, but subsystem makes
  540. no guarantees in this regard. For a static namespace identifier use its
  541. 'uuid' attribute.
  542. static struct ndctl_namespace *get_namespace_by_id(struct ndctl_region *region,
  543. unsigned int id)
  544. {
  545. struct ndctl_namespace *ndns;
  546. ndctl_namespace_foreach(region, ndns)
  547. if (ndctl_namespace_get_id(ndns) == id)
  548. return ndns;
  549. return NULL;
  550. }
  551. LIBNDCTL: namespace creation example
  552. Idle namespaces are automatically created by the kernel if a given
  553. region has enough available capacity to create a new namespace.
  554. Namespace instantiation involves finding an idle namespace and
  555. configuring it. For the most part the setting of namespace attributes
  556. can occur in any order, the only constraint is that 'uuid' must be set
  557. before 'size'. This enables the kernel to track DPA allocations
  558. internally with a static identifier.
  559. static int configure_namespace(struct ndctl_region *region,
  560. struct ndctl_namespace *ndns,
  561. struct namespace_parameters *parameters)
  562. {
  563. char devname[50];
  564. snprintf(devname, sizeof(devname), "namespace%d.%d",
  565. ndctl_region_get_id(region), paramaters->id);
  566. ndctl_namespace_set_alt_name(ndns, devname);
  567. /* 'uuid' must be set prior to setting size! */
  568. ndctl_namespace_set_uuid(ndns, paramaters->uuid);
  569. ndctl_namespace_set_size(ndns, paramaters->size);
  570. /* unlike pmem namespaces, blk namespaces have a sector size */
  571. if (parameters->lbasize)
  572. ndctl_namespace_set_sector_size(ndns, parameters->lbasize);
  573. ndctl_namespace_enable(ndns);
  574. }
  575. Why the Term "namespace"?
  576. 1. Why not "volume" for instance? "volume" ran the risk of confusing ND
  577. as a volume manager like device-mapper.
  578. 2. The term originated to describe the sub-devices that can be created
  579. within a NVME controller (see the nvme specification:
  580. http://www.nvmexpress.org/specifications/), and NFIT namespaces are
  581. meant to parallel the capabilities and configurability of
  582. NVME-namespaces.
  583. LIBNVDIMM/LIBNDCTL: Block Translation Table "btt"
  584. ---------------------------------------------
  585. A BTT (design document: http://pmem.io/2014/09/23/btt.html) is a stacked
  586. block device driver that fronts either the whole block device or a
  587. partition of a block device emitted by either a PMEM or BLK NAMESPACE.
  588. LIBNVDIMM: btt layout
  589. Every region will start out with at least one BTT device which is the
  590. seed device. To activate it set the "namespace", "uuid", and
  591. "sector_size" attributes and then bind the device to the nd_pmem or
  592. nd_blk driver depending on the region type.
  593. /sys/devices/platform/nfit_test.1/ndbus0/region0/btt0/
  594. |-- namespace
  595. |-- delete
  596. |-- devtype
  597. |-- modalias
  598. |-- numa_node
  599. |-- sector_size
  600. |-- subsystem -> ../../../../../bus/nd
  601. |-- uevent
  602. `-- uuid
  603. LIBNDCTL: btt creation example
  604. Similar to namespaces an idle BTT device is automatically created per
  605. region. Each time this "seed" btt device is configured and enabled a new
  606. seed is created. Creating a BTT configuration involves two steps of
  607. finding and idle BTT and assigning it to consume a PMEM or BLK namespace.
  608. static struct ndctl_btt *get_idle_btt(struct ndctl_region *region)
  609. {
  610. struct ndctl_btt *btt;
  611. ndctl_btt_foreach(region, btt)
  612. if (!ndctl_btt_is_enabled(btt)
  613. && !ndctl_btt_is_configured(btt))
  614. return btt;
  615. return NULL;
  616. }
  617. static int configure_btt(struct ndctl_region *region,
  618. struct btt_parameters *parameters)
  619. {
  620. btt = get_idle_btt(region);
  621. ndctl_btt_set_uuid(btt, parameters->uuid);
  622. ndctl_btt_set_sector_size(btt, parameters->sector_size);
  623. ndctl_btt_set_namespace(btt, parameters->ndns);
  624. /* turn off raw mode device */
  625. ndctl_namespace_disable(parameters->ndns);
  626. /* turn on btt access */
  627. ndctl_btt_enable(btt);
  628. }
  629. Once instantiated a new inactive btt seed device will appear underneath
  630. the region.
  631. Once a "namespace" is removed from a BTT that instance of the BTT device
  632. will be deleted or otherwise reset to default values. This deletion is
  633. only at the device model level. In order to destroy a BTT the "info
  634. block" needs to be destroyed. Note, that to destroy a BTT the media
  635. needs to be written in raw mode. By default, the kernel will autodetect
  636. the presence of a BTT and disable raw mode. This autodetect behavior
  637. can be suppressed by enabling raw mode for the namespace via the
  638. ndctl_namespace_set_raw_mode() api.
  639. Summary LIBNDCTL Diagram
  640. ------------------------
  641. For the given example above, here is the view of the objects as seen by the LIBNDCTL api:
  642. +---+
  643. |CTX| +---------+ +--------------+ +---------------+
  644. +-+-+ +-> REGION0 +---> NAMESPACE0.0 +--> PMEM8 "pm0.0" |
  645. | | +---------+ +--------------+ +---------------+
  646. +-------+ | | +---------+ +--------------+ +---------------+
  647. | DIMM0 <-+ | +-> REGION1 +---> NAMESPACE1.0 +--> PMEM6 "pm1.0" |
  648. +-------+ | | | +---------+ +--------------+ +---------------+
  649. | DIMM1 <-+ +-v--+ | +---------+ +--------------+ +---------------+
  650. +-------+ +-+BUS0+---> REGION2 +-+-> NAMESPACE2.0 +--> ND6 "blk2.0" |
  651. | DIMM2 <-+ +----+ | +---------+ | +--------------+ +----------------------+
  652. +-------+ | | +-> NAMESPACE2.1 +--> ND5 "blk2.1" | BTT2 |
  653. | DIMM3 <-+ | +--------------+ +----------------------+
  654. +-------+ | +---------+ +--------------+ +---------------+
  655. +-> REGION3 +-+-> NAMESPACE3.0 +--> ND4 "blk3.0" |
  656. | +---------+ | +--------------+ +----------------------+
  657. | +-> NAMESPACE3.1 +--> ND3 "blk3.1" | BTT1 |
  658. | +--------------+ +----------------------+
  659. | +---------+ +--------------+ +---------------+
  660. +-> REGION4 +---> NAMESPACE4.0 +--> ND2 "blk4.0" |
  661. | +---------+ +--------------+ +---------------+
  662. | +---------+ +--------------+ +----------------------+
  663. +-> REGION5 +---> NAMESPACE5.0 +--> ND1 "blk5.0" | BTT0 |
  664. +---------+ +--------------+ +---------------+------+