Message ID | 20240129164514.73104-1-philmd@linaro.org |
---|---|
Headers | show |
Series | hw, target: Prefer fast cpu_env() over slower CPU QOM cast macro | expand |
On 29/1/24 17:44, Philippe Mathieu-Daudé wrote: > Mechanical patch produced running the command documented > in scripts/coccinelle/cpu_env.cocci_template header. > > Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> > --- > target/i386/hvf/vmx.h | 13 ++------- > hw/i386/fw_cfg.c | 3 +- > hw/i386/vmmouse.c | 6 ++-- > hw/i386/xen/xen-hvm.c | 3 +- > target/i386/arch_dump.c | 11 ++------ > target/i386/arch_memory_mapping.c | 3 +- > target/i386/cpu-dump.c | 3 +- > target/i386/cpu.c | 37 ++++++++---------------- > target/i386/helper.c | 42 ++++++++-------------------- > target/i386/hvf/hvf.c | 8 ++---- > target/i386/hvf/x86.c | 4 +-- > target/i386/hvf/x86_emu.c | 6 ++-- > target/i386/hvf/x86_task.c | 10 ++----- > target/i386/hvf/x86hvf.c | 9 ++---- > target/i386/kvm/kvm.c | 6 ++-- > target/i386/kvm/xen-emu.c | 32 +++++++-------------- > target/i386/tcg/sysemu/bpt_helper.c | 3 +- > target/i386/tcg/sysemu/excp_helper.c | 3 +- > target/i386/tcg/tcg-cpu.c | 14 +++------- > target/i386/tcg/user/excp_helper.c | 6 ++-- > target/i386/tcg/user/seg_helper.c | 3 +- > 21 files changed, 67 insertions(+), 158 deletions(-) Actually this one had: Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Acked-by: David Woodhouse <dwmw@amazon.co.uk> Reviewed-by: Zhao Liu <zhao1.liu@intel.com> But since I addressed Zhao's suggestion in patch 1 ("bulk: Access existing variables initialized to &S->F when available") which added more changes to this patch, I dropped the tags.
On Mon, 29 Jan 2024, Philippe Mathieu-Daudé wrote: > When a variable is initialized to &struct->field, use it > in place. Rationale: while this makes the code more concise, > this also helps static analyzers. > > Mechanical change using the following Coccinelle spatch script: > > @@ > type S, F; > identifier s, m, v; > @@ > S *s; > ... > F *v = &s->m; > <+... > - &s->m > + v > ...+> > > Inspired-by: Zhao Liu <zhao1.liu@intel.com> > Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> > --- > hw/display/ati.c | 2 +- > hw/misc/macio/pmu.c | 2 +- > hw/misc/pvpanic-pci.c | 2 +- > hw/pci-bridge/cxl_root_port.c | 2 +- > hw/ppc/pnv.c | 20 ++++++++++---------- > hw/virtio/vhost-user-gpio.c | 8 ++++---- > hw/virtio/vhost-user-scmi.c | 6 +++--- > hw/virtio/virtio-pci.c | 2 +- > hw/xen/xen_pt.c | 6 +++--- > migration/multifd-zlib.c | 2 +- > target/arm/cpu.c | 4 ++-- > target/arm/kvm.c | 2 +- > target/arm/machine.c | 6 +++--- > target/i386/hvf/x86hvf.c | 2 +- > target/m68k/helper.c | 2 +- > target/ppc/kvm.c | 8 ++++---- > target/riscv/cpu_helper.c | 2 +- > 17 files changed, 39 insertions(+), 39 deletions(-) > > diff --git a/hw/display/ati.c b/hw/display/ati.c > index 569b8f6165..8d2501bd82 100644 > --- a/hw/display/ati.c > +++ b/hw/display/ati.c > @@ -991,7 +991,7 @@ static void ati_vga_realize(PCIDevice *dev, Error **errp) > } > vga_init(vga, OBJECT(s), pci_address_space(dev), > pci_address_space_io(dev), true); > - vga->con = graphic_console_init(DEVICE(s), 0, s->vga.hw_ops, &s->vga); > + vga->con = graphic_console_init(DEVICE(s), 0, s->vga.hw_ops, vga); > if (s->cursor_guest_mode) { > vga->cursor_invalidate = ati_cursor_invalidate; > vga->cursor_draw_line = ati_cursor_draw_line; > diff --git a/hw/misc/macio/pmu.c b/hw/misc/macio/pmu.c > index e9a90da88f..7fe1c4e517 100644 > --- a/hw/misc/macio/pmu.c > +++ b/hw/misc/macio/pmu.c > @@ -737,7 +737,7 @@ static void pmu_realize(DeviceState *dev, Error **errp) > timer_mod(s->one_sec_timer, s->one_sec_target); > > if (s->has_adb) { > - qbus_init(&s->adb_bus, sizeof(s->adb_bus), TYPE_ADB_BUS, > + qbus_init(adb_bus, sizeof(s->adb_bus), TYPE_ADB_BUS, Probably should change to use sizeof(*adb_bus) too. Although the next line is the only place the adb_bus local is used so maybe can drop the local and chnage next line to use &s->adb_bus instead. Regards, BALATON Zoltan > dev, "adb.0"); > adb_register_autopoll_callback(adb_bus, pmu_adb_poll, s); > }
On 1/30/24 02:44, Philippe Mathieu-Daudé wrote: > When a variable is initialized to &struct->field, use it > in place. Rationale: while this makes the code more concise, > this also helps static analyzers. > > Mechanical change using the following Coccinelle spatch script: > > @@ > type S, F; > identifier s, m, v; > @@ > S *s; > ... > F *v = &s->m; > <+... > - &s->m > + v > ...+> > > Inspired-by: Zhao Liu<zhao1.liu@intel.com> > Signed-off-by: Philippe Mathieu-Daudé<philmd@linaro.org> > --- Reviewed-by: Richard Henderson <richard.henderson@linaro.org> r~
On 1/30/24 02:44, Philippe Mathieu-Daudé wrote: > Mechanical patch produced running the command documented > in scripts/coccinelle/cpu_env.cocci_template header. > > Signed-off-by: Philippe Mathieu-Daudé<philmd@linaro.org> > --- > target/i386/hvf/vmx.h | 13 ++------- > hw/i386/fw_cfg.c | 3 +- > hw/i386/vmmouse.c | 6 ++-- > hw/i386/xen/xen-hvm.c | 3 +- > target/i386/arch_dump.c | 11 ++------ > target/i386/arch_memory_mapping.c | 3 +- > target/i386/cpu-dump.c | 3 +- > target/i386/cpu.c | 37 ++++++++---------------- > target/i386/helper.c | 42 ++++++++-------------------- > target/i386/hvf/hvf.c | 8 ++---- > target/i386/hvf/x86.c | 4 +-- > target/i386/hvf/x86_emu.c | 6 ++-- > target/i386/hvf/x86_task.c | 10 ++----- > target/i386/hvf/x86hvf.c | 9 ++---- > target/i386/kvm/kvm.c | 6 ++-- > target/i386/kvm/xen-emu.c | 32 +++++++-------------- > target/i386/tcg/sysemu/bpt_helper.c | 3 +- > target/i386/tcg/sysemu/excp_helper.c | 3 +- > target/i386/tcg/tcg-cpu.c | 14 +++------- > target/i386/tcg/user/excp_helper.c | 6 ++-- > target/i386/tcg/user/seg_helper.c | 3 +- > 21 files changed, 67 insertions(+), 158 deletions(-) Reviewed-by: Richard Henderson <richard.henderson@linaro.org> r~
On Mon, Jan 29, 2024 at 05:44:43PM +0100, Philippe Mathieu-Daudé wrote: > When a variable is initialized to &struct->field, use it > in place. Rationale: while this makes the code more concise, > this also helps static analyzers. > > Mechanical change using the following Coccinelle spatch script: > > @@ > type S, F; > identifier s, m, v; > @@ > S *s; > ... > F *v = &s->m; > <+... > - &s->m > + v > ...+> > > Inspired-by: Zhao Liu <zhao1.liu@intel.com> > Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> > --- > diff --git a/hw/xen/xen_pt.c b/hw/xen/xen_pt.c > index 36e6f93c37..10ddf6bc91 100644 > --- a/hw/xen/xen_pt.c > +++ b/hw/xen/xen_pt.c > @@ -710,7 +710,7 @@ static void xen_pt_destroy(PCIDevice *d) { > uint8_t intx; > int rc; > > - if (machine_irq && !xen_host_pci_device_closed(&s->real_device)) { > + if (machine_irq && !xen_host_pci_device_closed(host_dev)) { > intx = xen_pt_pci_intx(s); > rc = xc_domain_unbind_pt_irq(xen_xc, xen_domid, machine_irq, > PT_IRQ_TYPE_PCI, > @@ -759,8 +759,8 @@ static void xen_pt_destroy(PCIDevice *d) { > memory_listener_unregister(&s->io_listener); > s->listener_set = false; > } > - if (!xen_host_pci_device_closed(&s->real_device)) { > - xen_host_pci_device_put(&s->real_device); > + if (!xen_host_pci_device_closed(host_dev)) { > + xen_host_pci_device_put(host_dev); For the Xen part: Reviewed-by: Anthony PERARD <anthony.perard@citrix.com> Thanks,
On 29/01/2024 17.44, Philippe Mathieu-Daudé wrote: > Patches missing review: 1, 2, 5, 6, 8, 11, 14, 15, 29 > > It will be simpler if I get the whole series via my hw-cpus > tree once fully reviewed. > > Since v2: > - Rebased > - bsd/linux-user > - Preliminary clean cpu_reset_hold > - Add R-b > > Since v1: > - Avoid CPU() cast (Paolo) > - Split per targets (Thomas) > > Use cpu_env() -- which is fast path -- when possible. > Bulk conversion using Coccinelle spatch (script included). > > Philippe Mathieu-Daudé (29): > bulk: Access existing variables initialized to &S->F when available > hw/core: Declare CPUArchId::cpu as CPUState instead of Object > hw/acpi/cpu: Use CPUState typedef > bulk: Call in place single use cpu_env() > scripts/coccinelle: Add cpu_env.cocci script > target: Replace CPU_GET_CLASS(cpu -> obj) in cpu_reset_hold() handler > target/alpha: Prefer fast cpu_env() over slower CPU QOM cast macro > target/arm: Prefer fast cpu_env() over slower CPU QOM cast macro > target/avr: Prefer fast cpu_env() over slower CPU QOM cast macro > target/cris: Prefer fast cpu_env() over slower CPU QOM cast macro > target/hexagon: Prefer fast cpu_env() over slower CPU QOM cast macro > target/hppa: Prefer fast cpu_env() over slower CPU QOM cast macro > target/i386/hvf: Use CPUState typedef > target/i386: Prefer fast cpu_env() over slower CPU QOM cast macro > target/loongarch: Prefer fast cpu_env() over slower CPU QOM cast macro > target/m68k: Prefer fast cpu_env() over slower CPU QOM cast macro > target/microblaze: Prefer fast cpu_env() over slower CPU QOM cast > macro > target/mips: Prefer fast cpu_env() over slower CPU QOM cast macro > target/nios2: Prefer fast cpu_env() over slower CPU QOM cast macro > target/openrisc: Prefer fast cpu_env() over slower CPU QOM cast macro > target/ppc: Prefer fast cpu_env() over slower CPU QOM cast macro > target/riscv: Prefer fast cpu_env() over slower CPU QOM cast macro > target/rx: Prefer fast cpu_env() over slower CPU QOM cast macro > target/s390x: Prefer fast cpu_env() over slower CPU QOM cast macro > target/sh4: Prefer fast cpu_env() over slower CPU QOM cast macro > target/sparc: Prefer fast cpu_env() over slower CPU QOM cast macro > target/tricore: Prefer fast cpu_env() over slower CPU QOM cast macro > target/xtensa: Prefer fast cpu_env() over slower CPU QOM cast macro > user: Prefer fast cpu_env() over slower CPU QOM cast macro FYI, I'll try to queue those for my PR today except for: scripts/coccinelle: Add cpu_env.cocci script --> Still needs review and you mentioned a pending change target/arm: Prefer fast cpu_env() over slower CPU QOM cast macro --> Needs a rebase and review target/hppa: Prefer fast cpu_env() over slower CPU QOM cast macro --> Needs a rebase target/i386: Prefer fast cpu_env() over slower CPU QOM cast macro --> There were unaddressed review comments from Igor target/riscv: Prefer fast cpu_env() over slower CPU QOM cast macro --> Needs a rebase Thomas
On 30/01/2024 14.01, Igor Mammedov wrote: > On Mon, 29 Jan 2024 17:44:56 +0100 > Philippe Mathieu-Daudé <philmd@linaro.org> wrote: > >> Mechanical patch produced running the command documented >> in scripts/coccinelle/cpu_env.cocci_template header. > > > commenting here since, I'm not expert on coccinelle scripts. > > On negative side we are permanently loosing type checking in this area. Not really that much. Have a look at cpu_env(), it has a comment saying: "We validate that CPUArchState follows CPUState in cpu-all.h" So instead of run-time checking, the check should have already been done during compile time, i.e. when you have a valid CPUState pointer, it should be possible to derive a valid CPUArchState pointer from it without much further checking during runtime. > Is it worth it, what gains do we get with this series? It's a small optimization, but why not? > Side note, > QOM cast expenses you are replacing could be negated by disabling > CONFIG_QOM_CAST_DEBUG without killing type check code when it's enabled. > That way you will speed up not only cpuenv access but also all other casts > across the board. Yes, but that checking is enabled by default and does not have such compile-time checks that could be used instead, so I think Philippe's series here is still a good idea. >> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> >> --- > ... >> static inline void vmx_clear_nmi_blocking(CPUState *cpu) >> { >> - X86CPU *x86_cpu = X86_CPU(cpu); >> - CPUX86State *env = &x86_cpu->env; >> - >> - env->hflags2 &= ~HF2_NMI_MASK; > >> + cpu_env(cpu)->hflags2 &= ~HF2_NMI_MASK; > > this style of de-referencing return value of macro/function > was discouraged in past and preferred way was 'Foo f = CAST(me); f->some_access > > (it's just imprint speaking, I don't recall where it comes from) I agree, though the new code is perfectly valid, it looks nicer if we'd use a variable here instead. Thomas