aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tools/testing/selftests/kvm
diff options
context:
space:
mode:
authorAckerley Tng <ackerleytng@google.com>2024-02-22 16:42:50 -0800
committerSean Christopherson <seanjc@google.com>2024-02-28 20:58:12 +0000
commit57e19f05775847d9d8565dad2cee6bbec03cdb06 (patch)
tree19477911d2e8e60d0ae7844fff951ad3fea0b06f /tools/testing/selftests/kvm
parentKVM: selftests: Make sparsebit structs const where appropriate (diff)
downloadwireguard-linux-57e19f05775847d9d8565dad2cee6bbec03cdb06.tar.xz
wireguard-linux-57e19f05775847d9d8565dad2cee6bbec03cdb06.zip
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 <carlos.bilbao@amd.com> Signed-off-by: Ackerley Tng <ackerleytng@google.com> [sean: split to separate patch] Link: https://lore.kernel.org/r/20240223004258.3104051-4-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
Diffstat (limited to 'tools/testing/selftests/kvm')
-rw-r--r--tools/testing/selftests/kvm/include/sparsebit.h20
1 files changed, 20 insertions, 0 deletions
diff --git a/tools/testing/selftests/kvm/include/sparsebit.h b/tools/testing/selftests/kvm/include/sparsebit.h
index fb5170d57fcb..bc760761e1a3 100644
--- a/tools/testing/selftests/kvm/include/sparsebit.h
+++ b/tools/testing/selftests/kvm/include/sparsebit.h
@@ -66,6 +66,26 @@ void sparsebit_dump(FILE *stream, const struct sparsebit *sbit,
unsigned int indent);
void sparsebit_validate_internal(const struct sparsebit *sbit);
+/*
+ * Iterate over an inclusive ranges within sparsebit @s. In each iteration,
+ * @range_begin and @range_end will take the beginning and end of the set
+ * range, which are of type sparsebit_idx_t.
+ *
+ * For example, if the range [3, 7] (inclusive) is set, within the
+ * iteration,@range_begin will take the value 3 and @range_end will take
+ * the value 7.
+ *
+ * Ensure that there is at least one bit set before using this macro with
+ * sparsebit_any_set(), because sparsebit_first_set() will abort if none
+ * are set.
+ */
+#define sparsebit_for_each_set_range(s, range_begin, range_end) \
+ for (range_begin = sparsebit_first_set(s), \
+ range_end = sparsebit_next_clear(s, range_begin) - 1; \
+ range_begin && range_end; \
+ range_begin = sparsebit_next_set(s, range_end), \
+ range_end = sparsebit_next_clear(s, range_begin) - 1)
+
#ifdef __cplusplus
}
#endif