[PATCH V2 08/20] nvdimm/label: Include region label in slot validation

Neeraj Kumar posted 20 patches 2 months, 1 week ago
There is a newer version of this series
[PATCH V2 08/20] nvdimm/label: Include region label in slot validation
Posted by Neeraj Kumar 2 months, 1 week ago
slot validation routine validates label slot by calculating label
checksum. It was only validating namespace label. This changeset also
validates region label if present.

Also validate and calculate lsa v2.1 namespace label checksum

Signed-off-by: Neeraj Kumar <s.neeraj@samsung.com>
---
 drivers/nvdimm/label.c | 55 ++++++++++++++++++++++++++++++++++--------
 drivers/nvdimm/nd.h    | 19 +++++++++++++++
 2 files changed, 64 insertions(+), 10 deletions(-)

diff --git a/drivers/nvdimm/label.c b/drivers/nvdimm/label.c
index fd02b557612e..c4748e30f2b6 100644
--- a/drivers/nvdimm/label.c
+++ b/drivers/nvdimm/label.c
@@ -359,7 +359,7 @@ static bool nsl_validate_checksum(struct nvdimm_drvdata *ndd,
 {
 	u64 sum, sum_save;
 
-	if (!ndd->cxl && !efi_namespace_label_has(ndd, checksum))
+	if (!efi_namespace_label_has(ndd, checksum))
 		return true;
 
 	sum_save = nsl_get_checksum(ndd, nd_label);
@@ -374,13 +374,25 @@ static void nsl_calculate_checksum(struct nvdimm_drvdata *ndd,
 {
 	u64 sum;
 
-	if (!ndd->cxl && !efi_namespace_label_has(ndd, checksum))
+	if (!efi_namespace_label_has(ndd, checksum))
 		return;
 	nsl_set_checksum(ndd, nd_label, 0);
 	sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1);
 	nsl_set_checksum(ndd, nd_label, sum);
 }
 
+static bool rgl_validate_checksum(struct nvdimm_drvdata *ndd,
+				  struct cxl_region_label *rg_label)
+{
+	u64 sum, sum_save;
+
+	sum_save = rgl_get_checksum(rg_label);
+	rgl_set_checksum(rg_label, 0);
+	sum = nd_fletcher64(rg_label, sizeof_namespace_label(ndd), 1);
+	rgl_set_checksum(rg_label, sum_save);
+	return sum == sum_save;
+}
+
 static void rgl_calculate_checksum(struct nvdimm_drvdata *ndd,
 				   struct cxl_region_label *rg_label)
 {
@@ -395,14 +407,27 @@ static bool slot_valid(struct nvdimm_drvdata *ndd,
 		struct nd_lsa_label *lsa_label, u32 slot)
 {
 	bool valid;
+	char *label_name;
 	struct nd_namespace_label *nd_label = &lsa_label->ns_label;
+	struct cxl_region_label *rg_label = &lsa_label->rg_label;
 
 	/* check that we are written where we expect to be written */
-	if (slot != nsl_get_slot(ndd, nd_label))
-		return false;
-	valid = nsl_validate_checksum(ndd, nd_label);
+	if (is_region_label(ndd, lsa_label)) {
+		label_name = "rg";
+		if (slot != rgl_get_slot(rg_label))
+			return false;
+		valid = rgl_validate_checksum(ndd, rg_label);
+	} else {
+		label_name = "ns";
+		if (slot != nsl_get_slot(ndd, nd_label))
+			return false;
+		valid = nsl_validate_checksum(ndd, nd_label);
+	}
+
 	if (!valid)
-		dev_dbg(ndd->dev, "fail checksum. slot: %d\n", slot);
+		dev_dbg(ndd->dev, "%s label checksum fail. slot: %d\n",
+			label_name, slot);
+
 	return valid;
 }
 
@@ -580,18 +605,28 @@ int nd_label_active_count(struct nvdimm_drvdata *ndd)
 	for_each_clear_bit_le(slot, free, nslot) {
 		struct nd_lsa_label *lsa_label;
 		struct nd_namespace_label *nd_label;
+		struct cxl_region_label *rg_label;
+		u32 lslot;
+		u64 size, dpa;
 
 		lsa_label = to_label(ndd, slot);
 		nd_label = &lsa_label->ns_label;
+		rg_label = &lsa_label->rg_label;
 
 		if (!slot_valid(ndd, lsa_label, slot)) {
-			u32 label_slot = nsl_get_slot(ndd, nd_label);
-			u64 size = nsl_get_rawsize(ndd, nd_label);
-			u64 dpa = nsl_get_dpa(ndd, nd_label);
+			if (is_region_label(ndd, lsa_label)) {
+				lslot = __le32_to_cpu(rg_label->slot);
+				size = __le64_to_cpu(rg_label->rawsize);
+				dpa = __cpu_to_le64(rg_label->dpa);
+			} else {
+				lslot = nsl_get_slot(ndd, nd_label);
+				size = nsl_get_rawsize(ndd, nd_label);
+				dpa = nsl_get_dpa(ndd, nd_label);
+			}
 
 			dev_dbg(ndd->dev,
 				"slot%d invalid slot: %d dpa: %llx size: %llx\n",
-					slot, label_slot, dpa, size);
+					slot, lslot, dpa, size);
 			continue;
 		}
 		count++;
diff --git a/drivers/nvdimm/nd.h b/drivers/nvdimm/nd.h
index 6585747154c2..4145c7df2a8f 100644
--- a/drivers/nvdimm/nd.h
+++ b/drivers/nvdimm/nd.h
@@ -331,6 +331,20 @@ static inline bool nsl_region_uuid_equal(struct nd_namespace_label *ns_label,
 	return uuid_equal(&tmp, uuid);
 }
 
+static inline bool is_region_label(struct nvdimm_drvdata *ndd,
+				   struct nd_lsa_label *nd_label)
+{
+	uuid_t ns_type, region_type;
+
+	if (!ndd->cxl)
+		return false;
+
+	uuid_parse(CXL_REGION_UUID, &region_type);
+	import_uuid(&ns_type, nd_label->ns_label.cxl.type);
+	return uuid_equal(&region_type, &ns_type);
+
+}
+
 static inline bool rgl_uuid_equal(struct cxl_region_label *rg_label,
 				  const uuid_t *uuid)
 {
@@ -340,6 +354,11 @@ static inline bool rgl_uuid_equal(struct cxl_region_label *rg_label,
 	return uuid_equal(&tmp, uuid);
 }
 
+static inline u32 rgl_get_slot(struct cxl_region_label *rg_label)
+{
+	return __le32_to_cpu(rg_label->slot);
+}
+
 static inline u64 rgl_get_checksum(struct cxl_region_label *rg_label)
 {
 	return __le64_to_cpu(rg_label->checksum);
-- 
2.34.1
Re: [PATCH V2 08/20] nvdimm/label: Include region label in slot validation
Posted by Jonathan Cameron 1 month, 3 weeks ago
On Wed, 30 Jul 2025 17:41:57 +0530
Neeraj Kumar <s.neeraj@samsung.com> wrote:

> slot validation routine validates label slot by calculating label

Slot validation ... or
The slot validation routing ...


> checksum. It was only validating namespace label. This changeset also
> validates region label if present.
> 
> Also validate and calculate lsa v2.1 namespace label checksum

LSA v2.1 ...


> 
> Signed-off-by: Neeraj Kumar <s.neeraj@samsung.com>
Otherwise LGTM

Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com>
Re: [PATCH V2 08/20] nvdimm/label: Include region label in slot validation
Posted by Neeraj Kumar 1 month ago
On 13/08/25 04:07PM, Jonathan Cameron wrote:
>On Wed, 30 Jul 2025 17:41:57 +0530
>Neeraj Kumar <s.neeraj@samsung.com> wrote:
>
>> slot validation routine validates label slot by calculating label
>
>Slot validation ... or
>The slot validation routing ...

Sure, I will fix it in next patch-set

>
>
>> checksum. It was only validating namespace label. This changeset also
>> validates region label if present.
>>
>> Also validate and calculate lsa v2.1 namespace label checksum
>
>LSA v2.1 ...

Sure, I will fix it in next patch-set

>
>
>>
>> Signed-off-by: Neeraj Kumar <s.neeraj@samsung.com>
>Otherwise LGTM
>
>Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com>

Thanks Jonathan for your review and RB tag.

Regards,
Neeraj