Skip to contentSkip to conversation

KVM: selftests: Delete three pieces of dead library code

PATCH In review Gokul K 2 participants 7 messages Sep 18 – Oct 2

Subsystems (3)
lore
Share & tools
lore

Gokul K

author
#0 lore raw

cover KVM: selftests: Delete three pieces of dead library code

Three unused things in the selftest library, found by asking the linker rather than by reading: build the selftests, then look for global symbols in lib/ that no object refers to, and check each survivor against the whole source tree by hand.

1/3 sparsebit_copy() and sparsebit_num_set(), neither called 2/3 the declaration of vcpu_set_cpuid_maxphyaddr(), which has no
definition anywhere
3/3 guest_snprintf(), a varargs wrapper nobody calls

Each is independent and they can be taken in any order.

A note on what is deliberately *not* here, because the same scan turns it up and the obvious next step is wrong. sparsebit.c still exports nine more functions that no caller outside the file uses - sparsebit_all_set(), sparsebit_is_clear(), sparsebit_validate_internal() and friends. They are not dead: they are called from inside sparsebit.c, mostly from the assertions in sparsebit_validate_internal() and from asserts in live functions, so deleting them would not build. Making them static and dropping their declarations is a defensible follow-up, but it is a change of intent about what the library exposes rather than a removal of dead code, so it does not belong in this series.

No functional change intended. Built with and without -Wextra, and a sample of twelve x86 selftests gives identical results before and after; kvm_binary_stats_test and the rest still pass. The two that fail here fail identically without the series, because this host's kernel is older than the tree.

Gokul K (3):
KVM: selftests: Delete the unused sparsebit copy and count helpers
KVM: selftests: Delete the declaration of vcpu_set_cpuid_maxphyaddr()
KVM: selftests: Delete the unused guest_snprintf()

 tools/testing/selftests/kvm/include/sparsebit.h    |  2 -
 tools/testing/selftests/kvm/include/test_util.h    |  1 -
 .../testing/selftests/kvm/include/x86/processor.h  |  1 -
 tools/testing/selftests/kvm/lib/guest_sprintf.c    | 12 -----
 tools/testing/selftests/kvm/lib/sparsebit.c        | 63 +---------------------
 5 files changed, 1 insertion(+), 78 deletions(-)
Signature · 1 line
2.51.0
author
#1 lore raw

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

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.

---
 .../testing/selftests/kvm/include/sparsebit.h |  2 -
 tools/testing/selftests/kvm/lib/sparsebit.c   | 63 +------------------
 2 files changed, 1 insertion(+), 64 deletions(-)
tools/testing/selftests/kvm/include/sparsebit.h +0 -2
index e027e5790946..8a2ab11dae6f 100644
@@ -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);
tools/testing/selftests/kvm/lib/sparsebit.c +1 -62

87 lines not on this page · raw message

Signature · 1 line
2.54.0
#2 lore raw

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

Quoted 26 lines · Gokul K

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.

---
 .../testing/selftests/kvm/include/sparsebit.h |  2 -
 tools/testing/selftests/kvm/lib/sparsebit.c   | 63 +------------------
 2 files changed, 1 insertion(+), 64 deletions(-)
tools/testing/selftests/kvm/include/sparsebit.h +0 -0
index e027e5790946..8a2ab11dae6f 100644

tools/testing/selftests/kvm/include/sparsebit.h typedef u64 sparsebit_num_t; · line 33

Quoted 4 lines
tools/testing/selftests/kvm/include/sparsebit.h +0 -1
@@ -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);

I'm somewhat tempted to keep sparsebit_copy() even though there are no users. In the unlikely scenario someone wanted/needed to make a copy, it seems like a potentially painful waste of time to have to recreate this or hunt down its prior existence.

Anyone else have an opinion?

author
#3 lore raw

PATCH 2/3 KVM: selftests: Delete the declaration of vcpu_set_cpuid_maxphyaddr()

In reply to Gokul K

vcpu_set_cpuid_maxphyaddr() is declared in include/x86/processor.h and defined nowhere. The declaration is the only mention of the name in tools/testing/selftests, so nothing has ever been able to call it.

No Fixes: tag: a prototype for a function that does not exist cannot have regressed anything, and there is nothing to backport.

No functional change intended.

---
 tools/testing/selftests/kvm/include/x86/processor.h | 1 -
 1 file changed, 1 deletion(-)
tools/testing/selftests/kvm/include/x86/processor.h +0 -1
index 7579e692bc2b..ffe95ce1ad49 100644
@@ -1180,7 +1180,6 @@ static inline void vcpu_set_cpuid(struct kvm_vcpu *vcpu)
 void vcpu_set_cpuid_property(struct kvm_vcpu *vcpu,
 			     struct kvm_x86_cpu_property property,
 			     u32 value);
-void vcpu_set_cpuid_maxphyaddr(struct kvm_vcpu *vcpu, u8 maxphyaddr);
 
 void vcpu_clear_cpuid_entry(struct kvm_vcpu *vcpu, u32 function);
 
Signature · 1 line
2.54.0
author
#4 lore raw

PATCH 3/3 KVM: selftests: Delete the unused guest_snprintf()

In reply to Gokul K

guest_snprintf() is a varargs wrapper around guest_vsnprintf() that nothing calls. Its declaration in test_util.h and its definition in lib/guest_sprintf.c are the only two mentions anywhere under tools/testing/selftests.

The rest of the family is alive: guest_vsnprintf() does the work and ucall_common.c calls it, so this deletes the wrapper and nothing else.

No functional change intended.

---
 tools/testing/selftests/kvm/include/test_util.h |  1 -
 tools/testing/selftests/kvm/lib/guest_sprintf.c | 12 ------------
 2 files changed, 13 deletions(-)
tools/testing/selftests/kvm/include/test_util.h +0 -1
index a6a3e1657895..fbdf0e0c15d1 100644
@@ -238,7 +238,6 @@ static inline u32 atoi_non_negative(const char *name, const char *num_str)
 }
 
 int guest_vsnprintf(char *buf, int n, const char *fmt, va_list args);
-__printf(3, 4) int guest_snprintf(char *buf, int n, const char *fmt, ...);
 
 char *strdup_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2), nonnull(1)));
 
tools/testing/selftests/kvm/lib/guest_sprintf.c +0 -12
index 7a33965349a7..6caebb456f3d 100644
@@ -300,15 +300,3 @@ int guest_vsnprintf(char *buf, int n, const char *fmt, va_list args)
 	*str = '\0';
 	return str - buf;
 }
-
-int guest_snprintf(char *buf, int n, const char *fmt, ...)
-{
-	va_list va;
-	int len;
-
-	va_start(va, fmt);
-	len = guest_vsnprintf(buf, n, fmt, va);
-	va_end(va);
-
-	return len;
-}
Signature · 1 line
2.54.0
#5 lore raw

Re: [PATCH 3/3] KVM: selftests: Delete the unused guest_snprintf()

Quoted 38 lines · Gokul K

guest_snprintf() is a varargs wrapper around guest_vsnprintf() that nothing calls. Its declaration in test_util.h and its definition in lib/guest_sprintf.c are the only two mentions anywhere under tools/testing/selftests.

The rest of the family is alive: guest_vsnprintf() does the work and ucall_common.c calls it, so this deletes the wrapper and nothing else.

No functional change intended.

---
 tools/testing/selftests/kvm/include/test_util.h |  1 -
 tools/testing/selftests/kvm/lib/guest_sprintf.c | 12 ------------
 2 files changed, 13 deletions(-)
tools/testing/selftests/kvm/include/test_util.h +0 -1
index a6a3e1657895..fbdf0e0c15d1 100644
@@ -238,7 +238,6 @@ static inline u32 atoi_non_negative(const char *name, const char *num_str)
 }
 
 int guest_vsnprintf(char *buf, int n, const char *fmt, va_list args);
-__printf(3, 4) int guest_snprintf(char *buf, int n, const char *fmt, ...);
 
 char *strdup_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2), nonnull(1)));
 
tools/testing/selftests/kvm/lib/guest_sprintf.c +0 -2
index 7a33965349a7..6caebb456f3d 100644
@@ -300,15 +300,3 @@ int guest_vsnprintf(char *buf, int n, const char *fmt, va_list args)
 	*str = '\0';
 	return str - buf;
 }
-
-int guest_snprintf(char *buf, int n, const char *fmt, ...)

tools/testing/selftests/kvm/lib/guest_sprintf.c int guest_vsnprintf(char *buf, int n, const char *fmt, va_list args) · line 314

Quoted 10 lines
tools/testing/selftests/kvm/lib/guest_sprintf.c +0 -10
-{
-	va_list va;
-	int len;
-
-	va_start(va, fmt);
-	len = guest_vsnprintf(buf, n, fmt, va);
-	va_end(va);
-
-	return len;
-}

Hmm, I would rather turn guest_snprintf() into a macro so that it can be used in variadic functions. I can't think of any reason why this wouldn't work (and emperically, guest printing works as expected).

---

From: Sean Christopherson <[email protected]>
Date: Fri, 18 Sep 2026 08:57:37 -0700
Subject: [PATCH] KVM: selftests: Make guest_snprintf() a macro and use it as
appropriate

