Skip to contentSkip to conversation

Fix accessors for temperature field when it is negative

PATCH In review Jehoon Park 3 participants 9 messages Aug 7, 2023 – Aug 25, 2023 1 message not in the archive libnvdimm: non-volatile memory device

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CGME20230807063513epcas2p261ba4dfbfff34e99077596128eb6fc48@epcas2p2.samsung.com

#1 lore raw

v2 cover Fix accessors for temperature field when it is negative

In CXL 3.0 SPEC, 8.2.9.8.3.1 and 8.2.9.8.3.2 define temperature fields as a 2's complement value. However, they are retrieved by the same accessor for unsigned value. This causes inaccuracy when the value is negative.

The first patch updates the pre-defined value for temperature field of the Get Health Info command when it is not implemented by complying CXL 3.0 specification. (CXL 3.0 8.2.9.8.3.1)

The second patch fixes accessors for temperature fields.
Add a new payload accessor for a signed value, then use it for retrieving temperature properly. INT_MAX is used to indicate errors because negative errno codes are not distinguishable from the retrieved values when they are negative. Caller should check errno to know what kind of error occurs.

The third patch fixes the checking value when listing device's health info.

Changes in v2:
- Rebase on the latest pending branch
- Remove dbg() messages in libcxl accessors (Vishal)
- Make signed value accessors to return INT_MAX when error occurs and set errno as proper errno codes (Vishal)
- Use proper value for checking "life_used" and "device_temperature" fields are implemented
- Link to v1: https://lore.kernel.org/r/[email protected]/

Jehoon Park (3):
libcxl: Update a revision by CXL 3.0 specification
libcxl: Fix accessors for temperature field to support negative value
cxl: Fix the checking value when listing device's health info

 cxl/json.c        |  9 +++++----
 cxl/lib/libcxl.c  | 32 +++++++++++++++++++++-----------
 cxl/lib/private.h |  2 +-
 3 files changed, 27 insertions(+), 16 deletions(-)

base-commit: a871e6153b11fe63780b37cdcb1eb347b296095c

Signature · 1 line
2.17.1
#2 lore raw

PATCH v2 1/3 libcxl: Update a revision by CXL 3.0 specification

Update the predefined value for device temperature field when it is not implemented. (CXL 3.0.8.2.9.8.3.1)

---
 cxl/lib/private.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cxl/lib/private.h +1 -1
