* [Qemu-devel] [PATCH] bitmap: assert that start and nr are non negative
@ 2017-01-19 16:43 Peter Lieven
2017-01-20 2:17 ` Fam Zheng
2017-01-20 12:22 ` Paolo Bonzini
0 siblings, 2 replies; 3+ messages in thread
From: Peter Lieven @ 2017-01-19 16:43 UTC (permalink / raw)
To: qemu-devel; +Cc: famz, pbonzini, Peter Lieven
commit e1123a3b introduced a data corruption regression
in the iscsi driver because it passed -1 as nr to bitmap_set
and bitmap_clear. Add an assertion to catch such flaws earlier.
Suggested-by: Fam Zheng <famz@redhat.com>
Signed-off-by: Peter Lieven <pl@kamp.de>
---
util/bitmap.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/util/bitmap.c b/util/bitmap.c
index 43ed011..c1a84ca 100644
--- a/util/bitmap.c
+++ b/util/bitmap.c
@@ -164,6 +164,8 @@ void bitmap_set(unsigned long *map, long start, long nr)
int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
+ assert(start >= 0 && nr >= 0);
+
while (nr - bits_to_set >= 0) {
*p |= mask_to_set;
nr -= bits_to_set;
@@ -184,6 +186,8 @@ void bitmap_set_atomic(unsigned long *map, long start, long nr)
int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
+ assert(start >= 0 && nr >= 0);
+
/* First word */
if (nr - bits_to_set > 0) {
atomic_or(p, mask_to_set);
@@ -221,6 +225,8 @@ void bitmap_clear(unsigned long *map, long start, long nr)
int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
+ assert(start >= 0 && nr >= 0);
+
while (nr - bits_to_clear >= 0) {
*p &= ~mask_to_clear;
nr -= bits_to_clear;
@@ -243,6 +249,8 @@ bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr)
unsigned long dirty = 0;
unsigned long old_bits;
+ assert(start >= 0 && nr >= 0);
+
/* First word */
if (nr - bits_to_clear > 0) {
old_bits = atomic_fetch_and(p, ~mask_to_clear);
--
1.9.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [Qemu-devel] [PATCH] bitmap: assert that start and nr are non negative
2017-01-19 16:43 [Qemu-devel] [PATCH] bitmap: assert that start and nr are non negative Peter Lieven
@ 2017-01-20 2:17 ` Fam Zheng
2017-01-20 12:22 ` Paolo Bonzini
1 sibling, 0 replies; 3+ messages in thread
From: Fam Zheng @ 2017-01-20 2:17 UTC (permalink / raw)
To: Peter Lieven; +Cc: qemu-devel, pbonzini
On Thu, 01/19 17:43, Peter Lieven wrote:
> commit e1123a3b introduced a data corruption regression
> in the iscsi driver because it passed -1 as nr to bitmap_set
> and bitmap_clear. Add an assertion to catch such flaws earlier.
>
> Suggested-by: Fam Zheng <famz@redhat.com>
> Signed-off-by: Peter Lieven <pl@kamp.de>
> ---
> util/bitmap.c | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/util/bitmap.c b/util/bitmap.c
> index 43ed011..c1a84ca 100644
> --- a/util/bitmap.c
> +++ b/util/bitmap.c
> @@ -164,6 +164,8 @@ void bitmap_set(unsigned long *map, long start, long nr)
> int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
> unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
>
> + assert(start >= 0 && nr >= 0);
> +
> while (nr - bits_to_set >= 0) {
> *p |= mask_to_set;
> nr -= bits_to_set;
> @@ -184,6 +186,8 @@ void bitmap_set_atomic(unsigned long *map, long start, long nr)
> int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
> unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
>
> + assert(start >= 0 && nr >= 0);
> +
> /* First word */
> if (nr - bits_to_set > 0) {
> atomic_or(p, mask_to_set);
> @@ -221,6 +225,8 @@ void bitmap_clear(unsigned long *map, long start, long nr)
> int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
> unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
>
> + assert(start >= 0 && nr >= 0);
> +
> while (nr - bits_to_clear >= 0) {
> *p &= ~mask_to_clear;
> nr -= bits_to_clear;
> @@ -243,6 +249,8 @@ bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr)
> unsigned long dirty = 0;
> unsigned long old_bits;
>
> + assert(start >= 0 && nr >= 0);
> +
> /* First word */
> if (nr - bits_to_clear > 0) {
> old_bits = atomic_fetch_and(p, ~mask_to_clear);
> --
> 1.9.1
>
>
Reviewed-by: Fam Zheng <famz@redhat.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Qemu-devel] [PATCH] bitmap: assert that start and nr are non negative
2017-01-19 16:43 [Qemu-devel] [PATCH] bitmap: assert that start and nr are non negative Peter Lieven
2017-01-20 2:17 ` Fam Zheng
@ 2017-01-20 12:22 ` Paolo Bonzini
1 sibling, 0 replies; 3+ messages in thread
From: Paolo Bonzini @ 2017-01-20 12:22 UTC (permalink / raw)
To: Peter Lieven, qemu-devel; +Cc: famz
On 19/01/2017 17:43, Peter Lieven wrote:
> commit e1123a3b introduced a data corruption regression
> in the iscsi driver because it passed -1 as nr to bitmap_set
> and bitmap_clear. Add an assertion to catch such flaws earlier.
>
> Suggested-by: Fam Zheng <famz@redhat.com>
> Signed-off-by: Peter Lieven <pl@kamp.de>
> ---
> util/bitmap.c | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/util/bitmap.c b/util/bitmap.c
> index 43ed011..c1a84ca 100644
> --- a/util/bitmap.c
> +++ b/util/bitmap.c
> @@ -164,6 +164,8 @@ void bitmap_set(unsigned long *map, long start, long nr)
> int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
> unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
>
> + assert(start >= 0 && nr >= 0);
> +
> while (nr - bits_to_set >= 0) {
> *p |= mask_to_set;
> nr -= bits_to_set;
> @@ -184,6 +186,8 @@ void bitmap_set_atomic(unsigned long *map, long start, long nr)
> int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
> unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
>
> + assert(start >= 0 && nr >= 0);
> +
> /* First word */
> if (nr - bits_to_set > 0) {
> atomic_or(p, mask_to_set);
> @@ -221,6 +225,8 @@ void bitmap_clear(unsigned long *map, long start, long nr)
> int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
> unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
>
> + assert(start >= 0 && nr >= 0);
> +
> while (nr - bits_to_clear >= 0) {
> *p &= ~mask_to_clear;
> nr -= bits_to_clear;
> @@ -243,6 +249,8 @@ bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr)
> unsigned long dirty = 0;
> unsigned long old_bits;
>
> + assert(start >= 0 && nr >= 0);
> +
> /* First word */
> if (nr - bits_to_clear > 0) {
> old_bits = atomic_fetch_and(p, ~mask_to_clear);
>
Queued, thanks.
Paolo
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2017-01-20 12:22 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-01-19 16:43 [Qemu-devel] [PATCH] bitmap: assert that start and nr are non negative Peter Lieven
2017-01-20 2:17 ` Fam Zheng
2017-01-20 12:22 ` Paolo Bonzini
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).