Skip to contentSkip to conversation

KVM: selftests: Replace ulong with unsigned long

PATCH In review Hisam Mehboob 3 participants 11 messages Aug 3 – Sep 21

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 KVM: selftests: Replace ulong with unsigned long

KVM selftests use the non-standard ulong alias in several files. glibc exposes the alias transitively through its headers, whereas musl does not, causing musl builds to fail with ulong undeclared.

Use unsigned long directly instead of depending on a libc-specific typedef. This also avoids adding a duplicate typedef to a shared tools header.

---
Changes in v2:
- Replaced KVM selftest uses directly instead of adding a shared typedef.
- Dropped the tools/include/linux/types.h change.
- Added Reported-by and Closes tags.

Tested on x86_64 with:
- make -C tools/testing/selftests/kvm
- make -C tools/testing/selftests/kvm CC=musl-gcc

v1: https://lore.kernel.org/r/[email protected]/

 .../testing/selftests/kvm/arm64/hypercalls.c  |  2 +-
 .../testing/selftests/kvm/memslot_perf_test.c |  2 +-
 tools/testing/selftests/kvm/steal_time.c      | 20 +++++++++----------
 tools/testing/selftests/kvm/x86/amx_test.c    |  2 +-
 tools/testing/selftests/kvm/x86/cpuid_test.c  |  2 +-
 .../testing/selftests/kvm/x86/hyperv_clock.c  |  2 +-
 .../testing/selftests/kvm/x86/hyperv_evmcs.c  |  4 ++--
 .../selftests/kvm/x86/hyperv_svm_test.c       |  2 +-
 .../selftests/kvm/x86/set_boot_cpu_id.c       |  2 +-
 tools/testing/selftests/kvm/x86/state_test.c  |  4 ++--
 .../testing/selftests/kvm/x86/tsc_msrs_test.c |  2 +-
 .../kvm/x86/vmx_nested_la57_state_test.c      |  2 +-
 .../kvm/x86/vmx_preemption_timer_test.c       |  4 ++--
 13 files changed, 25 insertions(+), 25 deletions(-)
tools/testing/selftests/kvm/arm64/hypercalls.c +1 -1
index 5d96cdf382c4..90d22b2cb6cc 100644
@@ -162,7 +162,7 @@ struct st_time {
 
 static void steal_time_init(struct kvm_vcpu *vcpu)
 {
-	u64 st_ipa = (ulong)ST_GPA_BASE;
+	u64 st_ipa = (unsigned long)ST_GPA_BASE;
 	unsigned int gpages;
 
 	gpages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, STEAL_TIME_SIZE);
tools/testing/selftests/kvm/memslot_perf_test.c +1 -1
index 4d9ad6104a6e..8f26b1cee0cd 100644
@@ -152,7 +152,7 @@ static void *vcpu_worker(void *__data)
 		case UCALL_SYNC:
 			TEST_ASSERT(uc.args[1] == 0,
 				"Unexpected sync ucall, got %lx",
-				(ulong)uc.args[1]);
+				(unsigned long)uc.args[1]);
 			sem_post(&vcpu_ready);
 			continue;
 		case UCALL_NONE:
tools/testing/selftests/kvm/steal_time.c +10 -10
index 19a97d6a3607..b8e78b430669 100644
@@ -73,12 +73,12 @@ static void steal_time_init(struct kvm_vcpu *vcpu, u32 i)
 	st_gva[i] = (void *)(ST_GPA_BASE + i * STEAL_TIME_SIZE);
 	sync_global_to_guest(vcpu->vm, st_gva[i]);
 
