cxl: trace: Fix macro safety in CXL_EVT_TP_fast_assign
PATCH 1/1 cxl: trace: Fix macro safety in CXL_EVT_TP_fast_assign
Fix checkpatch.pl detected error
The CXL_EVT_TP_fast_assign macro assigns multiple fields, but does not wrap the body in a do { ... } while (0) block. This can lead to unexpected behavior when used in conditional branches.
Add checks to ensure cxlmd is valid before accessing its fields.
--- drivers/cxl/core/trace.h | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-)
drivers/cxl/core/trace.h +18 -12
@@ -249,18 +249,24 @@ TRACE_EVENT(cxl_overflow, __field(u8, hdr_maint_op_class) \ __field(u8, hdr_maint_op_sub_class) -#define CXL_EVT_TP_fast_assign (cxlmd, l, hdr) \ - __assign_str(memdev); \ - __assign_str(host); \ - __entry->log = (l); \ - __entry->serial = (cxlmd)->cxlds->serial; \ - __entry->hdr_length = (hdr).length; \ - __entry->hdr_flags = get_unaligned_le24((hdr).flags); \ - __entry->hdr_handle = le16_to_cpu((hdr).handle); \ - __entry->hdr_related_handle = le16_to_cpu((hdr).related_handle); \ - __entry->hdr_timestamp = le64_to_cpu((hdr).timestamp); \ - __entry->hdr_maint_op_class = (hdr).maint_op_class; \ - __entry->hdr_maint_op_sub_class = (hdr).maint_op_sub_class +#define CXL_EVT_TP_fast_assign (cxlmd, l, hdr) \ + do { \ + if (!(cxlmd)) { \ + pr_err("Invalid arguments to CXL_EVT_TP_fast_assign\n"); \ + break; \ + } \ + __assign_str(memdev); \ + __assign_str(host); \ + __entry->log = (l); \ + __entry->serial = (cxlmd)->cxlds->serial; \ + __entry->hdr_length = (hdr).length; \ + __entry->hdr_flags = get_unaligned_le24((hdr).flags); \ + __entry->hdr_handle = le16_to_cpu((hdr).handle); \ + __entry->hdr_related_handle = le16_to_cpu((hdr).related_handle); \ + __entry->hdr_timestamp = le64_to_cpu((hdr).timestamp); \ + __entry->hdr_maint_op_class = (hdr).maint_op_class; \ + __entry->hdr_maint_op_sub_class = (hdr).maint_op_sub_class; \ + } while (0)#define CXL_EVT_TP_printk (fmt, ...) \ TP_printk("memdev=%s host=%s serial=%lld log=%s : time=%llu uuid=%pUb " \
Signature · 1 line
2.43.0
Quoted 1 line · On Tue, 22 Apr 2025 00:15:17 +0530 "shaikh.kamal" <[email protected]>…
Fix checkpatch.pl detected error
First, checkpatch.pl should never be used on existing code unless it's yours. As the name suggests, it's for checking patches. It's not to check what's already been accepted. Please do not submit patches against accepted code because of what checkpatch.pl reports.
If you run it on code and it reports something that you find is a real bug, then sure, fix it. But don't submit patches on code you do not understand just because checkpatch.pl says there's an issue with it.
Quoted 3 lines
The
CXL_EVT_TP_fast_assignmacro assigns multiple fields, but does not wrap the body in ado { ... } while (0)block. This can lead to unexpected behavior when used in conditional branches.
Next, this is not a normal macro. It's a trace event macro and checkpatch.pl fails miserably on pretty much all tracing macros.
Quoted 2 lines
Add checks to ensure cxlmd is valid before accessing its fields.
If it is invalid, and we do what your patch suggests and just not write anything, the event it was writing into has already been created. If we exit out of this macro, then the event will simply contain garbage, but is already on its way to user space. That means the output to user space will be garbage. That's a bug. In other words, if cxlmd is NULL, it had better not be calling this macro in the fist place!
-- Steve
Quoted 8 lines · Steven Rostedt
Quoted 1 line · On Tue, 22 Apr 2025 00:15:17 +0530 "shaikh.kamal" <[email protected]>…
Fix checkpatch.pl detected error
First, checkpatch.pl should never be used on existing code unless it's yours. As the name suggests, it's for checking patches. It's not to check what's already been accepted. Please do not submit patches against accepted code because of what checkpatch.pl reports.
Thanks for your suggestion.
Quoted 4 lines
If you run it on code and it reports something that you find is a real bug, then sure, fix it. But don't submit patches on code you do not understand just because checkpatch.pl says there's an issue with it.
Okay got it.
Quoted 19 lines
Quoted 4 lines
The
CXL_EVT_TP_fast_assignmacro assigns multiple fields, but does not wrap the body in ado { ... } while (0)block. This can lead to unexpected behavior when used in conditional branches.Next, this is not a normal macro. It's a trace event macro and checkpatch.pl fails miserably on pretty much all tracing macros.
Thanks for your comments.
Quoted 1 line
Add checks to ensure cxlmd is valid before accessing its fields.
If it is invalid, and we do what your patch suggests and just not write anything, the event it was writing into has already been created. If we exit out of this macro, then the event will simply contain garbage, but is already on its way to user space. That means the output to user space will be garbage. That's a bug. In other words, if cxlmd is NULL, it had better not be calling this macro in the fist place!
-- Steve
Thanks for your suggestions and comments.
Ira Weiny
NAKQuoted 8 lines · shaikh.kamal
Fix checkpatch.pl detected error
TheCXL_EVT_TP_fast_assignmacro assigns multiple fields, but does not wrap the body in ado { ... } while (0)block. This can lead to unexpected behavior when used in conditional branches.Add checks to ensure cxlmd is valid before accessing its fields.
Signed-off-by shaikh.kamal <[email protected]>
Nak
This is not a normal macro. checkpatch.pl does not apply in this case. Also checking cxlmd here is not necessary. The callers of the tracepoint know very well that it needs to be valid.
Ira
Quoted 48 lines
--- drivers/cxl/core/trace.h | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-)drivers/cxl/core/trace.h +18
-12@@ -249,18 +249,24 @@ TRACE_EVENT(cxl_overflow, __field(u8, hdr_maint_op_class) \ __field(u8, hdr_maint_op_sub_class) -#define CXL_EVT_TP_fast_assign (cxlmd, l, hdr) \ - __assign_str(memdev); \ - __assign_str(host); \ - __entry->log = (l); \ - __entry->serial = (cxlmd)->cxlds->serial; \ - __entry->hdr_length = (hdr).length; \ - __entry->hdr_flags = get_unaligned_le24((hdr).flags); \ - __entry->hdr_handle = le16_to_cpu((hdr).handle); \ - __entry->hdr_related_handle = le16_to_cpu((hdr).related_handle); \ - __entry->hdr_timestamp = le64_to_cpu((hdr).timestamp); \ - __entry->hdr_maint_op_class = (hdr).maint_op_class; \ - __entry->hdr_maint_op_sub_class = (hdr).maint_op_sub_class +#define CXL_EVT_TP_fast_assign (cxlmd, l, hdr) \ + do { \ + if (!(cxlmd)) { \ + pr_err("Invalid arguments to CXL_EVT_TP_fast_assign\n"); \ + break; \ + } \ + __assign_str(memdev); \ + __assign_str(host); \ + __entry->log = (l); \ + __entry->serial = (cxlmd)->cxlds->serial; \ + __entry->hdr_length = (hdr).length; \ + __entry->hdr_flags = get_unaligned_le24((hdr).flags); \ + __entry->hdr_handle = le16_to_cpu((hdr).handle); \ + __entry->hdr_related_handle = le16_to_cpu((hdr).related_handle); \ + __entry->hdr_timestamp = le64_to_cpu((hdr).timestamp); \ + __entry->hdr_maint_op_class = (hdr).maint_op_class; \ + __entry->hdr_maint_op_sub_class = (hdr).maint_op_sub_class; \ + } while (0)#define CXL_EVT_TP_printk (fmt, ...) \ TP_printk("memdev=%s host=%s serial=%lld log=%s : time=%llu uuid=%pUb " \Signature · 1 line
2.43.0
Quoted 66 lines · Ira Weiny
Quoted 8 lines · shaikh.kamal
Fix checkpatch.pl detected error
TheCXL_EVT_TP_fast_assignmacro assigns multiple fields, but does not wrap the body in ado { ... } while (0)block. This can lead to unexpected behavior when used in conditional branches.Add checks to ensure cxlmd is valid before accessing its fields.
Signed-off-by shaikh.kamal <[email protected]>Nak
This is not a normal macro. checkpatch.pl does not apply in this case. Also checking cxlmd here is not necessary. The callers of the tracepoint know very well that it needs to be valid.
Ira
Thanks for your comments.Quoted 48 lines
--- drivers/cxl/core/trace.h | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-)drivers/cxl/core/trace.h +18
-12@@ -249,18 +249,24 @@ TRACE_EVENT(cxl_overflow, __field(u8, hdr_maint_op_class) \ __field(u8, hdr_maint_op_sub_class) -#define CXL_EVT_TP_fast_assign (cxlmd, l, hdr) \ - __assign_str(memdev); \ - __assign_str(host); \ - __entry->log = (l); \ - __entry->serial = (cxlmd)->cxlds->serial; \ - __entry->hdr_length = (hdr).length; \ - __entry->hdr_flags = get_unaligned_le24((hdr).flags); \ - __entry->hdr_handle = le16_to_cpu((hdr).handle); \ - __entry->hdr_related_handle = le16_to_cpu((hdr).related_handle); \ - __entry->hdr_timestamp = le64_to_cpu((hdr).timestamp); \ - __entry->hdr_maint_op_class = (hdr).maint_op_class; \ - __entry->hdr_maint_op_sub_class = (hdr).maint_op_sub_class +#define CXL_EVT_TP_fast_assign (cxlmd, l, hdr) \ + do { \ + if (!(cxlmd)) { \ + pr_err("Invalid arguments to CXL_EVT_TP_fast_assign\n"); \ + break; \ + } \ + __assign_str(memdev); \ + __assign_str(host); \ + __entry->log = (l); \ + __entry->serial = (cxlmd)->cxlds->serial; \ + __entry->hdr_length = (hdr).length; \ + __entry->hdr_flags = get_unaligned_le24((hdr).flags); \ + __entry->hdr_handle = le16_to_cpu((hdr).handle); \ + __entry->hdr_related_handle = le16_to_cpu((hdr).related_handle); \ + __entry->hdr_timestamp = le64_to_cpu((hdr).timestamp); \ + __entry->hdr_maint_op_class = (hdr).maint_op_class; \ + __entry->hdr_maint_op_sub_class = (hdr).maint_op_sub_class; \ + } while (0)#define CXL_EVT_TP_printk (fmt, ...) \ TP_printk("memdev=%s host=%s serial=%lld log=%s : time=%llu uuid=%pUb " \Signature · 1 line
2.43.0