Skip to contentSkip to conversation

cxl/monitor: coverity and misc other fixes

PATCH In review Vishal Verma 5 participants 14 messages Feb 18, 2023 – Feb 22, 2023 libnvdimm: non-volatile memory device

lore
Share & tools
lore

Vishal Verma

author maintainer
#0 lore raw

cover cxl/monitor: coverity and misc other fixes

Fix a couple of issues reported by Coverity in the cxl-monitor code, and an async probe wait in the cxl=security.sh test.

---
Vishal Verma (3):
cxl/event_trace: fix a resource leak in cxl_event_to_json() cxl/monitor: retain error code in monitor_event() test/cxl-security.sh: avoid intermittent failures due to sasync probe

 cxl/event_trace.c | 3 ++-
 cxl/monitor.c     | 3 ++-
 test/security.sh  | 1 +
 3 files changed, 5 insertions(+), 2 deletions(-)

---
base-commit: a88bdcfb4202c73aadfee6f83c5502eb5121cbd9
change-id: 20230217-coverity-fixes-e1d3f8bc2cfb

Best regards,

Signature · 1 line
Vishal Verma <[email protected]>
author maintainer
#1 lore raw

PATCH 1/3 cxl/event_trace: fix a resource leak in cxl_event_to_json()

Static analysis reports that a 'return -ENOMEM' in the above function bypasses the error unwinding and leaks 'jevent'.

Fix the error handling to use the right goto sequence before returning.

Fixes 8dedc6cf5e85 ("cxl: add a helper to parse trace events into a json object")
Cc Dave Jiang <[email protected]>
Cc Dan Williams <[email protected]>
---
 cxl/event_trace.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
cxl/event_trace.c +2 -1
index a973a1f..76dd4e7 100644
@@ -142,7 +142,8 @@ static int cxl_event_to_json(struct tep_event *event, struct tep_record *record,
 				jobj = num_to_json(data, f->elementsize, f->flags);
 				if (!jobj) {
 					json_object_put(jarray);
-					return -ENOMEM;
+					rc = -ENOMEM;
+					goto err_jevent;
 				}
 				json_object_array_add(jarray, jobj);
 				data += f->elementsize;
Signature · 1 line
2.39.1
maintainer Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH ndctl 1/3] cxl/event_trace: fix a resource leak in cxl_event_to_json()

Quoted 9 lines · Vishal Verma

Static analysis reports that a 'return -ENOMEM' in the above function bypasses the error unwinding and leaks 'jevent'.

Fix the error handling to use the right goto sequence before returning.

Fixes 8dedc6cf5e85 ("cxl: add a helper to parse trace events into a json object")
Cc Dave Jiang <[email protected]>
Cc Dan Williams <[email protected]>
Quoted 19 lines
---
  cxl/event_trace.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
cxl/event_trace.c +2 -1
index a973a1f..76dd4e7 100644
@@ -142,7 +142,8 @@ static int cxl_event_to_json(struct tep_event *event, struct tep_record *record,
  				jobj = num_to_json(data, f->elementsize, f->flags);
  				if (!jobj) {
  					json_object_put(jarray);
-					return -ENOMEM;
+					rc = -ENOMEM;
+					goto err_jevent;
  				}
  				json_object_array_add(jarray, jobj);
  				data += f->elementsize;
Reviewed-by on v1 1/3
#3 lore raw

Re: [PATCH ndctl 1/3] cxl/event_trace: fix a resource leak in cxl_event_to_json()

In reply to Vishal Verma
Quoted 8 lines · Vishal Verma

Static analysis reports that a 'return -ENOMEM' in the above function bypasses the error unwinding and leaks 'jevent'.

Fix the error handling to use the right goto sequence before returning.

Fixes 8dedc6cf5e85 ("cxl: add a helper to parse trace events into a json object")
Cc Dave Jiang <[email protected]>
Cc Dan Williams <[email protected]>
Quoted 24 lines
---
 cxl/event_trace.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
cxl/event_trace.c +2 -1
index a973a1f..76dd4e7 100644
@@ -142,7 +142,8 @@ static int cxl_event_to_json(struct tep_event *event, struct tep_record *record,
 				jobj = num_to_json(data, f->elementsize, f->flags);
 				if (!jobj) {
 					json_object_put(jarray);
-					return -ENOMEM;
+					rc = -ENOMEM;
+					goto err_jevent;
 				}
 				json_object_array_add(jarray, jobj);
 				data += f->elementsize;
Signature · 2 lines
-- 
2.39.1
author maintainer
#4 lore raw

PATCH 2/3 cxl/monitor: retain error code in monitor_event()

In reply to Vishal Verma

Static analysis reports that the error unwinding path in monitor_event() overwrites 'rc' with the return from cxl_event_tracing_disable(). This masks the actual error code from either epoll_wait() or cxl_parse_events() which is the one that should be propagated.

Print a spot error in case there's an error while disabling tracing, but otherwise retain the rc from the main body of the function.

Fixes 299f69f974a6 ("cxl/monitor: add a new monitor command for CXL trace events")
Cc Dave Jiang <[email protected]>
Cc Dan Williams <[email protected]>
---
 cxl/monitor.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
cxl/monitor.c +2 -1
index 31e6f98..749f472 100644
@@ -130,7 +130,8 @@ static int monitor_event(struct cxl_ctx *ctx)
 	}
 
 parse_err:
-	rc = cxl_event_tracing_disable(inst);
+	if (cxl_event_tracing_disable(inst) < 0)
+		err(&monitor, "failed to disable tracing\n");
 event_en_err:
 epoll_ctl_err:
 	close(fd);
Signature · 1 line
2.39.1
maintainer Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH ndctl 2/3] cxl/monitor: retain error code in monitor_event()

