hw/cxl/cxl-cdat: Make cxl_doe_cdat_init() return boolean
cover hw/cxl/cxl-cdat: Make cxl_doe_cdat_init() return boolean
From: Zhao Liu <[email protected]>
Hi list,
This series is the followup of the previous fix [1] to improve the error handling.
As error.h suggested, the best practice for callee is to return something to indicate success / failure instead of dereferencing @errp.
Thus make cxl_doe_cdat_init() as well as ct3_load_cdat() and ct3_build_cdat() return boolean.
[1]: https://lore.kernel.org/qemu-devel/[email protected]/
Thanks,
Zhao
---
Zhao Liu (3):
hw/cxl/cxl-cdat: Make ct3_load_cdat() return boolean hw/cxl/cxl-cdat: Make ct3_build_cdat() return boolean hw/cxl/cxl-cdat: Make cxl_doe_cdat_init() return boolean
hw/cxl/cxl-cdat.c | 28 +++++++++++++++------------- hw/mem/cxl_type3.c | 3 +-- hw/pci-bridge/cxl_upstream.c | 3 +-- include/hw/cxl/cxl_component.h | 2 +- 4 files changed, 18 insertions(+), 18 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/3 hw/cxl/cxl-cdat: Make ct3_load_cdat() return boolean
From: Zhao Liu <[email protected]>
As error.h suggested, the best practice for callee is to return something to indicate success / failure.
So make ct3_load_cdat() return boolean, and this is the preparation for cxl_doe_cdat_init() returning boolean.
--- hw/cxl/cxl-cdat.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
hw/cxl/cxl-cdat.c +7 -6
@@ -111,7 +111,7 @@ static void ct3_build_cdat(CDATObject *cdat, Error **errp) cdat->entry =g_steal_pointer (&cdat_st); } -static void ct3_load_cdat (CDATObject *cdat,Error **errp) +static bool ct3_load_cdat (CDATObject *cdat,Error **errp) { g_autofree CDATEntry *cdat_st = NULL; g_autofree uint8_t *buf = NULL;
@@ -127,11 +127,11 @@ static void ct3_load_cdat(CDATObject *cdat, Error **errp) &file_size, &error)) {error_setg (errp,"CDAT: File read failed: %s" , error->message);g_error_free (error); -return ; +return false; }if (file_size <sizeof (CDATTableHeader)) {error_setg (errp,"CDAT: File too short" ); -return ; +return false; } i =sizeof (CDATTableHeader); num_ent =1 ;
@@ -139,19 +139,19 @@ static void ct3_load_cdat(CDATObject *cdat, Error **errp) hdr = (CDATSubHeader *)(buf + i);if (i +sizeof (CDATSubHeader) > file_size) {error_setg (errp,"CDAT: Truncated table" ); -return ; +return false; }cdat_len_check (hdr, errp); i += hdr->length;if (i > file_size) {error_setg (errp,"CDAT: Truncated table" ); -return ; +return false; } num_ent++; }if (i != file_size) {error_setg (errp,"CDAT: File length mismatch" ); -return ; +return false; } cdat_st =g_new0 (CDATEntry, num_ent);
@@ -185,6 +185,7 @@ static void ct3_load_cdat(CDATObject *cdat, Error **errp) cdat->entry_len = num_ent; cdat->entry =g_steal_pointer (&cdat_st); cdat->buf =g_steal_pointer (&buf); +return true; }void cxl_doe_cdat_init (CXLComponentState *cxl_cstate,Error **errp)
Signature · 1 line
2.34.1
PATCH 2/3 hw/cxl/cxl-cdat: Make ct3_build_cdat() return boolean
In reply to Zhao LiuFrom: Zhao Liu <[email protected]>
As error.h suggested, the best practice for callee is to return something to indicate success / failure.
So make ct3_build_cdat() return boolean, and this is the preparation for cxl_doe_cdat_init() returning boolean.
--- hw/cxl/cxl-cdat.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
hw/cxl/cxl-cdat.c +5 -4
@@ -44,7 +44,7 @@ static void cdat_len_check(CDATSubHeader *hdr, Error **errp) } } -static void ct3_build_cdat (CDATObject *cdat,Error **errp) +static bool ct3_build_cdat (CDATObject *cdat,Error **errp) { g_autofree CDATTableHeader *cdat_header = NULL; g_autofree CDATEntry *cdat_st = NULL;
@@ -58,7 +58,7 @@ static void ct3_build_cdat(CDATObject *cdat, Error **errp) cdat_header =g_malloc0 (sizeof (*cdat_header));if (!cdat_header) {error_setg (errp,"Failed to allocate CDAT header" ); -return ; +return false; } cdat->built_buf_len = cdat->build_cdat_table(&cdat->built_buf,
@@ -67,14 +67,14 @@ static void ct3_build_cdat(CDATObject *cdat, Error **errp)if (cdat->built_buf_len <=0 ) {/* Build later as not all data available yet */ cdat->to_update = true; -return ; +return true; } cdat->to_update = false; cdat_st =g_malloc0 (sizeof (*cdat_st) * (cdat->built_buf_len +1 ));if (!cdat_st) {error_setg (errp,"Failed to allocate CDAT entry array" ); -return ; +return false; }/* Entry 0 for CDAT header, starts with Entry 1 */
@@ -109,6 +109,7 @@ static void ct3_build_cdat(CDATObject *cdat, Error **errp) cdat_st[0 ].length =sizeof (*cdat_header); cdat->entry_len =1 + cdat->built_buf_len; cdat->entry =g_steal_pointer (&cdat_st); +return true; }static bool ct3_load_cdat (CDATObject *cdat,Error **errp)
Signature · 1 line
2.34.1
PATCH 3/3 hw/cxl/cxl-cdat: Make cxl_doe_cdat_init() return boolean
In reply to Zhao LiuFrom: Zhao Liu <[email protected]>
As error.h suggested, the best practice for callee is to return something to indicate success / failure.
With returned boolean, there's no need to dereference @errp to check failure case.
--- hw/cxl/cxl-cdat.c | 6 +++--- hw/mem/cxl_type3.c | 3 +-- hw/pci-bridge/cxl_upstream.c | 3 +-- include/hw/cxl/cxl_component.h | 2 +- 4 files changed, 6 insertions(+), 8 deletions(-)
hw/cxl/cxl-cdat.c +3 -3
@@ -189,14 +189,14 @@ static bool ct3_load_cdat(CDATObject *cdat, Error **errp)return true; } -void cxl_doe_cdat_init (CXLComponentState *cxl_cstate,Error **errp) +bool cxl_doe_cdat_init (CXLComponentState *cxl_cstate,Error **errp) {CDATObject *cdat = &cxl_cstate->cdat;if (cdat->filename) { -ct3_load_cdat (cdat, errp); +return ct3_load_cdat (cdat, errp); }else { -ct3_build_cdat (cdat, errp); +return ct3_build_cdat (cdat, errp); } }
hw/mem/cxl_type3.c +1 -2
@@ -705,8 +705,7 @@ static void ct3_realize(PCIDevice *pci_dev, Error **errp) cxl_cstate->cdat.build_cdat_table = ct3_build_cdat_table; cxl_cstate->cdat.free_cdat_table = ct3_free_cdat_table; cxl_cstate->cdat.private = ct3d; -cxl_doe_cdat_init (cxl_cstate, errp); -if (*errp) { +if (!cxl_doe_cdat_init (cxl_cstate, errp)) { goto err_free_special_ops; }
hw/pci-bridge/cxl_upstream.c +1 -2
@@ -338,8 +338,7 @@ static void cxl_usp_realize(PCIDevice *d, Error **errp) cxl_cstate->cdat.build_cdat_table = build_cdat_table; cxl_cstate->cdat.free_cdat_table = free_default_cdat_table; cxl_cstate->cdat.private = d; -cxl_doe_cdat_init (cxl_cstate, errp); -if (*errp) { +if (!cxl_doe_cdat_init (cxl_cstate, errp)) { goto err_cap; }
include/hw/cxl/cxl_component.h +1 -1
@@ -273,7 +273,7 @@ hwaddr cxl_decode_ig(int ig);CXLComponentState *cxl_get_hb_cstate (PCIHostState *hb);bool cxl_get_hb_passthrough (PCIHostState *hb); -void cxl_doe_cdat_init (CXLComponentState *cxl_cstate,Error **errp); +bool cxl_doe_cdat_init (CXLComponentState *cxl_cstate,Error **errp);void cxl_doe_cdat_release (CXLComponentState *cxl_cstate);void cxl_doe_cdat_update (CXLComponentState *cxl_cstate,Error **errp);
Signature · 1 line
2.34.1
Quoted 23 lines · Zhao Liu
From: Zhao Liu <[email protected]>
As
error.hsuggested, the best practice for callee is to return something to indicate success / failure.With returned boolean, there's no need to dereference @errp to check failure case.
Suggested-by Markus Armbruster <[email protected]>Signed-off-by Zhao Liu <[email protected]>--- hw/cxl/cxl-cdat.c | 6 +++--- hw/mem/cxl_type3.c | 3 +-- hw/pci-bridge/cxl_upstream.c | 3 +-- include/hw/cxl/cxl_component.h | 2 +- 4 files changed, 6 insertions(+), 8 deletions(-)include/hw/cxl/cxl_component.h +0
-0
include/hw/cxl/cxl_component.h hwaddr cxl_decode_ig(int ig); · line 278
Quoted 7 lines
include/hw/cxl/cxl_component.h +1
-1@@ -273,7 +273,7 @@ hwaddr cxl_decode_ig(int ig);CXLComponentState *cxl_get_hb_cstate (PCIHostState *hb);bool cxl_get_hb_passthrough (PCIHostState *hb); -void cxl_doe_cdat_init (CXLComponentState *cxl_cstate,Error **errp); +bool cxl_doe_cdat_init (CXLComponentState *cxl_cstate,Error **errp);void cxl_doe_cdat_release (CXLComponentState *cxl_cstate);void cxl_doe_cdat_update (CXLComponentState *cxl_cstate,Error **errp);
Another candidate ;)
Hi Philippe,
On Thu, Apr 18, 2024 at 02:06:15PM +0200, Philippe Mathieu-Daudé wrote:
[snip]
Quoted 5 lines
Quoted 5 lines
include/hw/cxl/cxl_component.h +0
-0@@ -273,7 +273,7 @@ hwaddr cxl_decode_ig(int ig);
Quoted 9 lines
include/hw/cxl/cxl_component.h hwaddr cxl_decode_ig(int ig); · line 278
Quoted 6 lines
include/hw/cxl/cxl_component.h +1
-1CXLComponentState *cxl_get_hb_cstate (PCIHostState *hb);bool cxl_get_hb_passthrough (PCIHostState *hb); -void cxl_doe_cdat_init (CXLComponentState *cxl_cstate,Error **errp); +bool cxl_doe_cdat_init (CXLComponentState *cxl_cstate,Error **errp);void cxl_doe_cdat_release (CXLComponentState *cxl_cstate);void cxl_doe_cdat_update (CXLComponentState *cxl_cstate,Error **errp);Another candidate ;)
I guess you mean cxl_doe_cdat_update()? ;-)
It's a special case since it has only one use case and in that case, &error_fatal is passed as @errp. So then it doesn't need to check the return value.
Thanks,
Zhao
Quoted 8 lines · Zhao Liu
From: Zhao Liu <[email protected]>
---
Zhao Liu (3):
hw/cxl/cxl-cdat: Makect3_load_cdat()return boolean hw/cxl/cxl-cdat: Makect3_build_cdat()return boolean hw/cxl/cxl-cdat: Makecxl_doe_cdat_init()return boolean
Series:
Jonathan Cameron via
Acked-by Jonathan CameronQuoted 4 lines · On Thu, 18 Apr 2024 14:06:39 +0200 Philippe Mathieu-Daudé <[email protected]>…
Quoted 1 line · Zhao Liu
From: Zhao Liu <[email protected]>
Quoted 9 lines
Quoted 5 lines
---
Zhao Liu (3):
hw/cxl/cxl-cdat: Makect3_load_cdat()return boolean hw/cxl/cxl-cdat: Makect3_build_cdat()return boolean hw/cxl/cxl-cdat: Makecxl_doe_cdat_init()return booleanSeries:
Reviewed-by Philippe Mathieu-Daudé <[email protected]>
Quoted 8 lines · Zhao Liu
From: Zhao Liu <[email protected]>
---
Zhao Liu (3):
hw/cxl/cxl-cdat: Makect3_load_cdat()return boolean hw/cxl/cxl-cdat: Makect3_build_cdat()return boolean hw/cxl/cxl-cdat: Makecxl_doe_cdat_init()return boolean
Since Jonathan Ack'ed the series, I'm queuing it via my hw-misc tree.
Quoted 4 lines · On Fri, 19 Apr 2024 17:40:07 +0200 Philippe Mathieu-Daudé <[email protected]>…
Quoted 1 line · Zhao Liu
From: Zhao Liu <[email protected]>
Quoted 8 lines
Quoted 5 lines
---
Zhao Liu (3):
hw/cxl/cxl-cdat: Makect3_load_cdat()return boolean hw/cxl/cxl-cdat: Makect3_build_cdat()return boolean hw/cxl/cxl-cdat: Makecxl_doe_cdat_init()return booleanSince Jonathan Ack'ed the series, I'm queuing it via my hw-misc tree.
Thanks,
J