Skip to contentSkip to conversation

selftests: kvm: Fix a compile error in selftests/kvm/rseq_test.c

PATCH Applied Jinrong Liang 6 participants 12 messages Aug 2, 2022 – Sep 22, 2022

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

PATCH 1/1 selftests: kvm: Fix a compile error in selftests/kvm/rseq_test.c

From: Jinrong Liang <[email protected]>

The following warning appears when executing:
make -C tools/testing/selftests/kvm

rseq_test.c: In function ‘main’:
rseq_test.c:237:33: warning: implicit declaration of function ‘gettid’; did you mean ‘getgid’? [-Wimplicit-function-declaration]

          (void *)(unsigned long)gettid());
                                 ^~~~~~
                                 getgid

/usr/bin/ld: /tmp/ccr5mMko.o: in function `main':
../kvm/tools/testing/selftests/kvm/rseq_test.c:237: undefined reference to `gettid'
collect2: error: ld returned 1 exit status
make: *** [../lib.mk:173: ../kvm/tools/testing/selftests/kvm/rseq_test] Error 1

Use the more compatible syscall(SYS_gettid) instead of gettid() to fix it. More subsequent reuse may cause it to be wrapped in a lib file.

---
 tools/testing/selftests/kvm/rseq_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/rseq_test.c +1 -1
index a54d4d05a058..299d316cc759 100644
@@ -229,7 +229,7 @@ int main(int argc, char *argv[])
 	ucall_init(vm, NULL);
 
 	pthread_create(&migration_thread, NULL, migration_worker,
-		       (void *)(unsigned long)gettid());
+		       (void *)(unsigned long)syscall(SYS_gettid));
 
 	for (i = 0; !done; i++) {
 		vcpu_run(vcpu);
Signature · 1 line
2.37.1
#1 lore raw
Quoted 7 lines · Jinrong Liang

From: Jinrong Liang <[email protected]>

The following warning appears when executing:
make -C tools/testing/selftests/kvm

rseq_test.c: In function ‘main’:
rseq_test.c:237:33: warning: implicit declaration of function ‘gettid’; did you mean ‘getgid’? [-Wimplicit-function-declaration]

Quoted 7 lines
          (void *)(unsigned long)gettid());
                                 ^~~~~~
                                 getgid

/usr/bin/ld: /tmp/ccr5mMko.o: in function `main':
../kvm/tools/testing/selftests/kvm/rseq_test.c:237: undefined reference to `gettid'
collect2: error: ld returned 1 exit status
make: *** [../lib.mk:173: ../kvm/tools/testing/selftests/kvm/rseq_test] Error 1

The man page says we need

#define _GNU_SOURCE
#include <unistd.h>

which rseq_test.c doesn't have. We have _GNU_SOURCE, but not unistd.h. IOW, I think this patch can be

tools/testing/selftests/kvm/rseq_test.c +1 -0
index a54d4d05a058..8d3d5eab5e19 100644
@@ -9,6 +9,7 @@
 #include <string.h>
 #include <signal.h>
 #include <syscall.h>
+#include <unistd.h>
 #include <sys/ioctl.h>
 #include <sys/sysinfo.h>
 #include <asm/barrier.h>

Thanks,
drew

Quoted 25 lines

Use the more compatible syscall(SYS_gettid) instead of gettid() to fix it. More subsequent reuse may cause it to be wrapped in a lib file.

---
 tools/testing/selftests/kvm/rseq_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/rseq_test.c +1 -1
index a54d4d05a058..299d316cc759 100644
@@ -229,7 +229,7 @@ int main(int argc, char *argv[])
 	ucall_init(vm, NULL);
 
 	pthread_create(&migration_thread, NULL, migration_worker,
-		       (void *)(unsigned long)gettid());
+		       (void *)(unsigned long)syscall(SYS_gettid));
 
 	for (i = 0; !done; i++) {
 		vcpu_run(vcpu);
Signature · 1 line
2.37.1
#2 lore raw

My ldd version is (GNU libc) 2.28, and I get a compilation error in this case. But I use another ldd (Ubuntu GLIBC 2.31-0ubuntu9.2) 2.31 is compiling fine. This shows that compilation errors may occur in different GNU libc environments. Would it be more appropriate to use syscall for better compatibility?

Thanks,
Jinrong Liang

Quoted 66 lines · Andrew Jones <[email protected]> 于2022年8月2日周二 23:08写道:
Quoted 7 lines · Jinrong Liang

From: Jinrong Liang <[email protected]>

The following warning appears when executing:
make -C tools/testing/selftests/kvm

rseq_test.c: In function ‘main’:
rseq_test.c:237:33: warning: implicit declaration of function ‘gettid’; did you mean ‘getgid’? [-Wimplicit-function-declaration]

Quoted 7 lines
          (void *)(unsigned long)gettid());
                                 ^~~~~~
                                 getgid