Quoted 5 lines · Vishal Verma

Static analysis reports that the error unwinding path in monitor_event() overwrites 'rc' with the return from cxl_event_tracing_disable(). This masks the actual error code from either epoll_wait() or cxl_parse_events() which is the one that should be propagated.

Quoted 7 lines

Print a spot error in case there's an error while disabling tracing, but otherwise retain the rc from the main body of the function.

Fixes 299f69f974a6 ("cxl/monitor: add a new monitor command for CXL trace events")
Cc Dave Jiang <[email protected]>
Cc Dan Williams <[email protected]>
Quoted 19 lines
---
  cxl/monitor.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
cxl/monitor.c +2 -1
index 31e6f98..749f472 100644
@@ -130,7 +130,8 @@ static int monitor_event(struct cxl_ctx *ctx)
  	}
 
  parse_err:
-	rc = cxl_event_tracing_disable(inst);
+	if (cxl_event_tracing_disable(inst) < 0)
+		err(&monitor, "failed to disable tracing\n");
  event_en_err:
  epoll_ctl_err:
  	close(fd);
Reviewed-by on v1 2/3
#6 lore raw

Re: [PATCH ndctl 2/3] cxl/monitor: retain error code in monitor_event()

In reply to Vishal Verma
Quoted 21 lines · Vishal Verma

Static analysis reports that the error unwinding path in monitor_event() overwrites 'rc' with the return from cxl_event_tracing_disable(). This masks the actual error code from either epoll_wait() or cxl_parse_events() which is the one that should be propagated.

Print a spot error in case there's an error while disabling tracing, but otherwise retain the rc from the main body of the function.

Fixes 299f69f974a6 ("cxl/monitor: add a new monitor command for CXL trace events")
Cc Dave Jiang <[email protected]>
Cc Dan Williams <[email protected]>
---
 cxl/monitor.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
cxl/monitor.c +0 -0
index 31e6f98..749f472 100644

cxl/monitor.c static int monitor_event(struct cxl_ctx *ctx) · line 134

Quoted 6 lines
cxl/monitor.c +2 -1
@@ -130,7 +130,8 @@ static int monitor_event(struct cxl_ctx *ctx)
 	}
 
 parse_err:
-	rc = cxl_event_tracing_disable(inst);
+	if (cxl_event_tracing_disable(inst) < 0)
+		err(&monitor, "failed to disable tracing\n");

Is this even worth printing? Perhaps just make
cxl_event_tracing_disable() return void?

Either way:

Quoted 8 lines
cxl/monitor.c +0 -0
 event_en_err:
 epoll_ctl_err:
 	close(fd);
Signature · 2 lines
-- 
2.39.1
author maintainer
#7 lore raw

Re: [PATCH ndctl 2/3] cxl/monitor: retain error code in monitor_event()

Quoted 17 lines · Ira Weiny
Quoted 16 lines · Vishal Verma

Static analysis reports that the error unwinding path in monitor_event() overwrites 'rc' with the return from cxl_event_tracing_disable(). This masks the actual error code from either epoll_wait() or cxl_parse_events() which is the one that should be propagated.

