Skip to contentSkip to conversation

cleanup of ROUND_UP-like lines

PATCH In review Hu Tao 2 participants 7 messages Jun 26, 2014 – Jul 2, 2014 Block layer

lore
Share & tools
lore

Hu Tao

author
#0 lore raw

PATCH 1/1 cleanup of ROUND_UP-like lines

We already have ROUND_UP but there are similar macros and ROUND_UP-like lines all around, replace them with ROUND_UP and clean up similar macros.

---
 block/qcow.c         |  2 +-
 block/vdi.c          |  2 +-
 block/vpc.c          |  2 +-
 bsd-user/elfload.c   |  2 +-
 exec.c               |  2 +-
 hw/core/loader.c     |  5 ++---
 hw/display/qxl.c     |  7 ++-----
 hw/ppc/spapr.c       |  2 +-
 kvm-all.c            | 10 ++++------
 linux-user/elfload.c |  2 +-
 linux-user/syscall.c |  4 ++--
 tcg/ppc/tcg-target.c |  4 ++--
 thunk.c              |  4 ++--
 ui/vnc-enc-zlib.c    |  2 +-
 util/oslib-posix.c   |  2 +-
 xen-mapcache.c       |  2 +-
 16 files changed, 24 insertions(+), 30 deletions(-)
block/qcow.c +1 -1
index 1f2bac8..4ba7769 100644
@@ -323,7 +323,7 @@ static uint64_t get_cluster_offset(BlockDriverState *bs,
         /* allocate a new l2 entry */
         l2_offset = bdrv_getlength(bs->file);
         /* round to cluster size */
-        l2_offset = (l2_offset + s->cluster_size - 1) & ~(s->cluster_size - 1);
+        l2_offset = ROUND_UP(l2_offset, s->cluster_size);
         /* update the L1 entry */
         s->l1_table[l1_index] = l2_offset;
         tmp = cpu_to_be64(l2_offset);
block/vdi.c +1 -1
index 01fe22e..d07186d 100644
@@ -721,7 +721,7 @@ static int vdi_create(const char *filename, QemuOpts *opts, Error **errp)
     blocks = (bytes + block_size - 1) / block_size;
 
     bmap_size = blocks * sizeof(uint32_t);
-    bmap_size = ((bmap_size + SECTOR_SIZE - 1) & ~(SECTOR_SIZE -1));
+    bmap_size = ROUND_UP(bmap_size, SECTOR_SIZE);
 
     memset(&header, 0, sizeof(header));
     pstrcpy(header.text, sizeof(header.text), VDI_TEXT);
block/vpc.c +1 -1
index 798d854..cc53979 100644
@@ -280,7 +280,7 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags,
         }
 
         s->free_data_block_offset =
