Skip to contentSkip to conversation

cocci: Do not initialize variable used by *FOREACH*() macro

PATCH In review Philippe Mathieu-Daudé 2 participants 11 messages Apr 15 – May 6

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover cocci: Do not initialize variable used by *FOREACH*() macro

Most of our *FOREACH*() macros take a pointer argument, then assign it upon entry before iterating. Thus there is no need to initialize the variable used as argument. But sometimes we do, which is confusing (for example &first_cpu). This series uses a Coccinelle script to clean these unnecessary usages. (and 2 bonus cleanup patches at the end).

Philippe Mathieu-Daudé (8):
cocci: Do not initialize variable used by QLIST_FOREACH macro
cocci: Do not initialize variable used by QSLIST_FOREACH macro
cocci: Do not initialize variable used by QSIMPLEQ_FOREACH macro
cocci: Do not initialize variable used by QTAILQ_FOREACH macro
cocci: Do not initialize variable used by RAMBLOCK_FOREACH* macro
cocci: Do not initialize variable used by CPU_FOREACH macro target/mips: Reduce CPUState scope when used with CPU_FOREACH() target/riscv: Iterate vCPUs using CPU_FOREACH() macro

 target/mips/internal.h              | 11 ++++++-----
 block/blkdebug.c                    |  2 +-
 block/blkio.c                       |  2 +-
 block/gluster.c                     |  4 ++--
 ebpf/ebpf.c                         |  2 +-
 hw/arm/virt-acpi-build.c            |  2 +-
 hw/i386/xen/xen-hvm.c               |  4 ++--
 hw/misc/mips_cpc.c                  |  4 ++--
 hw/ppc/spapr.c                      |  2 +-
 hw/ppc/spapr_events.c               |  4 ++--
 hw/riscv/boot.c                     |  9 +++++----
 hw/riscv/riscv-iommu.c              |  2 +-
 hw/vfio/cpr-legacy.c                |  2 +-
 hw/vfio/listener.c                  |  4 ++--
 hw/xen/xen_pt_config_init.c         |  4 ++--
 migration/block-dirty-bitmap.c      |  2 +-
 migration/dirtyrate.c               |  4 ++--
 net/colo-compare.c                  |  4 ++--
 net/net.c                           |  2 +-
 target/mips/tcg/system/cp0_helper.c | 28 ++++++++++++++++------------
 target/mips/tcg/system/tlb_helper.c |  7 +++----
 target/s390x/kvm/stsi-topology.c    |  2 +-
 22 files changed, 56 insertions(+), 51 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/8 cocci: Do not initialize variable used by QLIST_FOREACH macro

The QLIST_FOREACH() macro, defined in "qemu/queue.h", always assigns its iterator variable when entering the loop. Remove the pointless and possibly misleading assignment.

Mechanical patch using the following coccinelle spatch:

@@
type T;
identifier e;
iterator FOREACH_MACRO =~ ".*_FOREACH.*";
statement S;
@@
- T *e = ...;

  +    T *e;
       ... when != e
       FOREACH_MACRO(e, ...) S
---
 block/blkio.c               | 2 +-
 block/gluster.c             | 4 ++--
 hw/arm/virt-acpi-build.c    | 2 +-
 hw/i386/xen/xen-hvm.c       | 4 ++--
 hw/riscv/riscv-iommu.c      | 2 +-
 hw/vfio/cpr-legacy.c        | 2 +-
 hw/vfio/listener.c          | 4 ++--
 hw/xen/xen_pt_config_init.c | 4 ++--
 8 files changed, 12 insertions(+), 12 deletions(-)