Print a spot error in case there's an error while disabling tracing, but otherwise retain the rc from the main body of the function.

Fixes 299f69f974a6 ("cxl/monitor: add a new monitor command for CXL trace events")
Cc Dave Jiang <[email protected]>
Cc Dan Williams <[email protected]>

---
 cxl/monitor.c | 3 ++-

 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 14 lines
Quoted 11 lines
cxl/monitor.c +0 -0
index 31e6f98..749f472 100644
@@ -130,7 +130,8 @@ static int monitor_event(struct cxl_ctx *ctx)
         }


 parse_err:
-       rc = cxl_event_tracing_disable(inst);
+       if (cxl_event_tracing_disable(inst) < 0)
+               err(&monitor, "failed to disable tracing\n");

Is this even worth printing?  Perhaps just make
cxl_event_tracing_disable() return void?

I thought about it, but the underlying tracefs_trace_off() returns an int, which is probably why cxl_event_tracing_disable() does too. Having the print satisfies static analyzers that we're checking the return value - other than that I agree it doesn't add much.

Quoted 4 lines

Either way:

Thanks Ira!

Quoted 11 lines
Quoted 9 lines

 event_en_err:
 epoll_ctl_err:
        close(fd);

Signature · 1 line
2.39.1
author maintainer
#8 lore raw

PATCH 3/3 test/cxl-security.sh: avoid intermittent failures due to sasync probe

In reply to Vishal Verma

This test failed intermittently because the ndctl-list operation right after a 'modprobe cxl_test' could race the actual nmem devices getting loaded.

Since CXL device probes are asynchronous, and cxl_acpi might've kicked off a cxl_bus_rescan(), a cxl_flush() (via cxl_wait_probe()) can ensure everything is loaded.

Add a plain cxl-list right after the modprobe to allow for a flush/wait cycle.

---
 test/security.sh | 1 +
 1 file changed, 1 insertion(+)
test/security.sh +1 -0
index 04f630e..fb04aa6 100755
@@ -225,6 +225,7 @@ if [ "$uid" -ne 0 ]; then
 fi
 
 modprobe "$KMOD_TEST"
+cxl list
 setup
 check_prereq "keyctl"
 rc=1
Signature · 1 line
2.39.1
maintainer Reviewed-by on v1 3/3
#9 lore raw

Re: [PATCH ndctl 3/3] test/cxl-security.sh: avoid intermittent failures due to sasync probe

Quoted 8 lines · Vishal Verma

This test failed intermittently because the ndctl-list operation right after a 'modprobe cxl_test' could race the actual nmem devices getting loaded.

Since CXL device probes are asynchronous, and cxl_acpi might've kicked off a cxl_bus_rescan(), a cxl_flush() (via cxl_wait_probe()) can ensure everything is loaded.

Quoted 6 lines

Add a plain cxl-list right after the modprobe to allow for a flush/wait cycle.

Quoted 17 lines
---
  test/security.sh | 1 +
  1 file changed, 1 insertion(+)
test/security.sh +1 -0
index 04f630e..fb04aa6 100755
@@ -225,6 +225,7 @@ if [ "$uid" -ne 0 ]; then
  fi
 
  modprobe "$KMOD_TEST"
+cxl list
  setup
  check_prereq "keyctl"
  rc=1
maintainer
#10 lore raw

Re: [PATCH ndctl 3/3] test/cxl-security.sh: avoid intermittent failures due to sasync probe

In reply to Vishal Verma
Quoted 10 lines · Vishal Verma

This test failed intermittently because the ndctl-list operation right after a 'modprobe cxl_test' could race the actual nmem devices getting loaded.

Since CXL device probes are asynchronous, and cxl_acpi might've kicked off a cxl_bus_rescan(), a cxl_flush() (via cxl_wait_probe()) can ensure everything is loaded.

Add a plain cxl-list right after the modprobe to allow for a flush/wait cycle.

Is this the preferred method to 'settle', instead of udevadm settle?

Quoted 25 lines
---
 test/security.sh | 1 +
 1 file changed, 1 insertion(+)
test/security.sh +1 -0
index 04f630e..fb04aa6 100755
@@ -225,6 +225,7 @@ if [ "$uid" -ne 0 ]; then
 fi
 
 modprobe "$KMOD_TEST"
+cxl list
 setup
 check_prereq "keyctl"
 rc=1
Signature · 2 lines
-- 
2.39.1
#11 lore raw

Re: [PATCH ndctl 3/3] test/cxl-security.sh: avoid intermittent failures due to sasync probe