Turn guest_snprintf() into a macro so that it can be used by variadic functions, and use it in the ucall APIs instead of open coding the VA goo.

---
 tools/testing/selftests/kvm/include/test_util.h | 12 +++++++++++-
 tools/testing/selftests/kvm/lib/guest_sprintf.c | 12 ------------
 tools/testing/selftests/kvm/lib/ucall_common.c  | 10 ++--------
 3 files changed, 13 insertions(+), 21 deletions(-)
tools/testing/selftests/kvm/include/test_util.h +11 -1
index a6a3e1657895..817d4d5ca7cc 100644
@@ -238,7 +238,17 @@ static inline u32 atoi_non_negative(const char *name, const char *num_str)
 }
 
 int guest_vsnprintf(char *buf, int n, const char *fmt, va_list args);
-__printf(3, 4) int guest_snprintf(char *buf, int n, const char *fmt, ...);
+
+#define guest_snprintf(__buf, __n, __fmt)		\
+({							\
+	va_list va;					\
+	int len;					\
+							\
+	va_start(va, __fmt);				\
+	len = guest_vsnprintf(__buf, __n, __fmt, va);	\
+	va_end(va);					\
+	len;						\
+})
 
 char *strdup_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2), nonnull(1)));
 
tools/testing/selftests/kvm/lib/guest_sprintf.c +0 -12
index 7a33965349a7..6caebb456f3d 100644
@@ -300,15 +300,3 @@ int guest_vsnprintf(char *buf, int n, const char *fmt, va_list args)
 	*str = '\0';
 	return str - buf;
 }
-
-int guest_snprintf(char *buf, int n, const char *fmt, ...)
-{
-	va_list va;
-	int len;
-
-	va_start(va, fmt);
-	len = guest_vsnprintf(buf, n, fmt, va);
-	va_end(va);
-
-	return len;
-}
tools/testing/selftests/kvm/lib/ucall_common.c +2 -8
index 029ce21f9f2f..e9df71197367 100644
@@ -83,7 +83,6 @@ void ucall_assert(u64 cmd, const char *exp, const char *file,
 		  unsigned int line, const char *fmt, ...)
 {
 	struct ucall *uc;
-	va_list va;
 
 	uc = ucall_alloc();
 	uc->cmd = cmd;
@@ -92,9 +91,7 @@ void ucall_assert(u64 cmd, const char *exp, const char *file,
 	WRITE_ONCE(uc->args[GUEST_FILE], (u64)(file));
 	WRITE_ONCE(uc->args[GUEST_LINE], line);
 
-	va_start(va, fmt);
-	guest_vsnprintf(uc->buffer, UCALL_BUFFER_LEN, fmt, va);
-	va_end(va);
+	guest_snprintf(uc->buffer, UCALL_BUFFER_LEN, fmt);
 
 	ucall_arch_do_ucall((gva_t)uc->hva);
 
@@ -104,14 +101,11 @@ void ucall_assert(u64 cmd, const char *exp, const char *file,
 void ucall_fmt(u64 cmd, const char *fmt, ...)
 {
 	struct ucall *uc;
-	va_list va;
 
 	uc = ucall_alloc();
 	uc->cmd = cmd;
 
-	va_start(va, fmt);
-	guest_vsnprintf(uc->buffer, UCALL_BUFFER_LEN, fmt, va);
-	va_end(va);
+	guest_snprintf(uc->buffer, UCALL_BUFFER_LEN, fmt);
 
 	ucall_arch_do_ucall((gva_t)uc->hva);
 

base-commit: 70c944caf570fda2d79baa71435589a8db39f048

#6 lore raw
In reply to Gokul K
Quoted 11 lines · Gokul K

Three unused things in the selftest library, found by asking the linker rather than by reading: build the selftests, then look for global symbols in lib/ that no object refers to, and check each survivor against the whole source tree by hand.

1/3 sparsebit_copy() and sparsebit_num_set(), neither called 2/3 the declaration of vcpu_set_cpuid_maxphyaddr(), which has no
definition anywhere
3/3 guest_snprintf(), a varargs wrapper nobody calls

[...]

Applied 2 and 3 to kvm-x86 selftests, thanks!

[1/3] KVM: selftests: Delete the unused sparsebit copy and count helpers
[SKIP, handled in a separate series]
[2/3] KVM: selftests: Delete the declaration of vcpu_set_cpuid_maxphyaddr()

      https://github.com/kvm-x86/linux/commit/dd572ec94640
[3/3] KVM: selftests: Delete the unused guest_snprintf()
      https://github.com/kvm-x86/linux/commit/3bac3b77c5e6

--
https://github.com/kvm-x86/linux/tree/next