/usr/bin/ld: /tmp/ccr5mMko.o: in function `main':
../kvm/tools/testing/selftests/kvm/rseq_test.c:237: undefined reference to `gettid'
collect2: error: ld returned 1 exit status
make: *** [../lib.mk:173: ../kvm/tools/testing/selftests/kvm/rseq_test] Error 1

The man page says we need

#define _GNU_SOURCE
#include <unistd.h>

which rseq_test.c doesn't have. We have _GNU_SOURCE, but not unistd.h. IOW, I think this patch can be

tools/testing/selftests/kvm/rseq_test.c +1 -0
index a54d4d05a058..8d3d5eab5e19 100644
@@ -9,6 +9,7 @@
 #include <string.h>
 #include <signal.h>
 #include <syscall.h>
+#include <unistd.h>
 #include <sys/ioctl.h>
 #include <sys/sysinfo.h>
 #include <asm/barrier.h>

Thanks,
drew

Quoted 25 lines

Use the more compatible syscall(SYS_gettid) instead of gettid() to fix it. More subsequent reuse may cause it to be wrapped in a lib file.

---
 tools/testing/selftests/kvm/rseq_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/rseq_test.c +1 -2
index a54d4d05a058..299d316cc759 100644
@@ -229,7 +229,7 @@ int main(int argc, char *argv[])
      ucall_init(vm, NULL);
 
      pthread_create(&migration_thread, NULL, migration_worker,
-                    (void *)(unsigned long)gettid());
+                    (void *)(unsigned long)syscall(SYS_gettid));
 
      for (i = 0; !done; i++) {
              vcpu_run(vcpu);
--

2.37.1

Reviewed-by
#3 lore raw
Quoted 4 lines · Jinrong Liang

My ldd version is (GNU libc) 2.28, and I get a compilation error in this case. But I use another ldd (Ubuntu GLIBC 2.31-0ubuntu9.2) 2.31 is compiling fine. This shows that compilation errors may occur in different GNU libc environments. Would it be more appropriate to use syscall for better compatibility?

OK, it's a pity, but no big deal to use syscall().

Thanks,
drew

#4 lore raw
Quoted 7 lines · Andrew Jones
Quoted 4 lines · Jinrong Liang

My ldd version is (GNU libc) 2.28, and I get a compilation error in this case. But I use another ldd (Ubuntu GLIBC 2.31-0ubuntu9.2) 2.31 is compiling fine. This shows that compilation errors may occur in different GNU libc environments. Would it be more appropriate to use syscall for better compatibility?

OK, it's a pity, but no big deal to use syscall().

Ya, https://man7.org/linux/man-pages/man2/gettid.2.html says:

The gettid() system call first appeared on Linux in kernel 2.4.11. Library support was added in glibc 2.30.

But there are already two other instances of syscall(SYS_gettid) in KVM selftests, tools/testing/selftests/kvm/lib/assert.c even adds a _gettid() wrapper.

So rather than having to remember (or discover) to use syscall(SYS_gettid), I wonder if it's possible to conditionally define gettid()? E.g. check for GLIBC version? Or do

  #define gettid() syscall(SYS_gettid)

so that it's always available and simply overrides the library's gettid() if it's provided?

#5 lore raw
Quoted 9 lines · Sean Christopherson
Quoted 7 lines · Andrew Jones
Quoted 4 lines · Jinrong Liang

My ldd version is (GNU libc) 2.28, and I get a compilation error in this case. But I use another ldd (Ubuntu GLIBC 2.31-0ubuntu9.2) 2.31 is compiling fine. This shows that compilation errors may occur in different GNU libc environments. Would it be more appropriate to use syscall for better compatibility?

OK, it's a pity, but no big deal to use syscall().

Quoted 7 lines

Ya, https://man7.org/linux/man-pages/man2/gettid.2.html says:

The gettid() system call first appeared on Linux in kernel 2.4.11. Library support was added in glibc 2.30.

But there are already two other instances of syscall(SYS_gettid) in KVM selftests, tools/testing/selftests/kvm/lib/assert.c even adds a _gettid() wrapper.

Ha! And I found four more in selftests...

testing/selftests/powerpc/include/utils.h
testing/selftests/proc/proc.h
testing/selftests/rseq/param_test.c
testing/selftests/sched/cs_prctl_test.c

and even more in tools...

Quoted 9 lines

So rather than having to remember (or discover) to use syscall(SYS_gettid), I wonder if it's possible to conditionally define gettid()? E.g. check for GLIBC version? Or do

  #define gettid() syscall(SYS_gettid)

so that it's always available and simply overrides the library's gettid() if it's provided?

Sounds good to me. Now the question is where to put it? kvm_util.h, test_util.h, or maybe we should create a new header just for stuff like this?

It doesn't really "fit" in kvm_util.h, but if we put it there, then we greatly reduce the chance that we'll have to revisit this issue again. We could also create a new header just for stuff like this and then include that from kvm_util.h...

Thanks,
drew

#6 lore raw
Quoted 18 lines · Andrew Jones
Quoted 9 lines · Sean Christopherson
Quoted 7 lines · Andrew Jones
Quoted 4 lines · Jinrong Liang

My ldd version is (GNU libc) 2.28, and I get a compilation error in this case. But I use another ldd (Ubuntu GLIBC 2.31-0ubuntu9.2) 2.31 is compiling fine. This shows that compilation errors may occur in different GNU libc environments. Would it be more appropriate to use syscall for better compatibility?

OK, it's a pity, but no big deal to use syscall().

Quoted 7 lines

Ya, https://man7.org/linux/man-pages/man2/gettid.2.html says:

The gettid() system call first appeared on Linux in kernel 2.4.11. Library support was added in glibc 2.30.

But there are already two other instances of syscall(SYS_gettid) in KVM selftests, tools/testing/selftests/kvm/lib/assert.c even adds a _gettid() wrapper.

Quoted 8 lines

Ha! And I found four more in selftests...

testing/selftests/powerpc/include/utils.h
testing/selftests/proc/proc.h
testing/selftests/rseq/param_test.c
testing/selftests/sched/cs_prctl_test.c

and even more in tools...

Ha, and tools/testing/selftests/sched/cs_prctl_test.c even has the GLIBC crud.

#if __GLIBC_PREREQ(2, 30) == 0
#include <sys/syscall.h>
static pid_t gettid(void)

{
	return syscall(SYS_gettid);
}

#endif

Quoted 4 lines
Quoted 4 lines

So rather than having to remember (or discover) to use syscall(SYS_gettid), I wonder if it's possible to conditionally define gettid()? E.g. check for GLIBC version? Or do

Quoted 8 lines
Quoted 4 lines
  #define gettid() syscall(SYS_gettid)

so that it's always available and simply overrides the library's gettid() if it's provided?

Sounds good to me. Now the question is where to put it? kvm_util.h, test_util.h, or maybe we should create a new header just for stuff like this?

tools/include/uapi/linux/syscall.h?

Kind of dirty, but not thaaaat dirty.

reviewer Reviewed-by on v1 1/1
#7 lore raw
In reply to Jinrong Liang
Quoted 15 lines · Jinrong Liang

From: Jinrong Liang <[email protected]>

The following warning appears when executing:
make -C tools/testing/selftests/kvm

rseq_test.c: In function ‘main’:
rseq_test.c:237:33: warning: implicit declaration of function ‘gettid’; did you mean ‘getgid’? [-Wimplicit-function-declaration]

          (void *)(unsigned long)gettid());
                                 ^~~~~~
                                 getgid