-            (s->bat_offset + (s->max_table_entries * 4) + 511) & ~511;
+            ROUND_UP(s->bat_offset + (s->max_table_entries * 4), 512);
 
         for (i = 0; i < s->max_table_entries; i++) {
             be32_to_cpus(&s->pagetable[i]);
bsd-user/elfload.c +1 -1
index 93fd9e4..a552acb 100644
@@ -777,7 +777,7 @@ static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
         k_platform = ELF_PLATFORM;
         if (k_platform) {
             size_t len = strlen(k_platform) + 1;
-            sp -= (len + n - 1) & ~(n - 1);
+            sp -= ROUND_UP(len, n);
             u_platform = sp;
             /* FIXME - check return value of memcpy_to_target() for failure */
             memcpy_to_target(sp, k_platform, len);
exec.c +1 -1
index c849405..3ca00f9 100644
@@ -1064,7 +1064,7 @@ static void *file_ram_alloc(RAMBlock *block,
     unlink(filename);
     g_free(filename);
 
-    memory = (memory+hpagesize-1) & ~(hpagesize-1);
+    memory = ROUND_UP(memory, hpagesize);
 
     /*
      * ftruncate is not supported by hugetlbfs in older
hw/core/loader.c +2 -3
index 2bf6b8f..4770714 100644
@@ -173,13 +173,12 @@ static void bswap_ahdr(struct exec *e)
     (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) :	\
      (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
 #define N_TXTADDR(x, target_page_size) (N_MAGIC(x) == QMAGIC ? target_page_size : 0)
-#define _N_SEGMENT_ROUND(x, target_page_size) (((x) + target_page_size - 1) & ~(target_page_size - 1))
 
 #define _N_TXTENDADDR(x, target_page_size) (N_TXTADDR(x, target_page_size)+(x).a_text)
 
 #define N_DATADDR(x, target_page_size) \
     (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x, target_page_size)) \
-     : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x, target_page_size), target_page_size)))
+     : (ROUND_UP(_N_TXTENDADDR(x, target_page_size), target_page_size)))
 
 
 int load_aout(const char *filename, hwaddr addr, int max_sz,
@@ -377,7 +376,7 @@ static void *zalloc(void *x, unsigned items, unsigned size)
     void *p;
 
     size *= items;
-    size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
+    size = ROUND_UP(size, ZALLOC_ALIGNMENT);
 
     p = g_malloc(size);
 
hw/display/qxl.c +2 -5
index d43aa49..34bf7b7 100644
@@ -61,9 +61,6 @@
         }                                                               \
     }
 
-#undef ALIGN
-#define ALIGN(a, b) (((a) + ((b) - 1)) & ~((b) - 1))
-
 #define PIXEL_SIZE 0.2936875 //1280x1024 is 14.8" x 11.9" 
 
 #define QXL_MODE(_x, _y, _b, _o)                  \
@@ -358,8 +355,8 @@ static void init_qxl_rom(PCIQXLDevice *d)
     }
     modes->n_modes     = cpu_to_le32(n);
 
-    ram_header_size    = ALIGN(sizeof(QXLRam), 4096);
-    surface0_area_size = ALIGN(d->vgamem_size, 4096);
+    ram_header_size    = ROUND_UP(sizeof(QXLRam), 4096);
+    surface0_area_size = ROUND_UP(d->vgamem_size, 4096);
     num_pages          = d->vga.vram_size;
     num_pages         -= ram_header_size;
     num_pages         -= surface0_area_size;
hw/ppc/spapr.c +1 -1
index 82f183f..98c2efb 100644
@@ -148,7 +148,7 @@ int spapr_allocate_irq_block(int num, bool lsi, bool msi)
     if (msi) {
         assert((num == 1) || (num == 2) || (num == 4) ||
                (num == 8) || (num == 16) || (num == 32));
-        hint = (spapr->next_irq + num - 1) & ~(num - 1);
+        hint = ROUND_UP(spapr->next_irq, num);
     }
 
     for (i = 0; i < num; ++i) {
kvm-all.c +4 -6
index 3ae30ee..f361094 100644
@@ -381,8 +381,6 @@ static int kvm_get_dirty_pages_log_range(MemoryRegionSection *section,
     return 0;
 }
 
-#define ALIGN(x, y)  (((x)+(y)-1) & ~((y)-1))
-
 /**
  * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space
  * This function updates qemu's dirty bitmap using
@@ -421,8 +419,8 @@ static int kvm_physical_sync_dirty_bitmap(MemoryRegionSection *section)
          * So for now, let's align to 64 instead of HOST_LONG_BITS here, in
          * a hope that sizeof(long) wont become >8 any time soon.
          */
-        size = ALIGN(((mem->memory_size) >> TARGET_PAGE_BITS),
-                     /*HOST_LONG_BITS*/ 64) / 8;
+        size = ROUND_UP(((mem->memory_size) >> TARGET_PAGE_BITS),
+                        /*HOST_LONG_BITS*/ 64) / 8;
         if (!d.dirty_bitmap) {
             d.dirty_bitmap = g_malloc(size);
         } else if (size > allocated_size) {
@@ -945,7 +943,7 @@ void kvm_init_irq_routing(KVMState *s)
         unsigned int gsi_bits, i;
 
         /* Round up so we can search ints using ffs */
-        gsi_bits = ALIGN(gsi_count, 32);
+        gsi_bits = ROUND_UP(gsi_count, 32);
         s->used_gsi_bitmap = g_malloc0(gsi_bits / 8);
         s->gsi_count = gsi_count;
 
@@ -1083,7 +1081,7 @@ static void kvm_flush_dynamic_msi_routes(KVMState *s)
 static int kvm_irqchip_get_virq(KVMState *s)
 {
     uint32_t *word = s->used_gsi_bitmap;
-    int max_words = ALIGN(s->gsi_count, 32) / 32;
+    int max_words = ROUND_UP(s->gsi_count, 32) / 32;
     int i, bit;
     bool retry = true;
 
linux-user/elfload.c +1 -1
index 1248eda..eda763d 100644
@@ -1498,7 +1498,7 @@ static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
     k_platform = ELF_PLATFORM;
     if (k_platform) {
         size_t len = strlen(k_platform) + 1;
-        sp -= (len + n - 1) & ~(n - 1);
+        sp -= ROUND_UP(len, n);
         u_platform = sp;
         /* FIXME - check return value of memcpy_to_target() for failure */
         memcpy_to_target(sp, k_platform, len);
linux-user/syscall.c +2 -2
index 7d74079..2ccaba7 100644
@@ -7432,7 +7432,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                 ret = -TARGET_EINVAL;
                 break;
             }
-            mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
+            mask_size = ROUND_UP(arg2, sizeof(*mask));
 
             mask = alloca(mask_size);
             ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
@@ -7473,7 +7473,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                 ret = -TARGET_EINVAL;
                 break;
             }
-            mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
+            mask_size = ROUND_UP(arg2, sizeof(*mask));
 
             mask = alloca(mask_size);
             if (!lock_user_struct(VERIFY_READ, p, arg3, 1)) {
tcg/ppc/tcg-target.c +2 -2
index c83fd9f..e56bb9d 100644
@@ -2630,14 +2630,14 @@ void flush_icache_range(uintptr_t start, uintptr_t stop)
     size_t isize = icache_bsize;
 
     start1 = start & ~(dsize - 1);
-    stop1 = (stop + dsize - 1) & ~(dsize - 1);
+    stop1 = ROUND_UP(stop, dsize);
     for (p = start1; p < stop1; p += dsize) {
         asm volatile ("dcbst 0,%0" : : "r"(p) : "memory");
     }
     asm volatile ("sync" : : : "memory");
 
     start &= start & ~(isize - 1);
-    stop1 = (stop + isize - 1) & ~(isize - 1);
+    stop1 = ROUND_UP(stop, isize);
     for (p = start1; p < stop1; p += isize) {
         asm volatile ("icbi 0,%0" : : "r"(p) : "memory");
     }
thunk.c +2 -2
index 3cca047..de7d969 100644
@@ -96,14 +96,14 @@ void thunk_register_struct(int id, const char *name, const argtype *types)
         for(j = 0;j < nb_fields; j++) {
             size = thunk_type_size(type_ptr, i);
             align = thunk_type_align(type_ptr, i);
-            offset = (offset + align - 1) & ~(align - 1);
+            offset = ROUND_UP(offset, align);
             se->field_offsets[i][j] = offset;
             offset += size;
             if (align > max_align)
                 max_align = align;
             type_ptr = thunk_type_next(type_ptr);
         }
-        offset = (offset + max_align - 1) & ~(max_align - 1);
+        offset = ROUND_UP(offset, max_align);
         se->size[i] = offset;
         se->align[i] = max_align;
 #ifdef DEBUG
ui/vnc-enc-zlib.c +1 -1
index d1b97f2..eaf5832 100644
@@ -33,7 +33,7 @@ void *vnc_zlib_zalloc(void *x, unsigned items, unsigned size)
     void *p;
 
     size *= items;
-    size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
+    size = ROUND_UP(size, ZALLOC_ALIGNMENT);
 
     p = g_malloc0(size);
 
util/oslib-posix.c +1 -1
index 1524ead..3f7b2b6 100644
@@ -392,7 +392,7 @@ void os_mem_prealloc(int fd, char *area, size_t memory)
     }
 
     /* MAP_POPULATE silently ignores failures */
-    memory = (memory + hpagesize - 1) & -hpagesize;
+    memory = ROUND_UP(memory, hpagesize);
     for (i = 0; i < (memory/hpagesize); i++) {
         memset(area + (hpagesize*i), 0, 1);
     }
xen-mapcache.c +1 -1
index eda914a..ab89cdc 100644
@@ -133,7 +133,7 @@ void xen_map_cache_init(phys_offset_to_gaddr_t f, void *opaque)
          (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT));
 
     size = mapcache->nr_buckets * sizeof (MapCacheEntry);
-    size = (size + XC_PAGE_SIZE - 1) & ~(XC_PAGE_SIZE - 1);
+    size = ROUND_UP(size, XC_PAGE_SIZE);
     DPRINTF("%s, nr_buckets = %lx size %lu\n", __func__,
             mapcache->nr_buckets, size);
     mapcache->entry = g_malloc0(size);
Signature · 1 line
1.9.3
#1 lore raw
Quoted 2 lines · Hu Tao

We already have ROUND_UP but there are similar macros and ROUND_UP-like lines all around, replace them with ROUND_UP and clean up similar macros.

Not a review (yet), but a question - how did you find lines to convert? Is there a particular grep pattern you looked for, or a Coccinelle script, or ...? I ask because it would be nice information in the commit message, so that the actual review can also look to see if you missed any instances that could use the conversion.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#2 lore raw
Quoted 9 lines · Eric Blake
Quoted 2 lines · Hu Tao

We already have ROUND_UP but there are similar macros and ROUND_UP-like lines all around, replace them with ROUND_UP and clean up similar macros.

Not a review (yet), but a question - how did you find lines to convert? Is there a particular grep pattern you looked for, or a Coccinelle script, or ...? I ask because it would be nice information in the commit message, so that the actual review can also look to see if you missed any instances that could use the conversion.

Good point. I used grep but indeed I've missed some instances.

Regards,
Hu

Reviewed-by on v1 1/1
#3 lore raw
In reply to Hu Tao
Quoted 5 lines · Hu Tao

We already have ROUND_UP but there are similar macros and ROUND_UP-like lines all around, replace them with ROUND_UP and clean up similar macros.

---

All of these conversions are correct, so:

However, without documenting how you found what to convert, I'm quite sure you missed some conversions. In fact, I found at least:

util/oslib-win32.c: memory = (memory + pagesize - 1) & -pagesize;

tcg/ppc/tcg-target.c-#define FRAME_SIZE ((TCG_TARGET_CALL_STACK_OFFSET   \
tcg/ppc/tcg-target.c-                     + TCG_STATIC_CALL_ARGS_SIZE    \
tcg/ppc/tcg-target.c-                     + CPU_TEMP_BUF_SIZE            \
tcg/ppc/tcg-target.c-                     + REG_SAVE_SIZE                \
tcg/ppc/tcg-target.c-                     + TCG_TARGET_STACK_ALIGN - 1)  \

tcg/ppc/tcg-target.c: & -TCG_TARGET_STACK_ALIGN)

block/cow.c: s->cow_sectors_offset = (bitmap_size + 511) & ~511;

block/qcow.c: header_size = (header_size + 7) & ~7;

before I quit looking for more. (I just manually read through
git grep '[^&]& [~-]'
which has a lot of noise for non-rounding uses, but definitely found some missed cases)

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#4 lore raw
Quoted 9 lines · Eric Blake
Quoted 5 lines · Hu Tao

We already have ROUND_UP but there are similar macros and ROUND_UP-like lines all around, replace them with ROUND_UP and clean up similar macros.

---

All of these conversions are correct, so:

Thanks for review!

Quoted 5 lines

However, without documenting how you found what to convert, I'm quite sure you missed some conversions. In fact, I found at least:

util/oslib-win32.c: memory = (memory + pagesize - 1) & -pagesize;

The pattern I used is '(.*+.*\-.*\&.*~'. Apparently I forgot the -pagesize case.

Quoted 7 lines
tcg/ppc/tcg-target.c-#define FRAME_SIZE ((TCG_TARGET_CALL_STACK_OFFSET   \
tcg/ppc/tcg-target.c-                     + TCG_STATIC_CALL_ARGS_SIZE    \
tcg/ppc/tcg-target.c-                     + CPU_TEMP_BUF_SIZE            \
tcg/ppc/tcg-target.c-                     + REG_SAVE_SIZE                \
tcg/ppc/tcg-target.c-                     + TCG_TARGET_STACK_ALIGN - 1)  \

