bitmap: add atomic test and clear

The new bitmap_test_and_clear_atomic() function clears a range and
returns whether or not the bits were set.

Backports commit 36546e5b803f6e363906607307f27c489441fd15 from qemu
This commit is contained in:
Stefan Hajnoczi 2018-02-13 10:01:12 -05:00 committed by Lioncash
parent 7ff5f05c82
commit 6172e3dc29
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7
2 changed files with 47 additions and 0 deletions

View file

@ -29,6 +29,7 @@
* bitmap_set(dst, pos, nbits) Set specified bit area * bitmap_set(dst, pos, nbits) Set specified bit area
* bitmap_set_atomic(dst, pos, nbits) Set specified bit area with atomic ops * bitmap_set_atomic(dst, pos, nbits) Set specified bit area with atomic ops
* bitmap_clear(dst, pos, nbits) Clear specified bit area * bitmap_clear(dst, pos, nbits) Clear specified bit area
* bitmap_test_and_clear_atomic(dst, pos, nbits) Test and clear area
*/ */
/* /*
@ -50,6 +51,7 @@
void bitmap_set(unsigned long *map, long i, long len); void bitmap_set(unsigned long *map, long i, long len);
void bitmap_set_atomic(unsigned long *map, long i, long len); void bitmap_set_atomic(unsigned long *map, long i, long len);
void bitmap_clear(unsigned long *map, long start, long nr); void bitmap_clear(unsigned long *map, long start, long nr);
bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr);
static inline unsigned long *bitmap_zero_extend(unsigned long *old, static inline unsigned long *bitmap_zero_extend(unsigned long *old,
long old_nbits, long new_nbits) long old_nbits, long new_nbits)

View file

@ -91,3 +91,48 @@ void bitmap_clear(unsigned long *map, long start, long nr)
*p &= ~mask_to_clear; *p &= ~mask_to_clear;
} }
} }
bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr)
{
unsigned long *p = map + BIT_WORD(start);
const long size = start + nr;
int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
unsigned long dirty = 0;
unsigned long old_bits;
/* First word */
if (nr - bits_to_clear > 0) {
old_bits = atomic_fetch_and(p, ~mask_to_clear);
dirty |= old_bits & mask_to_clear;
nr -= bits_to_clear;
bits_to_clear = BITS_PER_LONG;
mask_to_clear = ~0UL;
p++;
}
/* Full words */
if (bits_to_clear == BITS_PER_LONG) {
while (nr >= BITS_PER_LONG) {
if (*p) {
old_bits = atomic_xchg(p, 0);
dirty |= old_bits;
}
nr -= BITS_PER_LONG;
p++;
}
}
/* Last word */
if (nr) {
mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
old_bits = atomic_fetch_and(p, ~mask_to_clear);
dirty |= old_bits & mask_to_clear;
} else {
if (!dirty) {
smp_mb();
}
}
return dirty != 0;
}