index a641727..a692fd5 100644
@@ -360,7 +360,7 @@ struct cxl_cmd_set_partition {
 #define CXL_CMD_HEALTH_INFO_EXT_CORRECTED_PERSISTENT_WARNING		(1)
 
 #define CXL_CMD_HEALTH_INFO_LIFE_USED_NOT_IMPL				0xff
-#define CXL_CMD_HEALTH_INFO_TEMPERATURE_NOT_IMPL			0xffff
+#define CXL_CMD_HEALTH_INFO_TEMPERATURE_NOT_IMPL			0x7fff
 
 static inline int check_kmod(struct kmod_ctx *kmod_ctx)
 {
Signature · 1 line
2.17.1
Reviewed-by on v2 1/3
#3 lore raw

Re: [ndctl PATCH v2 1/3] libcxl: Update a revision by CXL 3.0 specification

Quoted 4 lines · On Mon, 7 Aug 2023 15:35:47 +0900 Jehoon Park <[email protected]> wrote:

Update the predefined value for device temperature field when it is not implemented. (CXL 3.0.8.2.9.8.3.1)

Hi Jehoon,

Key here is not that it was in 3.0, but that it was changed in 2.0 Errata F38 and as such software doesn't need to cope with the old (wrong) value.

Good to state that clearly in the patch description. If it had been merely a change for 3.0 there would have needed to be an enable bit to change the default behavior (or something like that).

Otherwise LGTM

Quoted 17 lines
---
 cxl/lib/private.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cxl/lib/private.h +1 -1
index a641727..a692fd5 100644
@@ -360,7 +360,7 @@ struct cxl_cmd_set_partition {
 #define CXL_CMD_HEALTH_INFO_EXT_CORRECTED_PERSISTENT_WARNING		(1)
 
 #define CXL_CMD_HEALTH_INFO_LIFE_USED_NOT_IMPL				0xff
-#define CXL_CMD_HEALTH_INFO_TEMPERATURE_NOT_IMPL			0xffff
+#define CXL_CMD_HEALTH_INFO_TEMPERATURE_NOT_IMPL			0x7fff
 
 static inline int check_kmod(struct kmod_ctx *kmod_ctx)
 {
#4 lore raw

Re: [ndctl PATCH v2 1/3] libcxl: Update a revision by CXL 3.0 specification

Quoted 12 lines · Jonathan Cameron
Quoted 4 lines · On Mon, 7 Aug 2023 15:35:47 +0900 Jehoon Park <[email protected]> wrote:

Update the predefined value for device temperature field when it is not implemented. (CXL 3.0.8.2.9.8.3.1)

Hi Jehoon,

Key here is not that it was in 3.0, but that it was changed in 2.0 Errata F38 and as such software doesn't need to cope with the old (wrong) value.

Quoted 7 lines

Good to state that clearly in the patch description. If it had been merely a change for 3.0 there would have needed to be an enable bit to change the default behavior (or something like that).

Otherwise LGTM

Hi Jonathan, thanks for the reviews.

I will correct the revision history in the next patch.

Quoted 18 lines
Quoted 17 lines
---
 cxl/lib/private.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cxl/lib/private.h +1 -1
index a641727..a692fd5 100644
@@ -360,7 +360,7 @@ struct cxl_cmd_set_partition {
 #define CXL_CMD_HEALTH_INFO_EXT_CORRECTED_PERSISTENT_WARNING		(1)
 
 #define CXL_CMD_HEALTH_INFO_LIFE_USED_NOT_IMPL				0xff
-#define CXL_CMD_HEALTH_INFO_TEMPERATURE_NOT_IMPL			0xffff
+#define CXL_CMD_HEALTH_INFO_TEMPERATURE_NOT_IMPL			0x7fff
 
 static inline int check_kmod(struct kmod_ctx *kmod_ctx)
 {
Reviewed-by on v2 1/3
#5 lore raw

Re: [ndctl PATCH v2 1/3] libcxl: Update a revision by CXL 3.0 specification

In reply to Jehoon Park
Quoted 4 lines · Jehoon Park

Update the predefined value for device temperature field when it is not implemented. (CXL 3.0.8.2.9.8.3.1)

With Jonathan's feedback in the changelog,

Quoted 20 lines

---
cxl/lib/private.h | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
cxl/lib/private.h +1 -2
index a641727..a692fd5 100644
@@ -360,7 +360,7 @@ struct cxl_cmd_set_partition {
 #define CXL_CMD_HEALTH_INFO_EXT_CORRECTED_PERSISTENT_WARNING		(1)
 
 #define CXL_CMD_HEALTH_INFO_LIFE_USED_NOT_IMPL				0xff
-#define CXL_CMD_HEALTH_INFO_TEMPERATURE_NOT_IMPL			0xffff
+#define CXL_CMD_HEALTH_INFO_TEMPERATURE_NOT_IMPL			0x7fff
 
 static inline int check_kmod(struct kmod_ctx *kmod_ctx)
 {
--

2.17.1

#6 lore raw

PATCH v2 2/3 libcxl: Fix accessors for temperature field to support negative value

In reply to Jehoon Park

Add a new macro function to retrieve a signed value such as a temperature. Modify accessors for signed value to return INT_MAX when error occurs and set errno to corresponding errno codes.

---
 cxl/lib/libcxl.c | 32 +++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)
cxl/lib/libcxl.c +21 -11
index af4ca44..fc64de1 100644
@@ -3661,11 +3661,23 @@ cxl_cmd_alert_config_get_life_used_prog_warn_threshold(struct cxl_cmd *cmd)
 			 life_used_prog_warn_threshold);
 }
 
+#define cmd_get_field_s16(cmd, n, N, field)				\
+do {									\
+	struct cxl_cmd_##n *c =						\
+		(struct cxl_cmd_##n *)cmd->send_cmd->out.payload;	\
+	int rc = cxl_cmd_validate_status(cmd, CXL_MEM_COMMAND_ID_##N);	\
+	if (rc)	{							\
+		errno = -rc;						\
+		return INT_MAX;						\
+	}								\
+	return (int16_t)le16_to_cpu(c->field);				\
+} while(0)
+
 CXL_EXPORT int
 cxl_cmd_alert_config_get_dev_over_temperature_crit_alert_threshold(
 	struct cxl_cmd *cmd)
 {
-	cmd_get_field_u16(cmd, get_alert_config, GET_ALERT_CONFIG,
+	cmd_get_field_s16(cmd, get_alert_config, GET_ALERT_CONFIG,
 			  dev_over_temperature_crit_alert_threshold);
 }
 
@@ -3673,7 +3685,7 @@ CXL_EXPORT int
 cxl_cmd_alert_config_get_dev_under_temperature_crit_alert_threshold(
 	struct cxl_cmd *cmd)
 {
-	cmd_get_field_u16(cmd, get_alert_config, GET_ALERT_CONFIG,
+	cmd_get_field_s16(cmd, get_alert_config, GET_ALERT_CONFIG,
 			  dev_under_temperature_crit_alert_threshold);
 }
 
@@ -3681,7 +3693,7 @@ CXL_EXPORT int
 cxl_cmd_alert_config_get_dev_over_temperature_prog_warn_threshold(
 	struct cxl_cmd *cmd)
 {
-	cmd_get_field_u16(cmd, get_alert_config, GET_ALERT_CONFIG,
+	cmd_get_field_s16(cmd, get_alert_config, GET_ALERT_CONFIG,
 			  dev_over_temperature_prog_warn_threshold);
 }
 
@@ -3689,7 +3701,7 @@ CXL_EXPORT int
 cxl_cmd_alert_config_get_dev_under_temperature_prog_warn_threshold(
 	struct cxl_cmd *cmd)
 {
-	cmd_get_field_u16(cmd, get_alert_config, GET_ALERT_CONFIG,
+	cmd_get_field_s16(cmd, get_alert_config, GET_ALERT_CONFIG,
 			  dev_under_temperature_prog_warn_threshold);
 }
 
@@ -3905,8 +3917,6 @@ CXL_EXPORT int cxl_cmd_health_info_get_life_used(struct cxl_cmd *cmd)
 {
 	int rc = health_info_get_life_used_raw(cmd);
 
-	if (rc < 0)
-		return rc;
 	if (rc == CXL_CMD_HEALTH_INFO_LIFE_USED_NOT_IMPL)
 		return -EOPNOTSUPP;
 	return rc;
@@ -3914,7 +3924,7 @@ CXL_EXPORT int cxl_cmd_health_info_get_life_used(struct cxl_cmd *cmd)
 
 static int health_info_get_temperature_raw(struct cxl_cmd *cmd)
 {
-	cmd_get_field_u16(cmd, get_health_info, GET_HEALTH_INFO,
+	cmd_get_field_s16(cmd, get_health_info, GET_HEALTH_INFO,
 				 temperature);
 }
 
@@ -3922,10 +3932,10 @@ CXL_EXPORT int cxl_cmd_health_info_get_temperature(struct cxl_cmd *cmd)
 {
 	int rc = health_info_get_temperature_raw(cmd);
 
-	if (rc < 0)
-		return rc;
-	if (rc == CXL_CMD_HEALTH_INFO_TEMPERATURE_NOT_IMPL)
-		return -EOPNOTSUPP;
+	if (rc == CXL_CMD_HEALTH_INFO_TEMPERATURE_NOT_IMPL) {
+		errno = EOPNOTSUPP;
+		return INT_MAX;
+	}
 	return rc;
 }
 
Signature · 1 line
2.17.1
#7 lore raw

Re: [ndctl PATCH v2 2/3] libcxl: Fix accessors for temperature field to support negative value

Quoted 3 lines · On Mon, 7 Aug 2023 15:35:48 +0900 Jehoon Park <[email protected]> wrote:

Add a new macro function to retrieve a signed value such as a temperature. Modify accessors for signed value to return INT_MAX when error occurs and set errno to corresponding errno codes.

None of the callers have been modified to deal with INTMAX until next patch. So I think you need to combine the two to avoid temporary breakage.

Also you seem to be also changing the health status. That seems to be unrelated to the negative temperature support so shouldn't really be in same patch.

Quoted 63 lines
---
 cxl/lib/libcxl.c | 32 +++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)
cxl/lib/libcxl.c +16 -4
index af4ca44..fc64de1 100644
@@ -3661,11 +3661,23 @@ cxl_cmd_alert_config_get_life_used_prog_warn_threshold(struct cxl_cmd *cmd)
 			 life_used_prog_warn_threshold);
 }
 
+#define cmd_get_field_s16(cmd, n, N, field)				\
+do {									\
+	struct cxl_cmd_##n *c =						\
+		(struct cxl_cmd_##n *)cmd->send_cmd->out.payload;	\
+	int rc = cxl_cmd_validate_status(cmd, CXL_MEM_COMMAND_ID_##N);	\
+	if (rc)	{							\
+		errno = -rc;						\
+		return INT_MAX;						\
+	}								\
+	return (int16_t)le16_to_cpu(c->field);				\
+} while(0)
+
 CXL_EXPORT int
 cxl_cmd_alert_config_get_dev_over_temperature_crit_alert_threshold(
 	struct cxl_cmd *cmd)
 {
-	cmd_get_field_u16(cmd, get_alert_config, GET_ALERT_CONFIG,
+	cmd_get_field_s16(cmd, get_alert_config, GET_ALERT_CONFIG,
 			  dev_over_temperature_crit_alert_threshold);
 }
 
@@ -3673,7 +3685,7 @@ CXL_EXPORT int
 cxl_cmd_alert_config_get_dev_under_temperature_crit_alert_threshold(
 	struct cxl_cmd *cmd)
 {
-	cmd_get_field_u16(cmd, get_alert_config, GET_ALERT_CONFIG,
+	cmd_get_field_s16(cmd, get_alert_config, GET_ALERT_CONFIG,
 			  dev_under_temperature_crit_alert_threshold);
 }
 
@@ -3681,7 +3693,7 @@ CXL_EXPORT int
 cxl_cmd_alert_config_get_dev_over_temperature_prog_warn_threshold(
 	struct cxl_cmd *cmd)
 {
-	cmd_get_field_u16(cmd, get_alert_config, GET_ALERT_CONFIG,
+	cmd_get_field_s16(cmd, get_alert_config, GET_ALERT_CONFIG,
 			  dev_over_temperature_prog_warn_threshold);
 }
 
@@ -3689,7 +3701,7 @@ CXL_EXPORT int
 cxl_cmd_alert_config_get_dev_under_temperature_prog_warn_threshold(
 	struct cxl_cmd *cmd)
 {
-	cmd_get_field_u16(cmd, get_alert_config, GET_ALERT_CONFIG,
+	cmd_get_field_s16(cmd, get_alert_config, GET_ALERT_CONFIG,
 			  dev_under_temperature_prog_warn_threshold);
 }
 

cxl/lib/libcxl.c CXL_EXPORT int cxl_cmd_health_info_get_life_used(struct cxl_cmd *cmd) · line 3909

Quoted 5 lines
cxl/lib/libcxl.c +0 -2
@@ -3905,8 +3917,6 @@ CXL_EXPORT int cxl_cmd_health_info_get_life_used(struct cxl_cmd *cmd)
 {
 	int rc = health_info_get_life_used_raw(cmd);
 
-	if (rc < 0)
-		return rc;

Why has this one changed? It's a u8 so not as far as I can see affected by your new signed accessor.

Quoted 27 lines
cxl/lib/libcxl.c +5 -5
 	if (rc == CXL_CMD_HEALTH_INFO_LIFE_USED_NOT_IMPL)
 		return -EOPNOTSUPP;
 	return rc;
@@ -3914,7 +3924,7 @@ CXL_EXPORT int cxl_cmd_health_info_get_life_used(struct cxl_cmd *cmd)
 
 static int health_info_get_temperature_raw(struct cxl_cmd *cmd)
 {
-	cmd_get_field_u16(cmd, get_health_info, GET_HEALTH_INFO,
+	cmd_get_field_s16(cmd, get_health_info, GET_HEALTH_INFO,
 				 temperature);
 }
 
@@ -3922,10 +3932,10 @@ CXL_EXPORT int cxl_cmd_health_info_get_temperature(struct cxl_cmd *cmd)
 {
 	int rc = health_info_get_temperature_raw(cmd);
 
-	if (rc < 0)
-		return rc;
-	if (rc == CXL_CMD_HEALTH_INFO_TEMPERATURE_NOT_IMPL)
-		return -EOPNOTSUPP;
+	if (rc == CXL_CMD_HEALTH_INFO_TEMPERATURE_NOT_IMPL) {
+		errno = EOPNOTSUPP;
+		return INT_MAX;
+	}
 	return rc;
 }
 
#8 lore raw

Re: [ndctl PATCH v2 2/3] libcxl: Fix accessors for temperature field to support negative value

Quoted 7 lines · Jonathan Cameron
Quoted 3 lines · On Mon, 7 Aug 2023 15:35:48 +0900 Jehoon Park <[email protected]> wrote:

Add a new macro function to retrieve a signed value such as a temperature. Modify accessors for signed value to return INT_MAX when error occurs and set errno to corresponding errno codes.

Quoted 7 lines

None of the callers have been modified to deal with INTMAX until next patch. So I think you need to combine the two to avoid temporary breakage.

Also you seem to be also changing the health status. That seems to be unrelated to the negative temperature support so shouldn't really be in same patch.

Thank you for comments,

I will re-organize these patches in the next revision.

Quoted 66 lines
Quoted 66 lines
---
 cxl/lib/libcxl.c | 32 +++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)
cxl/lib/libcxl.c +16 -4
index af4ca44..fc64de1 100644
@@ -3661,11 +3661,23 @@ cxl_cmd_alert_config_get_life_used_prog_warn_threshold(struct cxl_cmd *cmd)
 			 life_used_prog_warn_threshold);
 }
 
+#define cmd_get_field_s16(cmd, n, N, field)				\
+do {									\
+	struct cxl_cmd_##n *c =						\
+		(struct cxl_cmd_##n *)cmd->send_cmd->out.payload;	\
+	int rc = cxl_cmd_validate_status(cmd, CXL_MEM_COMMAND_ID_##N);	\
+	if (rc)	{							\
+		errno = -rc;						\
+		return INT_MAX;						\
+	}								\
+	return (int16_t)le16_to_cpu(c->field);				\
+} while(0)
+
 CXL_EXPORT int
 cxl_cmd_alert_config_get_dev_over_temperature_crit_alert_threshold(
 	struct cxl_cmd *cmd)
 {
-	cmd_get_field_u16(cmd, get_alert_config, GET_ALERT_CONFIG,
+	cmd_get_field_s16(cmd, get_alert_config, GET_ALERT_CONFIG,
 			  dev_over_temperature_crit_alert_threshold);
 }
 
@@ -3673,7 +3685,7 @@ CXL_EXPORT int
 cxl_cmd_alert_config_get_dev_under_temperature_crit_alert_threshold(
 	struct cxl_cmd *cmd)
 {
-	cmd_get_field_u16(cmd, get_alert_config, GET_ALERT_CONFIG,
+	cmd_get_field_s16(cmd, get_alert_config, GET_ALERT_CONFIG,
 			  dev_under_temperature_crit_alert_threshold);
 }
 
@@ -3681,7 +3693,7 @@ CXL_EXPORT int
 cxl_cmd_alert_config_get_dev_over_temperature_prog_warn_threshold(
 	struct cxl_cmd *cmd)
 {
-	cmd_get_field_u16(cmd, get_alert_config, GET_ALERT_CONFIG,
+	cmd_get_field_s16(cmd, get_alert_config, GET_ALERT_CONFIG,
 			  dev_over_temperature_prog_warn_threshold);
 }
 
@@ -3689,7 +3701,7 @@ CXL_EXPORT int
 cxl_cmd_alert_config_get_dev_under_temperature_prog_warn_threshold(
 	struct cxl_cmd *cmd)
 {
-	cmd_get_field_u16(cmd, get_alert_config, GET_ALERT_CONFIG,
+	cmd_get_field_s16(cmd, get_alert_config, GET_ALERT_CONFIG,
 			  dev_under_temperature_prog_warn_threshold);
 }
 
@@ -3905,8 +3917,6 @@ CXL_EXPORT int cxl_cmd_health_info_get_life_used(struct cxl_cmd *cmd)
 {
 	int rc = health_info_get_life_used_raw(cmd);
 
Quoted 7 lines

cxl/lib/libcxl.c CXL_EXPORT int cxl_cmd_health_info_get_life_used(struct cxl_cmd *cmd) · line 3909

Quoted 2 lines
cxl/lib/libcxl.c +0 -2
-	if (rc < 0)
-		return rc;

Why has this one changed? It's a u8 so not as far as I can see affected by your new signed accessor.

I removed it because it was unnecessary code. (No action after error checking) However, as you stated, this code cleaning is irrelevant to this patch. I will revert this in the next patch.

Quoted 28 lines
Quoted 27 lines
cxl/lib/libcxl.c +5 -5
 	if (rc == CXL_CMD_HEALTH_INFO_LIFE_USED_NOT_IMPL)
 		return -EOPNOTSUPP;
 	return rc;
@@ -3914,7 +3924,7 @@ CXL_EXPORT int cxl_cmd_health_info_get_life_used(struct cxl_cmd *cmd)
 
 static int health_info_get_temperature_raw(struct cxl_cmd *cmd)
 {
-	cmd_get_field_u16(cmd, get_health_info, GET_HEALTH_INFO,
+	cmd_get_field_s16(cmd, get_health_info, GET_HEALTH_INFO,
 				 temperature);
 }
 
@@ -3922,10 +3932,10 @@ CXL_EXPORT int cxl_cmd_health_info_get_temperature(struct cxl_cmd *cmd)
 {
 	int rc = health_info_get_temperature_raw(cmd);
 
-	if (rc < 0)
-		return rc;
-	if (rc == CXL_CMD_HEALTH_INFO_TEMPERATURE_NOT_IMPL)
-		return -EOPNOTSUPP;
+	if (rc == CXL_CMD_HEALTH_INFO_TEMPERATURE_NOT_IMPL) {
+		errno = EOPNOTSUPP;
+		return INT_MAX;
+	}
 	return rc;
 }
 
#9 lore raw

PATCH v2 3/3 cxl: Fix the checking value when listing device's health info

In reply to Jehoon Park

Fix the value for checking device's life used and temperature fields are implemented.

---
 cxl/json.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
cxl/json.c +5 -4
index 7678d02..102bfaf 100644
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 // Copyright (C) 2015-2021 Intel Corporation. All rights reserved.
+#include <errno.h>
 #include <limits.h>
 #include <util/json.h>
 #include <uuid/uuid.h>
@@ -238,15 +239,15 @@ static struct json_object *util_cxl_memdev_health_to_json(
 		json_object_object_add(jhealth, "ext_corrected_persistent", jobj);
 
 	/* other fields */
-	field = cxl_cmd_health_info_get_life_used(cmd);
-	if (field != 0xff) {
-		jobj = json_object_new_int(field);
+	rc = cxl_cmd_health_info_get_life_used(cmd);
+	if (rc != -EOPNOTSUPP) {
+		jobj = json_object_new_int(rc);
 		if (jobj)
 			json_object_object_add(jhealth, "life_used_percent", jobj);
 	}
 
 	field = cxl_cmd_health_info_get_temperature(cmd);
-	if (field != 0xffff) {
+	if (field != INT_MAX) {
 		jobj = json_object_new_int(field);
 		if (jobj)
 			json_object_object_add(jhealth, "temperature", jobj);
Signature · 1 line
2.17.1