-	vcpu_set_msr(vcpu, MSR_KVM_STEAL_TIME, (ulong)st_gva[i] | KVM_MSR_ENABLED);
+	vcpu_set_msr(vcpu, MSR_KVM_STEAL_TIME, (unsigned long)st_gva[i] | KVM_MSR_ENABLED);
 }
 
 static void steal_time_dump(struct kvm_vm *vm, u32 vcpu_idx)
 {
-	struct kvm_steal_time *st = addr_gva2hva(vm, (ulong)st_gva[vcpu_idx]);
+	struct kvm_steal_time *st = addr_gva2hva(vm, (unsigned long)st_gva[vcpu_idx]);
 
 	ksft_print_msg("VCPU%d:\n", vcpu_idx);
 	ksft_print_msg("    steal:     %lld\n", st->steal);
@@ -102,7 +102,7 @@ static void check_steal_time_uapi(void)
 	vm = vm_create_with_one_vcpu(&vcpu, NULL);
 
 	ret = _vcpu_set_msr(vcpu, MSR_KVM_STEAL_TIME,
-			    (ulong)ST_GPA_BASE | KVM_STEAL_RESERVED_MASK);
+			    (unsigned long)ST_GPA_BASE | KVM_STEAL_RESERVED_MASK);
 	TEST_ASSERT(ret == 0, "Bad GPA didn't fail");
 
 	kvm_vm_free(vm);
@@ -151,7 +151,7 @@ static void guest_code(int cpu)
 
 	status = smccc(PV_TIME_ST, 0);
 	GUEST_ASSERT_NE(status, -1);
-	GUEST_ASSERT_EQ(status, (ulong)st_gva[cpu]);
+	GUEST_ASSERT_EQ(status, (unsigned long)st_gva[cpu]);
 
 	st = (struct st_time *)status;
 	GUEST_SYNC(0);
@@ -190,13 +190,13 @@ static void steal_time_init(struct kvm_vcpu *vcpu, u32 i)
 	st_gva[i] = (void *)(ST_GPA_BASE + i * STEAL_TIME_SIZE);
 	sync_global_to_guest(vm, st_gva[i]);
 
-	st_ipa = (ulong)st_gva[i];
+	st_ipa = (unsigned long)st_gva[i];
 	vcpu_ioctl(vcpu, KVM_SET_DEVICE_ATTR, &dev);
 }
 
 static void steal_time_dump(struct kvm_vm *vm, u32 vcpu_idx)
 {
-	struct st_time *st = addr_gva2hva(vm, (ulong)st_gva[vcpu_idx]);
+	struct st_time *st = addr_gva2hva(vm, (unsigned long)st_gva[vcpu_idx]);
 
 	ksft_print_msg("VCPU%d:\n", vcpu_idx);
 	ksft_print_msg("    rev:     %d\n", st->rev);
@@ -223,11 +223,11 @@ static void check_steal_time_uapi(void)
 	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, 1, 0);
 	virt_map(vm, ST_GPA_BASE, ST_GPA_BASE, 1);
 
-	st_ipa = (ulong)ST_GPA_BASE | 1;
+	st_ipa = (unsigned long)ST_GPA_BASE | 1;
 	ret = __vcpu_ioctl(vcpu, KVM_SET_DEVICE_ATTR, &dev);
 	TEST_ASSERT(ret == -1 && errno == EINVAL, "Bad IPA didn't report EINVAL");
 
-	st_ipa = (ulong)ST_GPA_BASE;
+	st_ipa = (unsigned long)ST_GPA_BASE;
 	vcpu_ioctl(vcpu, KVM_SET_DEVICE_ATTR, &dev);
 
 	ret = __vcpu_ioctl(vcpu, KVM_SET_DEVICE_ATTR, &dev);
