anitazha / rpms / ndctl

Forked from rpms/ndctl 2 years ago
Clone

Blame 0129-ndctl-libndctl-Update-nvdimm-flags-after-smart-injec.patch

Jeff Moyer 2c91dc
From c5ccbf29c54b5a3d9cb1c138c06c8d5ac3ee80c2 Mon Sep 17 00:00:00 2001
Jeff Moyer 2c91dc
From: Vaibhav Jain <vaibhav@linux.ibm.com>
Jeff Moyer 2c91dc
Date: Tue, 22 Feb 2022 17:45:19 +0530
Jeff Moyer 2c91dc
Subject: [PATCH 129/217] ndctl,libndctl: Update nvdimm flags after
Jeff Moyer 2c91dc
 smart-inject
Jeff Moyer 2c91dc
Jeff Moyer 2c91dc
Presently after performing an inject-smart command the nvdimm flags reported are
Jeff Moyer 2c91dc
out of date as shown below where no 'smart_notify' or 'flush_fail' flags were
Jeff Moyer 2c91dc
reported even though they are set after injecting the smart error:
Jeff Moyer 2c91dc
Jeff Moyer 2c91dc
$ sudo ndctl inject-smart -fU nmem0
Jeff Moyer 2c91dc
[
Jeff Moyer 2c91dc
  {
Jeff Moyer 2c91dc
    "dev":"nmem0",
Jeff Moyer 2c91dc
    "health":{
Jeff Moyer 2c91dc
      "health_state":"fatal",
Jeff Moyer 2c91dc
      "shutdown_state":"dirty",
Jeff Moyer 2c91dc
      "shutdown_count":0
Jeff Moyer 2c91dc
    }
Jeff Moyer 2c91dc
  }
Jeff Moyer 2c91dc
]
Jeff Moyer 2c91dc
$ sudo cat /sys/class/nd/ndctl0/device/nmem0/papr/flags
Jeff Moyer 2c91dc
flush_fail smart_notify
Jeff Moyer 2c91dc
Jeff Moyer 2c91dc
This happens because nvdimm flags are only parsed once during its probe
Jeff Moyer 2c91dc
and not refreshed even after a inject-smart operation makes them out of
Jeff Moyer 2c91dc
date. To fix this the patch forces an update of nvdimm flags via newly
Jeff Moyer 2c91dc
introduced export from libndctl named ndctl_dimm_refresh_flags() thats called
Jeff Moyer 2c91dc
from dimm_inject_smart() after inject-smart command is successfully
Jeff Moyer 2c91dc
submitted. This ensures that correct nvdimm flags are displayed later in that
Jeff Moyer 2c91dc
function. With this implemented correct nvdimm flags are reported after a
Jeff Moyer 2c91dc
inject-smart operation:
Jeff Moyer 2c91dc
Jeff Moyer 2c91dc
$ sudo ndctl inject-smart -fU nmem0
Jeff Moyer 2c91dc
[
Jeff Moyer 2c91dc
  {
Jeff Moyer 2c91dc
    "dev":"nmem0",
Jeff Moyer 2c91dc
    "flag_failed_flush":true,
Jeff Moyer 2c91dc
    "flag_smart_event":true,
Jeff Moyer 2c91dc
    "health":{
Jeff Moyer 2c91dc
      "health_state":"fatal",
Jeff Moyer 2c91dc
      "shutdown_state":"dirty",
Jeff Moyer 2c91dc
      "shutdown_count":0
Jeff Moyer 2c91dc
    }
Jeff Moyer 2c91dc
  }
Jeff Moyer 2c91dc
]
Jeff Moyer 2c91dc
Jeff Moyer 2c91dc
The patch refactors populate_dimm_attributes() to move the nvdimm flags
Jeff Moyer 2c91dc
parsing code to the newly introduced ndctl_dimm_refresh_flags()
Jeff Moyer 2c91dc
export. Since reading nvdimm flags requires constructing path using
Jeff Moyer 2c91dc
'bus_prefix' which is only available during add_dimm(), the patch
Jeff Moyer 2c91dc
introduces a new member 'struct ndctl_dimm.bus_prefix' to cache its
Jeff Moyer 2c91dc
value. During ndctl_dimm_refresh_flags() the cached bus_prefix is used to
Jeff Moyer 2c91dc
read the contents of the nvdimm flag file and pass it on to the appropriate
Jeff Moyer 2c91dc
flag parsing function. Finally dimm_inject_smart() is updated to issue call to
Jeff Moyer 2c91dc
ndctl_dimm_refresh_flags() before generating json output of the nvdimm status
Jeff Moyer 2c91dc
Jeff Moyer 2c91dc
Link: https://lore.kernel.org/r/20220222121519.1674117-1-vaibhav@linux.ibm.com
Jeff Moyer 2c91dc
Signed-off-by: Vaibhav Jain <vaibhav@linux.ibm.com>
Jeff Moyer 2c91dc
Signed-off-by: Shivaprasad G Bhat <sbhat@linux.ibm.com>
Jeff Moyer 2c91dc
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
Jeff Moyer 2c91dc
---
Jeff Moyer 2c91dc
 ndctl/inject-smart.c   |  4 +++