Quoted 5 lines · Alison Schofield
Quoted 4 lines · Vishal Verma

This test failed intermittently because the ndctl-list operation right after a 'modprobe cxl_test' could race the actual nmem devices getting loaded.

Quoted 8 lines
Quoted 6 lines

Since CXL device probes are asynchronous, and cxl_acpi might've kicked off a cxl_bus_rescan(), a cxl_flush() (via cxl_wait_probe()) can ensure everything is loaded.

Add a plain cxl-list right after the modprobe to allow for a flush/wait cycle.

Is this the preferred method to 'settle', instead of udevadm settle?

In general, 'udevadm settle' is only the first phase of flushing subsystem setup work in that it can only flush the udev event queue. I.e. a device arriving kicks off a KOBJ_ADD event, and once that event is processed without kicking off more KOBJ_* events the queue is settled. For CXL this means that the cxl_acpi and cxl_pci modules are loaded, but the asynchronous work they kick off to probe devices and rescan the bus is invisible to 'udevadm settle'.

Internally 'cxl list' is performing a 'udevadm settle' and then flushing the follow on async work.

author maintainer
#12 lore raw

Re: [PATCH ndctl 3/3] test/cxl-security.sh: avoid intermittent failures due to sasync probe

In reply to Alison Schofield
Quoted 5 lines · Alison Schofield
Quoted 4 lines · Vishal Verma

This test failed intermittently because the ndctl-list operation right after a 'modprobe cxl_test' could race the actual nmem devices getting loaded.

Quoted 8 lines
Quoted 6 lines

Since CXL device probes are asynchronous, and cxl_acpi might've kicked off a cxl_bus_rescan(), a cxl_flush() (via cxl_wait_probe()) can ensure everything is loaded.

Add a plain cxl-list right after the modprobe to allow for a flush/wait cycle.

Is this the preferred method to 'settle', instead of udevadm settle?

Generally, no. Usually cxl tests would use cxl-cli commands, which now have the necessary waits via cxl_wait_probe(), so even a 'udevadm settle' shouldn't be needed.

In this case, the first thing we run is ndctl list, which waits for nvdimm things to 'settle', but we were racing with cxl_test coming up, which it (ndctl) knows nothing about.

Quoted 26 lines
Quoted 26 lines

---
 test/security.sh | 1 +

 1 file changed, 1 insertion(+)
test/security.sh +0 -0
index 04f630e..fb04aa6 100755
@@ -225,6 +225,7 @@ if [ "$uid" -ne 0 ]; then

 fi

 modprobe "$KMOD_TEST"
+cxl list
 setup
 check_prereq "keyctl"
 rc=1

Signature · 1 line
2.39.1
maintainer
#13 lore raw

Re: [PATCH ndctl 3/3] test/cxl-security.sh: avoid intermittent failures due to sasync probe

Quoted 15 lines · Vishal Verma
Quoted 5 lines · Alison Schofield
Quoted 4 lines · Vishal Verma

This test failed intermittently because the ndctl-list operation right after a 'modprobe cxl_test' could race the actual nmem devices getting loaded.

Quoted 8 lines
Quoted 6 lines

Since CXL device probes are asynchronous, and cxl_acpi might've kicked off a cxl_bus_rescan(), a cxl_flush() (via cxl_wait_probe()) can ensure everything is loaded.

Add a plain cxl-list right after the modprobe to allow for a flush/wait cycle.

Is this the preferred method to 'settle', instead of udevadm settle?

Quoted 7 lines

Generally, no. Usually cxl tests would use cxl-cli commands, which now have the necessary waits via cxl_wait_probe(), so even a 'udevadm settle' shouldn't be needed.

In this case, the first thing we run is ndctl list, which waits for nvdimm things to 'settle', but we were racing with cxl_test coming up, which it (ndctl) knows nothing about.

OK - I'll stop doing the udevadm settle, since what I'm doing is pure 'cxl' and modprobe is always followed by cxl-cli commands.

Quoted 28 lines
Quoted 27 lines
Quoted 27 lines
---
 test/security.sh | 1 +
 1 file changed, 1 insertion(+)
test/security.sh +1 -1
index 04f630e..fb04aa6 100755
@@ -225,6 +225,7 @@ if [ "$uid" -ne 0 ]; then
 fi
 
 modprobe "$KMOD_TEST"
+cxl list
 setup
 check_prereq "keyctl"
 rc=1
 
--

2.39.1