Skip to contentSkip to conversation

hw/cxl/cxl-cdat: Make cxl_doe_cdat_init() return boolean

PATCH In review Zhao Liu 4 participants 10 messages Apr 18, 2024 – Apr 22, 2024

lore
Share & tools
lore

Zhao Liu

author
#0 lore raw

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
author
#1 lore raw

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
index 551545f7823b..b3e496857a77 100644
@@ -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
author
#2 lore raw

PATCH 2/3 hw/cxl/cxl-cdat: Make ct3_build_cdat() return boolean

In reply to Zhao Liu

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_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
index b3e496857a77..e7bc1380bfbf 100644
@@ -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
author
#3 lore raw

PATCH 3/3 hw/cxl/cxl-cdat: Make cxl_doe_cdat_init() return boolean

In reply to Zhao Liu

From: 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
index e7bc1380bfbf..959a55518e65 100644
@@ -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
index b0a7e9f11b64..3e42490b6ce8 100644
@@ -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
index 783fa6adac19..e51221a5f334 100644
@@ -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
index 5012fab6f763..945ee6ffd045 100644
@@ -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
#4 lore raw
Quoted 23 lines · Zhao Liu

From: 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(-)
include/hw/cxl/cxl_component.h +0 -0
index 5012fab6f763..945ee6ffd045 100644

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 ;)

#5 lore raw

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
index 5012fab6f763..945ee6ffd045 100644
@@ -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 -1
  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 ;)

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

Acked-by Jonathan Cameron
#7 lore raw
Quoted 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: 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

Series:

#8 lore raw
In reply to Zhao Liu
Quoted 8 lines · Zhao Liu

From: Zhao Liu <[email protected]>

---
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

Since Jonathan Ack'ed the series, I'm queuing it via my hw-misc tree.

#9 lore raw
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: 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

Since Jonathan Ack'ed the series, I'm queuing it via my hw-misc tree.

Thanks,

J