Jeff Moyer 2c91dc
 ndctl/lib/libndctl.c   | 55 +++++++++++++++++++++++++++++++-----------
Jeff Moyer 2c91dc
 ndctl/lib/libndctl.sym |  4 +++
Jeff Moyer 2c91dc
 ndctl/lib/private.h    |  1 +
Jeff Moyer 2c91dc
 ndctl/libndctl.h       |  1 +
Jeff Moyer 2c91dc
 5 files changed, 51 insertions(+), 14 deletions(-)
Jeff Moyer 2c91dc
Jeff Moyer 2c91dc
diff -up ndctl-71.1/ndctl/inject-smart.c.orig ndctl-71.1/ndctl/inject-smart.c
Jeff Moyer 2c91dc
--- ndctl-71.1/ndctl/inject-smart.c.orig	2022-10-07 16:40:24.610615979 -0400
Jeff Moyer 2c91dc
+++ ndctl-71.1/ndctl/inject-smart.c	2022-10-07 16:40:35.031651459 -0400
Jeff Moyer 2c91dc
@@ -467,6 +467,10 @@ static int dimm_inject_smart(struct ndct
Jeff Moyer 2c91dc
 		jdimms = json_object_new_array();
Jeff Moyer 2c91dc
 		if (!jdimms)
Jeff Moyer 2c91dc
 			goto out;
Jeff Moyer 2c91dc
+
Jeff Moyer 2c91dc
+		/* Ensure the dimm flags are upto date before reporting them */
Jeff Moyer 2c91dc
+		ndctl_dimm_refresh_flags(dimm);
Jeff Moyer 2c91dc
+
Jeff Moyer 2c91dc
 		jdimm = util_dimm_to_json(dimm, sctx.flags);
Jeff Moyer 2c91dc
 		if (!jdimm)
Jeff Moyer 2c91dc
 			goto out;
Jeff Moyer 2c91dc
diff -up ndctl-71.1/ndctl/lib/libndctl.c.orig ndctl-71.1/ndctl/lib/libndctl.c
Jeff Moyer 2c91dc
--- ndctl-71.1/ndctl/lib/libndctl.c.orig	2022-10-07 16:40:24.608615972 -0400
Jeff Moyer 2c91dc
+++ ndctl-71.1/ndctl/lib/libndctl.c	2022-10-07 16:40:35.032651462 -0400
Jeff Moyer 2c91dc
@@ -588,6 +588,7 @@ static void free_dimm(struct ndctl_dimm
Jeff Moyer 2c91dc
 	free(dimm->unique_id);
Jeff Moyer 2c91dc
 	free(dimm->dimm_buf);
Jeff Moyer 2c91dc
 	free(dimm->dimm_path);
Jeff Moyer 2c91dc
+	free(dimm->bus_prefix);
Jeff Moyer 2c91dc
 	if (dimm->module)
Jeff Moyer 2c91dc
 		kmod_module_unref(dimm->module);
Jeff Moyer 2c91dc
 	if (dimm->health_eventfd > -1)
Jeff Moyer 2c91dc
@@ -1645,14 +1646,34 @@ static enum ndctl_fwa_result fwa_result_
Jeff Moyer 2c91dc
 	return NDCTL_FWA_RESULT_INVALID;
Jeff Moyer 2c91dc
 }
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
+NDCTL_EXPORT void ndctl_dimm_refresh_flags(struct ndctl_dimm *dimm)
Jeff Moyer 2c91dc
+{
Jeff Moyer 2c91dc
+	struct ndctl_ctx *ctx = dimm->bus->ctx;
Jeff Moyer 2c91dc
+	char *path = dimm->dimm_buf;
Jeff Moyer 2c91dc
+	char buf[SYSFS_ATTR_SIZE];
Jeff Moyer 2c91dc
+
Jeff Moyer 2c91dc
+	/* Construct path to dimm flags sysfs file */
Jeff Moyer 2c91dc
+	sprintf(path, "%s/%s/flags", dimm->dimm_path, dimm->bus_prefix);
Jeff Moyer 2c91dc
+
Jeff Moyer 2c91dc
+	if (sysfs_read_attr(ctx, path, buf) < 0)
Jeff Moyer 2c91dc
+		return;
Jeff Moyer 2c91dc
+
Jeff Moyer 2c91dc
+	/* Reset the flags */
Jeff Moyer 2c91dc
+	dimm->flags.flags = 0;
Jeff Moyer 2c91dc
+	if (ndctl_bus_has_nfit(dimm->bus))
Jeff Moyer 2c91dc
+		parse_nfit_mem_flags(dimm, buf);
Jeff Moyer 2c91dc
+	else if (ndctl_bus_is_papr_scm(dimm->bus))
Jeff Moyer 2c91dc
+		parse_papr_flags(dimm, buf);
Jeff Moyer 2c91dc
+}
Jeff Moyer 2c91dc
+
Jeff Moyer 2c91dc
 static int populate_dimm_attributes(struct ndctl_dimm *dimm,
Jeff Moyer 2c91dc
-				    const char *dimm_base,
Jeff Moyer 2c91dc
-				    const char *bus_prefix)
Jeff Moyer 2c91dc
+				    const char *dimm_base)
Jeff Moyer 2c91dc
 {
Jeff Moyer 2c91dc
 	int i, rc = -1;
Jeff Moyer 2c91dc
 	char buf[SYSFS_ATTR_SIZE];
Jeff Moyer 2c91dc
 	struct ndctl_ctx *ctx = dimm->bus->ctx;
Jeff Moyer 2c91dc
 	char *path = calloc(1, strlen(dimm_base) + 100);
Jeff Moyer 2c91dc
+	const char *bus_prefix = dimm->bus_prefix;
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
 	if (!path)
Jeff Moyer 2c91dc
 		return -ENOMEM;
Jeff Moyer 2c91dc
@@ -1736,16 +1757,10 @@ static int populate_dimm_attributes(stru
Jeff Moyer 2c91dc
 	}
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
 	sprintf(path, "%s/%s/flags", dimm_base, bus_prefix);
Jeff Moyer 2c91dc
-	if (sysfs_read_attr(ctx, path, buf) == 0) {
Jeff Moyer 2c91dc
-		if (ndctl_bus_has_nfit(dimm->bus))
Jeff Moyer 2c91dc
-			parse_nfit_mem_flags(dimm, buf);
Jeff Moyer 2c91dc
-		else if (ndctl_bus_is_papr_scm(dimm->bus)) {
Jeff Moyer 2c91dc
-			dimm->cmd_family = NVDIMM_FAMILY_PAPR;
Jeff Moyer 2c91dc
-			parse_papr_flags(dimm, buf);
Jeff Moyer 2c91dc
-		}
Jeff Moyer 2c91dc
-	}
Jeff Moyer 2c91dc
-
Jeff Moyer 2c91dc
 	dimm->health_eventfd = open(path, O_RDONLY|O_CLOEXEC);
Jeff Moyer 2c91dc
+
Jeff Moyer 2c91dc
+	ndctl_dimm_refresh_flags(dimm);
Jeff Moyer 2c91dc
+
Jeff Moyer 2c91dc
 	rc = 0;
Jeff Moyer 2c91dc
  err_read:
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
@@ -1801,8 +1816,9 @@ static int add_papr_dimm(struct ndctl_di
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
 		rc = 0;
Jeff Moyer 2c91dc
 	} else if (strcmp(buf, "nvdimm_test") == 0) {
Jeff Moyer 2c91dc
+		dimm->cmd_family = NVDIMM_FAMILY_PAPR;
Jeff Moyer 2c91dc
 		/* probe via common populate_dimm_attributes() */
Jeff Moyer 2c91dc
-		rc = populate_dimm_attributes(dimm, dimm_base, "papr");
Jeff Moyer 2c91dc
+		rc = populate_dimm_attributes(dimm, dimm_base);
Jeff Moyer 2c91dc
 	}
Jeff Moyer 2c91dc
 out:
Jeff Moyer 2c91dc
 	free(path);
Jeff Moyer 2c91dc
@@ -1899,9 +1915,20 @@ static void *add_dimm(void *parent, int
Jeff Moyer 2c91dc
 	dimm->formats = formats;
Jeff Moyer 2c91dc
 	/* Check if the given dimm supports nfit */
Jeff Moyer 2c91dc
 	if (ndctl_bus_has_nfit(bus)) {
Jeff Moyer 2c91dc
-		rc = populate_dimm_attributes(dimm, dimm_base, "nfit");
Jeff Moyer 2c91dc
+		dimm->bus_prefix = strdup("nfit");
Jeff Moyer 2c91dc
+		if (!dimm->bus_prefix) {
Jeff Moyer 2c91dc
+			rc = -ENOMEM;
Jeff Moyer 2c91dc
+			goto out;
Jeff Moyer 2c91dc
+		}
Jeff Moyer 2c91dc
+		rc =  populate_dimm_attributes(dimm, dimm_base);
Jeff Moyer 2c91dc
+
Jeff Moyer 2c91dc
 	} else if (ndctl_bus_has_of_node(bus)) {
Jeff Moyer 2c91dc
-		rc = add_papr_dimm(dimm, dimm_base);
Jeff Moyer 2c91dc
+		dimm->bus_prefix = strdup("papr");
Jeff Moyer 2c91dc
+		if (!dimm->bus_prefix) {
Jeff Moyer 2c91dc
+			rc = -ENOMEM;
Jeff Moyer 2c91dc
+			goto out;
Jeff Moyer 2c91dc
+		}
Jeff Moyer 2c91dc
+		rc =  add_papr_dimm(dimm, dimm_base);
Jeff Moyer 2c91dc
 	}
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
 	if (rc == -ENODEV) {
Jeff Moyer 2c91dc
diff -up ndctl-71.1/ndctl/lib/libndctl.sym.orig ndctl-71.1/ndctl/lib/libndctl.sym
Jeff Moyer 2c91dc
--- ndctl-71.1/ndctl/lib/libndctl.sym.orig	2022-10-07 16:40:24.344615073 -0400
Jeff Moyer 2c91dc
+++ ndctl-71.1/ndctl/lib/libndctl.sym	2022-10-07 16:40:35.032651462 -0400
Jeff Moyer 2c91dc
@@ -456,3 +456,7 @@ LIBNDCTL_26 {
Jeff Moyer 2c91dc
 	ndctl_bus_nfit_translate_spa;
Jeff Moyer 2c91dc
 	ndctl_dimm_sizeof_namespace_index;
Jeff Moyer 2c91dc
 } LIBNDCTL_25;
Jeff Moyer 2c91dc
+
Jeff Moyer 2c91dc
+LIBNDCTL_27 {
Jeff Moyer 2c91dc
+	ndctl_dimm_refresh_flags;
Jeff Moyer 2c91dc
+} LIBNDCTL_26;
Jeff Moyer 2c91dc
diff -up ndctl-71.1/ndctl/lib/private.h.orig ndctl-71.1/ndctl/lib/private.h
Jeff Moyer 2c91dc
--- ndctl-71.1/ndctl/lib/private.h.orig	2022-10-07 16:40:24.455615451 -0400
Jeff Moyer 2c91dc
+++ ndctl-71.1/ndctl/lib/private.h	2022-10-07 16:40:35.032651462 -0400
Jeff Moyer 2c91dc
@@ -75,6 +75,7 @@ struct ndctl_dimm {
Jeff Moyer 2c91dc
 	char *unique_id;
Jeff Moyer 2c91dc
 	char *dimm_path;
Jeff Moyer 2c91dc
 	char *dimm_buf;
Jeff Moyer 2c91dc
+	char *bus_prefix;
Jeff Moyer 2c91dc
 	int health_eventfd;
Jeff Moyer 2c91dc
 	int buf_len;
Jeff Moyer 2c91dc
 	int id;
Jeff Moyer 2c91dc
diff -up ndctl-71.1/ndctl/libndctl.h.orig ndctl-71.1/ndctl/libndctl.h
Jeff Moyer 2c91dc
--- ndctl-71.1/ndctl/libndctl.h.orig	2022-10-07 16:40:24.611615982 -0400
Jeff Moyer 2c91dc
+++ ndctl-71.1/ndctl/libndctl.h	2022-10-07 16:40:35.033651466 -0400
Jeff Moyer 2c91dc
@@ -221,6 +221,7 @@ int ndctl_dimm_is_active(struct ndctl_di
Jeff Moyer 2c91dc
 int ndctl_dimm_is_enabled(struct ndctl_dimm *dimm);
Jeff Moyer 2c91dc
 int ndctl_dimm_disable(struct ndctl_dimm *dimm);
Jeff Moyer 2c91dc
 int ndctl_dimm_enable(struct ndctl_dimm *dimm);
Jeff Moyer 2c91dc
+void ndctl_dimm_refresh_flags(struct ndctl_dimm *dimm);
Jeff Moyer 2c91dc
 
Jeff Moyer 2c91dc
 struct ndctl_cmd;
Jeff Moyer 2c91dc
 struct ndctl_cmd *ndctl_bus_cmd_new_ars_cap(struct ndctl_bus *bus,