libndctl: Use the supported_alignment attribute
PATCH v4 1/5 libndctl: Use the supported_alignment attribute
Newer kernels provide the "supported_alignments" sysfs attribute that indicates what alignments can be used with a PFN or DAX namespace. This patch adds the plumbing inside of libndctl to allow users to query this information through using:
ndctl_{dax|pfn}_get_supported_alignment(), and ndctl_{dax|pfn}_get_num_alignments()
---
v4: Changed return code of ndctl_pfn_get_supported_alignment from -1 to
-1 to -EINVAL.
Reworded comment about why we default to 4K and 2M alignments when the sysfs attribute is missing.
Shuffled around prototypes in ndctl.h.
80 char compliance fixes.
rebased onto pending branch
v3: Changed the return type of the *_get_supported_alignment() functions
to unsigned long to match the existing *_get_alignment() functions.
--- ndctl/lib/libndctl.c | 44 ++++++++++++++++++++++++++++++++++++++++++ ndctl/lib/libndctl.sym | 4 ++++ ndctl/libndctl.h | 4 ++++ 3 files changed, 52 insertions(+)
ndctl/lib/libndctl.c +44 -0
@@ -31,6 +31,7 @@#include <ccan/build_assert/build_assert.h> #include <ndctl.h> +#include <util/size.h> #include <util/sysfs.h> #include <ndctl/libndctl.h> #include <ndctl/namespace.h>
@@ -237,6 +238,7 @@ struct ndctl_pfn {int buf_len;uuid_t uuid;int id, generation; +struct ndctl_lbasize alignments; };struct ndctl_dax {
@@ -4814,6 +4816,20 @@ static void *__add_pfn(struct ndctl_pfn *pfn, const char *pfn_base)else pfn->size =strtoull (buf,NULL ,0 ); +/* +* The supported_alignments attribute was added before arches other +* than x86 had pmem support. If the kernel doesn't provide the +* attribute then it's safe to a attribute then it's safe to assume +* that we are on x86 which will always support 2MB and 4KB +* alignments. +*/ +sprintf (path,"%s/supported_alignments" , pfn_base); +if (sysfs_read_attr (ctx, path, buf) <0 ) +sprintf (buf,"%d %d" ,SZ_4K ,SZ_2M ); + +if (parse_lbasize_supported (ctx, pfn_base, buf, &pfn->alignments) <0 ) + goto err_read; +free (path);return pfn;
@@ -5048,6 +5064,23 @@ NDCTL_EXPORT int ndctl_pfn_set_align(struct ndctl_pfn *pfn, unsigned long align)return 0 ; } +NDCTL_EXPORT int ndctl_pfn_get_num_alignments(struct ndctl_pfn *pfn) +{ +return pfn->alignments.num; +} + +NDCTL_EXPORT unsigned long ndctl_pfn_get_supported_alignment ( +struct ndctl_pfn *pfn,int i) +{ +if (pfn->alignments.num ==0 ) +return 0 ; + +if (i <0 || i > pfn->alignments.num) +return -EINVAL ; +else +return pfn->alignments.supported[i]; +} + NDCTL_EXPORT int ndctl_pfn_set_namespace(struct ndctl_pfn *pfn,struct ndctl_namespace *ndns) {
@@ -5270,6 +5303,17 @@ NDCTL_EXPORT unsigned long ndctl_dax_get_align(struct ndctl_dax *dax)return ndctl_pfn_get_align (&dax->pfn); } +NDCTL_EXPORT int ndctl_dax_get_num_alignments(struct ndctl_dax *dax) +{ +return ndctl_pfn_get_num_alignments (&dax->pfn); +} + +NDCTL_EXPORT unsigned long ndctl_dax_get_supported_alignment ( +struct ndctl_dax *dax,int i) +{ +return ndctl_pfn_get_supported_alignment (&dax->pfn, i); +} + NDCTL_EXPORT int ndctl_dax_has_align(struct ndctl_dax *dax) {return ndctl_pfn_has_align (&dax->pfn);
ndctl/lib/libndctl.sym +4 -0
@@ -390,4 +390,8 @@ LIBNDCTL_19 { global: ndctl_cmd_xlat_firmware_status; ndctl_cmd_submit_xlat; + ndctl_pfn_get_supported_alignment; + ndctl_pfn_get_num_alignments; + ndctl_dax_get_supported_alignment; + ndctl_dax_get_num_alignments; } LIBNDCTL_18;
ndctl/libndctl.h +4 -0
@@ -597,7 +597,9 @@ int ndctl_pfn_set_uuid(struct ndctl_pfn *pfn, uuid_t uu);void ndctl_pfn_get_uuid (struct ndctl_pfn *pfn,uuid_t uu);int ndctl_pfn_has_align (struct ndctl_pfn *pfn);int ndctl_pfn_set_align (struct ndctl_pfn *pfn,unsigned long align); +int ndctl_pfn_get_num_alignments (struct ndctl_pfn *pfn);unsigned long ndctl_pfn_get_align (struct ndctl_pfn *pfn); +unsigned long ndctl_pfn_get_supported_alignment (struct ndctl_pfn *pfn,int i);unsigned long long ndctl_pfn_get_resource (struct ndctl_pfn *pfn);unsigned long long ndctl_pfn_get_size (struct ndctl_pfn *pfn);int ndctl_pfn_set_namespace (struct ndctl_pfn *pfn,struct ndctl_namespace *ndns);
@@ -628,7 +630,9 @@ unsigned long long ndctl_dax_get_resource(struct ndctl_dax *dax);int ndctl_dax_set_uuid (struct ndctl_dax *dax,uuid_t uu);enum ndctl_pfn_loc ndctl_dax_get_location (struct ndctl_dax *dax);int ndctl_dax_set_location (struct ndctl_dax *dax,enum ndctl_pfn_loc loc); +int ndctl_dax_get_num_alignments (struct ndctl_dax *dax);unsigned long ndctl_dax_get_align (struct ndctl_dax *dax); +unsigned long ndctl_dax_get_supported_alignment (struct ndctl_dax *dax,int i);int ndctl_dax_has_align (struct ndctl_dax *dax);int ndctl_dax_set_align (struct ndctl_dax *dax,unsigned long align); int ndctl_dax_set_namespace(struct ndctl_dax *dax,
Signature · 6 lines
2.20.1 _______________________________________________ Linux-nvdimm mailing list [email protected] https://lists.01.org/mailman/listinfo/linux-nvdimm
PATCH v4 2/5 ndctl/namespace: Check for seed namespaces earlier
When creating an fsdax or devdax namespace we need to verify that the seed namespaces exist. This patch reworks the validation so that it's done earlier to simplify the subsequent patches in the series.
---
No functional changes, I hope.
v3: Don't fail early if the user specifies fsdax mode without pfn
support. Instead, if we try to validate a feature that requires pfn support, fail the validation at that point.
--- ndctl/namespace.c | 48 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 25 deletions(-)
ndctl/namespace.c +23 -25
@@ -469,6 +469,8 @@ static int validate_namespace_options(struct ndctl_region *region, {const char *region_name =ndctl_region_get_devname (region);unsigned long long size_align =SZ_4K , units =1 , resource; +struct ndctl_pfn *pfn =NULL ; +struct ndctl_dax *dax =NULL ;unsigned int ways;int rc =0 ;
@@ -525,10 +527,24 @@ static int validate_namespace_options(struct ndctl_region *region, }else if (ndns) p->mode =ndctl_namespace_get_mode (ndns); -if (param.align) { -struct ndctl_pfn *pfn =ndctl_region_get_pfn_seed (region); -struct ndctl_dax *dax =ndctl_region_get_dax_seed (region); +if (p->mode ==NDCTL_NS_MODE_MEMORY ) { + pfn =ndctl_region_get_pfn_seed (region); +if (!pfn && param.mode_default) { +debug ("%s fsdax mode not available\n" , region_name); + p->mode =NDCTL_NS_MODE_RAW ; + } +/* +* NB: We only fail validation if a pfn-specific option is used +*/ + }else if (p->mode ==NDCTL_NS_MODE_DAX ) { + dax =ndctl_region_get_dax_seed (region); +if (!dax) { +error ("Kernel does not support devdax mode\n" ); +return -ENODEV ; + } + } +if (param.align) { p->align =parse_size64 (param.align); if (p->mode == NDCTL_NS_MODE_MEMORY && p->align != SZ_2M
@@ -709,30 +725,12 @@ static int validate_namespace_options(struct ndctl_region *region, || p->mode == NDCTL_NS_MODE_DAX) p->loc =NDCTL_PFN_LOC_PMEM ; -/* check if we need, and whether the kernel supports, pfn devices */ -if (do_setup_pfn (ndns, p)) { -struct ndctl_pfn *pfn =ndctl_region_get_pfn_seed (region); - -if (!pfn && param.mode_default) { -debug ("%s fsdax mode not available\n" , region_name); - p->mode =NDCTL_NS_MODE_RAW ; - }else if (!pfn) { -error ("operation failed, %s fsdax mode not available\n" , - region_name); -return -EINVAL ; - } +if (!pfn &&do_setup_pfn (ndns, p)) { +error ("operation failed, %s cannot support requested mode\n" , + region_name); +return -EINVAL ; } -/* check if we need, and whether the kernel supports, dax devices */ -if (p->mode ==NDCTL_NS_MODE_DAX ) { -struct ndctl_dax *dax =ndctl_region_get_dax_seed (region); - -if (!dax) { -error ("operation failed, %s devdax mode not available\n" , - region_name); -return -EINVAL ; - } - } p->autolabel = param.autolabel;
Signature · 6 lines
2.20.1 _______________________________________________ Linux-nvdimm mailing list [email protected] https://lists.01.org/mailman/listinfo/linux-nvdimm
PATCH v4 3/5 ndctl/namespace: Use seed alignment as the default
In reply to Oliver O'HalloranWhen creating a pfn or dax namespace ndctl uses a default alignment of 2MB when the user does not explicitly supply one. This works on most systems (x86, ARM, PPC64 with radix MMU), but it fails when the kernel does not support a 2MB page size (PPC64 with hash MMU).
This patch makes ndctl use the alignment of the relevant seed namespace as the default instead. The kernel will always pick a valid default alignment so this should be a bit more portable.
---
v3: Only use the pfn seed for the default alignment if there is a pfn
seed.
--- ndctl/namespace.c | 96 +++++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 53 deletions(-)
ndctl/namespace.c +43 -53
@@ -39,7 +39,6 @@ static bool logfix; static struct parameters { bool do_scan; bool mode_default; - bool align_default; bool autolabel; const char *bus; const char *map;
@@ -226,9 +225,6 @@ static int set_defaults(enum device_action mode)error ("failed to parse namespace alignment '%s'\n" , param.align); rc = -EINVAL ; - }else if (!param.align) { - param.align ="2M" ; - param.align_default = true; }if (param.uuid) {
@@ -468,7 +464,7 @@ static int validate_namespace_options(struct ndctl_region *region, struct ndctl_namespace *ndns, struct parsed_parameters *p) { const char *region_name = ndctl_region_get_devname(region); -unsigned long long size_align =SZ_4K , units =1 , resource; +unsigned long long size_align, units =1 , resource;struct ndctl_pfn *pfn =NULL ;struct ndctl_dax *dax =NULL ;unsigned int ways;
@@ -545,53 +541,15 @@ static int validate_namespace_options(struct ndctl_region *region, }if (param.align) { - p->align =parse_size64 (param.align); - -if (p->mode ==NDCTL_NS_MODE_MEMORY && p->align !=SZ_2M - && (!pfn || !ndctl_pfn_has_align (pfn))) { -/* -* Initial pfn device support in the kernel -* supported a 2M default alignment when -* ndctl_pfn_has_align() returns false. -*/ -debug ("%s not support 'align' for fsdax mode\n" , - region_name); -return -EAGAIN ; - }else if (p->mode ==NDCTL_NS_MODE_DAX - && (!dax || !ndctl_dax_has_align (dax))) { -/* -* Unlike the pfn case, we require the kernel to -* have 'align' support for device-dax. -*/ -debug ("%s not support 'align' for devdax mode\n" , - region_name); -return -EAGAIN ; - }else if (!param.align_default - && (p->mode ==NDCTL_NS_MODE_SAFE - || p->mode ==NDCTL_NS_MODE_RAW )) { -/* -* Specifying an alignment has no effect for -* raw, or btt mode namespaces. -*/ +if (p->mode !=NDCTL_NS_MODE_MEMORY && + p->mode !=NDCTL_NS_MODE_DAX ) {error ("%s mode does not support setting an alignment\n" , p->mode ==NDCTL_NS_MODE_SAFE ?"sector" :"raw" );return -ENXIO ; } -/* -* Fallback to a 4K default alignment if the region is -* not 2MB (typical default) aligned. This mainly helps -* the nfit_test use case where it is backed by vmalloc -* memory. -*/ - resource =ndctl_region_get_resource (region); -if (param.align_default && resource <ULLONG_MAX - && (resource & (SZ_2M -1 ))) { -debug ("%s: falling back to a 4K alignment\n" , - region_name); - p->align =SZ_4K ; - } + p->align =parse_size64 (param.align);switch (p->align) {case SZ_4K :
@@ -602,16 +560,48 @@ static int validate_namespace_options(struct ndctl_region *region,error ("unsupported align: %s\n" , param.align);return -ENXIO ; } + }else { +/* +* Use the seed namespace alignment as the default if we need +* one. If we don't then use PAGE_SIZE so the size_align +* checking works. +*/ +if (p->mode ==NDCTL_NS_MODE_MEMORY ) { +/* +* The initial pfn device support in the kernel didn't +* have the 'align' sysfs attribute and assumed a 2MB +* alignment. Fall back to that if we don't have the +* attribute. +*/ +if (pfn &&ndctl_pfn_has_align (pfn)) + p->align =ndctl_pfn_get_align (pfn); +else + p->align =SZ_2M ; + }else if (p->mode ==NDCTL_NS_MODE_DAX ) { +/* +* device dax mode was added after the align attribute +* so checking for it is unnecessary. +*/ + p->align =ndctl_dax_get_align (dax); + }else { + p->align =sysconf (_SC_PAGE_SIZE); + }/* -* 'raw' and 'sector' mode namespaces don't support an -* alignment attribute. +* Fallback to a page alignment if the region is not aligned +* to the default. This is mainly useful for the nfit_test +* use case where it is backed by vmalloc memory. */ -if (p->mode ==NDCTL_NS_MODE_MEMORY - || p->mode ==NDCTL_NS_MODE_DAX ) - size_align = p->align; + resource =ndctl_region_get_resource (region); +if (resource <ULLONG_MAX && (resource & (p->align -1 ))) { +debug ("%s: falling back to a page alignment\n" , + region_name); + p->align =sysconf (_SC_PAGE_SIZE); + } } + size_align = p->align; +/* (re-)validate that the size satisfies the alignment */ ways =ndctl_region_get_interleave_ways (region);if (p->size % (size_align * ways)) {
@@ -637,8 +627,8 @@ static int validate_namespace_options(struct ndctl_region *region, p->size *= size_align; p->size /= units;error ("'--size=' must align to interleave-width: %d and alignment: %ld\n" -" did you intend --size=%lld%s?\n" , ways, param.align - ? p->align :SZ_4K , p->size, suffix); +"did you intend --size=%lld%s?\n" , + ways, p->align, p->size, suffix);return -EINVAL ; }
Signature · 6 lines
2.20.1 _______________________________________________ Linux-nvdimm mailing list [email protected] https://lists.01.org/mailman/listinfo/linux-nvdimm
PATCH v4 4/5 ndctl/namespace: Validate alignment from the {pfn|dax} seed
In reply to Oliver O'HalloranThis patch adds support to the ndctl tool for validating that the namespace alignment is valid.
---
v3: Fail validation if an alignment is specified, but pfn mode is
not supported.
--- ndctl/namespace.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-)
ndctl/namespace.c +29 -8
@@ -541,8 +541,23 @@ static int validate_namespace_options(struct ndctl_region *region, }if (param.align) { -if (p->mode !=NDCTL_NS_MODE_MEMORY && - p->mode !=NDCTL_NS_MODE_DAX ) { +int i, alignments; + +switch (p->mode) { +case NDCTL_NS_MODE_MEMORY : +if (!pfn) { +error ("Kernel does not support setting an alignment in fsdax mode\n" ); +return -EINVAL ; + } + + alignments =ndctl_pfn_get_num_alignments (pfn); +break ; + +case NDCTL_NS_MODE_DAX : + alignments =ndctl_dax_get_num_alignments (dax); +break ; + +default :error ("%s mode does not support setting an alignment\n" , p->mode ==NDCTL_NS_MODE_SAFE ?"sector" :"raw" );
@@ -550,13 +565,19 @@ static int validate_namespace_options(struct ndctl_region *region, } p->align =parse_size64 (param.align); +for (i =0 ; i < alignments; i++) { +uint64_t a; -switch (p->align) { -case SZ_4K : -case SZ_2M : -case SZ_1G : -break ; -default : +if (p->mode ==NDCTL_NS_MODE_MEMORY ) + a =ndctl_pfn_get_supported_alignment (pfn, i); +else + a =ndctl_dax_get_supported_alignment (dax, i); + +if (p->align == a) +break ; + } + +if (i >= alignments) {error ("unsupported align: %s\n" , param.align);return -ENXIO ; }
Signature · 6 lines
2.20.1 _______________________________________________ Linux-nvdimm mailing list [email protected] https://lists.01.org/mailman/listinfo/linux-nvdimm
PATCH v4 5/5 ndctl: Add alignment to the namespace JSON output
In reply to Oliver O'HalloranAutomated tooling probably wants to know this and it's helpful output for command line users for ndctl.
--- util/json.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
util/json.c +10 -0
@@ -753,6 +753,7 @@ struct json_object *util_namespace_to_json(struct ndctl_namespace *ndns,struct ndctl_btt *btt;struct ndctl_pfn *pfn;struct ndctl_dax *dax; +unsigned long align =0 ;char buf[40 ];uuid_t uuid;int numa;
@@ -825,6 +826,7 @@ struct json_object *util_namespace_to_json(struct ndctl_namespace *ndns,util_raw_uuid_to_json (ndns, flags, jndns); bdev =ndctl_btt_get_block_device (btt); }else if (pfn) { + align =ndctl_pfn_get_align (pfn);ndctl_pfn_get_uuid (pfn, uuid);uuid_unparse (uuid, buf); jobj =json_object_new_string (buf);
@@ -837,6 +839,7 @@ struct json_object *util_namespace_to_json(struct ndctl_namespace *ndns,struct daxctl_region *dax_region; dax_region =ndctl_dax_get_daxctl_region (dax); + align =ndctl_dax_get_align (dax);ndctl_dax_get_uuid (dax, uuid);uuid_unparse (uuid, buf); jobj =json_object_new_string (buf);
@@ -897,6 +900,13 @@ struct json_object *util_namespace_to_json(struct ndctl_namespace *ndns,json_object_object_add (jndns,"sector_size" , jobj); } +if (align) { + jobj =json_object_new_int64 (align); +if (!jobj) + goto err; +json_object_object_add (jndns,"align" , jobj); + } +if (bdev && bdev[0 ]) { jobj =json_object_new_string (bdev);if (!jobj)
Signature · 6 lines
2.20.1 _______________________________________________ Linux-nvdimm mailing list [email protected] https://lists.01.org/mailman/listinfo/linux-nvdimm
Quoted 10 lines
-----Original Message-----
From: Linux-nvdimm [mailto:[email protected]] On Behalf Of Oliver O'Halloran
Sent: Tuesday, January 29, 2019 8:49 AM
To: [email protected]
Subject: [PATCH v4 1/5] libndctl: Use thesupported_alignmentattributeReworded comment about why we default to 4K and 2M alignments when the sysfs attribute is missing.
...
ndctl/lib/libndctl.c static void *__add_pfn(struct ndctl_pfn *pfn, const char *pfn_base) · line 4823
Quoted 5 lines
ndctl/lib/libndctl.c +5
-0+/* +* The supported_alignments attribute was added before arches other +* than x86 had pmem support. If the kernel doesn't provide the +* attribute then it's safe to a attribute then it's safe to assume +* that we are on x86 which will always support 2MB and 4KB
"then it's safe to a attribute" is extraneous.
Also, please use MiB and KiB everywhere.
_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm