Skip to content

Commit 57e19f0

Browse files
Ackerley Tngsean-jc
authored andcommitted
KVM: selftests: Add a macro to iterate over a sparsebit range
Add sparsebit_for_each_set_range() to allow iterator over a range of set bits in a range. This will be used by x86 SEV guests to process protected physical pages (each such page needs to be encrypted _after_ being "added" to the VM). Tested-by: Carlos Bilbao <[email protected]> Signed-off-by: Ackerley Tng <[email protected]> [sean: split to separate patch] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 35f50c9 commit 57e19f0

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

tools/testing/selftests/kvm/include/sparsebit.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,26 @@ void sparsebit_dump(FILE *stream, const struct sparsebit *sbit,
6666
unsigned int indent);
6767
void sparsebit_validate_internal(const struct sparsebit *sbit);
6868

69+
/*
70+
* Iterate over an inclusive ranges within sparsebit @s. In each iteration,
71+
* @range_begin and @range_end will take the beginning and end of the set
72+
* range, which are of type sparsebit_idx_t.
73+
*
74+
* For example, if the range [3, 7] (inclusive) is set, within the
75+
* iteration,@range_begin will take the value 3 and @range_end will take
76+
* the value 7.
77+
*
78+
* Ensure that there is at least one bit set before using this macro with
79+
* sparsebit_any_set(), because sparsebit_first_set() will abort if none
80+
* are set.
81+
*/
82+
#define sparsebit_for_each_set_range(s, range_begin, range_end) \
83+
for (range_begin = sparsebit_first_set(s), \
84+
range_end = sparsebit_next_clear(s, range_begin) - 1; \
85+
range_begin && range_end; \
86+
range_begin = sparsebit_next_set(s, range_end), \
87+
range_end = sparsebit_next_clear(s, range_begin) - 1)
88+
6989
#ifdef __cplusplus
7090
}
7191
#endif

0 commit comments

Comments
 (0)