@@ -318,7 +318,7 @@ static void steal_time_init(struct kvm_vcpu *vcpu, u32 i)
 
 static void steal_time_dump(struct kvm_vm *vm, u32 vcpu_idx)
 {
-	struct sta_struct *st = addr_gva2hva(vm, (ulong)st_gva[vcpu_idx]);
+	struct sta_struct *st = addr_gva2hva(vm, (unsigned long)st_gva[vcpu_idx]);
 	int i;
 
 	pr_info("VCPU%d:\n", vcpu_idx);
@@ -455,7 +455,7 @@ static void steal_time_init(struct kvm_vcpu *vcpu, u32 i)
 
 static void steal_time_dump(struct kvm_vm *vm, u32 vcpu_idx)
 {
-	struct kvm_steal_time *st = addr_gva2hva(vm, (ulong)st_gva[vcpu_idx]);
+	struct kvm_steal_time *st = addr_gva2hva(vm, (unsigned long)st_gva[vcpu_idx]);
 
 	ksft_print_msg("VCPU%d:\n", vcpu_idx);
 	ksft_print_msg("    steal:     %lld\n", st->steal);
tools/testing/selftests/kvm/x86/amx_test.c +1 -1
index 4e63da2b1889..66b6e442c41c 100644
@@ -325,7 +325,7 @@ int main(int argc, char *argv[])
 				vcpu_regs_get(vcpu, &regs2);
 				TEST_ASSERT(!memcmp(&regs1, &regs2, sizeof(regs2)),
 					    "Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx",
-					    (ulong) regs2.rdi, (ulong) regs2.rsi);
+					    (unsigned long)regs2.rdi, (unsigned long)regs2.rsi);
 			}
 			break;
 		case UCALL_DONE:
tools/testing/selftests/kvm/x86/cpuid_test.c +1 -1
index ef0ddd240887..dcd99a0501ee 100644
@@ -128,7 +128,7 @@ static void run_vcpu(struct kvm_vcpu *vcpu, int stage)
 		TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&
 			    uc.args[1] == stage + 1,
 			    "Stage %d: Unexpected register values vmexit, got %lx",
-			    stage + 1, (ulong)uc.args[1]);
+			    stage + 1, (unsigned long)uc.args[1]);
 		return;
 	case UCALL_DONE:
 		return;
tools/testing/selftests/kvm/x86/hyperv_clock.c +1 -1
index c083cea546dc..3dc9a9c2aa54 100644
@@ -248,7 +248,7 @@ int main(void)
 		TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&
 			    uc.args[1] == stage,
 			    "Stage %d: Unexpected register values vmexit, got %lx",
-			    stage, (ulong)uc.args[1]);
+			    stage, (unsigned long)uc.args[1]);
 
 		/* Reset kvmclock triggering TSC page update */
 		if (stage == 7 || stage == 8 || stage == 10) {
tools/testing/selftests/kvm/x86/hyperv_evmcs.c +2 -2
index 1bda2cd3f739..6bca5afccef0 100644
@@ -221,7 +221,7 @@ static struct kvm_vcpu *save_restore_vm(struct kvm_vm *vm,
 	vcpu_regs_get(vcpu, &regs2);
 	TEST_ASSERT(!memcmp(&regs1, &regs2, sizeof(regs2)),
 		    "Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx",
-		    (ulong) regs2.rdi, (ulong) regs2.rsi);
+		    (unsigned long)regs2.rdi, (unsigned long)regs2.rsi);
 	return vcpu;
 }
 
@@ -277,7 +277,7 @@ int main(int argc, char *argv[])
 		/* UCALL_SYNC is handled here.  */
 		TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&
 			    uc.args[1] == stage, "Stage %d: Unexpected register values vmexit, got %lx",
-			    stage, (ulong)uc.args[1]);
+			    stage, (unsigned long)uc.args[1]);
 
 		vcpu = save_restore_vm(vm, vcpu);
 
tools/testing/selftests/kvm/x86/hyperv_svm_test.c +1 -1
index 1f74b0fa9b83..3a3dc31072e8 100644
@@ -186,7 +186,7 @@ int main(int argc, char *argv[])
 		/* UCALL_SYNC is handled here.  */
 		TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&
 			    uc.args[1] == stage, "Stage %d: Unexpected register values vmexit, got %lx",
-			    stage, (ulong)uc.args[1]);
+			    stage, (unsigned long)uc.args[1]);
 
 	}
 
tools/testing/selftests/kvm/x86/set_boot_cpu_id.c +1 -1
index 8e3898646c69..909cd9e83fda 100644
@@ -69,7 +69,7 @@ static void run_vcpu(struct kvm_vcpu *vcpu)
 			TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&
 					uc.args[1] == stage + 1,
 					"Stage %d: Unexpected register values vmexit, got %lx",
-					stage + 1, (ulong)uc.args[1]);
+					stage + 1, (unsigned long)uc.args[1]);
 			test_set_bsp_busy(vcpu, "while running vm");
 			break;
 		case UCALL_DONE:
tools/testing/selftests/kvm/x86/state_test.c +2 -2
index 4a1056a6cb8d..e6f521a6e737 100644
@@ -296,7 +296,7 @@ int main(int argc, char *argv[])
 		/* UCALL_SYNC is handled here.  */
 		TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&
 			    uc.args[1] == stage, "Stage %d: Unexpected register values vmexit, got %lx",
-			    stage, (ulong)uc.args[1]);
+			    stage, (unsigned long)uc.args[1]);
 
 		state = vcpu_save_state(vcpu);
 		memset(&regs1, 0, sizeof(regs1));
@@ -343,7 +343,7 @@ int main(int argc, char *argv[])
 		vcpu_regs_get(vcpu, &regs2);
 		TEST_ASSERT(!memcmp(&regs1, &regs2, sizeof(regs2)),
 			    "Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx",
-			    (ulong) regs2.rdi, (ulong) regs2.rsi);
+			    (unsigned long)regs2.rdi, (unsigned long)regs2.rsi);
 	}
 
 done:
tools/testing/selftests/kvm/x86/tsc_msrs_test.c +1 -1
index 91583969a14f..55779773614b 100644
@@ -78,7 +78,7 @@ static void run_vcpu(struct kvm_vcpu *vcpu, int stage)
 		else
 			ksft_test_result_fail(
 				"stage %d: Unexpected register values vmexit, got %lx",
-				stage + 1, (ulong)uc.args[1]);
+				stage + 1, (unsigned long)uc.args[1]);
 		return;
 	case UCALL_DONE:
 		ksft_test_result_pass("stage %d passed\n", stage + 1);
tools/testing/selftests/kvm/x86/vmx_nested_la57_state_test.c +1 -1
index 75073efa926d..448721b54181 100644
@@ -109,7 +109,7 @@ int main(int argc, char *argv[])
 		}
 
 		TEST_ASSERT(uc.args[1] == stage,
-			    "Expected stage %d, got stage %lu", stage, (ulong)uc.args[1]);
+			    "Expected stage %d, got stage %lu", stage, (unsigned long)uc.args[1]);
 		if (stage == 1) {
 			pr_info("L2 is active; performing save/restore.\n");
 			state = vcpu_save_state(vcpu);
tools/testing/selftests/kvm/x86/vmx_preemption_timer_test.c +2 -2
index eb8021c33cd4..72fbf1b70e9f 100644
@@ -193,7 +193,7 @@ int main(int argc, char *argv[])
 		/* UCALL_SYNC is handled here.  */
 		TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&
 			    uc.args[1] == stage, "Stage %d: Unexpected register values vmexit, got %lx",
-			    stage, (ulong)uc.args[1]);
+			    stage, (unsigned long)uc.args[1]);
 		/*
 		 * If this stage 2 then we should verify the vmx pt expiry
 		 * is as expected.
@@ -234,7 +234,7 @@ int main(int argc, char *argv[])
 		vcpu_regs_get(vcpu, &regs2);
 		TEST_ASSERT(!memcmp(&regs1, &regs2, sizeof(regs2)),
 			    "Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx",
-			    (ulong) regs2.rdi, (ulong) regs2.rsi);
+			    (unsigned long)regs2.rdi, (unsigned long)regs2.rsi);
 	}
 
 done:
Signature · 1 line
2.51.0
#1 lore raw

Gentle ping on this patch; it has been on the list since Aug 3 without any comments.

The musl build failure it fixes is still present in current trees, and the v1 feedback was addressed in this version. Could it be queued in kvm-next?

Thanks,
Hisam

#2 lore raw
Quoted 2 lines · Hisam Mehboob

Gentle ping on this patch; it has been on the list since Aug 3 without any comments.

Patience, please. This is on my list of things to grab for 7.4. You posted right at the end of a cycle, shortly before the merge window, and the code in question has existed for years, i.e. it didn't seem urgent enough to squeeze into 7.3.

If this is the very last thing needed to unblock musl builds of KVM selftests, than that changes things, but IIRC there are other "fix selftests builds with musl" patches floating around.

Quoted 6 lines

The musl build failure it fixes is still present in current trees, and the v1 feedback was addressed in this version. Could it be queued in kvm-next?

Thanks,
Hisam

#3 lore raw
Quoted 11 lines · Sean Christopherson
Quoted 2 lines · Hisam Mehboob

Gentle ping on this patch; it has been on the list since Aug 3 without any comments.

Patience, please. This is on my list of things to grab for 7.4. You posted right at the end of a cycle, shortly before the merge window, and the code in question has existed for years, i.e. it didn't seem urgent enough to squeeze into 7.3.

If this is the very last thing needed to unblock musl builds of KVM selftests, than that changes things, but IIRC there are other "fix selftests builds with musl" patches floating around.

No, this isn't the last blocker. I verified with musl-gcc on x86_64:
with only this patch applied, the build still fails in steal_time.c on pthread_attr_setaffinity_np() -- it also needs your task-pinning fix ("KVM: selftests: Use KVM's task pinning APIs in steal_time", which I resent). With both applied, the musl build passes.

Quoted 7 lines
Quoted 7 lines

The musl build failure it fixes is still present in current trees, and the v1 feedback was addressed in this version. Could it be queued in kvm-next?

Thanks,
Hisam

#4 lore raw
Quoted 13 lines · Hisam Mehboob
Quoted 11 lines · Sean Christopherson
Quoted 2 lines · Hisam Mehboob

Gentle ping on this patch; it has been on the list since Aug 3 without any comments.

Patience, please. This is on my list of things to grab for 7.4. You posted right at the end of a cycle, shortly before the merge window, and the code in question has existed for years, i.e. it didn't seem urgent enough to squeeze into 7.3.

If this is the very last thing needed to unblock musl builds of KVM selftests, than that changes things, but IIRC there are other "fix selftests builds with musl" patches floating around.

Quoted 5 lines

No, this isn't the last blocker. I verified with musl-gcc on x86_64:
with only this patch applied, the build still fails in steal_time.c on pthread_attr_setaffinity_np() -- it also needs your task-pinning fix ("KVM: selftests: Use KVM's task pinning APIs in steal_time", which I resent). With both applied, the musl build passes.

Oh, so on top of "https://github.com/kvm-x86/linux.git next", this *is* the last blocker? If so, then I'll grab this for 7.3.

#5 lore raw
Quoted 2 lines · Sean Christopherson

Oh, so on top of "https://github.com/kvm-x86/linux.git next", this *is* the last blocker? If so, then I'll grab this for 7.3.

Not quite -- I tested kvm-x86/next + this patch with musl-gcc, and the build still fails. On top of the steal_time fix already in your tree, two more musl blockers remain, both from code that is only in kvm-x86/next so far:

1. hardware_disable_test.c, from 496779b54943 ("Pre-set threads affinity in hardware disable test when possible"):

hardware_disable_test.c:75: error: implicit declaration of function 'pthread_attr_setaffinity_np'

The call sits under #ifdef _GNU_SOURCE, but lib.mk defines _GNU_SOURCE unconditionally, so the path is always taken and musl (which lacks the function) breaks.

2. The libvfio wiring from a262fc49e0aa ("Build and link selftests/vfio/lib into KVM selftests"):

vfio_pci_device.c:25: fatal error: uuid/uuid.h: No such file or
directory
sysfs.c:31: error: implicit declaration of function 'basename'

The former adds a hard libuuid dependency (its headers aren't visible to musl-gcc here); the latter because musl declares basename only in <libgen.h>, while glibc exposes it via <string.h> under _GNU_SOURCE.

The rseq __GNUC_PREREQ failure is already covered by my patch in linux-next. With the above sorted, the musl build passes.

#6 lore raw

+David for the VFIO thing

Quoted 13 lines · Hisam Mehboob
Quoted 2 lines · Sean Christopherson

Oh, so on top of "https://github.com/kvm-x86/linux.git next", this *is* the last blocker? If so, then I'll grab this for 7.3.

Not quite -- I tested kvm-x86/next + this patch with musl-gcc, and the build still fails. On top of the steal_time fix already in your tree, two more musl blockers remain, both from code that is only in kvm-x86/next so far:

1. hardware_disable_test.c, from 496779b54943 ("Pre-set threads affinity in hardware disable test when possible"):

Quoted 6 lines

hardware_disable_test.c:75: error: implicit declaration of function 'pthread_attr_setaffinity_np'

The call sits under #ifdef _GNU_SOURCE, but lib.mk defines _GNU_SOURCE unconditionally, so the path is always taken and musl (which lacks the function) breaks.

Heh, Sashiko flagged that as problematic, and I was trying to figure out which macro to key off of[*], but was (obviously) unsuccessful. I don't suppose you know the canonical way for checking for support of glibc-only functionality of this nature?

[*] https://lore.kernel.org/all/[email protected]

Quoted 10 lines

2. The libvfio wiring from a262fc49e0aa ("Build and link selftests/vfio/lib into KVM selftests"):

vfio_pci_device.c:25: fatal error: uuid/uuid.h: No such file or
directory
sysfs.c:31: error: implicit declaration of function 'basename'

The former adds a hard libuuid dependency (its headers aren't visible to musl-gcc here);

Can you elaborate on what you mean by "its headers aren't visible to musl-gcc"?

Quoted 2 lines

the latter because musl declares basename only in <libgen.h>, while glibc exposes it via <string.h> under _GNU_SOURCE.

IIUC, sysfs.c just needs to explicitly include libgen.h?

Quoted 2 lines

The rseq __GNUC_PREREQ failure is already covered by my patch in linux-next. With the above sorted, the musl build passes.

#7 lore raw
Quoted 23 lines · Sean Christopherson

+David for the VFIO thing

Quoted 13 lines · Hisam Mehboob
Quoted 2 lines · Sean Christopherson

Oh, so on top of "https://github.com/kvm-x86/linux.git next", this *is* the last blocker? If so, then I'll grab this for 7.3.

Not quite -- I tested kvm-x86/next + this patch with musl-gcc, and the build still fails. On top of the steal_time fix already in your tree, two more musl blockers remain, both from code that is only in kvm-x86/next so far:

1. hardware_disable_test.c, from 496779b54943 ("Pre-set threads affinity in hardware disable test when possible"):

Quoted 6 lines

hardware_disable_test.c:75: error: implicit declaration of function 'pthread_attr_setaffinity_np'

The call sits under #ifdef _GNU_SOURCE, but lib.mk defines _GNU_SOURCE unconditionally, so the path is always taken and musl (which lacks the function) breaks.

Quoted 7 lines

Heh, Sashiko flagged that as problematic, and I was trying to figure out which macro to key off of[*], but was (obviously) unsuccessful. I don't suppose you know the canonical way for checking for support of glibc-only functionality of this nature?

[*] https://lore.kernel.org/all/[email protected]

__GLIBC__ is the pragmatic in-tree answer.
_GNU_SOURCE is a request macro (input to the libc headers), not an availability indicator -- lib.mk defines it unconditionally, so it cannot detect anything. __USE_GNU works but is glibc-internal. __GLIBC__ is defined by glibc's <features.h>, which any libc header pulls in, and is the established idiom in selftests already:
kvm/lib/assert.c uses "#ifdef __GLIBC__" for execinfo.h, as do bpf/test_progs.c and nolibc. musl deliberately provides no __MUSL__ -- its FAQ's stance is "test for properties, don't assume them", which strictly implies a compile test (autoconf-style); with no configure step in selftests, __GLIBC__ is the established in-tree idiom. For hardware_disable_test.c that is s/_GNU_SOURCE/__GLIBC__/ in the three spots; build-tested with musl-gcc, passes.

Quoted 4 lines
Quoted 4 lines

2. The libvfio wiring from a262fc49e0aa ("Build and link selftests/vfio/lib into KVM selftests"):

Quoted 8 lines
Quoted 6 lines

vfio_pci_device.c:25: fatal error: uuid/uuid.h: No such file or
directory
sysfs.c:31: error: implicit declaration of function 'basename'

The former adds a hard libuuid dependency (its headers aren't visible to musl-gcc here);

Can you elaborate on what you mean by "its headers aren't visible to musl-gcc"?

musl-gcc searches only its own include directories
(here: /usr/include/x86_64-linux-musl and gcc's internal dir) and deliberately not /usr/include, to avoid mixing glibc-oriented headers with musl. uuid/uuid.h comes from libuuid (util-linux) and is installed at /usr/include/uuid/uuid.h for the glibc toolchain, so glibc builds find it and musl builds cannot. I see 2f0c30d0d1 already limits the libvfio link to x86; the remaining gap on x86 is that libuuid is now a hard dependency of the KVM selftests build (libvfio.mk adds -luuid), and for musl-x86 the header is not visible at all without a musl-targeted libuuid install. A uuid.h compile check to skip the vfio pieces would keep it optional; otherwise the new dependency is probably worth documenting.

Quoted 5 lines
Quoted 3 lines

the latter because musl declares basename only in <libgen.h>, while glibc exposes it via <string.h> under _GNU_SOURCE.

IIUC, sysfs.c just needs to explicitly include libgen.h?

Yes, adding <libgen.h> suffices. One nuance: there are two flavors -- glibc under _GNU_SOURCE provides the GNU variant via <string.h> (char *basename(const char *), non-modifying), while <libgen.h> provides the POSIX variant (char *basename(char *), may modify) and takes precedence on glibc as well. Safe at this call site:
rl_path is a writable buffer filled by readlink() (no trailing slashes), used once by sscanf(). Verified with compile tests on both libcs, and with <libgen.h> added, uuid/uuid.h is the only remaining musl failure in the whole build.

Quoted 2 lines
Quoted 2 lines

The rseq __GNUC_PREREQ failure is already covered by my patch in linux-next. With the above sorted, the musl build passes.

#8 lore raw
Quoted 60 lines · Hisam Mehboob
Quoted 23 lines · Sean Christopherson

+David for the VFIO thing

Quoted 13 lines · Hisam Mehboob
Quoted 2 lines · Sean Christopherson

Oh, so on top of "https://github.com/kvm-x86/linux.git next", this *is* the last blocker? If so, then I'll grab this for 7.3.

Not quite -- I tested kvm-x86/next + this patch with musl-gcc, and the build still fails. On top of the steal_time fix already in your tree, two more musl blockers remain, both from code that is only in kvm-x86/next so far:

1. hardware_disable_test.c, from 496779b54943 ("Pre-set threads affinity in hardware disable test when possible"):

Quoted 6 lines

hardware_disable_test.c:75: error: implicit declaration of function 'pthread_attr_setaffinity_np'

The call sits under #ifdef _GNU_SOURCE, but lib.mk defines _GNU_SOURCE unconditionally, so the path is always taken and musl (which lacks the function) breaks.

Quoted 7 lines

Heh, Sashiko flagged that as problematic, and I was trying to figure out which macro to key off of[*], but was (obviously) unsuccessful. I don't suppose you know the canonical way for checking for support of glibc-only functionality of this nature?

[*] https://lore.kernel.org/all/[email protected]

__GLIBC__ is the pragmatic in-tree answer.
_GNU_SOURCE is a request macro (input to the libc headers), not an availability indicator -- lib.mk defines it unconditionally, so it cannot detect anything. __USE_GNU works but is glibc-internal. __GLIBC__ is defined by glibc's <features.h>, which any libc header pulls in, and is the established idiom in selftests already:
kvm/lib/assert.c uses "#ifdef __GLIBC__" for execinfo.h, as do bpf/test_progs.c and nolibc. musl deliberately provides no __MUSL__ -- its FAQ's stance is "test for properties, don't assume them", which strictly implies a compile test (autoconf-style); with no configure step in selftests, __GLIBC__ is the established in-tree idiom. For hardware_disable_test.c that is s/_GNU_SOURCE/__GLIBC__/ in the three spots; build-tested with musl-gcc, passes.

Quoted 4 lines
Quoted 4 lines

2. The libvfio wiring from a262fc49e0aa ("Build and link selftests/vfio/lib into KVM selftests"):

Quoted 8 lines
Quoted 6 lines

vfio_pci_device.c:25: fatal error: uuid/uuid.h: No such file or
directory
sysfs.c:31: error: implicit declaration of function 'basename'

The former adds a hard libuuid dependency (its headers aren't visible to musl-gcc here);

Can you elaborate on what you mean by "its headers aren't visible to musl-gcc"?

Quoted 12 lines

musl-gcc searches only its own include directories
(here: /usr/include/x86_64-linux-musl and gcc's internal dir) and deliberately not /usr/include, to avoid mixing glibc-oriented headers with musl. uuid/uuid.h comes from libuuid (util-linux) and is installed at /usr/include/uuid/uuid.h for the glibc toolchain, so glibc builds find it and musl builds cannot. I see 2f0c30d0d1 already limits the libvfio link to x86; the remaining gap on x86 is that libuuid is now a hard dependency of the KVM selftests build (libvfio.mk adds -luuid), and for musl-x86 the header is not visible at all without a musl-targeted libuuid install. A uuid.h compile check to skip the vfio pieces would keep it optional; otherwise the new dependency is probably worth documenting.

libuuid is required to build libvfio, which VFIO and KVM selftests both depend on.

How is this problem solved for other libraries, like -lpthread?

Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

the latter because musl declares basename only in <libgen.h>, while glibc exposes it via <string.h> under _GNU_SOURCE.

IIUC, sysfs.c just needs to explicitly include libgen.h?

Quoted 9 lines

Yes, adding <libgen.h> suffices. One nuance: there are two flavors -- glibc under _GNU_SOURCE provides the GNU variant via <string.h> (char *basename(const char *), non-modifying), while <libgen.h> provides the POSIX variant (char *basename(char *), may modify) and takes precedence on glibc as well. Safe at this call site:
rl_path is a writable buffer filled by readlink() (no trailing slashes), used once by sscanf(). Verified with compile tests on both libcs, and with <libgen.h> added, uuid/uuid.h is the only remaining musl failure in the whole build.

Feel free to send a patch, or I can if you prefer.

Quoted 4 lines
Quoted 3 lines
Quoted 3 lines

The rseq __GNUC_PREREQ failure is already covered by my patch in linux-next. With the above sorted, the musl build passes.

#9 lore raw
Quoted 4 lines · David Matlack

libuuid is required to build libvfio, which VFIO and KVM selftests both depend on.

How is this problem solved for other libraries, like -lpthread?

musl does not need a separate pthread library. pthread support is part of libc, and libpthread.so is provided as a compatibility stub, so -lpthread works without an extra package.

libuuid is an external library from util-linux. On Alpine, installing util-linux-dev provides the musl-built libuuid and uuid/uuid.h, and the selftests build succeeds. I confirmed this on a fresh Alpine install.

I sent the basename fix as a separate patch:
https://lore.kernel.org/kvm/[email protected]/

#10 lore raw
In reply to Hisam Mehboob
Quoted 8 lines · Hisam Mehboob

KVM selftests use the non-standard ulong alias in several files. glibc exposes the alias transitively through its headers, whereas musl does not, causing musl builds to fail with ulong undeclared.

Use unsigned long directly instead of depending on a libc-specific typedef. This also avoids adding a duplicate typedef to a shared tools header.

[...]

Applied to kvm-x86 selftests, thanks!

[1/1] KVM: selftests: Replace ulong with unsigned long

      https://github.com/kvm-x86/linux/commit/5be2d20f9818

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