make range overlap check more readable
Subsystems (3)
cover make range overlap check more readable
Currently, some components still open-coding the range overlap check. Sometimes this check may be fail because some patterns are missed.
To avoid the above problems and improve the readability of the code, it is better to use the ranges_overlap() to do this check.
Yao Xingtao (13):
range: Make ranges_overlap() return bool
arm/boot: make range overlap check more readable core/loader: make range overlap check more readable cxl/mailbox: make range overlap check more readable display/sm501: make range overlap check more readable
aspeed_smc: make range overlap check more readable qtest/fuzz: make range overlap check more readable sparc/ldst_helper: make range overlap check more readable system/memory_mapping: make range overlap check more readable block/vhdx: make range overlap check more readable crypto/block-luks: make range overlap check more readable
dump: make range overlap check more readable block/qcow2-cluster: make range overlap check more readable
block/qcow2-cluster.c | 23 +++++++++++++---------- block/vhdx.c | 12 +++++++----- crypto/block-luks.c | 3 ++- dump/dump.c | 12 ++++++++---- hw/arm/boot.c | 5 +++-- hw/core/loader.c | 4 +++- hw/cxl/cxl-mailbox-utils.c | 8 ++++---- hw/display/sm501.c | 12 ++++++------ hw/ssi/aspeed_smc.c | 4 ++-- include/qemu/range.h | 4 ++-- system/memory_mapping.c | 4 ++-- target/sparc/ldst_helper.c | 5 ++--- tests/qtest/fuzz/generic_fuzz.c | 3 ++- 13 files changed, 56 insertions(+), 43 deletions(-)
Signature · 1 line
2.41.0
PATCH 1/13 range: Make ranges_overlap() return bool
Just like range_overlaps_range(), use the returned bool value to check whether 2 given ranges overlap.
--- include/qemu/range.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/qemu/range.h +2 -2
@@ -210,8 +210,8 @@ static inline int range_covers_byte(uint64_t offset, uint64_t len,/* Check whether 2 given ranges overlap. * Undefined if ranges that wrap around 0. */ -static inline int ranges_overlap (uint64_t first1,uint64_t len1, -uint64_t first2,uint64_t len2) +static inline bool ranges_overlap (uint64_t first1,uint64_t len1, +uint64_t first2,uint64_t len2) {uint64_t last1 =range_get_last (first1, len1);uint64_t last2 =range_get_last (first2, len2);
Signature · 1 line
2.41.0
Re: [PATCH 01/13] range: Make ranges_overlap() return bool
Quoted 7 lines · Yao Xingtao via
Just like
range_overlaps_range(), use the returned bool value to check whether 2 given ranges overlap.Signed-off-by Yao Xingtao <[email protected]>--- include/qemu/range.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
PATCH 3/13 core/loader: make range overlap check more readable
In reply to Yao Xingtao viause ranges_overlap() instead of open-coding the overlap check to improve the readability of the code.
--- hw/core/loader.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
hw/core/loader.c +3 -1
@@ -63,6 +63,7 @@#include "qemu/cutils.h" #include "sysemu/runstate.h" #include "tcg/debuginfo.h" +#include "qemu/range.h" #include <zlib.h>
@@ -1278,7 +1279,8 @@ static bool roms_overlap(Rom *last_rom, Rom *this_rom)return false; }return last_rom->as == this_rom->as && - last_rom->addr + last_rom->romsize > this_rom->addr; +ranges_overlap (last_rom->addr, last_rom->romsize, + this_rom->addr, this_rom->romsize); }static const char *rom_as_name (Rom *rom)
Signature · 1 line
2.41.0
PATCH 2/13 arm/boot: make range overlap check more readable
In reply to Yao Xingtao viause ranges_overlap() instead of open-coding the overlap check to improve the readability of the code.
--- hw/arm/boot.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
hw/arm/boot.c +3 -2
@@ -26,6 +26,7 @@#include "qemu/config-file.h" #include "qemu/option.h" #include "qemu/units.h" +#include "qemu/range.h" /* Kernel boot protocol is specified in the kernel docs * Documentation/arm/Booting and Documentation/arm64/booting.txt
@@ -238,8 +239,8 @@ void arm_write_secure_board_setup_dummy_smc(ARMCPU *cpu,assert ((mvbar_addr &0x1f ) ==0 && (mvbar_addr >>4 ) <0x100 );/* check that these blobs don't overlap */ -assert ((mvbar_addr +sizeof (mvbar_blob) <= info->board_setup_addr) - || (info->board_setup_addr +sizeof (board_setup_blob) <= mvbar_addr)); +assert (!ranges_overlap (mvbar_addr,sizeof (mvbar_blob), + info->board_setup_addr,sizeof (board_setup_blob)));for (n =0 ; n <ARRAY_SIZE (mvbar_blob); n++) { mvbar_blob[n] =tswap32 (mvbar_blob[n]);
Signature · 1 line
2.41.0
Re: [PATCH 02/13] arm/boot: make range overlap check more readable
Quoted 21 lines · Yao Xingtao via
use
ranges_overlap()instead of open-coding the overlap check to improve the readability of the code.Signed-off-by Yao Xingtao <[email protected]>--- hw/arm/boot.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)hw/arm/boot.c +1
-0@@ -26,6 +26,7 @@#include "qemu/config-file.h" #include "qemu/option.h" #include "qemu/units.h" +#include "qemu/range.h" /* Kernel boot protocol is specified in the kernel docs * Documentation/arm/Booting and Documentation/arm64/booting.txt
hw/arm/boot.c void arm_write_secure_board_setup_dummy_smc(ARMCPU *cpu, · line 243
Quoted 7 lines
hw/arm/boot.c +2
-2@@ -238,8 +239,8 @@ void arm_write_secure_board_setup_dummy_smc(ARMCPU *cpu,assert ((mvbar_addr &0x1f ) ==0 && (mvbar_addr >>4 ) <0x100 );/* check that these blobs don't overlap */ -assert ((mvbar_addr +sizeof (mvbar_blob) <= info->board_setup_addr) - || (info->board_setup_addr +sizeof (board_setup_blob) <= mvbar_addr)); +assert (!ranges_overlap (mvbar_addr,sizeof (mvbar_blob), + info->board_setup_addr,sizeof (board_setup_blob)));
Indentation is of, otherwise:
PATCH 4/13 cxl/mailbox: make range overlap check more readable
In reply to Yao Xingtao viause ranges_overlap() instead of open-coding the overlap check to improve the readability of the code.
--- hw/cxl/cxl-mailbox-utils.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
hw/cxl/cxl-mailbox-utils.c +4 -4
@@ -1086,8 +1086,8 @@ static CXLRetCode cmd_media_get_poison_list(const struct cxl_cmd *cmd, QLIST_FOREACH(ent, poison_list, node) {/* Check for no overlap */ -if (ent->start >= query_start + query_length || - ent->start + ent->length <= query_start) { +if (!ranges_overlap (ent->start, ent->length, + query_start, query_length)) {continue ; } record_count++;
@@ -1100,8 +1100,8 @@ static CXLRetCode cmd_media_get_poison_list(const struct cxl_cmd *cmd,uint64_t start, stop;/* Check for no overlap */ -if (ent->start >= query_start + query_length || - ent->start + ent->length <= query_start) { +if (!ranges_overlap (ent->start, ent->length, + query_start, query_length)) {continue ; }
Signature · 1 line
2.41.0
Re: [PATCH 04/13] cxl/mailbox: make range overlap check more readable
Quoted 7 lines · Yao Xingtao via
use
ranges_overlap()instead of open-coding the overlap check to improve the readability of the code.Signed-off-by Yao Xingtao <[email protected]>--- hw/cxl/cxl-mailbox-utils.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
PATCH 5/13 display/sm501: make range overlap check more readable
In reply to Yao Xingtao viause ranges_overlap() instead of open-coding the overlap check to improve the readability of the code.
--- hw/display/sm501.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
hw/display/sm501.c +6 -6
@@ -823,15 +823,15 @@ static void sm501_2d_operation(SM501State *s) }/* If reverse blit do simple check for overlaps */ if (rtl && src_base == dst_base && src_pitch == dst_pitch) { - overlap = (src_x < dst_x + width && src_x + width > dst_x && - src_y < dst_y + height && src_y + height > dst_y); + overlap = (ranges_overlap (src_x, width, dst_x, width) && +ranges_overlap (src_y, height, dst_y, height)); }else if (rtl) { -unsigned int sb, se, db, de; +unsigned int sb, sl, db, dl; sb = src_base + (src_x + src_y * src_pitch) * bypp; - se = sb + (width + (height -1 ) * src_pitch) * bypp; + sl = (width + (height -1 ) * src_pitch) * bypp; db = dst_base + (dst_x + dst_y * dst_pitch) * bypp; - de = db + (width + (height -1 ) * dst_pitch) * bypp; - overlap = (db < se && sb < de); + dl = (width + (height -1 ) * dst_pitch) * bypp; + overlap =ranges_overlap (sb, sl, db, dl); }#ifdef CONFIG_PIXMAN if (overlap && (s->use_pixman &BIT (2 ))) {
Signature · 1 line
2.41.0
PATCH 7/13 qtest/fuzz: make range overlap check more readable
In reply to Yao Xingtao viause ranges_overlap() instead of open-coding the overlap check to improve the readability of the code.
--- tests/qtest/fuzz/generic_fuzz.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tests/qtest/fuzz/generic_fuzz.c +2 -1
@@ -11,6 +11,7 @@*/ #include "qemu/osdep.h" +#include "qemu/range.h" #include <wordexp.h>
@@ -211,7 +212,7 @@ void fuzz_dma_read_cb(size_t addr, size_t len, MemoryRegion *mr) i < dma_regions->len && (avoid_double_fetches || qtest_log_enabled); ++i) { region =g_array_index (dma_regions, address_range, i); -if (addr < region.addr + region.size && addr + len > region.addr) { +if (ranges_overlap (addr, len, region.addr, region.size)) { double_fetch = true;if (addr < region.addr && avoid_double_fetches) {
Signature · 1 line
2.41.0
Re: [PATCH 07/13] qtest/fuzz: make range overlap check more readable
Quoted 4 lines · Yao Xingtao
use
ranges_overlap()instead of open-coding the overlap check to improve the readability of the code.Signed-off-by Yao Xingtao <[email protected]>
Thank you
Quoted 28 lines
--- tests/qtest/fuzz/generic_fuzz.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)tests/qtest/fuzz/generic_fuzz.c +2
-1@@ -11,6 +11,7 @@*/ #include "qemu/osdep.h" +#include "qemu/range.h" #include <wordexp.h> @@ -211,7 +212,7 @@ void fuzz_dma_read_cb(size_t addr, size_t len, MemoryRegion *mr) i < dma_regions->len && (avoid_double_fetches || qtest_log_enabled); ++i) { region =g_array_index (dma_regions, address_range, i); -if (addr < region.addr + region.size && addr + len > region.addr) { +if (ranges_overlap (addr, len, region.addr, region.size)) { double_fetch = true;if (addr < region.addr && avoid_double_fetches) {Signature · 1 line
2.41.0
Re: [PATCH 07/13] qtest/fuzz: make range overlap check more readable
In reply to Yao Xingtao viaQuoted 7 lines · Yao Xingtao via
use
ranges_overlap()instead of open-coding the overlap check to improve the readability of the code.Signed-off-by Yao Xingtao <[email protected]>--- tests/qtest/fuzz/generic_fuzz.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
PATCH 8/13 sparc/ldst_helper: make range overlap check more readable
In reply to Yao Xingtao viause ranges_overlap() instead of open-coding the overlap check to improve the readability of the code.
--- target/sparc/ldst_helper.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
target/sparc/ldst_helper.c +2 -3
@@ -19,6 +19,7 @@#include "qemu/osdep.h" #include "qemu/log.h" +#include "qemu/range.h" #include "cpu.h" #include "tcg/tcg.h" #include "exec/helper-proto.h"
@@ -240,9 +241,7 @@ static void replace_tlb_1bit_lru(SparcTLBEntry *tlb,if (new_ctx == ctx) {uint64_t vaddr = tlb[i].tag & ~0x1fffULL ;uint64_t size =8192ULL <<3 *TTE_PGSIZE (tlb[i].tte); -if (new_vaddr == vaddr - || (new_vaddr < vaddr + size - && vaddr < new_vaddr + new_size)) { +if (ranges_overlap (new_vaddr, new_size, vaddr, size)) {DPRINTF_MMU ("auto demap entry [%d] %lx->%lx\n" , i, vaddr, new_vaddr);replace_tlb_entry (&tlb[i], tlb_tag, tlb_tte, env1);
Signature · 1 line
2.41.0
Re: [PATCH 08/13] sparc/ldst_helper: make range overlap check more readable
Quoted 21 lines · Yao Xingtao via
use
ranges_overlap()instead of open-coding the overlap check to improve the readability of the code.Signed-off-by Yao Xingtao <[email protected]>--- target/sparc/ldst_helper.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)target/sparc/ldst_helper.c +1
-0@@ -19,6 +19,7 @@#include "qemu/osdep.h" #include "qemu/log.h" +#include "qemu/range.h" #include "cpu.h" #include "tcg/tcg.h" #include "exec/helper-proto.h"
target/sparc/ldst_helper.c static void replace_tlb_1bit_lru(SparcTLBEntry *tlb, · line 243
Quoted 4 lines
target/sparc/ldst_helper.c +0
-1@@ -240,9 +241,7 @@ static void replace_tlb_1bit_lru(SparcTLBEntry *tlb,if (new_ctx == ctx) {uint64_t vaddr = tlb[i].tag & ~0x1fffULL ;uint64_t size =8192ULL <<3 *TTE_PGSIZE (tlb[i].tte); - if (new_vaddr == vaddr
Please mention in the patch description why it is safe to remove this equality check. With that:
Quoted 6 lines
target/sparc/ldst_helper.c +1
-2- || (new_vaddr < vaddr + size - && vaddr < new_vaddr + new_size)) { +if (ranges_overlap (new_vaddr, new_size, vaddr, size)) {DPRINTF_MMU ("auto demap entry [%d] %lx->%lx\n" , i, vaddr, new_vaddr);replace_tlb_entry (&tlb[i], tlb_tag, tlb_tte, env1);
PATCH 9/13 system/memory_mapping: make range overlap check more readable
In reply to Yao Xingtao viause ranges_overlap() instead of open-coding the overlap check to improve the readability of the code.
--- system/memory_mapping.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
system/memory_mapping.c +2 -2
@@ -12,6 +12,7 @@*/ #include "qemu/osdep.h" +#include "qemu/range.h" #include "qapi/error.h" #include "sysemu/memory_mapping.h"
@@ -353,8 +354,7 @@ void memory_mapping_filter(MemoryMappingList *list, int64_t begin,MemoryMapping *cur, *next; QTAILQ_FOREACH_SAFE(cur, &list->head, next, next) { -if (cur->phys_addr >= begin + length || - cur->phys_addr + cur->length <= begin) { +if (!ranges_overlap (cur->phys_addr, cur->length, begin, length)) {QTAILQ_REMOVE (&list->head, cur, next);g_free (cur); list->num--;
Signature · 1 line
2.41.0
Re: [PATCH 09/13] system/memory_mapping: make range overlap check more readable
Quoted 21 lines · Yao Xingtao
use
ranges_overlap()instead of open-coding the overlap check to improve the readability of the code.Signed-off-by Yao Xingtao <[email protected]>--- system/memory_mapping.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)system/memory_mapping.c +1
-0@@ -12,6 +12,7 @@*/ #include "qemu/osdep.h" +#include "qemu/range.h" #include "qapi/error.h" #include "sysemu/memory_mapping.h"
system/memory_mapping.c void memory_mapping_filter(MemoryMappingList *list, int64_t begin, · line 357
Quoted 6 lines
system/memory_mapping.c +1
-2@@ -353,8 +354,7 @@ void memory_mapping_filter(MemoryMappingList *list, int64_t begin,MemoryMapping *cur, *next; QTAILQ_FOREACH_SAFE(cur, &list->head, next, next) { -if (cur->phys_addr >= begin + length || - cur->phys_addr + cur->length <= begin) { +if (!ranges_overlap (cur->phys_addr, cur->length, begin, length)) {
Maybe this one is easier to read the other way around:
if (!ranges_overlap(begin, length, cur->phys_addr, cur->length)) {Anyhow,
Re: [PATCH 09/13] system/memory_mapping: make range overlap check more readable
In reply to Yao Xingtao viaQuoted 21 lines · Yao Xingtao
use
ranges_overlap()instead of open-coding the overlap check to improve the readability of the code.Signed-off-by Yao Xingtao <[email protected]>--- system/memory_mapping.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)system/memory_mapping.c +1
-0@@ -12,6 +12,7 @@*/ #include "qemu/osdep.h" +#include "qemu/range.h" #include "qapi/error.h" #include "sysemu/memory_mapping.h"
system/memory_mapping.c void memory_mapping_filter(MemoryMappingList *list, int64_t begin, · line 360
Quoted 9 lines
system/memory_mapping.c +1
-2@@ -353,8 +354,7 @@ void memory_mapping_filter(MemoryMappingList *list, int64_t begin,MemoryMapping *cur, *next; QTAILQ_FOREACH_SAFE(cur, &list->head, next, next) { -if (cur->phys_addr >= begin + length || - cur->phys_addr + cur->length <= begin) { +if (!ranges_overlap (cur->phys_addr, cur->length, begin, length)) {QTAILQ_REMOVE (&list->head, cur, next);g_free (cur); list->num--;
Signature · 3 lines
Cheers, David / dhildenb
PATCH 10/13 block/vhdx: make range overlap check more readable
In reply to Yao Xingtao viause range_overlaps_range() instead of open-coding the overlap check to improve the readability of the code.
--- block/vhdx.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
block/vhdx.c +7 -5
@@ -32,6 +32,7 @@#include "qapi/qmp/qdict.h" #include "qapi/qobject-input-visitor.h" #include "qapi/qapi-visit-block-core.h" +#include "qemu/range.h" /* Options for VHDX creation */
@@ -231,15 +232,16 @@ void vhdx_guid_generate(MSGUID *guid)static int vhdx_region_check (BDRVVHDXState *s,uint64_t start,uint64_t length) {int ret =0 ; -uint64_t end;VHDXRegionEntry *r; +Range range1, range2; - end = start + length; +range_init_nofail (&range1, start, length); QLIST_FOREACH(r, &s->regions, entries) { -if (!((start >= r->end) || (end <= r->start))) { +range_init_nofail (&range2, r->start, r->end - r->start); +if (range_overlaps_range (&range1, &range2)) {error_report ("VHDX region %" PRIu64"-%" PRIu64" overlaps with " -"region %" PRIu64"-%." PRIu64, start, end, r->start, - r->end); +"region %" PRIu64"-%." PRIu64, start, start + length, + r->start, r->end); ret = -EINVAL ; goto exit; }
Signature · 1 line
2.41.0
PATCH 11/13 crypto/block-luks: make range overlap check more readable
In reply to Yao Xingtao viause ranges_overlap() instead of open-coding the overlap check to improve the readability of the code.
--- crypto/block-luks.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
crypto/block-luks.c +2 -1
@@ -33,6 +33,7 @@#include "qemu/uuid.h" #include "qemu/bitmap.h" +#include "qemu/range.h" /* * Reference for the LUKS format implemented here is
@@ -572,7 +573,7 @@ qcrypto_block_luks_check_header(const QCryptoBlockLUKS *luks, header_sectors, slot2->stripes); -if (start1 + len1 > start2 && start2 + len2 > start1) { +if (ranges_overlap (start1, len1, start2, len2)) {error_setg (errp,"Keyslots %zu and %zu are overlapping in the header" , i, j);
Signature · 1 line
2.41.0
Re: [PATCH 11/13] crypto/block-luks: make range overlap check more readable
Quoted 7 lines · Yao Xingtao via
use
ranges_overlap()instead of open-coding the overlap check to improve the readability of the code.Signed-off-by Yao Xingtao <[email protected]>--- crypto/block-luks.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Re: [PATCH 11/13] crypto/block-luks: make range overlap check more readable
In reply to Yao Xingtao viaQuoted 7 lines · Yao Xingtao
use
ranges_overlap()instead of open-coding the overlap check to improve the readability of the code.Signed-off-by Yao Xingtao <[email protected]>--- crypto/block-luks.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
PATCH 12/13 dump: make range overlap check more readable
In reply to Yao Xingtao viause ranges_overlap() instead of open-coding the overlap check to improve the readability of the code.
--- dump/dump.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
dump/dump.c +8 -4
@@ -30,6 +30,7 @@#include "migration/blocker.h" #include "hw/core/cpu.h" #include "win_dump.h" +#include "qemu/range.h" #include <zlib.h> #ifdef CONFIG_LZO
@@ -574,8 +575,10 @@ static void get_offset_range(hwaddr phys_addr, QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {if (dump_has_filter (s)) { -if (block->target_start >= s->filter_area_begin + s->filter_area_length || - block->target_end <= s->filter_area_begin) { +if (!ranges_overlap (block->target_start, + block->target_end - block->target_start, + s->filter_area_begin, + s->filter_area_length)) {/* This block is out of the range */ continue ; }
@@ -734,8 +737,9 @@ int64_t dump_filtered_memblock_start(GuestPhysBlock *block, {if (filter_area_length) {/* return -1 if the block is not within filter area */ -if (block->target_start >= filter_area_start + filter_area_length || - block->target_end <= filter_area_start) { +if (!ranges_overlap (block->target_start, + block->target_end - block->target_start, + filter_area_start, filter_area_length)) {return -1 ; }
Signature · 1 line
2.41.0
Re: [PATCH 12/13] dump: make range overlap check more readable
Quoted 5 lines · Yao Xingtao
use
ranges_overlap()instead of open-coding the overlap check to improve the readability of the code.Signed-off-by Yao Xingtao <[email protected]>
Quoted 48 lines
--- dump/dump.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)dump/dump.c +1
-1@@ -30,6 +30,7 @@#include "migration/blocker.h" #include "hw/core/cpu.h" #include "win_dump.h" +#include "qemu/range.h" #include <zlib.h> #ifdef CONFIG_LZO @@ -574,8 +575,10 @@ static void get_offset_range(hwaddr phys_addr, QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {if (dump_has_filter (s)) { - if (block->target_start >= s->filter_area_begin +
s->filter_area_length||dump/dump.c +4
-2- block->target_end <= s->filter_area_begin) { +if (!ranges_overlap (block->target_start, + block->target_end - block->target_start, + s->filter_area_begin, + s->filter_area_length)) {/* This block is out of the range */ continue ; }@@ -734,8 +737,9 @@ int64_t dump_filtered_memblock_start(GuestPhysBlock *block, {if (filter_area_length) {/* return -1 if the block is not within filter area */ - if (block->target_start >= filter_area_start + filter_area_length||
dump/dump.c +3
-2- block->target_end <= filter_area_start) { +if (!ranges_overlap (block->target_start, + block->target_end - block->target_start, + filter_area_start, filter_area_length)) {return -1 ; } --2.41.0
PATCH 13/13 block/qcow2-cluster: make range overlap check more readable
In reply to Yao Xingtao viause range_overlaps_range() instead of open-coding the overlap check to improve the readability of the code.
--- block/qcow2-cluster.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-)
block/qcow2-cluster.c +13 -10
@@ -30,6 +30,7 @@#include "qcow2.h" #include "qemu/bswap.h" #include "qemu/memalign.h" +#include "qemu/range.h" #include "trace.h" int coroutine_fn qcow2_shrink_l1_table(BlockDriverState *bs,
@@ -1408,23 +1409,25 @@ static int coroutine_fn handle_dependencies(BlockDriverState *bs,BDRVQcow2State *s = bs->opaque;QCowL2Meta *old_alloc;uint64_t bytes = *cur_bytes; +Range range1, range2, range3; +range_init_nofail (&range1, guest_offset, bytes); QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) { -uint64_t start = guest_offset; -uint64_t end = start + bytes; -uint64_t old_start =start_of_cluster (s,l2meta_cow_start (old_alloc)); -uint64_t old_end =ROUND_UP (l2meta_cow_end (old_alloc), s->cluster_size); +uint64_t cow_start =l2meta_cow_start (old_alloc); +uint64_t cow_end =l2meta_cow_end (old_alloc); +uint64_t start =start_of_cluster (s, cow_start); +uint64_t end =ROUND_UP (cow_end, s->cluster_size); -if (end <= old_start || start >= old_end) { +range_init_nofail (&range2, start, end - start); +if (!range_overlaps_range (&range1, &range2)) {/* No intersection */ continue ; } +range_init_nofail (&range3, cow_start, cow_end - cow_start);if (old_alloc->keep_old_clusters && - (end <=l2meta_cow_start (old_alloc) || - start >=l2meta_cow_end (old_alloc))) - { + !range_overlaps_range (&range1, &range3)) {/* * Clusters intersect but COW areas don't. And cluster itself is * already allocated. So, there is no actual conflict.
@@ -1434,9 +1437,9 @@ static int coroutine_fn handle_dependencies(BlockDriverState *bs,/* Conflict */ -if (start < old_start) { +if (guest_offset < start) {/* Stop at the start of a running allocation */ - bytes = old_start - start; + bytes = start - guest_offset; }else { bytes =0 ; }
Signature · 1 line
2.41.0
RE: [PATCH 13/13] block/qcow2-cluster: make range overlap check more readable
ping.
Quoted 80 lines
-----Original Message-----
From: Yao Xingtao <[email protected]>
Sent: Monday, July 22, 2024 12:08 PM
To: [email protected]; Kevin Wolf <[email protected]>; Hanna Reitz <[email protected]>
Cc: Yao, Xingtao/姚 幸涛 <[email protected]>; [email protected]
Subject: [PATCH 13/13]block/qcow2-cluster: make range overlap check more readableuse
range_overlaps_range()instead of open-coding the overlap check to improve the readability of the code.Signed-off-by Yao Xingtao <[email protected]>--- block/qcow2-cluster.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-)block/qcow2-cluster.c +11
-8@@ -30,6 +30,7 @@#include "qcow2.h" #include "qemu/bswap.h" #include "qemu/memalign.h" +#include "qemu/range.h" #include "trace.h" int coroutine_fn qcow2_shrink_l1_table(BlockDriverState *bs,@@ -1408,23 +1409,25 @@ static int coroutine_fnhandle_dependencies (BlockDriverState *bs, BDRVQcow2State *s = bs->opaque;QCowL2Meta *old_alloc;uint64_t bytes = *cur_bytes; +Range range1, range2, range3; +range_init_nofail (&range1, guest_offset, bytes); QLIST_FOREACH(old_alloc, &s->cluster_allocs, next_in_flight) { -uint64_t start = guest_offset; -uint64_t end = start + bytes; -uint64_t old_start =start_of_cluster (s,l2meta_cow_start (old_alloc)); -uint64_t old_end =ROUND_UP (l2meta_cow_end (old_alloc), s->cluster_size); +uint64_t cow_start =l2meta_cow_start (old_alloc); +uint64_t cow_end =l2meta_cow_end (old_alloc); +uint64_t start =start_of_cluster (s, cow_start); +uint64_t end =ROUND_UP (cow_end, s->cluster_size); -if (end <= old_start || start >= old_end) { +range_init_nofail (&range2, start, end - start); +if (!range_overlaps_range (&range1, &range2)) {/* No intersection */ continue ; } +range_init_nofail (&range3, cow_start, cow_end - cow_start);if (old_alloc->keep_old_clusters && - (end <=l2meta_cow_start (old_alloc) || - start >=l2meta_cow_end (old_alloc))) - { + !range_overlaps_range (&range1, &range3)) {/* * Clusters intersect but COW areas don't. And cluster itself is
* already allocated. So, there is no actual conflict.block/qcow2-cluster.c +2
-2@@ -1434,9 +1437,9 @@ static int coroutine_fn handle_dependencies(BlockDriverState *bs, /* Conflict */ - if (start < old_start) { + if (guest_offset < start) { /* Stop at the start of a running allocation */ - bytes = old_start - start; + bytes = start - guest_offset; } else { bytes = 0;}--
2.41.0
PATCH 6/13 aspeed_smc: make range overlap check more readable
In reply to Yao Xingtao viause ranges_overlap() instead of open-coding the overlap check to improve the readability of the code.
--- hw/ssi/aspeed_smc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
hw/ssi/aspeed_smc.c +2 -2
@@ -31,6 +31,7 @@#include "qemu/error-report.h" #include "qapi/error.h" #include "qemu/units.h" +#include "qemu/range.h" #include "trace.h" #include "hw/irq.h"
@@ -246,8 +247,7 @@ static bool aspeed_smc_flash_overlap(const AspeedSMCState *s, asc->reg_to_segment (s, s->regs[R_SEG_ADDR0 + i], &seg); -if (new->addr + new->size > seg.addr && - new->addr < seg.addr + seg.size) { +if (ranges_overlap (new->addr, new->size, seg.addr, seg.size)) { aspeed_smc_error("new segment CS%d [ 0x%" HWADDR_PRIx" - 0x%"HWADDR_PRIx" ] overlaps with " "CS%d [ 0x%"HWADDR_PRIx" - 0x%"HWADDR_PRIx" ]",
Signature · 1 line
2.41.0
Re: [PATCH 06/13] aspeed_smc: make range overlap check more readable
Quoted 7 lines · Yao Xingtao via
use
ranges_overlap()instead of open-coding the overlap check to improve the readability of the code.Signed-off-by Yao Xingtao <[email protected]>--- hw/ssi/aspeed_smc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Hi Yao,
Quoted 2 lines · Yao Xingtao via
Currently, some components still open-coding the range overlap check. Sometimes this check may be fail because some patterns are missed.
How did you catch all these use cases?
Quoted 17 lines
To avoid the above problems and improve the readability of the code, it is better to use the
ranges_overlap()to do this check.Yao Xingtao (13):
range: Makeranges_overlap()return bool
arm/boot: make range overlap check more readable core/loader: make range overlap check more readable cxl/mailbox: make range overlap check more readable display/sm501: make range overlap check more readable
aspeed_smc: make range overlap check more readable qtest/fuzz: make range overlap check more readable sparc/ldst_helper: make range overlap check more readable system/memory_mapping: make range overlap check more readable block/vhdx: make range overlap check more readablecrypto/block-luks: make range overlap check more readable
dump: make range overlap check more readableblock/qcow2-cluster: make range overlap check more readable
Quoted 8 lines
-----Original Message-----
From: Philippe Mathieu-Daudé <[email protected]>
Sent: Monday, July 22, 2024 2:43 PM
To: Yao, Xingtao/姚 幸涛 <[email protected]>; [email protected]
Subject: Re: [PATCH 00/13] make range overlap check more readable
Quoted 7 lines
Hi Yao,
Quoted 2 lines · Yao Xingtao via
Currently, some components still open-coding the range overlap check. Sometimes this check may be fail because some patterns are missed.
How did you catch all these use cases?
I used the Coccinelle to match these use cases, the pattern is below range_overlap.cocci:
// use ranges_overlap() instead of open-coding the overlap check @@
expression E1, E2, E3, E4;
@@
(
- E2 <= E3 || E1 >= E4
+ !ranges_overlap(E1, E2, E3, E4)
|
- (E2 <= E3) || (E1 >= E4)
+ !ranges_overlap(E1, E2, E3, E4)
|
- E1 < E4 && E2 > E3
+ ranges_overlap(E1, E2, E3, E4)
|
- (E1 < E4) && (E2 > E3)
+ ranges_overlap(E1, E2, E3, E4)
|
- (E1 >= E3 && E1 < E4) || (E2 > E3 && E2 <= E4)
+ ranges_overlap(E1, E2, E3, E4)
|
- ((E1 >= E3) && (E1 < E4)) || ((E2 > E3) && (E2 <= E4))
+ ranges_overlap(E1, E2, E3, E4)
)
then execute the command:
# spatch --macro-file scripts/cocci-macro-file.h --sp-file range_overlap.cocci --keep-comments --in-place --use-gitgrep --dir .
but some of the matched cases are not valid and need to be manually judged.
there may be cases that have not been matched yet.
Quoted 18 lines
Quoted 18 lines
To avoid the above problems and improve the readability of the code, it is better to use the
ranges_overlap()to do this check.Yao Xingtao (13):
range: Makeranges_overlap()return bool
arm/boot: make range overlap check more readable core/loader: make range overlap check more readable cxl/mailbox: make range overlap check more readable display/sm501: make range overlap check more readable
aspeed_smc: make range overlap check more readable qtest/fuzz: make range overlap check more readable sparc/ldst_helper: make range overlap check more readable system/memory_mapping: make range overlap check more readable block/vhdx: make range overlap check more readablecrypto/block-luks: make range overlap check more readable
dump: make range overlap check more readableblock/qcow2-cluster: make range overlap check more readable
Quoted 39 lines · Xingtao Yao (Fujitsu)
Quoted 8 lines
-----Original Message-----
From: Philippe Mathieu-Daudé <[email protected]>
Sent: Monday, July 22, 2024 2:43 PM
To: Yao, Xingtao/姚 幸涛 <[email protected]>; [email protected]
Subject: Re: [PATCH 00/13] make range overlap check more readableQuoted 7 lines
Hi Yao,
Quoted 2 lines · Yao Xingtao via
Currently, some components still open-coding the range overlap check. Sometimes this check may be fail because some patterns are missed.
How did you catch all these use cases?
I used the Coccinelle to match these use cases, the pattern is below
range_overlap.cocci:// use
ranges_overlap()instead of open-coding the overlap check @@
expression E1, E2, E3, E4;
@@
(
- E2 <= E3 || E1 >= E4
+ !ranges_overlap(E1, E2, E3, E4)
|- (E2 <= E3) || (E1 >= E4)
+ !ranges_overlap(E1, E2, E3, E4)
|- E1 < E4 && E2 > E3
+ranges_overlap(E1, E2, E3, E4)
|- (E1 < E4) && (E2 > E3)
+ranges_overlap(E1, E2, E3, E4)
|
Quoted 7 lines
- (E1 >= E3 && E1 < E4) || (E2 > E3 && E2 <= E4)
+ranges_overlap(E1, E2, E3, E4)|
- ((E1 >= E3) && (E1 < E4)) || ((E2 > E3) && (E2 <= E4))
+ranges_overlap(E1, E2, E3, E4)
)
Please add to scripts/coccinelle/range.cocci.
Quoted 8 lines
then execute the command:
# spatch --macro-file scripts/cocci-macro-file.h --sp-file range_overlap.cocci --keep-comments --in-place --use-gitgrep --dir .but some of the matched cases are not valid and need to be manually judged.
there may be cases that have not been matched yet.
Quoted 51 lines
-----Original Message-----
From: Philippe Mathieu-Daudé <[email protected]>
Sent: Monday, July 22, 2024 3:37 PM
To: Yao, Xingtao/姚 幸涛 <[email protected]>; [email protected]
Subject: Re: [PATCH 00/13] make range overlap check more readableQuoted 42 lines · Xingtao Yao (Fujitsu)
Quoted 8 lines
-----Original Message-----
From: Philippe Mathieu-Daudé <[email protected]>
Sent: Monday, July 22, 2024 2:43 PM
To: Yao, Xingtao/姚 幸涛 <[email protected]>; [email protected]
Subject: Re: [PATCH 00/13] make range overlap check more readableQuoted 7 lines
Hi Yao,
Quoted 2 lines · Yao Xingtao via
Currently, some components still open-coding the range overlap check. Sometimes this check may be fail because some patterns are missed.
How did you catch all these use cases?
I used the Coccinelle to match these use cases, the pattern is below
range_overlap.cocci:// use
ranges_overlap()instead of open-coding the overlap check @@
expression E1, E2, E3, E4;
@@
(
- E2 <= E3 || E1 >= E4
+ !ranges_overlap(E1, E2, E3, E4)
|- (E2 <= E3) || (E1 >= E4)
+ !ranges_overlap(E1, E2, E3, E4)
|- E1 < E4 && E2 > E3
+ranges_overlap(E1, E2, E3, E4)
|- (E1 < E4) && (E2 > E3)
+ranges_overlap(E1, E2, E3, E4)
|- (E1 >= E3 && E1 < E4) || (E2 > E3 && E2 <= E4)
+ranges_overlap(E1, E2, E3, E4)
Quoted 6 lines
Quoted 4 lines
|
- ((E1 >= E3) && (E1 < E4)) || ((E2 > E3) && (E2 <= E4))
+ranges_overlap(E1, E2, E3, E4)
)Please add to
scripts/coccinelle/range.cocci.
OK, I will add this file in next revision.
Quoted 10 lines
Quoted 4 lines
then execute the command:
# spatch --macro-file scripts/cocci-macro-file.h --sp-file range_overlap.cocci--keep-comments --in-place --use-gitgrep --dir .
Quoted 5 lines
but some of the matched cases are not valid and need to be manually judged.
there may be cases that have not been matched yet.
Hi Yao,
Quoted 2 lines · Xingtao Yao (Fujitsu)
Quoted 1 line
Please add to
scripts/coccinelle/range.cocci.OK, I will add this file in next revision.
Please Cc me on v3 (I'm dropping v2 from my queue).
Thanks,
Phil.
Quoted 19 lines · Xingtao Yao (Fujitsu) via
Quoted 9 lines
-----Original Message-----
From: Philippe Mathieu-Daudé <[email protected]>
Sent: Monday, July 22, 2024 2:43 PM
To: Yao, Xingtao/姚 幸涛 <[email protected]>; [email protected]
Subject: Re: [PATCH 00/13] make range overlap check more readableQuoted 7 lines
Hi Yao,
Quoted 2 lines · Yao Xingtao via
Currently, some components still open-coding the range overlap check. Sometimes this check may be fail because some patterns are missed.
How did you catch all these use cases?
I used the Coccinelle to match these use cases, the pattern is below
range_overlap.cocci:
Quoted 8 lines
// use
ranges_overlap()instead of open-coding the overlap check @@
expression E1, E2, E3, E4;
@@
(
- E2 <= E3 || E1 >= E4
+ !ranges_overlap(E1, E2, E3, E4)
|
Maybe I'm misunderstanding the coccinelle patch here, but I don't see how it produces the results in the patchset. ranges_overlap() takes arguments (start1, len1, start2, len2), but an expression like "E2 <= E3 || E1 >= E4" is working with start,end pairs to indicate the ranges. And looking at e.g. patch 9:
- if (cur->phys_addr >= begin + length ||
- cur->phys_addr + cur->length <= begin) {
+ if (!ranges_overlap(cur->phys_addr, cur->length, begin, length)) {
the kind of if() check you get for start, length pairs has an addition in it, which I don't see in any of these coccinelle script fragments.
thanks
-- PMM
Quoted 46 lines
-----Original Message-----
From: Peter Maydell <[email protected]>
Sent: Thursday, July 25, 2024 11:14 PM
To: Yao, Xingtao/姚 幸涛 <[email protected]>
Cc: Philippe Mathieu-Daudé <[email protected]>; [email protected]
Subject: Re: [PATCH 00/13] make range overlap check more readableQuoted 19 lines · Xingtao Yao (Fujitsu) via
Quoted 9 lines
-----Original Message-----
From: Philippe Mathieu-Daudé <[email protected]>
Sent: Monday, July 22, 2024 2:43 PM
To: Yao, Xingtao/姚 幸涛 <[email protected]>; [email protected]
Subject: Re: [PATCH 00/13] make range overlap check more readableQuoted 7 lines
Hi Yao,
Quoted 2 lines · Yao Xingtao via
Currently, some components still open-coding the range overlap check. Sometimes this check may be fail because some patterns are missed.
How did you catch all these use cases?
I used the Coccinelle to match these use cases, the pattern is below
range_overlap.cocci:Quoted 8 lines
// use
ranges_overlap()instead of open-coding the overlap check @@
expression E1, E2, E3, E4;
@@
(
- E2 <= E3 || E1 >= E4
+ !ranges_overlap(E1, E2, E3, E4)
|Maybe I'm misunderstanding the coccinelle patch here, but I don't see how it produces the results in the patchset.
ranges_overlap()takes arguments (start1, len1, start2, len2), but an expression like "E2 <= E3 || E1 >= E4" is working with start,end pairs to indicate the ranges. And looking at e.g. patch 9:
Quoted 7 lines
- if (
cur->phys_addr>= begin + length ||
-cur->phys_addr+cur->length<= begin) {
+ if (!ranges_overlap(cur->phys_addr,cur->length, begin, length)) {the kind of
if()check you get for start, length pairs has an addition in it, which I don't see in any of these coccinelle script fragments.
I understand your confusion, but it is difficult to match the region overlap check because it has many variations, like below:
case1:
start >= old_start +old_len || start + len <= old_start
case2:
start >= old_end || end <= old_start
case3:cur->phys_addr >= begin + length || cur->phys_addr + cur->length <= begin
case4:new->base >= old.base +old.size || new->base + new->size <= old.base
......
and sometimes the length or end may be also an expression, I can not find a way to handle all the variants.
So I decided to use the above pattern to find out the region overlap checks as much as possible, then manually drop the cases that does not meet the requirements, and then manually modify the cases that meets the requirements.
Quoted 2 lines
thanks
-- PMM
Quoted 71 lines · Xingtao Yao (Fujitsu)
Quoted 47 lines
-----Original Message-----
From: Peter Maydell <[email protected]>
Sent: Thursday, July 25, 2024 11:14 PM
To: Yao, Xingtao/姚 幸涛 <[email protected]>
Cc: Philippe Mathieu-Daudé <[email protected]>; [email protected]
Subject: Re: [PATCH 00/13] make range overlap check more readableQuoted 19 lines · Xingtao Yao (Fujitsu) via
Quoted 9 lines
-----Original Message-----
From: Philippe Mathieu-Daudé <[email protected]>
Sent: Monday, July 22, 2024 2:43 PM
To: Yao, Xingtao/姚 幸涛 <[email protected]>; [email protected]
Subject: Re: [PATCH 00/13] make range overlap check more readableQuoted 7 lines
Hi Yao,
Quoted 2 lines · Yao Xingtao via
Currently, some components still open-coding the range overlap check. Sometimes this check may be fail because some patterns are missed.
How did you catch all these use cases?
I used the Coccinelle to match these use cases, the pattern is below
range_overlap.cocci:Quoted 8 lines
// use
ranges_overlap()instead of open-coding the overlap check @@
expression E1, E2, E3, E4;
@@
(
- E2 <= E3 || E1 >= E4
+ !ranges_overlap(E1, E2, E3, E4)
|Maybe I'm misunderstanding the coccinelle patch here, but I don't see how it produces the results in the patchset.
ranges_overlap()takes arguments (start1, len1, start2, len2), but an expression like "E2 <= E3 || E1 >= E4" is working with start,end pairs to indicate the ranges. And looking at e.g. patch 9:Quoted 7 lines
- if (
cur->phys_addr>= begin + length ||
-cur->phys_addr+cur->length<= begin) {
+ if (!ranges_overlap(cur->phys_addr,cur->length, begin, length)) {the kind of
if()check you get for start, length pairs has an addition in it, which I don't see in any of these coccinelle script fragments.I understand your confusion, but it is difficult to match the region overlap check because it has many variations, like below:
case1:
start >=old_start+old_len || start + len <=old_startcase2:
start >=old_end|| end <=old_startcase3:
cur->phys_addr>= begin + length ||cur->phys_addr+cur->length<= begincase4:
new->base>= old.base +old.size ||new->base+new->size<= old.base
......
and sometimes the length or end may be also an expression, I can not find a way to handle all the variants.
Quoted 3 lines
So I decided to use the above pattern to find out the region overlap checks as much as possible, then manually drop the cases that does not meet the requirements, and then manually modify the cases that meets the requirements.
Ah, I see -- you just used coccinelle as a more flexible grep, effectively. That's fine -- it just means that when we review the series we need to review each patch for e.g. off-by-one errors rather than being able to do that only on the coccinelle patch.
thanks
-- PMM