/usr/bin/ld: /tmp/ccr5mMko.o: in function `main':
../kvm/tools/testing/selftests/kvm/rseq_test.c:237: undefined reference to `gettid'
collect2: error: ld returned 1 exit status
make: *** [../lib.mk:173: ../kvm/tools/testing/selftests/kvm/rseq_test] Error 1

Quoted 5 lines

Use the more compatible syscall(SYS_gettid) instead of gettid() to fix it. More subsequent reuse may cause it to be wrapped in a lib file.

---

Paolo, do you want to grab this for 6.0? It doesn't look like we're going to have a more elegant solution anytime soon...

Reviewed-by
#8 lore raw
Quoted 16 lines · Sean Christopherson
Quoted 15 lines · Jinrong Liang

From: Jinrong Liang <[email protected]>

The following warning appears when executing:
make -C tools/testing/selftests/kvm

rseq_test.c: In function ‘main’:
rseq_test.c:237:33: warning: implicit declaration of function ‘gettid’; did you mean ‘getgid’? [-Wimplicit-function-declaration]

           (void *)(unsigned long)gettid());
                                  ^~~~~~
                                  getgid

/usr/bin/ld: /tmp/ccr5mMko.o: in function `main':
../kvm/tools/testing/selftests/kvm/rseq_test.c:237: undefined reference to `gettid'
collect2: error: ld returned 1 exit status
make: *** [../lib.mk:173: ../kvm/tools/testing/selftests/kvm/rseq_test] Error 1

Quoted 8 lines
Quoted 5 lines

Use the more compatible syscall(SYS_gettid) instead of gettid() to fix it. More subsequent reuse may cause it to be wrapped in a lib file.

---

Can a 'Cc: [email protected]' be added also as e923b0537d28 got backported to v5.15.58

Quoted 3 lines

Paolo, do you want to grab this for 6.0? It doesn't look like we're going to have a more elegant solution anytime soon...

#9 lore raw
Quoted 23 lines · Liam Merwick
Quoted 22 lines · Sean Christopherson
Quoted 20 lines · Jinrong Liang

From: Jinrong Liang <[email protected]>

The following warning appears when executing:
    make -C tools/testing/selftests/kvm

rseq_test.c: In function ‘main’:
rseq_test.c:237:33: warning: implicit declaration of function ‘gettid’; did you mean ‘getgid’? [-Wimplicit-function-declaration] (void *)(unsigned long)gettid());
                                  ^~~~~~
                                  getgid
/usr/bin/ld: /tmp/ccr5mMko.o: in function `main':
../kvm/tools/testing/selftests/kvm/rseq_test.c:237: undefined reference to `gettid'
collect2: error: ld returned 1 exit status
make: *** [../lib.mk:173: ../kvm/tools/testing/selftests/kvm/rseq_test] Error 1

Use the more compatible syscall(SYS_gettid) instead of gettid() to fix it. More subsequent reuse may cause it to be wrapped in a lib file.

---

Quoted 12 lines
Quoted 2 lines

Can a 'Cc: [email protected]' be added also as e923b0537d28 got backported to v5.15.58

Quoted 3 lines

Paolo, do you want to grab this for 6.0?  It doesn't look like we're going to have a more elegant solution anytime soon...

Ping?

#10 lore raw
Quoted 41 lines · JinrongLiang
Quoted 37 lines · Liam Merwick
Quoted 19 lines · Sean Christopherson
Quoted 18 lines · Jinrong Liang

From: Jinrong Liang <[email protected]>

The following warning appears when executing:
make -C tools/testing/selftests/kvm

rseq_test.c: In function ‘main’:
rseq_test.c:237:33: warning: implicit declaration of function ‘gettid’; did you mean ‘getgid’? [-Wimplicit-function-declaration]

           (void *)(unsigned long)gettid());
                                  ^~~~~~
                                  getgid

/usr/bin/ld: /tmp/ccr5mMko.o: in function `main':
../kvm/tools/testing/selftests/kvm/rseq_test.c:237: undefined reference to `gettid'
collect2: error: ld returned 1 exit status
make: *** [../lib.mk:173:
../kvm/tools/testing/selftests/kvm/rseq_test] Error 1

Quoted 9 lines
Quoted 6 lines

Use the more compatible syscall(SYS_gettid) instead of gettid() to fix it.
More subsequent reuse may cause it to be wrapped in a lib file.

---

Can a 'Cc: [email protected]' be added also as e923b0537d28 got backported to v5.15.58

Quoted 1 line
Quoted 5 lines
Quoted 4 lines
Quoted 3 lines

Paolo, do you want to grab this for 6.0? It doesn't look like we're going to have
a more elegant solution anytime soon...

Ping?

+1, please grab this. Every new branch I create (for new development and to review upstream code) I have to remember to cherry-pick this patch in because I use Clang.