tcg/ppc/tcg-target.c: & -TCG_TARGET_STACK_ALIGN)

How did you find this one? Your pattern below just shows the last line.

Quoted 14 lines

block/cow.c: s->cow_sectors_offset = (bitmap_size + 511) & ~511;

block/qcow.c: header_size = (header_size + 7) & ~7;

before I quit looking for more. (I just manually read through
git grep '[^&]& [~-]'
which has a lot of noise for non-rounding uses, but definitely found some missed cases)

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#5 lore raw
Quoted 13 lines · Hu Tao
Quoted 9 lines · Eric Blake
Quoted 5 lines · Hu Tao

We already have ROUND_UP but there are similar macros and ROUND_UP-like lines all around, replace them with ROUND_UP and clean up similar macros.

---

All of these conversions are correct, so:

Thanks for review!

Quoted 8 lines
Quoted 6 lines
tcg/ppc/tcg-target.c-#define FRAME_SIZE ((TCG_TARGET_CALL_STACK_OFFSET   \
tcg/ppc/tcg-target.c-                     + TCG_STATIC_CALL_ARGS_SIZE    \
tcg/ppc/tcg-target.c-                     + CPU_TEMP_BUF_SIZE            \
tcg/ppc/tcg-target.c-                     + REG_SAVE_SIZE                \
tcg/ppc/tcg-target.c-                     + TCG_TARGET_STACK_ALIGN - 1)  \