block/blkio.c +1 -1
index 20f138a38c5..fb8bec27d71 100644
@@ -124,7 +124,7 @@ blkio_do_alloc_bounce_buffer(BDRVBlkioState *s, BlkioBounceBuf *bounce,
                              int64_t bytes)
 {
     void *addr = s->bounce_pool.addr;
-    BlkioBounceBuf *cur = NULL;
+    BlkioBounceBuf *cur;
     BlkioBounceBuf *prev = NULL;
     ptrdiff_t space;
 
block/gluster.c +2 -2
index 4fb25b2c6d8..279f650241c 100644
@@ -251,7 +251,7 @@ static void glfs_set_preopened(const char *volume, glfs_t *fs)
 
 static glfs_t *glfs_find_preopened(const char *volume)
 {
-    ListElement *entry = NULL;
+    ListElement *entry;
 
      QLIST_FOREACH(entry, &glfs_list, list) {
         if (strcmp(entry->saved.volume, volume) == 0) {
@@ -265,7 +265,7 @@ static glfs_t *glfs_find_preopened(const char *volume)
 
 static void glfs_clear_preopened(glfs_t *fs)
 {
-    ListElement *entry = NULL;
+    ListElement *entry;
     ListElement *next;
 
     if (fs == NULL) {
hw/arm/virt-acpi-build.c +1 -1
index 591cfc993c6..a38a835d48d 100644
@@ -146,7 +146,7 @@ static void acpi_dsdt_add_pci(Aml *scope, const MemMapEntry *memmap,
 {
     int ecam_id = VIRT_ECAM_ID(vms->highmem_ecam);
     bool cxl_present = false;
-    PCIBus *bus = vms->bus;
+    PCIBus *bus;
     bool acpi_pcihp = false;
 
     if (vms->acpi_dev) {
hw/i386/xen/xen-hvm.c +2 -2
index 67d3e836eb1..d3ce082e07a 100644
@@ -184,7 +184,7 @@ static void xen_ram_init(PCMachineState *pcms,
 static XenPhysmap *get_physmapping(hwaddr start_addr, ram_addr_t size,
                                    int page_mask)
 {
-    XenPhysmap *physmap = NULL;
+    XenPhysmap *physmap;
 
     start_addr &= page_mask;
 
@@ -200,7 +200,7 @@ static hwaddr xen_phys_offset_to_gaddr(hwaddr phys_offset, ram_addr_t size,
                                        int page_mask)
 {
     hwaddr addr = phys_offset & page_mask;
-    XenPhysmap *physmap = NULL;
+    XenPhysmap *physmap;
 
     QLIST_FOREACH(physmap, &xen_physmap, list) {
         if (range_covers_byte(physmap->phys_offset, physmap->size, addr)) {
hw/riscv/riscv-iommu.c +1 -1
index c3c9ed6469a..bdc64b70ff2 100644
@@ -1681,7 +1681,7 @@ static void riscv_iommu_ats(RISCVIOMMUState *s,
     IOMMUAccessFlags perm,
     void (*trace_fn)(const char *id))
 {
-    RISCVIOMMUSpace *as = NULL;
+    RISCVIOMMUSpace *as;
     IOMMUNotifier *n;
     IOMMUTLBEvent event;
     uint32_t pid;
hw/vfio/cpr-legacy.c +1 -1
index 033a546c301..c431d899731 100644
@@ -212,7 +212,7 @@ void vfio_legacy_cpr_unregister_container(VFIOLegacyContainer *container)
 void vfio_cpr_giommu_remap(VFIOContainer *bcontainer,
                            MemoryRegionSection *section)
 {
-    VFIOGuestIOMMU *giommu = NULL;
+    VFIOGuestIOMMU *giommu;
     hwaddr as_offset = section->offset_within_address_space;
     hwaddr iommu_offset = as_offset - section->offset_within_region;
 
hw/vfio/listener.c +2 -2
index 960da9e0a93..cf2f9ef5e8f 100644
@@ -337,7 +337,7 @@ static void vfio_ram_discard_unregister_listener(VFIOContainer *bcontainer,
                                                  MemoryRegionSection *section)
 {
     RamDiscardManager *rdm = memory_region_get_ram_discard_manager(section->mr);
-    VFIORamDiscardListener *vrdl = NULL;
+    VFIORamDiscardListener *vrdl;
 
     QLIST_FOREACH(vrdl, &bcontainer->vrdl_list, next) {
         if (vrdl->mr == section->mr &&
@@ -467,7 +467,7 @@ static void vfio_device_error_append(VFIODevice *vbasedev, Error **errp)
 VFIORamDiscardListener *vfio_find_ram_discard_listener(
     VFIOContainer *bcontainer, MemoryRegionSection *section)
 {
-    VFIORamDiscardListener *vrdl = NULL;
+    VFIORamDiscardListener *vrdl;
 
     QLIST_FOREACH(vrdl, &bcontainer->vrdl_list, next) {
         if (vrdl->mr == section->mr &&
hw/xen/xen_pt_config_init.c +2 -2
index 3edaeab1e34..bbc82a2bcfa 100644
@@ -62,7 +62,7 @@ static int xen_pt_hide_dev_cap(const XenHostPCIDevice *d, uint8_t grp_id)
 /*   find emulate register group entry */
 XenPTRegGroup *xen_pt_find_reg_grp(XenPCIPassthroughState *s, uint32_t address)
 {
-    XenPTRegGroup *entry = NULL;
+    XenPTRegGroup *entry;
 
     /* find register group entry */
     QLIST_FOREACH(entry, &s->reg_grps, entries) {
@@ -80,7 +80,7 @@ XenPTRegGroup *xen_pt_find_reg_grp(XenPCIPassthroughState *s, uint32_t address)
 /* find emulate register entry */
 XenPTReg *xen_pt_find_reg(XenPTRegGroup *reg_grp, uint32_t address)
 {
-    XenPTReg *reg_entry = NULL;
+    XenPTReg *reg_entry;
     XenPTRegInfo *reg = NULL;
     uint32_t real_offset = 0;
 
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/8 cocci: Do not initialize variable used by QSLIST_FOREACH macro

In reply to Philippe Mathieu-Daudé

The QSLIST_FOREACH() macro, defined in "qemu/queue.h", always assigns its iterator variable when entering the loop. Remove the pointless and possibly misleading assignment.

Mechanical patch using the following coccinelle spatch:

@@
type T;
identifier e;
iterator FOREACH_MACRO =~ ".*_FOREACH.*";
statement S;
@@
- T *e = ...;

  +    T *e;
       ... when != e
       FOREACH_MACRO(e, ...) S
---
 ebpf/ebpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ebpf/ebpf.c +1 -1
index 2d73beb4796..a8d441b02c1 100644
@@ -40,7 +40,7 @@ void ebpf_register_binary_data(int id, const void *data, size_t datalen)
 
 const void *ebpf_find_binary_by_id(int id, size_t *sz, Error **errp)
 {
-    struct ElfBinaryDataEntry *it = NULL;
+    struct ElfBinaryDataEntry *it;
     QSLIST_FOREACH(it, &ebpf_elf_obj_list, node) {
         if (id == it->id) {
             *sz = it->datalen;
Signature · 1 line
2.53.0
#3 lore raw

PATCH 3/8 cocci: Do not initialize variable used by QSIMPLEQ_FOREACH macro

In reply to Philippe Mathieu-Daudé

The QSIMPLEQ_FOREACH() macro, defined in "qemu/queue.h", always assigns its iterator variable when entering the loop. Remove the pointless and possibly misleading assignment.

Mechanical patch using the following coccinelle spatch:

@@
type T;
identifier e;
iterator FOREACH_MACRO =~ ".*_FOREACH.*";
statement S;
@@
- T *e = ...;

  +    T *e;
       ... when != e
       FOREACH_MACRO(e, ...) S
---
 block/blkdebug.c               | 2 +-
 migration/block-dirty-bitmap.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
block/blkdebug.c +1 -1
index 8a4a8cb85ea..fdc96d1f45d 100644
@@ -591,7 +591,7 @@ static int coroutine_fn rule_check(BlockDriverState *bs, uint64_t offset,
                                    uint64_t bytes, BlkdebugIOType iotype)
 {
     BDRVBlkdebugState *s = bs->opaque;
-    BlkdebugRule *rule = NULL;
+    BlkdebugRule *rule;
     int error;
     bool immediately;
 
migration/block-dirty-bitmap.c +1 -1
index a061aad8177..87009c45182 100644
@@ -1216,7 +1216,7 @@ fail:
 static int dirty_bitmap_save_setup(QEMUFile *f, void *opaque, Error **errp)
 {
     DBMSaveState *s = &((DBMState *)opaque)->save;
-    SaveBitmapState *dbms = NULL;
+    SaveBitmapState *dbms;
 
     if (init_dirty_bitmap_migration(s, errp) < 0) {
         return -1;
Signature · 1 line
2.53.0
#4 lore raw

PATCH 4/8 cocci: Do not initialize variable used by QTAILQ_FOREACH macro

In reply to Philippe Mathieu-Daudé

The QTAILQ_FOREACH() macro, defined in "qemu/queue.h", always assigns its iterator variable when entering the loop. Remove the pointless and possibly misleading assignment.

Mechanical patch using the following coccinelle spatch:

@@
type T;
identifier e;
iterator FOREACH_MACRO =~ ".*_FOREACH.*";
statement S;
@@
- T *e = ...;

  +    T *e;
       ... when != e
       FOREACH_MACRO(e, ...) S
---
 hw/ppc/spapr.c                   | 2 +-
 hw/ppc/spapr_events.c            | 4 ++--
 net/colo-compare.c               | 4 ++--
 net/net.c                        | 2 +-
 target/s390x/kvm/stsi-topology.c | 2 +-
 5 files changed, 7 insertions(+), 7 deletions(-)
hw/ppc/spapr.c +1 -1
index 0ab39dfea6b..bc7232e774a 100644
@@ -3685,7 +3685,7 @@ struct SpaprDimmState {
 static SpaprDimmState *spapr_pending_dimm_unplugs_find(SpaprMachineState *s,
                                                        PCDIMMDevice *dimm)
 {
-    SpaprDimmState *dimm_state = NULL;
+    SpaprDimmState *dimm_state;
 
     QTAILQ_FOREACH(dimm_state, &s->pending_dimm_unplugs, next) {
         if (dimm_state->dimm == dimm) {
hw/ppc/spapr_events.c +2 -2
index 1a21ac7bbf8..ab23e3f477b 100644
@@ -460,7 +460,7 @@ static void rtas_event_log_queue(SpaprMachineState *spapr,
 static SpaprEventLogEntry *rtas_event_log_dequeue(SpaprMachineState *spapr,
                                                   uint32_t event_mask)
 {
-    SpaprEventLogEntry *entry = NULL;
+    SpaprEventLogEntry *entry;
 
     QTAILQ_FOREACH(entry, &spapr->pending_events, next) {
         const SpaprEventSource *source =
@@ -482,7 +482,7 @@ static SpaprEventLogEntry *rtas_event_log_dequeue(SpaprMachineState *spapr,
 
 static bool rtas_event_log_contains(SpaprMachineState *spapr, uint32_t event_mask)
 {
-    SpaprEventLogEntry *entry = NULL;
+    SpaprEventLogEntry *entry;
 
     QTAILQ_FOREACH(entry, &spapr->pending_events, next) {
         const SpaprEventSource *source =
net/colo-compare.c +2 -2
index c356419d6a8..abc1326b704 100644
@@ -1396,7 +1396,7 @@ static void colo_compare_init(Object *obj)
 
 void colo_compare_cleanup(void)
 {
-    CompareState *tmp = NULL;
+    CompareState *tmp;
     CompareState *n = NULL;
 
     QTAILQ_FOREACH_SAFE(tmp, &net_compares, next, n) {
@@ -1407,7 +1407,7 @@ void colo_compare_cleanup(void)
 static void colo_compare_finalize(Object *obj)
 {
     CompareState *s = COLO_COMPARE(obj);
-    CompareState *tmp = NULL;
+    CompareState *tmp;
 
     qemu_mutex_lock(&colo_compare_mutex);
     QTAILQ_FOREACH(tmp, &net_compares, next) {
net/net.c +1 -1
index a176936f9bc..2892f1730d1 100644
@@ -653,7 +653,7 @@ static ssize_t filter_receive_iov(NetClientState *nc,
                                   NetPacketSent *sent_cb)
 {
     ssize_t ret = 0;
-    NetFilterState *nf = NULL;
+    NetFilterState *nf;
 
     if (direction == NET_FILTER_DIRECTION_TX) {
         QTAILQ_FOREACH(nf, &nc->filters, next) {
target/s390x/kvm/stsi-topology.c +1 -1
index c8d6389cd87..301d41b68d2 100644
@@ -285,7 +285,7 @@ static void s390_topology_fill_list_sorted(S390TopologyList *topology_list)
  */
 static void s390_topology_empty_list(S390TopologyList *topology_list)
 {
-    S390TopologyEntry *entry = NULL;
+    S390TopologyEntry *entry;
     S390TopologyEntry *tmp = NULL;
 
     QTAILQ_FOREACH_SAFE(entry, topology_list, next, tmp) {
Signature · 1 line
2.53.0
#5 lore raw

PATCH 5/8 cocci: Do not initialize variable used by RAMBLOCK_FOREACH* macro

In reply to Philippe Mathieu-Daudé

The RAMBLOCK_FOREACH_MIGRATABLE() macro, defined in migration/ram.h, ends up calling QLIST_FOREACH_RCU() which always assigns its iterator variable when entering the loop. Remove the pointless and possibly misleading assignment.

Mechanical patch using the following coccinelle spatch:

@@
type T;
identifier e;
iterator FOREACH_MACRO =~ ".*_FOREACH.*";
statement S;
@@
- T *e = ...;

  +    T *e;
       ... when != e
       FOREACH_MACRO(e, ...) S
---
 migration/dirtyrate.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
migration/dirtyrate.c +2 -2
index 58f04670629..b360f49efea 100644
@@ -485,7 +485,7 @@ static bool record_ramblock_hash_info(struct RamblockDirtyInfo **block_dinfo,
 {
     struct RamblockDirtyInfo *info = NULL;
     struct RamblockDirtyInfo *dinfo = NULL;
-    RAMBlock *block = NULL;
+    RAMBlock *block;
     int total_count = 0;
     int index = 0;
     bool ret = false;
@@ -568,7 +568,7 @@ static bool compare_page_hash_info(struct RamblockDirtyInfo *info,
                                   int block_count)
 {
     struct RamblockDirtyInfo *block_dinfo = NULL;
-    RAMBlock *block = NULL;
+    RAMBlock *block;
 
     RAMBLOCK_FOREACH_MIGRATABLE(block) {
         if (skip_sample_ramblock(block)) {
Signature · 1 line
2.53.0
#6 lore raw

PATCH 6/8 cocci: Do not initialize variable used by CPU_FOREACH macro

In reply to Philippe Mathieu-Daudé

The CPU_FOREACH() macro, defined in "hw/core/cpu.h", ends up calling QTAILQ_FOREACH_RCU() which always assigns its iterator variable when entering the loop. Remove the pointless and possibly misleading assignment.

Mechanical patch using the following coccinelle spatch:

@@
type T;
identifier e;
iterator FOREACH_MACRO =~ ".*_FOREACH.*"; statement S;
@@
- T *e = ...;

  +    T *e;
       ... when != e
       FOREACH_MACRO(e, ...) S
---
 target/mips/internal.h              | 2 +-
 hw/misc/mips_cpc.c                  | 4 ++--
 target/mips/tcg/system/cp0_helper.c | 8 ++++----
 target/mips/tcg/system/tlb_helper.c | 2 +-
 4 files changed, 8 insertions(+), 8 deletions(-)
target/mips/internal.h +1 -1
index 28eb28936ba..c6cf69dfb6a 100644
@@ -279,7 +279,7 @@ static inline int mips_vpe_active(CPUMIPSState *env)
 
 static inline int mips_vp_active(CPUMIPSState *env)
 {
-    CPUState *other_cs = first_cpu;
+    CPUState *other_cs;
 
     /* Check if the VP disabled other VPs (which means the VP is enabled) */
     if ((env->CP0_VPControl >> CP0VPCtl_DIS) & 1) {
hw/misc/mips_cpc.c +2 -2
index 924de855ee2..240ccf4d312 100644
@@ -44,7 +44,7 @@ static void mips_cpu_reset_async_work(CPUState *cs, run_on_cpu_data data)
 
 static void cpc_run_vp(MIPSCPCState *cpc, uint64_t vp_run)
 {
-    CPUState *cs = first_cpu;
+    CPUState *cs;
 
     CPU_FOREACH(cs) {
         uint64_t i = 1ULL << cs->cpu_index;
@@ -62,7 +62,7 @@ static void cpc_run_vp(MIPSCPCState *cpc, uint64_t vp_run)
 
 static void cpc_stop_vp(MIPSCPCState *cpc, uint64_t vp_stop)
 {
-    CPUState *cs = first_cpu;
+    CPUState *cs;
 
     CPU_FOREACH(cs) {
         uint64_t i = 1ULL << cs->cpu_index;
target/mips/tcg/system/cp0_helper.c +4 -4
index b69e70d7fcf..99f43ad9e11 100644
@@ -1559,7 +1559,7 @@ target_ulong helper_emt(void)
 
 target_ulong helper_dvpe(CPUMIPSState *env)
 {
-    CPUState *other_cs = first_cpu;
+    CPUState *other_cs;
     target_ulong prev = env->mvp->CP0_MVPControl;
 
     if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP)) {
@@ -1577,7 +1577,7 @@ target_ulong helper_dvpe(CPUMIPSState *env)
 
 target_ulong helper_evpe(CPUMIPSState *env)
 {
-    CPUState *other_cs = first_cpu;
+    CPUState *other_cs;
     target_ulong prev = env->mvp->CP0_MVPControl;
 
     if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP)) {
@@ -1599,7 +1599,7 @@ target_ulong helper_evpe(CPUMIPSState *env)
 /* R6 Multi-threading */
 target_ulong helper_dvp(CPUMIPSState *env)
 {
-    CPUState *other_cs = first_cpu;
+    CPUState *other_cs;
     target_ulong prev = env->CP0_VPControl;
 
     if (!((env->CP0_VPControl >> CP0VPCtl_DIS) & 1)) {
@@ -1617,7 +1617,7 @@ target_ulong helper_dvp(CPUMIPSState *env)
 
 target_ulong helper_evp(CPUMIPSState *env)
 {
-    CPUState *other_cs = first_cpu;
+    CPUState *other_cs;
     target_ulong prev = env->CP0_VPControl;
 
     if ((env->CP0_VPControl >> CP0VPCtl_DIS) & 1) {
target/mips/tcg/system/tlb_helper.c +1 -1
index 566924b079e..680b4da29fa 100644
@@ -346,7 +346,7 @@ void helper_ginvt(CPUMIPSState *env, target_ulong arg, uint32_t type)
     uint32_t invMsgVPN2 = arg & (TARGET_PAGE_MASK << 1);
     uint8_t invMsgR = 0;
     uint32_t invMsgMMid = env->CP0_MemoryMapID;
-    CPUState *other_cs = first_cpu;
+    CPUState *other_cs;
 
 #ifdef TARGET_MIPS64
     invMsgR = extract64(arg, 62, 2);
Signature · 1 line
2.53.0
#7 lore raw

PATCH 7/8 target/mips: Reduce CPUState scope when used with CPU_FOREACH()

In reply to Philippe Mathieu-Daudé

When possible, reduce CPUState variable scope.
Prefer cpu_env(cpu) over &cpu->env.

---
 target/mips/internal.h              | 11 ++++++-----
 target/mips/tcg/system/cp0_helper.c | 28 ++++++++++++++++------------
 target/mips/tcg/system/tlb_helper.c |  7 +++----
 3 files changed, 25 insertions(+), 21 deletions(-)
target/mips/internal.h +6 -5
index c6cf69dfb6a..cc031662ef9 100644
@@ -279,7 +279,7 @@ static inline int mips_vpe_active(CPUMIPSState *env)
 
 static inline int mips_vp_active(CPUMIPSState *env)
 {
-    CPUState *other_cs;
+    CPUState *cs;
 
     /* Check if the VP disabled other VPs (which means the VP is enabled) */
     if ((env->CP0_VPControl >> CP0VPCtl_DIS) & 1) {
@@ -287,10 +287,11 @@ static inline int mips_vp_active(CPUMIPSState *env)
     }
 
     /* Check if the virtual processor is disabled due to a DVP */
-    CPU_FOREACH(other_cs) {
-        MIPSCPU *other_cpu = MIPS_CPU(other_cs);
-        if ((&other_cpu->env != env) &&
-            ((other_cpu->env.CP0_VPControl >> CP0VPCtl_DIS) & 1)) {
+    CPU_FOREACH(cs) {
+        CPUMIPSState *other_env = cpu_env(cs);
+
+        if ((other_env != env) &&
+            ((other_env->CP0_VPControl >> CP0VPCtl_DIS) & 1)) {
             return 0;
         }
     }
target/mips/tcg/system/cp0_helper.c +16 -12
index 99f43ad9e11..4ec1312f016 100644
@@ -1559,12 +1559,13 @@ target_ulong helper_emt(void)
 
 target_ulong helper_dvpe(CPUMIPSState *env)
 {
-    CPUState *other_cs;
     target_ulong prev = env->mvp->CP0_MVPControl;
 
     if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP)) {
-        CPU_FOREACH(other_cs) {
-            MIPSCPU *other_cpu = MIPS_CPU(other_cs);
+        CPUState *cpu;
+
+        CPU_FOREACH(cpu) {
+            MIPSCPU *other_cpu = MIPS_CPU(cpu);
             /* Turn off all VPEs except the one executing the dvpe.  */
             if (&other_cpu->env != env) {
                 other_cpu->env.mvp->CP0_MVPControl &= ~(1 << CP0MVPCo_EVP);
@@ -1577,12 +1578,13 @@ target_ulong helper_dvpe(CPUMIPSState *env)
 
 target_ulong helper_evpe(CPUMIPSState *env)
 {
-    CPUState *other_cs;
     target_ulong prev = env->mvp->CP0_MVPControl;
 
     if (env->CP0_VPEConf0 & (1 << CP0VPEC0_MVP)) {
-        CPU_FOREACH(other_cs) {
-            MIPSCPU *other_cpu = MIPS_CPU(other_cs);
+        CPUState *cpu;
+
+        CPU_FOREACH(cpu) {
+            MIPSCPU *other_cpu = MIPS_CPU(cpu);
 
             if (&other_cpu->env != env
                 /* If the VPE is WFI, don't disturb its sleep.  */
@@ -1599,12 +1601,13 @@ target_ulong helper_evpe(CPUMIPSState *env)
 /* R6 Multi-threading */
 target_ulong helper_dvp(CPUMIPSState *env)
 {
-    CPUState *other_cs;
     target_ulong prev = env->CP0_VPControl;
 
     if (!((env->CP0_VPControl >> CP0VPCtl_DIS) & 1)) {
-        CPU_FOREACH(other_cs) {
-            MIPSCPU *other_cpu = MIPS_CPU(other_cs);
+        CPUState *cpu;
+
+        CPU_FOREACH(cpu) {
+            MIPSCPU *other_cpu = MIPS_CPU(cpu);
             /* Turn off all VPs except the one executing the dvp. */
             if (&other_cpu->env != env) {
                 mips_vpe_sleep(other_cpu);
@@ -1617,12 +1620,13 @@ target_ulong helper_dvp(CPUMIPSState *env)
 
 target_ulong helper_evp(CPUMIPSState *env)
 {
-    CPUState *other_cs;
     target_ulong prev = env->CP0_VPControl;
 
     if ((env->CP0_VPControl >> CP0VPCtl_DIS) & 1) {
-        CPU_FOREACH(other_cs) {
-            MIPSCPU *other_cpu = MIPS_CPU(other_cs);
+        CPUState *cpu;
+
+        CPU_FOREACH(cpu) {
+            MIPSCPU *other_cpu = MIPS_CPU(cpu);
             if ((&other_cpu->env != env) && !mips_vp_is_wfi(other_cpu)) {
                 /*
                  * If the VP is WFI, don't disturb its sleep.
target/mips/tcg/system/tlb_helper.c +3 -4
index 680b4da29fa..8537798a53f 100644
@@ -346,15 +346,14 @@ void helper_ginvt(CPUMIPSState *env, target_ulong arg, uint32_t type)
     uint32_t invMsgVPN2 = arg & (TARGET_PAGE_MASK << 1);
     uint8_t invMsgR = 0;
     uint32_t invMsgMMid = env->CP0_MemoryMapID;
-    CPUState *other_cs;
+    CPUState *cpu;
 
 #ifdef TARGET_MIPS64
     invMsgR = extract64(arg, 62, 2);
 #endif
 
-    CPU_FOREACH(other_cs) {
-        MIPSCPU *other_cpu = MIPS_CPU(other_cs);
-        global_invalidate_tlb(&other_cpu->env, invMsgVPN2, invMsgR, invMsgMMid,
+    CPU_FOREACH(cpu) {
+        global_invalidate_tlb(cpu_env(cpu), invMsgVPN2, invMsgR, invMsgMMid,
                               invAll, invVAMMid, invMMid, invVA);
     }
 }
Signature · 1 line
2.53.0
#8 lore raw

PATCH 8/8 target/riscv: Iterate vCPUs using CPU_FOREACH() macro

In reply to Philippe Mathieu-Daudé

Most code iterates over vCPUs using the CPU_FOREACH() macro. Prefer cpu_env(cpu) over &cpu->env.

---
 hw/riscv/boot.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
hw/riscv/boot.c +5 -4
index e5490beda00..61e21678cd1 100644
@@ -490,10 +490,11 @@ void riscv_setup_direct_kernel(hwaddr kernel_addr, hwaddr fdt_addr)
 {
     CPUState *cs;
 
-    for (cs = first_cpu; cs; cs = CPU_NEXT(cs)) {
-        RISCVCPU *riscv_cpu = RISCV_CPU(cs);
-        riscv_cpu->env.kernel_addr = kernel_addr;
-        riscv_cpu->env.fdt_addr = fdt_addr;
+    CPU_FOREACH(cs) {
+        CPURISCVState *env = cpu_env(cs);
+
+        env->kernel_addr = kernel_addr;
+        env->fdt_addr = fdt_addr;
     }
 }
 
Signature · 1 line
2.53.0
Reviewed-by on the v1 series
#9 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 33 lines · Philippe Mathieu-Daudé

Most of our *FOREACH*() macros take a pointer argument, then assign it upon entry before iterating. Thus there is no need to initialize the variable used as argument. But sometimes we do, which is confusing (for example &first_cpu). This series uses a Coccinelle script to clean these unnecessary usages. (and 2 bonus cleanup patches at the end).

Philippe Mathieu-Daudé (8):
cocci: Do not initialize variable used by QLIST_FOREACH macro
cocci: Do not initialize variable used by QSLIST_FOREACH macro
cocci: Do not initialize variable used by QSIMPLEQ_FOREACH macro
cocci: Do not initialize variable used by QTAILQ_FOREACH macro
cocci: Do not initialize variable used by RAMBLOCK_FOREACH* macro
cocci: Do not initialize variable used by CPU_FOREACH macro target/mips: Reduce CPUState scope when used with CPU_FOREACH() target/riscv: Iterate vCPUs using CPU_FOREACH() macro

  target/mips/internal.h              | 11 ++++++-----
  block/blkdebug.c                    |  2 +-
  block/blkio.c                       |  2 +-
  block/gluster.c                     |  4 ++--
  ebpf/ebpf.c                         |  2 +-
  hw/arm/virt-acpi-build.c            |  2 +-
  hw/i386/xen/xen-hvm.c               |  4 ++--
  hw/misc/mips_cpc.c                  |  4 ++--
  hw/ppc/spapr.c                      |  2 +-
  hw/ppc/spapr_events.c               |  4 ++--
  hw/riscv/boot.c                     |  9 +++++----
  hw/riscv/riscv-iommu.c              |  2 +-
  hw/vfio/cpr-legacy.c                |  2 +-
  hw/vfio/listener.c                  |  4 ++--
  hw/xen/xen_pt_config_init.c         |  4 ++--
Quoted 9 lines
  migration/block-dirty-bitmap.c      |  2 +-
  migration/dirtyrate.c               |  4 ++--
  net/colo-compare.c                  |  4 ++--
  net/net.c                           |  2 +-
  target/mips/tcg/system/cp0_helper.c | 28 ++++++++++++++++------------
  target/mips/tcg/system/tlb_helper.c |  7 +++----
  target/s390x/kvm/stsi-topology.c    |  2 +-
  22 files changed, 56 insertions(+), 51 deletions(-)

For the series:

#10 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 9 lines

Philippe Mathieu-Daudé (8):
cocci: Do not initialize variable used by QLIST_FOREACH macro
cocci: Do not initialize variable used by QSLIST_FOREACH macro
cocci: Do not initialize variable used by QSIMPLEQ_FOREACH macro
cocci: Do not initialize variable used by QTAILQ_FOREACH macro
cocci: Do not initialize variable used by RAMBLOCK_FOREACH* macro
cocci: Do not initialize variable used by CPU_FOREACH macro target/mips: Reduce CPUState scope when used with CPU_FOREACH() target/riscv: Iterate vCPUs using CPU_FOREACH() macro

Series queued, thanks.