When debugfs is not mounted or CONFIG_DEBUG_FS is disabled, 'cxl list -v'
crashes while generating the protocol_injectable attribute for downstream
ports.

The crash occurs because cxl_dport_get_einj_path() passes a NULL debugfs
path to strlen().

Return NULL from cxl_dport_get_einj_path() when debugfs is unavailable,
and omit protocol_injectable from the JSON output, consistent with other
debugfs-dependent attributes. Update the documentation to match.

Thanks to Christopher Kim who reported and provided the fixup.

Originally-by: Christopher Kim
Fixes: 5d596866f8c5 ("cxl/list: add injectable errors in output")
Closes: https://github.com/pmem/ndctl/issues/300
Signed-off-by: Alison Schofield <[email protected]>
---
 Documentation/cxl/cxl-inject-protocol-error.txt |  5 ++---
 cxl/json.c                                      | 15 +++++++++------
 cxl/lib/libcxl.c                                |  3 +++
 3 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/Documentation/cxl/cxl-inject-protocol-error.txt 
b/Documentation/cxl/cxl-inject-protocol-error.txt
index 196b6f6af7e3..179066ff7111 100644
--- a/Documentation/cxl/cxl-inject-protocol-error.txt
+++ b/Documentation/cxl/cxl-inject-protocol-error.txt
@@ -81,9 +81,8 @@ must be enabled. For more information, view the Linux kernel 
documentation on EI
 
 This command depends on the CXL debug filesystem (normally mounted at
 "/sys/kernel/debug/cxl") to inject protocol errors. If the CXL debugfs is not
-accessible the "protocol_injectable" attribute of dports will always be
-set to false, and the "injectable_protocol_errors" attribute of CXL busses
-will always be empty.
+accessible, the "protocol_injectable" attribute of dports and the
+"injectable_protocol_errors" attribute of CXL buses are omitted.
 
 OPTIONS
 -------
diff --git a/cxl/json.c b/cxl/json.c
index a92548828582..0caa3a87b61d 100644
--- a/cxl/json.c
+++ b/cxl/json.c
@@ -746,12 +746,15 @@ void util_cxl_dports_append_json(struct json_object 
*jport,
                if (jobj)
                        json_object_object_add(jdport, "id", jobj);
 
-               einj_path = cxl_dport_get_einj_path(dport);
-               jobj = json_object_new_boolean(einj_path != NULL);
-               if (jobj)
-                       json_object_object_add(jdport, "protocol_injectable",
-                                              jobj);
-               free(einj_path);
+               if (cxl_debugfs_exists(cxl_port_get_ctx(port))) {
+                       einj_path = cxl_dport_get_einj_path(dport);
+                       jobj = json_object_new_boolean(einj_path != NULL);
+                       if (jobj)
+                               json_object_object_add(jdport,
+                                                      "protocol_injectable",
+                                                      jobj);
+                       free(einj_path);
+               }
 
                json_object_array_add(jdports, jdport);
                json_object_set_userdata(jdport, dport, NULL);
diff --git a/cxl/lib/libcxl.c b/cxl/lib/libcxl.c
index e55a7b41d77a..16348783e3de 100644
--- a/cxl/lib/libcxl.c
+++ b/cxl/lib/libcxl.c
@@ -3589,6 +3589,9 @@ CXL_EXPORT char *cxl_dport_get_einj_path(struct cxl_dport 
*dport)
        char *path;
        int rc;
 
+       if (!ctx->cxl_debugfs)
+               return NULL;
+
        path_len = strlen(ctx->cxl_debugfs) + 100;
        path = calloc(path_len, sizeof(char));
        if (!path)
-- 
2.37.3


Reply via email to