tcg/ppc/tcg-target.c: & -TCG_TARGET_STACK_ALIGN)

How did you find this one? Your pattern below just shows the last line.

But the last line had the word ALIGN in it, so I looked at the context and found that it was indeed a rounding operation.

Quoted 4 lines
Quoted 4 lines

before I quit looking for more. (I just manually read through
git grep '[^&]& [~-]'
which has a lot of noise for non-rounding uses, but definitely found some missed cases)

Are you going to submit a v2? As this does not appear to be a bug fix, and today is hard freeze, this is probably 2.2 material.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#6 lore raw
Quoted 26 lines · Eric Blake
Quoted 13 lines · Hu Tao
Quoted 9 lines · Eric Blake
Quoted 5 lines · Hu Tao

We already have ROUND_UP but there are similar macros and ROUND_UP-like lines all around, replace them with ROUND_UP and clean up similar macros.

---

All of these conversions are correct, so:

Thanks for review!

Quoted 8 lines
Quoted 6 lines
tcg/ppc/tcg-target.c-#define FRAME_SIZE ((TCG_TARGET_CALL_STACK_OFFSET   \
tcg/ppc/tcg-target.c-                     + TCG_STATIC_CALL_ARGS_SIZE    \
tcg/ppc/tcg-target.c-                     + CPU_TEMP_BUF_SIZE            \
tcg/ppc/tcg-target.c-                     + REG_SAVE_SIZE                \
tcg/ppc/tcg-target.c-                     + TCG_TARGET_STACK_ALIGN - 1)  \

tcg/ppc/tcg-target.c: & -TCG_TARGET_STACK_ALIGN)

How did you find this one? Your pattern below just shows the last line.

But the last line had the word ALIGN in it, so I looked at the context and found that it was indeed a rounding operation.

Quoted 7 lines
Quoted 4 lines
Quoted 4 lines

before I quit looking for more. (I just manually read through
git grep '[^&]& [~-]'
which has a lot of noise for non-rounding uses, but definitely found some missed cases)

Are you going to submit a v2? As this does not appear to be a bug fix, and today is hard freeze, this is probably 2.2 material.

Yes. I'm fine it gets in after 2.1.

Regards,
Hu