cxl: Fix issues with g_steal_pointer()
cover cxl: Fix issues with g_steal_pointer()
When setting GLIB_VERSION_MAX_ALLOWED to GLIB_VERSION_2_58 or higher (which we'll certainly do in the not too distant future), glib adds type safety checks to the g_steal_pointer() macro. This triggers errors in the cxl code since the pointer types do not always match here. Let's fix those errors now so we can switch to a newer version of the glib in a future version of QEMU.
Thomas Huth (3):
hw/cxl/cxl-cdat: Fix type of buf in ct3_load_cdat() hw/pci-bridge/cxl_upstream: Fix problem with g_steal_pointer() hw/mem/cxl_type3: Fix problem with g_steal_pointer()
include/hw/cxl/cxl_cdat.h | 17 +++++++++++------ hw/cxl/cxl-cdat.c | 4 ++-- hw/mem/cxl_type3.c | 24 ++++++++++++------------ hw/pci-bridge/cxl_upstream.c | 8 ++++---- 4 files changed, 29 insertions(+), 24 deletions(-)
Signature · 1 line
2.44.0
PATCH 1/3 hw/cxl/cxl-cdat: Fix type of buf in ct3_load_cdat()
When setting GLIB_VERSION_MAX_ALLOWED to GLIB_VERSION_2_58 or higher (which we'll certainly do in the not too distant future), glib adds type safety checks to the g_steal_pointer() macro. This trigger an error in the ct3_load_cdat() function: The local char *buf variable is assigned to uint8_t *buf in CDATObject, i.e. a pointer of a different type. Change the local variable to the same type as buf in CDATObject to avoid the error.
--- hw/cxl/cxl-cdat.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
hw/cxl/cxl-cdat.c +2 -2
@@ -114,7 +114,7 @@ static void ct3_build_cdat(CDATObject *cdat, Error **errp)static void ct3_load_cdat (CDATObject *cdat,Error **errp) { g_autofree CDATEntry *cdat_st = NULL; - g_autofree char *buf = NULL; + g_autofree uint8_t *buf = NULL;uint8_t sum =0 ;int num_ent;int i =0 , ent =1 ;
@@ -171,7 +171,7 @@ static void ct3_load_cdat(CDATObject *cdat, Error **errp) cdat_st[ent].base = hdr; cdat_st[ent].length = hdr->length; -while (buf + i < (char *)cdat_st[ent].base + cdat_st[ent].length) { +while (buf + i < (uint8_t *)cdat_st[ent].base + cdat_st[ent].length) {assert (i < file_size); sum += buf[i++]; }
Signature · 1 line
2.44.0
Re: [PATCH 1/3] hw/cxl/cxl-cdat: Fix type of buf in ct3_load_cdat()
Quoted 9 lines · On Mon, 4 Mar 2024 11:44:04 +0100 Thomas Huth <[email protected]> wrote:
When setting
GLIB_VERSION_MAX_ALLOWEDtoGLIB_VERSION_2_58or higher (which we'll certainly do in the not too distant future), glib adds type safety checks to theg_steal_pointer()macro. This trigger an error in thect3_load_cdat()function: The local char *buf variable is assigned touint8_t*buf in CDATObject, i.e. a pointer of a different type. Change the local variable to the same type as buf in CDATObject to avoid the error.Signed-off-by Thomas Huth <[email protected]>
Quoted 26 lines
--- hw/cxl/cxl-cdat.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)hw/cxl/cxl-cdat.c +2
-2@@ -114,7 +114,7 @@ static void ct3_build_cdat(CDATObject *cdat, Error **errp)static void ct3_load_cdat (CDATObject *cdat,Error **errp) { g_autofree CDATEntry *cdat_st = NULL; - g_autofree char *buf = NULL; + g_autofree uint8_t *buf = NULL;uint8_t sum =0 ;int num_ent;int i =0 , ent =1 ;@@ -171,7 +171,7 @@ static void ct3_load_cdat(CDATObject *cdat, Error **errp) cdat_st[ent].base = hdr; cdat_st[ent].length = hdr->length; -while (buf + i < (char *)cdat_st[ent].base + cdat_st[ent].length) { +while (buf + i < (uint8_t *)cdat_st[ent].base + cdat_st[ent].length) {assert (i < file_size); sum += buf[i++]; }
PATCH 2/3 hw/pci-bridge/cxl_upstream: Fix problem with g_steal_pointer()
In reply to Thomas HuthWhen setting GLIB_VERSION_MAX_ALLOWED to GLIB_VERSION_2_58 or higher, glib adds type safety checks to the g_steal_pointer() macro. This triggers errors in the build_cdat_table() function which uses the g_steal_pointer() for type-casting from one pointer type to the other (which also looks quite weird since the local pointers have all been declared with g_autofree though they are never freed here). Let's fix it by using a proper typecast instead. For making this possible, we have to remove the QEMU_PACKED attribute from some structs since GCC otherwise complains that the source and destination pointer might have different alignment restrictions. Removing the QEMU_PACKED should be fine here since the structs are already naturally aligned. Anyway, add some QEMU_BUILD_BUG_ON() statements to make sure that we've got the right sizes (without padding in the structs).
--- include/hw/cxl/cxl_cdat.h | 8 +++++--- hw/pci-bridge/cxl_upstream.c | 8 ++++---- 2 files changed, 9 insertions(+), 7 deletions(-)
include/hw/cxl/cxl_cdat.h +5 -3
@@ -130,7 +130,8 @@ typedef struct CDATSslbisHeader {uint8_t data_type;uint8_t reserved[3 ];uint64_t entry_base_unit; -}QEMU_PACKED CDATSslbisHeader; +} CDATSslbisHeader; +QEMU_BUILD_BUG_ON (sizeof (CDATSslbisHeader) !=16 );#define CDAT_PORT_ID_USP 0x100/* Switch Scoped Latency and Bandwidth Entry - CDAT Table 10 */
@@ -139,12 +140,13 @@ typedef struct CDATSslbe {uint16_t port_y_id;uint16_t latency_bandwidth;uint16_t reserved; -}QEMU_PACKED CDATSslbe; +} CDATSslbe; +QEMU_BUILD_BUG_ON (sizeof (CDATSslbe) !=8 );typedef struct CDATSslbis {CDATSslbisHeader sslbis_header;CDATSslbe sslbe[]; -} QEMU_PACKED CDATSslbis; +}CDATSslbis ; typedef struct CDATEntry { void *base;
hw/pci-bridge/cxl_upstream.c +4 -4
@@ -192,8 +192,8 @@ enum {static int build_cdat_table (CDATSubHeader ***cdat_table,void *priv) { - g_autofree CDATSslbis *sslbis_latency = NULL; - g_autofree CDATSslbis *sslbis_bandwidth = NULL; +CDATSslbis *sslbis_latency; +CDATSslbis *sslbis_bandwidth;CXLUpstreamPort *us =CXL_USP (priv);PCIBus *bus = &PCI_BRIDGE (us)->sec_bus;int devfn, sslbis_size, i;
@@ -270,8 +270,8 @@ static int build_cdat_table(CDATSubHeader ***cdat_table, void *priv) *cdat_table = g_new0(CDATSubHeader *, CXL_USP_CDAT_NUM_ENTRIES);/* Header always at start of structure */ - (*cdat_table)[CXL_USP_CDAT_SSLBIS_LAT ] =g_steal_pointer (&sslbis_latency); - (*cdat_table)[CXL_USP_CDAT_SSLBIS_BW ] =g_steal_pointer (&sslbis_bandwidth); + (*cdat_table)[CXL_USP_CDAT_SSLBIS_LAT ] = (CDATSubHeader *)sslbis_latency; + (*cdat_table)[CXL_USP_CDAT_SSLBIS_BW ] = (CDATSubHeader *)sslbis_bandwidth;return CXL_USP_CDAT_NUM_ENTRIES ; }
Signature · 1 line
2.44.0
Re: [PATCH 2/3] hw/pci-bridge/cxl_upstream: Fix problem with g_steal_pointer()
Quoted 6 lines · On Mon, 4 Mar 2024 11:44:05 +0100 Thomas Huth <[email protected]> wrote:
When setting
GLIB_VERSION_MAX_ALLOWEDtoGLIB_VERSION_2_58or higher, glib adds type safety checks to theg_steal_pointer()macro. This triggers errors in thebuild_cdat_table()function which uses theg_steal_pointer()for type-casting from one pointer type to the other (which also looks quite weird since the local pointers have all been declared withg_autofreethough they are never freed here).
Left over of an earlier cleanup where I failed to notice there were no longer any error return paths. Great to clean this out.
Quoted 8 lines
Let's fix
it by using a proper typecast instead. For making this possible, we have to remove theQEMU_PACKEDattribute from some structs since GCC otherwise complains that the source and destination pointer might have different alignment restrictions. Removing theQEMU_PACKEDshould be fine here since the structs are already naturally aligned. Anyway, add someQEMU_BUILD_BUG_ON()statements to make sure that we've got the right sizes (without padding in the structs).
Ok. In these cases indeed seem to be fine unpacked. That's not in general true of the CXL spec structures.
Maybe alternative if we run into problems with future versions of these structures will be to define struct CDATSubHeader as packed.
Still we can cross that bridge when we come to it.
Quoted 2 lines
Signed-off-by Thomas Huth <[email protected]>
Quoted 61 lines
--- include/hw/cxl/cxl_cdat.h | 8 +++++--- hw/pci-bridge/cxl_upstream.c | 8 ++++---- 2 files changed, 9 insertions(+), 7 deletions(-)include/hw/cxl/cxl_cdat.h +5
-3@@ -130,7 +130,8 @@ typedef struct CDATSslbisHeader {uint8_t data_type;uint8_t reserved[3 ];uint64_t entry_base_unit; -}QEMU_PACKED CDATSslbisHeader; +} CDATSslbisHeader; +QEMU_BUILD_BUG_ON (sizeof (CDATSslbisHeader) !=16 );#define CDAT_PORT_ID_USP 0x100/* Switch Scoped Latency and Bandwidth Entry - CDAT Table 10 */ @@ -139,12 +140,13 @@ typedef struct CDATSslbe {uint16_t port_y_id;uint16_t latency_bandwidth;uint16_t reserved; -}QEMU_PACKED CDATSslbe; +} CDATSslbe; +QEMU_BUILD_BUG_ON (sizeof (CDATSslbe) !=8 );typedef struct CDATSslbis {CDATSslbisHeader sslbis_header;CDATSslbe sslbe[]; -} QEMU_PACKED CDATSslbis; +}CDATSslbis ; typedef struct CDATEntry { void *base;hw/pci-bridge/cxl_upstream.c +4
-4@@ -192,8 +192,8 @@ enum {static int build_cdat_table (CDATSubHeader ***cdat_table,void *priv) { - g_autofree CDATSslbis *sslbis_latency = NULL; - g_autofree CDATSslbis *sslbis_bandwidth = NULL; +CDATSslbis *sslbis_latency; +CDATSslbis *sslbis_bandwidth;CXLUpstreamPort *us =CXL_USP (priv);PCIBus *bus = &PCI_BRIDGE (us)->sec_bus;int devfn, sslbis_size, i;@@ -270,8 +270,8 @@ static int build_cdat_table(CDATSubHeader ***cdat_table, void *priv) *cdat_table = g_new0(CDATSubHeader *, CXL_USP_CDAT_NUM_ENTRIES);/* Header always at start of structure */ - (*cdat_table)[CXL_USP_CDAT_SSLBIS_LAT ] =g_steal_pointer (&sslbis_latency); - (*cdat_table)[CXL_USP_CDAT_SSLBIS_BW ] =g_steal_pointer (&sslbis_bandwidth); + (*cdat_table)[CXL_USP_CDAT_SSLBIS_LAT ] = (CDATSubHeader *)sslbis_latency; + (*cdat_table)[CXL_USP_CDAT_SSLBIS_BW ] = (CDATSubHeader *)sslbis_bandwidth;return CXL_USP_CDAT_NUM_ENTRIES ; }
Re: [PATCH 2/3] hw/pci-bridge/cxl_upstream: Fix problem with g_steal_pointer()
Quoted 22 lines · On Mon, 4 Mar 2024 15:06:51 +0000 Jonathan Cameron…
Quoted 6 lines · On Mon, 4 Mar 2024 11:44:05 +0100 Thomas Huth <[email protected]> wrote:
When setting
GLIB_VERSION_MAX_ALLOWEDtoGLIB_VERSION_2_58or higher, glib adds type safety checks to theg_steal_pointer()macro. This triggers errors in thebuild_cdat_table()function which uses theg_steal_pointer()for type-casting from one pointer type to the other (which also looks quite weird since the local pointers have all been declared withg_autofreethough they are never freed here).Left over of an earlier cleanup where I failed to notice there were no longer any error return paths. Great to clean this out.
Quoted 8 lines
Let's fix
it by using a proper typecast instead. For making this possible, we have to remove theQEMU_PACKEDattribute from some structs since GCC otherwise complains that the source and destination pointer might have different alignment restrictions. Removing theQEMU_PACKEDshould be fine here since the structs are already naturally aligned. Anyway, add someQEMU_BUILD_BUG_ON()statements to make sure that we've got the right sizes (without padding in the structs).
Quoted 6 lines
Ok. In these cases indeed seem to be fine unpacked. That's not in general true of the CXL spec structures.
Maybe alternative if we run into problems with future versions of these structures will be to define struct CDATSubHeader as packed.Still we can cross that bridge when we come to it.
I notice in next patch we could just assign the pointer to the contained structure header. Maybe a cleaner solution and would make it clear why it is valid to assign this lot to a pointer of the CDATSubHeader type.
Jonathan
Quoted 67 lines
Quoted 3 lines
Signed-off-by Thomas Huth <[email protected]>Reviewed-by Jonathan Cameron <[email protected]>Quoted 61 lines
--- include/hw/cxl/cxl_cdat.h | 8 +++++--- hw/pci-bridge/cxl_upstream.c | 8 ++++---- 2 files changed, 9 insertions(+), 7 deletions(-)include/hw/cxl/cxl_cdat.h +5
-3@@ -130,7 +130,8 @@ typedef struct CDATSslbisHeader {uint8_t data_type;uint8_t reserved[3 ];uint64_t entry_base_unit; -}QEMU_PACKED CDATSslbisHeader; +} CDATSslbisHeader; +QEMU_BUILD_BUG_ON (sizeof (CDATSslbisHeader) !=16 );#define CDAT_PORT_ID_USP 0x100/* Switch Scoped Latency and Bandwidth Entry - CDAT Table 10 */ @@ -139,12 +140,13 @@ typedef struct CDATSslbe {uint16_t port_y_id;uint16_t latency_bandwidth;uint16_t reserved; -}QEMU_PACKED CDATSslbe; +} CDATSslbe; +QEMU_BUILD_BUG_ON (sizeof (CDATSslbe) !=8 );typedef struct CDATSslbis {CDATSslbisHeader sslbis_header;CDATSslbe sslbe[]; -} QEMU_PACKED CDATSslbis; +}CDATSslbis ; typedef struct CDATEntry { void *base;hw/pci-bridge/cxl_upstream.c +4
-4@@ -192,8 +192,8 @@ enum {static int build_cdat_table (CDATSubHeader ***cdat_table,void *priv) { - g_autofree CDATSslbis *sslbis_latency = NULL; - g_autofree CDATSslbis *sslbis_bandwidth = NULL; +CDATSslbis *sslbis_latency; +CDATSslbis *sslbis_bandwidth;CXLUpstreamPort *us =CXL_USP (priv);PCIBus *bus = &PCI_BRIDGE (us)->sec_bus;int devfn, sslbis_size, i;@@ -270,8 +270,8 @@ static int build_cdat_table(CDATSubHeader ***cdat_table, void *priv) *cdat_table = g_new0(CDATSubHeader *, CXL_USP_CDAT_NUM_ENTRIES);/* Header always at start of structure */ - (*cdat_table)[CXL_USP_CDAT_SSLBIS_LAT ] =g_steal_pointer (&sslbis_latency); - (*cdat_table)[CXL_USP_CDAT_SSLBIS_BW ] =g_steal_pointer (&sslbis_bandwidth); + (*cdat_table)[CXL_USP_CDAT_SSLBIS_LAT ] = (CDATSubHeader *)sslbis_latency; + (*cdat_table)[CXL_USP_CDAT_SSLBIS_BW ] = (CDATSubHeader *)sslbis_bandwidth;return CXL_USP_CDAT_NUM_ENTRIES ; }
PATCH 3/3 hw/mem/cxl_type3: Fix problem with g_steal_pointer()
In reply to Thomas HuthWhen setting GLIB_VERSION_MAX_ALLOWED to GLIB_VERSION_2_58 or higher, glib adds type safety checks to the g_steal_pointer() macro. This triggers errors in the ct3_build_cdat_entries_for_mr() function which uses the g_steal_pointer() for type-casting from one pointer type to the other (which also looks quite weird since the local pointers have all been declared with g_autofree though they are never freed here). Fix it by using a proper typecast instead. For making this possible, we have to remove the QEMU_PACKED attribute from some structs since GCC otherwise complains that the source and destination pointer might have different alignment restrictions. Removing the QEMU_PACKED should be fine here since the structs are already naturally aligned. Anyway, add some QEMU_BUILD_BUG_ON() statements to make sure that we've got the right sizes (without padding in the structs).
--- include/hw/cxl/cxl_cdat.h | 9 ++++++--- hw/mem/cxl_type3.c | 24 ++++++++++++------------ 2 files changed, 18 insertions(+), 15 deletions(-)
include/hw/cxl/cxl_cdat.h +6 -3
@@ -82,7 +82,8 @@ typedef struct CDATDsmas {uint16_t reserved;uint64_t DPA_base;uint64_t DPA_length; -}QEMU_PACKED CDATDsmas; +} CDATDsmas; +QEMU_BUILD_BUG_ON (sizeof (CDATDsmas) !=24 );/* Device Scoped Latency and Bandwidth Information Structure - CDAT Table 5 */ typedef struct CDATDslbis {
@@ -95,7 +96,8 @@ typedef struct CDATDslbis {uint64_t entry_base_unit;uint16_t entry[3 ];uint16_t reserved2; -}QEMU_PACKED CDATDslbis; +} CDATDslbis; +QEMU_BUILD_BUG_ON (sizeof (CDATDslbis) !=24 );/* Device Scoped Memory Side Cache Information Structure - CDAT Table 6 */ typedef struct CDATDsmscis {
@@ -122,7 +124,8 @@ typedef struct CDATDsemts {uint16_t reserved;uint64_t DPA_offset;uint64_t DPA_length; -}QEMU_PACKED CDATDsemts; +} CDATDsemts; +QEMU_BUILD_BUG_ON (sizeof (CDATDsemts) !=24 );/* Switch Scoped Latency and Bandwidth Information Structure - CDAT Table 9 */ typedef struct CDATSslbisHeader {
hw/mem/cxl_type3.c +12 -12
@@ -46,12 +46,12 @@ static void ct3_build_cdat_entries_for_mr(CDATSubHeader **cdat_table, int dsmad_handle, MemoryRegion *mr, bool is_pmem, uint64_t dpa_base) { - g_autofree CDATDsmas *dsmas = NULL; - g_autofree CDATDslbis *dslbis0 = NULL; - g_autofree CDATDslbis *dslbis1 = NULL; - g_autofree CDATDslbis *dslbis2 = NULL; - g_autofree CDATDslbis *dslbis3 = NULL; - g_autofree CDATDsemts *dsemts = NULL; + CDATDsmas *dsmas; +CDATDslbis *dslbis0; +CDATDslbis *dslbis1; +CDATDslbis *dslbis2; +CDATDslbis *dslbis3; +CDATDsemts *dsemts; dsmas =g_malloc (sizeof (*dsmas)); *dsmas = (CDATDsmas) {
@@ -135,12 +135,12 @@ static void ct3_build_cdat_entries_for_mr(CDATSubHeader **cdat_table, };/* Header always at start of structure */ - cdat_table[CT3_CDAT_DSMAS ] =g_steal_pointer (&dsmas); - cdat_table[CT3_CDAT_DSLBIS0 ] =g_steal_pointer (&dslbis0); - cdat_table[CT3_CDAT_DSLBIS1 ] =g_steal_pointer (&dslbis1); - cdat_table[CT3_CDAT_DSLBIS2 ] =g_steal_pointer (&dslbis2); - cdat_table[CT3_CDAT_DSLBIS3 ] =g_steal_pointer (&dslbis3); - cdat_table[CT3_CDAT_DSEMTS ] =g_steal_pointer (&dsemts); + cdat_table[CT3_CDAT_DSMAS ] = (CDATSubHeader *)dsmas; + cdat_table[CT3_CDAT_DSLBIS0 ] = (CDATSubHeader *)dslbis0; + cdat_table[CT3_CDAT_DSLBIS1 ] = (CDATSubHeader *)dslbis1; + cdat_table[CT3_CDAT_DSLBIS2 ] = (CDATSubHeader *)dslbis2; + cdat_table[CT3_CDAT_DSLBIS3 ] = (CDATSubHeader *)dslbis3; + cdat_table[CT3_CDAT_DSEMTS ] = (CDATSubHeader *)dsemts; }static int ct3_build_cdat_table (CDATSubHeader ***cdat_table,void *priv)
Signature · 1 line
2.44.0
Re: [PATCH 3/3] hw/mem/cxl_type3: Fix problem with g_steal_pointer()
Quoted 5 lines · On Mon, 4 Mar 2024 11:44:06 +0100 Thomas Huth <[email protected]> wrote:
When setting
GLIB_VERSION_MAX_ALLOWEDtoGLIB_VERSION_2_58or higher, glib adds type safety checks to theg_steal_pointer()macro. This triggers errors in thect3_build_cdat_entries_for_mr()function which uses theg_steal_pointer()for type-casting from one pointer type to the other (which also looks quite weird since the local pointers have
Quoted 8 lines
all been declared with
g_autofreethough they are never freed here). Fix it by using a proper typecast instead. For making this possible, we have to remove theQEMU_PACKEDattribute from some structs since GCC otherwise complains that the source and destination pointer might have different alignment restrictions. Removing theQEMU_PACKEDshould be fine here since the structs are already naturally aligned. Anyway, add someQEMU_BUILD_BUG_ON()statements to make sure that we've got the right sizes (without padding in the structs).
I missed these as well when getting rid of the false handling of failure of g_new0 calls.
Another alternative would be to point to the head structures rather than the containing structure - would avoid need to cast. That might be neater? Should I think also remove the alignment question?
Quoted 65 lines
Signed-off-by Thomas Huth <[email protected]>--- include/hw/cxl/cxl_cdat.h | 9 ++++++--- hw/mem/cxl_type3.c | 24 ++++++++++++------------ 2 files changed, 18 insertions(+), 15 deletions(-)include/hw/cxl/cxl_cdat.h +6
-3@@ -82,7 +82,8 @@ typedef struct CDATDsmas {uint16_t reserved;uint64_t DPA_base;uint64_t DPA_length; -}QEMU_PACKED CDATDsmas; +} CDATDsmas; +QEMU_BUILD_BUG_ON (sizeof (CDATDsmas) !=24 );/* Device Scoped Latency and Bandwidth Information Structure - CDAT Table 5 */ typedef struct CDATDslbis {@@ -95,7 +96,8 @@ typedef struct CDATDslbis {uint64_t entry_base_unit;uint16_t entry[3 ];uint16_t reserved2; -}QEMU_PACKED CDATDslbis; +} CDATDslbis; +QEMU_BUILD_BUG_ON (sizeof (CDATDslbis) !=24 );/* Device Scoped Memory Side Cache Information Structure - CDAT Table 6 */ typedef struct CDATDsmscis {@@ -122,7 +124,8 @@ typedef struct CDATDsemts {uint16_t reserved;uint64_t DPA_offset;uint64_t DPA_length; -}QEMU_PACKED CDATDsemts; +} CDATDsemts; +QEMU_BUILD_BUG_ON (sizeof (CDATDsemts) !=24 );/* Switch Scoped Latency and Bandwidth Information Structure - CDAT Table 9 */ typedef struct CDATSslbisHeader {hw/mem/cxl_type3.c +6
-6@@ -46,12 +46,12 @@ static void ct3_build_cdat_entries_for_mr(CDATSubHeader **cdat_table, int dsmad_handle, MemoryRegion *mr, bool is_pmem, uint64_t dpa_base) { - g_autofree CDATDsmas *dsmas = NULL; - g_autofree CDATDslbis *dslbis0 = NULL; - g_autofree CDATDslbis *dslbis1 = NULL; - g_autofree CDATDslbis *dslbis2 = NULL; - g_autofree CDATDslbis *dslbis3 = NULL; - g_autofree CDATDsemts *dsemts = NULL; + CDATDsmas *dsmas; +CDATDslbis *dslbis0; +CDATDslbis *dslbis1; +CDATDslbis *dslbis2; +CDATDslbis *dslbis3; +CDATDsemts *dsemts; dsmas =g_malloc (sizeof (*dsmas)); *dsmas = (CDATDsmas) {
hw/mem/cxl_type3.c static void ct3_build_cdat_entries_for_mr(CDATSubHeader **cdat_table, · line 138
Quoted 10 lines
hw/mem/cxl_type3.c +1
-6@@ -135,12 +135,12 @@ static void ct3_build_cdat_entries_for_mr(CDATSubHeader **cdat_table, };/* Header always at start of structure */ - cdat_table[CT3_CDAT_DSMAS ] =g_steal_pointer (&dsmas); - cdat_table[CT3_CDAT_DSLBIS0 ] =g_steal_pointer (&dslbis0); - cdat_table[CT3_CDAT_DSLBIS1 ] =g_steal_pointer (&dslbis1); - cdat_table[CT3_CDAT_DSLBIS2 ] =g_steal_pointer (&dslbis2); - cdat_table[CT3_CDAT_DSLBIS3 ] =g_steal_pointer (&dslbis3); - cdat_table[CT3_CDAT_DSEMTS ] =g_steal_pointer (&dsemts); + cdat_table[CT3_CDAT_DSMAS ] = (CDATSubHeader *)dsmas;
Could do
cdat_table[CT3_CDAT_DSMAS] = &dsmas->header;
etc
Quoted 8 lines
hw/mem/cxl_type3.c +5
-0+ cdat_table[CT3_CDAT_DSLBIS0 ] = (CDATSubHeader *)dslbis0; + cdat_table[CT3_CDAT_DSLBIS1 ] = (CDATSubHeader *)dslbis1; + cdat_table[CT3_CDAT_DSLBIS2 ] = (CDATSubHeader *)dslbis2; + cdat_table[CT3_CDAT_DSLBIS3 ] = (CDATSubHeader *)dslbis3; + cdat_table[CT3_CDAT_DSEMTS ] = (CDATSubHeader *)dsemts; }static int ct3_build_cdat_table (CDATSubHeader ***cdat_table,void *priv)
Re: [PATCH 3/3] hw/mem/cxl_type3: Fix problem with g_steal_pointer()
Quoted 17 lines · Jonathan Cameron
Quoted 5 lines · On Mon, 4 Mar 2024 11:44:06 +0100 Thomas Huth <[email protected]> wrote:
When setting
GLIB_VERSION_MAX_ALLOWEDtoGLIB_VERSION_2_58or higher, glib adds type safety checks to theg_steal_pointer()macro. This triggers errors in thect3_build_cdat_entries_for_mr()function which uses theg_steal_pointer()for type-casting from one pointer type to the other (which also looks quite weird since the local pointers haveQuoted 8 lines
all been declared with
g_autofreethough they are never freed here). Fix it by using a proper typecast instead. For making this possible, we have to remove theQEMU_PACKEDattribute from some structs since GCC otherwise complains that the source and destination pointer might have different alignment restrictions. Removing theQEMU_PACKEDshould be fine here since the structs are already naturally aligned. Anyway, add someQEMU_BUILD_BUG_ON()statements to make sure that we've got the right sizes (without padding in the structs).
Quoted 7 lines
I missed these as well when getting rid of the false handling of failure of
g_new0calls.Another alternative would be to point to the head structures rather than the containing structure - would avoid need to cast. That might be neater? Should I think also remove the alignment question?
I gave it a try, but it does not help against the alignment issue, I still get:
../../devel/qemu/hw/mem/cxl_type3.c: In function ‘ct3_build_cdat_entries_for_mr’:../../devel/qemu/hw/mem/cxl_type3.c:138:34: error: taking address of packed member of ‘struct CDATDsmas’ may result in an unaligned pointer value [-Werror=address-of-packed-member]
138 | cdat_table[CT3_CDAT_DSMAS] = &dsmas->header;
| ^~~~~~~~~~~~~~From my experience, it's better anyway to avoid __attribute__((packed)) on structures unless it is really really required. At least we should avoid it as good as possible as long as we still support running QEMU on Sparc hosts (that don't support misaligned memory accesses), since otherwise you can end up with non-working code there, see e.g.:
https://www.mail-archive.com/[email protected]/msg439899.html
or:
https://gitlab.com/qemu-project/qemu/-/commit/cb89b349074310ff9eb7ebe18a
Thus I'd rather prefer to keep this patch as it is right now.
Thomas
Jonathan Cameron via
Reviewed-by Jonathan CameronRe: [PATCH 3/3] hw/mem/cxl_type3: Fix problem with g_steal_pointer()
Quoted 44 lines · On Tue, 5 Mar 2024 08:27:52 +0100 Thomas Huth <[email protected]> wrote:
Quoted 17 lines · Jonathan Cameron
Quoted 5 lines · On Mon, 4 Mar 2024 11:44:06 +0100 Thomas Huth <[email protected]> wrote:
When setting
GLIB_VERSION_MAX_ALLOWEDtoGLIB_VERSION_2_58or higher, glib adds type safety checks to theg_steal_pointer()macro. This triggers errors in thect3_build_cdat_entries_for_mr()function which uses theg_steal_pointer()for type-casting from one pointer type to the other (which also looks quite weird since the local pointers haveQuoted 8 lines
all been declared with
g_autofreethough they are never freed here). Fix it by using a proper typecast instead. For making this possible, we have to remove theQEMU_PACKEDattribute from some structs since GCC otherwise complains that the source and destination pointer might have different alignment restrictions. Removing theQEMU_PACKEDshould be fine here since the structs are already naturally aligned. Anyway, add someQEMU_BUILD_BUG_ON()statements to make sure that we've got the right sizes (without padding in the structs).Quoted 7 lines
I missed these as well when getting rid of the false handling of failure of
g_new0calls.Another alternative would be to point to the head structures rather than the containing structure - would avoid need to cast. That might be neater? Should I think also remove the alignment question?
I gave it a try, but it does not help against the alignment issue, I still get:
../../devel/qemu/hw/mem/cxl_type3.c: In function ‘ct3_build_cdat_entries_for_mr’:../../devel/qemu/hw/mem/cxl_type3.c:138:34: error: taking address of packed member of ‘struct CDATDsmas’ may result in an unaligned pointer value [-Werror=address-of-packed-member]138 | cdat_table[CT3_CDAT_DSMAS] = &dsmas->header; | ^~~~~~~~~~~~~~From my experience, it's better anyway to avoid __attribute__((packed)) on
structures unless it is really really required. At least we should avoid it as good as possible as long as we still support running QEMU on Sparc hosts (that don't support misaligned memory accesses), since otherwise you can end up with non-working code there, see e.g.:https://www.mail-archive.com/[email protected]/msg439899.html
Quoted 7 lines
or:
https://gitlab.com/qemu-project/qemu/-/commit/cb89b349074310ff9eb7ebe18a
Thus I'd rather prefer to keep this patch as it is right now.
Thomas
Fair enough.
Quoted 1 line
Michael Tokarev
AppliedQuoted 6 lines · Thomas Huth
When setting
GLIB_VERSION_MAX_ALLOWEDtoGLIB_VERSION_2_58or higher (which we'll certainly do in the not too distant future), glib adds type safety checks to theg_steal_pointer()macro. This triggers errors in the cxl code since the pointer types do not always match here. Let's fix those errors now so we can switch to a newer version of the glib in a future version of QEMU.
Picked up for qemu-trivial, thank you!
/mjt