From: Jack Lange Date: Wed, 4 Jul 2012 22:03:15 +0000 (-0400) Subject: make bitmap operations atomic, fixes potential barrier race condition X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=commitdiff_plain;h=7ec6a6ee7f4a472587d49ddca3a9f5b587371fd5;p=palacios.releases.git make bitmap operations atomic, fixes potential barrier race condition --- diff --git a/palacios/include/palacios/vmm_bitmap.h b/palacios/include/palacios/vmm_bitmap.h index 29cabb7..04ae22d 100644 --- a/palacios/include/palacios/vmm_bitmap.h +++ b/palacios/include/palacios/vmm_bitmap.h @@ -24,10 +24,12 @@ #ifdef __V3VEE__ #include +#include struct v3_bitmap { + v3_lock_t lock; int num_bits; // number of valid bit positions in the bitmap uint8_t * bits; // actual bitmap. Dynamically allocated... ugly }; diff --git a/palacios/src/palacios/vmm_bitmap.c b/palacios/src/palacios/vmm_bitmap.c index 14b042d..ff7a7a7 100644 --- a/palacios/src/palacios/vmm_bitmap.c +++ b/palacios/src/palacios/vmm_bitmap.c @@ -24,9 +24,11 @@ int v3_bitmap_init(struct v3_bitmap * bitmap, int num_bits) { int num_bytes = (num_bits / 8) + ((num_bits % 8) > 0); + v3_lock_init(&(bitmap->lock)); bitmap->num_bits = num_bits; bitmap->bits = V3_Malloc(num_bytes); + if (bitmap->bits == NULL) { PrintError("Could not allocate bitmap of %d bits\n", num_bits); return -1; @@ -55,6 +57,7 @@ int v3_bitmap_set(struct v3_bitmap * bitmap, int index) { int major = index / 8; int minor = index % 8; int old_val = 0; + uint32_t flags = 0; if (index > (bitmap->num_bits - 1)) { PrintError("Index out of bitmap range: (pos = %d) (num_bits = %d)\n", @@ -62,9 +65,14 @@ int v3_bitmap_set(struct v3_bitmap * bitmap, int index) { return -1; } + + flags = v3_lock_irqsave(bitmap->lock); + old_val = (bitmap->bits[major] & (0x1 << minor)); bitmap->bits[major] |= (0x1 << minor); + v3_unlock_irqrestore(bitmap->lock, flags); + return old_val; } @@ -73,6 +81,7 @@ int v3_bitmap_clear(struct v3_bitmap * bitmap, int index) { int major = index / 8; int minor = index % 8; int old_val = 0; + uint32_t flags = 0; if (index > (bitmap->num_bits - 1)) { PrintError("Index out of bitmap range: (pos = %d) (num_bits = %d)\n", @@ -80,9 +89,13 @@ int v3_bitmap_clear(struct v3_bitmap * bitmap, int index) { return -1; } + flags = v3_lock_irqsave(bitmap->lock); + old_val = (bitmap->bits[major] & (0x1 << minor)); bitmap->bits[major] &= ~(0x1 << minor); + v3_unlock_irqrestore(bitmap->lock, flags); + return old_val; }