[PATCH 1/3] KVM: selftests: Delete the unused sparsebit copy and count helpers

From: Gokul K

Date: Fri Sep 18 2026 - 08:11:22 EST


sparsebit_copy() and sparsebit_num_set() have no callers. Nor does the
FUZZ harness at the bottom of lib/sparsebit.c, which exercises most of
the rest of the API and still builds and links with both removed.

sparsebit_copy() was the only non-recursive caller of
node_copy_subtree(), which leaves that unreachable too. It is static,
so keeping it would trip -Wunused-function; delete it as well.

The comment on the num_set member gives sparsebit_num_set() as the
reason the count is maintained. The member stays - it is what lets
sparsebit_all_set() answer in O(1), and the dump path prints it - so
point the comment at what still uses it.

No functional change intended.

Signed-off-by: Gokul K <gokul02k@xxxxxxxxx>
---
.../testing/selftests/kvm/include/sparsebit.h | 2 -
tools/testing/selftests/kvm/lib/sparsebit.c | 63 +------------------
2 files changed, 1 insertion(+), 64 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/sparsebit.h b/tools/testing/selftests/kvm/include/sparsebit.h
index e027e5790946..8a2ab11dae6f 100644
--- a/tools/testing/selftests/kvm/include/sparsebit.h
+++ b/tools/testing/selftests/kvm/include/sparsebit.h
@@ -30,7 +30,6 @@ typedef u64 sparsebit_num_t;

struct sparsebit *sparsebit_alloc(void);
void sparsebit_free(struct sparsebit **sbitp);
-void sparsebit_copy(struct sparsebit *dstp, const struct sparsebit *src);

bool sparsebit_is_set(const struct sparsebit *sbit, sparsebit_idx_t idx);
bool sparsebit_is_set_num(const struct sparsebit *sbit,
@@ -38,7 +37,6 @@ bool sparsebit_is_set_num(const struct sparsebit *sbit,
bool sparsebit_is_clear(const struct sparsebit *sbit, sparsebit_idx_t idx);
bool sparsebit_is_clear_num(const struct sparsebit *sbit,
sparsebit_idx_t idx, sparsebit_num_t num);
-sparsebit_num_t sparsebit_num_set(const struct sparsebit *sbit);
bool sparsebit_any_set(const struct sparsebit *sbit);
bool sparsebit_any_clear(const struct sparsebit *sbit);
bool sparsebit_all_set(const struct sparsebit *sbit);
diff --git a/tools/testing/selftests/kvm/lib/sparsebit.c b/tools/testing/selftests/kvm/lib/sparsebit.c
index 4d845000de15..8ce230af4f14 100644
--- a/tools/testing/selftests/kvm/lib/sparsebit.c
+++ b/tools/testing/selftests/kvm/lib/sparsebit.c
@@ -184,8 +184,7 @@ struct sparsebit {

/*
* A redundant count of the total number of bits set. Used for
- * diagnostic purposes and to change the time complexity of
- * sparsebit_num_set() from O(n) to O(1).
+ * diagnostic purposes and to answer sparsebit_all_set() in O(1).
* Note: Due to overflow, a value of 0 means none or all set.
*/
sparsebit_num_t num_set;
@@ -269,39 +268,6 @@ static struct node *node_prev(const struct sparsebit *s, struct node *np)
}


-/* Allocates space to hold a copy of the node sub-tree pointed to by
- * subtree and duplicates the bit settings to the newly allocated nodes.
- * Returns the newly allocated copy of subtree.
- */
-static struct node *node_copy_subtree(const struct node *subtree)
-{
- struct node *root;
-
- /* Duplicate the node at the root of the subtree */
- root = calloc(1, sizeof(*root));
- if (!root) {
- perror("calloc");
- abort();
- }
-
- root->idx = subtree->idx;
- root->mask = subtree->mask;
- root->num_after = subtree->num_after;
-
- /* As needed, recursively duplicate the left and right subtrees */
- if (subtree->left) {
- root->left = node_copy_subtree(subtree->left);
- root->left->parent = root;
- }
-
- if (subtree->right) {
- root->right = node_copy_subtree(subtree->right);
- root->right->parent = root;
- }
-
- return root;
-}
-
/* Searches for and returns a pointer to the node that describes the setting
* of the bit given by idx. A node describes the setting of a bit if its
* index is within the bits described by the mask bits or the number of
@@ -964,22 +930,6 @@ void sparsebit_free(struct sparsebit **sbitp)
*sbitp = NULL;
}

-/* Makes a copy of the sparsebit array given by s, to the sparsebit
- * array given by d. Note, d must have already been allocated via
- * sparsebit_alloc(). It can though already have bits set, which
- * if different from src will be cleared.
- */
-void sparsebit_copy(struct sparsebit *d, const struct sparsebit *s)
-{
- /* First clear any bits already set in the destination */
- sparsebit_clear_all(d);
-
- if (s->root) {
- d->root = node_copy_subtree(s->root);
- d->num_set = s->num_set;
- }
-}
-
/* Returns whether num consecutive bits starting at idx are all set. */
bool sparsebit_is_set_num(const struct sparsebit *s,
sparsebit_idx_t idx, sparsebit_num_t num)
@@ -1035,17 +985,6 @@ bool sparsebit_is_clear_num(const struct sparsebit *s,
return next_set == 0 || next_set - idx >= num;
}

-/* Returns the total number of bits set. Note: 0 is also returned for
- * the case of all bits set. This is because with all bits set, there
- * is 1 additional bit set beyond what can be represented in the return
- * value. Use sparsebit_any_set(), instead of sparsebit_num_set() > 0,
- * to determine if the sparsebit array has any bits set.
- */
-sparsebit_num_t sparsebit_num_set(const struct sparsebit *s)
-{
- return s->num_set;
-}
-
/* Returns whether any bit is set in the sparsebit array. */
bool sparsebit_any_set(const struct sparsebit *s)
{
--
2.54.0