spapr_pci: various cleanups and improvements
cover spapr_pci: various cleanups and improvements
Before resuming the huge work on PHB hotplug, here are some patches that maybe worth to apply.
Patches 1 to 3 are basic improvements.
Patch 4 and 5 may be a bit controversial. Everywhere in the spapr code where we build an FDT portion, libfdt failures cause QEMU to exit, even on hotplug paths. Only spapr_pci doesn't do that and propagates the error instead. My understanding is that a failure when building the FDT is likely to happen because of a bug in QEMU.
Hence the choice to convert spapr_pci to do like the others. We may even consider changing _FDT() to abort() instead of exit().
Alternatively, if libfdt failures shouldn't be necessarily fatal, especially on post-realize paths, then we should probably introduce an _FDT_ERR() helper to propagate errors. And use it in may places where we currently terminate QEMU: memory hotplug, CPU hotplug, CAS, machine reset...
--
Greg
---
Greg Kurz (5):
spapr_pci: drop useless check in spapr_phb_vfio_get_loc_code()
spapr_pci: drop useless check in spapr_populate_pci_child_dt()
spapr_pci: use g_strdup_printf()
spapr_pci: use the common _FDT() helper
spapr_pci: handle FDT creation errors with _FDT()
hw/ppc/spapr_pci.c | 69 ++++++++++++++++------------------------------------ 1 file changed, 21 insertions(+), 48 deletions(-)
PATCH 1/5 spapr_pci: drop useless check in spapr_phb_vfio_get_loc_code()
g_strdup_printf() either returns a non-null pointer, either aborts if it failed to allocate memory.
--- hw/ppc/spapr_pci.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
hw/ppc/spapr_pci.c +2 -2
@@ -766,7 +766,7 @@ static char *spapr_phb_vfio_get_loc_code(sPAPRPHBState *sphb, PCIDevice *pdev)/* Construct the path of the file that will give us the DT location */ path =g_strdup_printf ("/sys/bus/pci/devices/%s/devspec" , host);g_free (host); -if (!path || !g_file_get_contents (path, &buf,NULL ,NULL )) { +if (!g_file_get_contents (path, &buf,NULL ,NULL )) { goto err_out; }g_free (path);
@@ -774,7 +774,7 @@ static char *spapr_phb_vfio_get_loc_code(sPAPRPHBState *sphb, PCIDevice *pdev)/* Construct and read from host device tree the loc-code */ path =g_strdup_printf ("/proc/device-tree%s/ibm,loc-code" , buf);g_free (buf); -if (!path || !g_file_get_contents (path, &buf,NULL ,NULL )) { +if (!g_file_get_contents (path, &buf,NULL ,NULL )) { goto err_out; }return buf;
Re: [Qemu-devel] [PATCH 1/5] spapr_pci: drop useless check in spapr_phb_vfio_get_loc_code()
Quoted 4 lines · Greg Kurz
g_strdup_printf()either returns a non-null pointer, either aborts if it failed to allocate memory.Signed-off-by Greg Kurz <[email protected]>
Quoted 28 lines
--- hw/ppc/spapr_pci.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)hw/ppc/spapr_pci.c +2
-2@@ -766,7 +766,7 @@ static char *spapr_phb_vfio_get_loc_code(sPAPRPHBState *sphb, PCIDevice *pdev)/* Construct the path of the file that will give us the DT location */ path =g_strdup_printf ("/sys/bus/pci/devices/%s/devspec" , host);g_free (host); -if (!path || !g_file_get_contents (path, &buf,NULL ,NULL )) { +if (!g_file_get_contents (path, &buf,NULL ,NULL )) { goto err_out; }g_free (path);@@ -774,7 +774,7 @@ static char *spapr_phb_vfio_get_loc_code(sPAPRPHBState *sphb, PCIDevice *pdev)/* Construct and read from host device tree the loc-code */ path =g_strdup_printf ("/proc/device-tree%s/ibm,loc-code" , buf);g_free (buf); -if (!path || !g_file_get_contents (path, &buf,NULL ,NULL )) { +if (!g_file_get_contents (path, &buf,NULL ,NULL )) { goto err_out; }return buf;
PATCH 2/5 spapr_pci: drop useless check in spapr_populate_pci_child_dt()
In reply to Greg Kurzspapr_phb_get_loc_code() either returns a non-null pointer, either aborts if g_strdup_printf() failed to allocate memory.
--- hw/ppc/spapr_pci.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
hw/ppc/spapr_pci.c +1 -5
@@ -1282,12 +1282,8 @@ static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset,pci_find_device_name ((ccode >>16 ) &0xff , (ccode >>8 ) &0xff , ccode & 0xff))); - buf =spapr_phb_get_loc_code (sphb, dev); -if (!buf) { -error_report ("Failed setting the ibm,loc-code" ); -return -1 ; - } + buf =spapr_phb_get_loc_code (sphb, dev); err =fdt_setprop_string (fdt, offset,"ibm,loc-code" , buf);g_free (buf);if (err <0 ) {
Re: [Qemu-devel] [PATCH 2/5] spapr_pci: drop useless check in spapr_populate_pci_child_dt()
Quoted 4 lines · Greg Kurz
spapr_phb_get_loc_code()either returns a non-null pointer, either aborts ifg_strdup_printf()failed to allocate memory.Signed-off-by Greg Kurz <[email protected]>
Quoted 24 lines
--- hw/ppc/spapr_pci.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)hw/ppc/spapr_pci.c +1
-5@@ -1282,12 +1282,8 @@ static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset,pci_find_device_name ((ccode >>16 ) &0xff , (ccode >>8 ) &0xff , ccode & 0xff))); - buf =spapr_phb_get_loc_code (sphb, dev); -if (!buf) { -error_report ("Failed setting the ibm,loc-code" ); -return -1 ; - } + buf =spapr_phb_get_loc_code (sphb, dev); err =fdt_setprop_string (fdt, offset,"ibm,loc-code" , buf);g_free (buf);if (err <0 ) {
PATCH 3/5 spapr_pci: use g_strdup_printf()
In reply to Greg KurzBuilding strings with g_strdup_printf() instead of snprintf() is a QEMU common practice.
--- hw/ppc/spapr_pci.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
hw/ppc/spapr_pci.c +10 -9
@@ -61,8 +61,6 @@#define RTAS_TYPE_MSI 1#define RTAS_TYPE_MSIX 2 -#define FDT_NAME_MAX 128 -#define _FDT (exp) \ do { \ int ret = (exp); \
@@ -1194,7 +1192,7 @@ static const char *pci_find_device_name(uint8_t class, uint8_t subclass,return name; } -static void pci_get_node_name (char *nodename,int len,PCIDevice *dev) +static gchar *pci_get_node_name (PCIDevice *dev) {int slot =PCI_SLOT (dev->devfn);int func =PCI_FUNC (dev->devfn);
@@ -1205,9 +1203,9 @@ static void pci_get_node_name(char *nodename, int len, PCIDevice *dev) ccode & 0xff);if (func !=0 ) { -snprintf (nodename, len,"%s@%x,%x" , name, slot, func); +return g_strdup_printf ("%s@%x,%x" , name, slot, func); }else { -snprintf (nodename, len,"%s@%x" , name, slot); +return g_strdup_printf ("%s@%x" , name, slot); } }
@@ -1325,10 +1323,12 @@ static int spapr_create_pci_child_dt(sPAPRPHBState *phb, PCIDevice *dev, void *fdt, int node_offset) { int offset, ret; -char nodename[FDT_NAME_MAX ]; +gchar *nodename; -pci_get_node_name (nodename,FDT_NAME_MAX , dev); + nodename =pci_get_node_name (dev); offset =fdt_add_subnode (fdt, node_offset, nodename); +g_free (nodename); + ret =spapr_populate_pci_child_dt (dev, fdt, offset, phb);g_assert (!ret);
@@ -2072,7 +2072,7 @@ int spapr_populate_pci_dt(sPAPRPHBState *phb, void *fdt) {int bus_off, i, j, ret; -char nodename[FDT_NAME_MAX ]; +gchar *nodename;uint32_t bus_range[] = {cpu_to_be32 (0 ),cpu_to_be32 (0xff ) };struct {uint32_t hi;
@@ -2121,8 +2121,9 @@ int spapr_populate_pci_dt(sPAPRPHBState *phb,sPAPRFDT s_fdt;/* Start populating the FDT */ -snprintf (nodename,FDT_NAME_MAX ,"pci@%" PRIx64, phb->buid); + nodename =g_strdup_printf ("pci@%" PRIx64, phb->buid); bus_off =fdt_add_subnode (fdt,0 , nodename); +g_free (nodename);if (bus_off <0 ) {return bus_off; }
PATCH 4/5 spapr_pci: use the common _FDT() helper
In reply to Greg KurzAll other users in hw/ppc already consider an error when building the FDT to be fatal, even on hotplug paths. There's no valid reason for spapr_pci to behave differently. So let's used the common _FDT() helper which terminates QEMU when libfdt fails.
--- hw/ppc/spapr_pci.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-)
hw/ppc/spapr_pci.c +1 -9
@@ -40,7 +40,7 @@#include "trace.h" #include "qemu/error-report.h" #include "qapi/qmp/qerror.h" - +#include "hw/ppc/fdt.h" #include "hw/pci/pci_bridge.h" #include "hw/pci/pci_bus.h" #include "hw/pci/pci_ids.h"
@@ -61,14 +61,6 @@#define RTAS_TYPE_MSI 1#define RTAS_TYPE_MSIX 2 -#define _FDT (exp) \ - do { \ - int ret = (exp); \ - if (ret < 0) { \ - return ret; \ - } \ - } while (0) -sPAPRPHBState *spapr_pci_find_phb (sPAPRMachineState *spapr,uint64_t buid) {sPAPRPHBState *sphb;
PATCH 5/5 spapr_pci: handle FDT creation errors with _FDT()
In reply to Greg Kurzlibfdt failures when creating the FDT should cause QEMU to terminate.
Let's use the _FDT() macro which does just that instead of propagating the error to the caller. spapr_populate_pci_child_dt() no longer needs to return a value in this case.
Note that, on the way, this get rids of the following nonsensical lines:
g_assert(!ret);
if (ret) {--- hw/ppc/spapr_pci.c | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-)
hw/ppc/spapr_pci.c +7 -23
@@ -1204,12 +1204,12 @@ static gchar *pci_get_node_name(PCIDevice *dev)static uint32_t spapr_phb_get_pci_drc_index (sPAPRPHBState *phb,PCIDevice *pdev); -static int spapr_populate_pci_child_dt (PCIDevice *dev,void *fdt,int offset, +static void spapr_populate_pci_child_dt (PCIDevice *dev,void *fdt,int offset,sPAPRPHBState *sphb) {ResourceProps rp;bool is_bridge = false; -int pci_status, err; +int pci_status;char *buf =NULL ;uint32_t drc_index =spapr_phb_get_pci_drc_index (sphb, dev);uint32_t ccode =pci_default_read_config (dev,PCI_CLASS_PROG ,3 );
@@ -1274,11 +1274,8 @@ static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset, ccode & 0xff))); buf =spapr_phb_get_loc_code (sphb, dev); - err =fdt_setprop_string (fdt, offset,"ibm,loc-code" , buf); +_FDT (fdt_setprop_string (fdt, offset,"ibm,loc-code" , buf));g_free (buf); -if (err <0 ) { -return err; - }if (drc_index) {_FDT (fdt_setprop_cell (fdt, offset,"ibm,my-drc-index" , drc_index));
@@ -1306,27 +1303,21 @@ static int spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset,if (sphb->pcie_ecs &&pci_is_express (dev)) {_FDT (fdt_setprop_cell (fdt, offset,"ibm,pci-config-space-type" ,0x1 )); } - -return 0 ; }/* create OF node for pci device and required OF DT properties */ static int spapr_create_pci_child_dt (sPAPRPHBState *phb,PCIDevice *dev,void *fdt,int node_offset) { -int offset, ret; +int offset;gchar *nodename; nodename =pci_get_node_name (dev); - offset =fdt_add_subnode (fdt, node_offset, nodename); +_FDT (offset =fdt_add_subnode (fdt, node_offset, nodename));g_free (nodename); - ret =spapr_populate_pci_child_dt (dev, fdt, offset, phb); +spapr_populate_pci_child_dt (dev, fdt, offset, phb); -g_assert (!ret); -if (ret) { -return 0 ; - }return offset; }
@@ -1416,10 +1407,6 @@ static void spapr_pci_plug(HotplugHandler *plug_handler, fdt =create_device_tree (&fdt_size); fdt_start_offset =spapr_create_pci_child_dt (phb, pdev, fdt,0 ); -if (!fdt_start_offset) { -error_setg (&local_err,"Failed to create pci child device tree node" ); - goto out; - }spapr_drc_attach (drc,DEVICE (pdev), fdt, fdt_start_offset, &local_err);if (local_err) {
@@ -2114,11 +2101,8 @@ int spapr_populate_pci_dt(sPAPRPHBState *phb,/* Start populating the FDT */ nodename =g_strdup_printf ("pci@%" PRIx64, phb->buid); - bus_off =fdt_add_subnode (fdt,0 , nodename); +_FDT (bus_off =fdt_add_subnode (fdt,0 , nodename));g_free (nodename); -if (bus_off <0 ) { -return bus_off; - }/* Write PHB properties */ _FDT (fdt_setprop_string (fdt, bus_off,"device_type" ,"pci" ));
David Gibson
AppliedQuoted 4 lines · Greg Kurz
Before resuming the huge work on PHB hotplug, here are some patches that maybe worth to apply.
Patches 1 to 3 are basic improvements.
I've applied these to ppc-for-2.11.
Quoted 5 lines
Patch 4 and 5 may be a bit controversial. Everywhere in the spapr code where we build an FDT portion, libfdt failures cause QEMU to exit, even on hotplug paths. Only
spapr_pcidoesn't do that and propagates the error instead. My understanding is that a failure when building the FDT is likely to happen because of a bug in QEMU.
Still looking at these.
Quoted 10 lines
Hence the choice to convert
spapr_pcito do like the others. We may even consider changing_FDT()toabort()instead ofexit().Alternatively, if libfdt failures shouldn't be necessarily fatal, especially on post-realize paths, then we should probably introduce an
_FDT_ERR()helper to propagate errors. And use it in may places where we currently terminate QEMU: memory hotplug, CPU hotplug, CAS, machine reset...
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Quoted 11 lines · Greg Kurz
Before resuming the huge work on PHB hotplug, here are some patches that maybe worth to apply.
Patches 1 to 3 are basic improvements.
Patch 4 and 5 may be a bit controversial. Everywhere in the spapr code where we build an FDT portion, libfdt failures cause QEMU to exit, even on hotplug paths. Only
spapr_pcidoesn't do that and propagates the error instead. My understanding is that a failure when building the FDT is likely to happen because of a bug in QEMU.
Quoted 8 lines
Hence the choice to convert
spapr_pcito do like the others. We may even consider changing_FDT()toabort()instead ofexit().Alternatively, if libfdt failures shouldn't be necessarily fatal, especially on post-realize paths, then we should probably introduce an
_FDT_ERR()helper to propagate errors. And use it in may places where we currently terminate QEMU: memory hotplug, CPU hotplug, CAS, machine reset...
I've applied 4 & 5 as well now. If I get to it, I hope to make them obsolete by replacing most of the fdt wrangling with a qemu internal live tree representation, but in the meantime it's still a reasonable cleanup.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson