Skip to contentSkip to conversation

vsyscall: use __iter_div_u64_rem()

PATCH In review Arnd Bergmann 7 participants 12 messages Jul 10, 2019 – Jul 22, 2019

Subsystems (6)
lore
Share & tools
lore

Arnd Bergmann

author maintainer
#0 lore raw

PATCH 1/1 vsyscall: use __iter_div_u64_rem()

On 32-bit x86 when building with clang-9, the loop gets turned back into an inefficient division that causes a link error:

kernel/time/vsyscall.o: In function `update_vsyscall':
vsyscall.c:(.text+0xe3): undefined reference to `__udivdi3'

Use the provided __iter_div_u64_rem() function that is meant to address the same case in other files.

Fixes 44f57d788e7d ("timekeeping: Provide a generic update_vsyscall() implementation")
---
 kernel/time/vsyscall.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/time/vsyscall.c +1 -5
index a80893180826..8cf3596a4ce6 100644
@@ -104,11 +104,7 @@ void update_vsyscall(struct timekeeper *tk)
 	vdso_ts->sec	= tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
 	nsec		= tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift;
 	nsec		= nsec + tk->wall_to_monotonic.tv_nsec;
-	while (nsec >= NSEC_PER_SEC) {
-		nsec = nsec - NSEC_PER_SEC;
-		vdso_ts->sec++;
-	}
-	vdso_ts->nsec	= nsec;
+	vdso_ts->sec	+= __iter_div_u64_rem(nsec, NSEC_PER_SEC, &vdso_ts->nsec);
 
 	if (__arch_use_vsyscall(vdata))
 		update_vdso_data(vdata, tk);
Signature · 1 line
2.20.0
#1 lore raw

[tip:timers/urgent] timekeeping/vsyscall: Use __iter_div_u64_rem()

timekeeping/vsyscall: Use __iter_div_u64_rem()
Commit-ID:  510c12149954eb9db6a30c2b9f7526af71afb7a4
Gitweb:     https://git.kernel.org/tip/510c12149954eb9db6a30c2b9f7526af71afb7a4
Author:     Arnd Bergmann <[email protected]>
AuthorDate: Wed, 10 Jul 2019 15:01:53 +0200
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Wed, 10 Jul 2019 17:17:49 +0200

timekeeping/vsyscall: Use __iter_div_u64_rem()

On 32-bit x86 when building with clang-9, the 'division' loop gets turned back into an inefficient division that causes a link error:

kernel/time/vsyscall.o: In function `update_vsyscall':
vsyscall.c:(.text+0xe3): undefined reference to `__udivdi3'

Use the existing __iter_div_u64_rem() function which is used to address the same issue in other places.

Fixes 44f57d788e7d ("timekeeping: Provide a generic update_vsyscall() implementation")
---
 kernel/time/vsyscall.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/time/vsyscall.c +1 -5
index a80893180826..8cf3596a4ce6 100644
@@ -104,11 +104,7 @@ void update_vsyscall(struct timekeeper *tk)
 	vdso_ts->sec	= tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
 	nsec		= tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift;
 	nsec		= nsec + tk->wall_to_monotonic.tv_nsec;
-	while (nsec >= NSEC_PER_SEC) {
-		nsec = nsec - NSEC_PER_SEC;
-		vdso_ts->sec++;
-	}
-	vdso_ts->nsec	= nsec;
+	vdso_ts->sec	+= __iter_div_u64_rem(nsec, NSEC_PER_SEC, &vdso_ts->nsec);
 
 	if (__arch_use_vsyscall(vdata))
 		update_vdso_data(vdata, tk);
Reviewed-by Tested-by on v1 1/1
#2 lore raw
In reply to Arnd Bergmann
Quoted 35 lines · Arnd Bergmann

On 32-bit x86 when building with clang-9, the loop gets turned back into an inefficient division that causes a link error:

kernel/time/vsyscall.o: In function `update_vsyscall':
vsyscall.c:(.text+0xe3): undefined reference to `__udivdi3'

Use the provided __iter_div_u64_rem() function that is meant to address the same case in other files.

Fixes 44f57d788e7d ("timekeeping: Provide a generic update_vsyscall() implementation")
---
 kernel/time/vsyscall.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/time/vsyscall.c +1 -5
index a80893180826..8cf3596a4ce6 100644
@@ -104,11 +104,7 @@ void update_vsyscall(struct timekeeper *tk)
 	vdso_ts->sec	= tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
 	nsec		= tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift;
 	nsec		= nsec + tk->wall_to_monotonic.tv_nsec;
-	while (nsec >= NSEC_PER_SEC) {
-		nsec = nsec - NSEC_PER_SEC;
-		vdso_ts->sec++;
-	}
-	vdso_ts->nsec	= nsec;
+	vdso_ts->sec	+= __iter_div_u64_rem(nsec, NSEC_PER_SEC, &vdso_ts->nsec);
 
 	if (__arch_use_vsyscall(vdata))
 		update_vdso_data(vdata, tk);
Signature · 1 line
2.20.0

What an interesting function. Looks good to me and I can confirm it fixes the link error.

#3 lore raw

[tip:timers/urgent] timekeeping/vsyscall: Use __iter_div_u64_rem()

In reply to Arnd Bergmann
timekeeping/vsyscall: Use __iter_div_u64_rem()
Commit-ID:  0df1c9868c3a1916198ee09c323ca5932a0b8a11
Gitweb:     https://git.kernel.org/tip/0df1c9868c3a1916198ee09c323ca5932a0b8a11
Author:     Arnd Bergmann <[email protected]>
AuthorDate: Wed, 10 Jul 2019 15:01:53 +0200
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Wed, 10 Jul 2019 20:37:49 +0200

timekeeping/vsyscall: Use __iter_div_u64_rem()

On 32-bit x86 when building with clang-9, the 'division' loop gets turned back into an inefficient division that causes a link error:

kernel/time/vsyscall.o: In function `update_vsyscall':
vsyscall.c:(.text+0xe3): undefined reference to `__udivdi3'

Use the existing __iter_div_u64_rem() function which is used to address the same issue in other places.

Fixes 44f57d788e7d ("timekeeping: Provide a generic update_vsyscall() implementation")
---
 kernel/time/vsyscall.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/time/vsyscall.c +1 -5
index a80893180826..8cf3596a4ce6 100644
@@ -104,11 +104,7 @@ void update_vsyscall(struct timekeeper *tk)
 	vdso_ts->sec	= tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
 	nsec		= tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift;
 	nsec		= nsec + tk->wall_to_monotonic.tv_nsec;
-	while (nsec >= NSEC_PER_SEC) {
-		nsec = nsec - NSEC_PER_SEC;
-		vdso_ts->sec++;
-	}
-	vdso_ts->nsec	= nsec;
+	vdso_ts->sec	+= __iter_div_u64_rem(nsec, NSEC_PER_SEC, &vdso_ts->nsec);
 
 	if (__arch_use_vsyscall(vdata))
 		update_vdso_data(vdata, tk);
#4 lore raw
In reply to Arnd Bergmann

Hi Arnd,

Quoted 23 lines · Arnd Bergmann

On 32-bit x86 when building with clang-9, the loop gets turned back into an inefficient division that causes a link error:

kernel/time/vsyscall.o: In function `update_vsyscall':
vsyscall.c:(.text+0xe3): undefined reference to `__udivdi3'

Use the provided __iter_div_u64_rem() function that is meant to address the same case in other files.

Fixes 44f57d788e7d ("timekeeping: Provide a generic update_vsyscall() implementation")
---
 kernel/time/vsyscall.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/time/vsyscall.c +0 -0
index a80893180826..8cf3596a4ce6 100644
@@ -104,11 +104,7 @@ void update_vsyscall(struct timekeeper *tk)
 	vdso_ts->sec	= tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
 	nsec		= tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift;

kernel/time/vsyscall.c void update_vsyscall(struct timekeeper *tk) · line 110

Quoted 10 lines
kernel/time/vsyscall.c +1 -5
 	nsec		= nsec + tk->wall_to_monotonic.tv_nsec;
-	while (nsec >= NSEC_PER_SEC) {
-		nsec = nsec - NSEC_PER_SEC;
-		vdso_ts->sec++;
-	}
-	vdso_ts->nsec	= nsec;
+	vdso_ts->sec	+= __iter_div_u64_rem(nsec, NSEC_PER_SEC, &vdso_ts->nsec);
 
 	if (__arch_use_vsyscall(vdata))
 		update_vdso_data(vdata, tk);

I am trying to test this patch using clang-9 tip:

# clang -v
clang version 9.0.0 ([email protected]:llvm-mirror/clang.git
6ed0749151866894a67a3e7eefdc1f3a547daa0e) ([email protected]:llvm-mirror/llvm.git
a10a70238ace1093cad3adeb94814b422bd1b5c1)

but I get a lot of errors similar to the one below:

In file included from ~/linux/arch/x86/events/amd/core.c:11:
~/linux/arch/x86/events/amd/../perf_event.h:824:21: error: invalid output size for constraint '=q'

        u64 disable_mask = __this_cpu_read(cpu_hw_events.perf_ctr_virt_mask);
                           ^
~/linux/include/linux/percpu-defs.h:447:2: note: expanded from macro
'__this_cpu_read'
        raw_cpu_read(pcp);                                              \
        ^
~/linux/include/linux/percpu-defs.h:421:28: note: expanded from macro 'raw_cpu_read'
#define raw_cpu_read(pcp)               __pcpu_size_call_return(raw_cpu_read_, pcp)
                                        ^
~/linux/include/linux/percpu-defs.h:322:23: note: expanded from macro
'__pcpu_size_call_return'
        case 1: pscr_ret__ = stem##1(variable); break;                  \
                             ^

<scratch space>:110:1: note: expanded from here raw_cpu_read_1
^
~/linux/arch/x86/include/asm/percpu.h:394:30: note: expanded from macro 'raw_cpu_read_1'

#define raw_cpu_read_1(pcp)             percpu_from_op(, "mov", pcp)
                                        ^
~/linux/arch/x86/include/asm/percpu.h:189:15: note: expanded from macro
'percpu_from_op'
                    : "=q" (pfo_ret__)                  \

Could you please tell me which version of the compiler did you use?

My building command is:

# make mrproper && make CC=clang HOSTCC=clang i386_defconfig && make ARCH=i386
CC=clang HOSTCC=clang -j56
Signature · 2 lines
Regards,
Vincenzo
author maintainer
#5 lore raw
Quoted 9 lines · Vincenzo Frascino

Could you please tell me which version of the compiler did you use?

My building command is:

# make mrproper && make CC=clang HOSTCC=clang i386_defconfig && make ARCH=i386
CC=clang HOSTCC=clang -j56

See below for the patch I am using locally to work around this. That patch is probably wrong, so I have not submitted it yet, but it gives you a clean build ;-)

Arnd

8<---
Subject: [PATCH] x86: percpu: fix clang 32-bit build

clang does not like an inline assembly with a "=q" contraint for a 64-bit output:

arch/x86/events/perf_event.h:824:21: error: invalid output size for constraint '=q'

        u64 disable_mask = __this_cpu_read(cpu_hw_events.perf_ctr_virt_mask);
                           ^
include/linux/percpu-defs.h:447:2: note: expanded from macro '__this_cpu_read'
        raw_cpu_read(pcp);                                              \
        ^
include/linux/percpu-defs.h:421:28: note: expanded from macro 'raw_cpu_read'
 #define raw_cpu_read(pcp)
__pcpu_size_call_return(raw_cpu_read_, pcp)
                                        ^
include/linux/percpu-defs.h:322:23: note: expanded from macro
'__pcpu_size_call_return'
        case 1: pscr_ret__ = stem##1(variable); break;                  \
                             ^

<scratch space>:357:1: note: expanded from here
raw_cpu_read_1
^
arch/x86/include/asm/percpu.h:394:30: note: expanded from macro 'raw_cpu_read_1'

 #define raw_cpu_read_1(pcp)             percpu_from_op(, "mov", pcp)
                                        ^
arch/x86/include/asm/percpu.h:189:15: note: expanded from macro 'percpu_from_op'
                    : "=q" (pfo_ret__)                  \
                            ^

According to the commit that introduced the "q" constraint, this was needed to fix miscompilation, but it gives no further detail.

Using the normal "=r" constraint seems to work so far.

Fixes 3c598766a2ba ("x86: fix percpu_{to,from}_op()")
Cc Jan Beulich <[email protected]>
arch/x86/include/asm/percpu.h +2 -2
index 2278797c769d..e791fbf4018f 100644
@@ -99,7 +99,7 @@ do {                                                  \
        case 1:                                         \
                asm qual (op "b %1,"__percpu_arg(0)     \
                    : "+m" (var)                        \
-                   : "qi" ((pto_T__)(val)));           \
+                   : "ri" ((pto_T__)(val)));           \
                break;                                  \
        case 2:                                         \
                asm qual (op "w %1,"__percpu_arg(0)     \
@@ -144,7 +144,7 @@ do {
                         \
                else                                                    \
                        asm qual ("addb %1, "__percpu_arg(0)            \
                            : "+m" (var)                                \
-                           : "qi" ((pao_T__)(val)));                   \
+                           : "ri" ((pao_T__)(val)));                   \
                break;                                                  \
        case 2:                                                         \
                if (pao_ID__ == 1)                                      \
@@ -186,7 +186,7 @@ do {
                         \
        switch (sizeof(var)) {                          \
        case 1:                                         \
                asm qual (op "b "__percpu_arg(1)",%0"   \
-                   : "=q" (pfo_ret__)                  \
+                   : "=r" (pfo_ret__)                  \
                    : "m" (var));                       \
                break;                                  \
        case 2:                                         \
@@ -215,7 +215,7 @@ do {
                         \
        switch (sizeof(var)) {                          \
        case 1:                                         \
                asm(op "b "__percpu_arg(P1)",%0"        \
-                   : "=q" (pfo_ret__)                  \
+                   : "=r" (pfo_ret__)                  \
                    : "p" (&(var)));                    \
                break;                                  \
        case 2:                                         \
#6 lore raw

Hi Arnd,

Quoted 12 lines · Arnd Bergmann
Quoted 9 lines · Vincenzo Frascino

Could you please tell me which version of the compiler did you use?

My building command is:

# make mrproper && make CC=clang HOSTCC=clang i386_defconfig && make ARCH=i386
CC=clang HOSTCC=clang -j56
Quoted 6 lines

See below for the patch I am using locally to work around this. That patch is probably wrong, so I have not submitted it yet, but it gives you a clean build ;-)

Arnd

Thank you, I will give it a go :-)

Quoted 84 lines

8<---
Subject: [PATCH] x86: percpu: fix clang 32-bit build

clang does not like an inline assembly with a "=q" contraint for a 64-bit output:

arch/x86/events/perf_event.h:824:21: error: invalid output size for constraint '=q'

        u64 disable_mask = __this_cpu_read(cpu_hw_events.perf_ctr_virt_mask);
                           ^
include/linux/percpu-defs.h:447:2: note: expanded from macro '__this_cpu_read'
        raw_cpu_read(pcp);                                              \
        ^
include/linux/percpu-defs.h:421:28: note: expanded from macro 'raw_cpu_read'
 #define raw_cpu_read(pcp)
__pcpu_size_call_return(raw_cpu_read_, pcp)
                                        ^
include/linux/percpu-defs.h:322:23: note: expanded from macro
'__pcpu_size_call_return'
        case 1: pscr_ret__ = stem##1(variable); break;                  \
                             ^

<scratch space>:357:1: note: expanded from here
raw_cpu_read_1
^
arch/x86/include/asm/percpu.h:394:30: note: expanded from macro 'raw_cpu_read_1'

 #define raw_cpu_read_1(pcp)             percpu_from_op(, "mov", pcp)
                                        ^
arch/x86/include/asm/percpu.h:189:15: note: expanded from macro 'percpu_from_op'
                    : "=q" (pfo_ret__)                  \
                            ^

According to the commit that introduced the "q" constraint, this was needed to fix miscompilation, but it gives no further detail.

Using the normal "=r" constraint seems to work so far.

Fixes 3c598766a2ba ("x86: fix percpu_{to,from}_op()")
Cc Jan Beulich <[email protected]>
arch/x86/include/asm/percpu.h +2 -2
index 2278797c769d..e791fbf4018f 100644
@@ -99,7 +99,7 @@ do {                                                  \
        case 1:                                         \
                asm qual (op "b %1,"__percpu_arg(0)     \
                    : "+m" (var)                        \
-                   : "qi" ((pto_T__)(val)));           \
+                   : "ri" ((pto_T__)(val)));           \
                break;                                  \
        case 2:                                         \
                asm qual (op "w %1,"__percpu_arg(0)     \
@@ -144,7 +144,7 @@ do {
                         \
                else                                                    \
                        asm qual ("addb %1, "__percpu_arg(0)            \
                            : "+m" (var)                                \
-                           : "qi" ((pao_T__)(val)));                   \
+                           : "ri" ((pao_T__)(val)));                   \
                break;                                                  \
        case 2:                                                         \
                if (pao_ID__ == 1)                                      \
arch/x86/include/asm/percpu.h +1 -1
@@ -186,7 +186,7 @@ do {
                         \
        switch (sizeof(var)) {                          \
        case 1:                                         \
                asm qual (op "b "__percpu_arg(1)",%0"   \
-                   : "=q" (pfo_ret__)                  \
+                   : "=r" (pfo_ret__)                  \
                    : "m" (var));                       \
                break;                                  \
        case 2:                                         \
arch/x86/include/asm/percpu.h +1 -1
@@ -215,7 +215,7 @@ do {
                         \
        switch (sizeof(var)) {                          \
        case 1:                                         \
                asm(op "b "__percpu_arg(P1)",%0"        \
-                   : "=q" (pfo_ret__)                  \
+                   : "=r" (pfo_ret__)                  \
                    : "p" (&(var)));                    \
                break;                                  \
        case 2:                                         \
Signature · 2 lines
Regards,
Vincenzo
#7 lore raw
In reply to Arnd Bergmann
Quoted 2 lines · Arnd Bergmann

clang does not like an inline assembly with a "=q" contraint for a 64-bit output:

Seems like starting in GCC 7, GCC may not like it either:
https://godbolt.org/z/UyBUfh
it simply warns then proceeds with code gen. Another difference may come from when GCC vs Clang perform dead code elimination (DCE) vs semantic analysis.

Signature · 2 lines
Thanks,
~Nick Desaulniers
author maintainer
#8 lore raw
Quoted 10 lines · Nick Desaulniers' via Clang Built Linux
Quoted 2 lines · Arnd Bergmann

clang does not like an inline assembly with a "=q" contraint for a 64-bit output:

Seems like starting in GCC 7, GCC may not like it either:
https://godbolt.org/z/UyBUfh
it simply warns then proceeds with code gen. Another difference may come from when GCC vs Clang perform dead code elimination (DCE) vs semantic analysis.

Right, I also had the idea to work around it with a set of __builtin_choos_expr()
instead of the switch()/case but did not complete that patch as the percpu code is rather complex and this would touch lots of code.

Arnd

#9 lore raw
In reply to Arnd Bergmann

On Thu, 11 Jul 2019, Arnd Bergmann wrote:

Trimmed CC list and added Jan

Quoted 30 lines

See below for the patch I am using locally to work around this. That patch is probably wrong, so I have not submitted it yet, but it gives you a clean build ;-)

Arnd
8<---
Subject: [PATCH] x86: percpu: fix clang 32-bit build

clang does not like an inline assembly with a "=q" contraint for a 64-bit output:

arch/x86/events/perf_event.h:824:21: error: invalid output size for constraint '=q'

        u64 disable_mask = __this_cpu_read(cpu_hw_events.perf_ctr_virt_mask);
                           ^
include/linux/percpu-defs.h:447:2: note: expanded from macro '__this_cpu_read'
        raw_cpu_read(pcp);                                              \
        ^
include/linux/percpu-defs.h:421:28: note: expanded from macro 'raw_cpu_read'
 #define raw_cpu_read(pcp)
__pcpu_size_call_return(raw_cpu_read_, pcp)
                                        ^
include/linux/percpu-defs.h:322:23: note: expanded from macro
'__pcpu_size_call_return'
        case 1: pscr_ret__ = stem##1(variable); break;                  \
                             ^

<scratch space>:357:1: note: expanded from here
raw_cpu_read_1
^
arch/x86/include/asm/percpu.h:394:30: note: expanded from macro 'raw_cpu_read_1'

Quoted 8 lines
 #define raw_cpu_read_1(pcp)             percpu_from_op(, "mov", pcp)
                                        ^
arch/x86/include/asm/percpu.h:189:15: note: expanded from macro 'percpu_from_op'
                    : "=q" (pfo_ret__)                  \
                            ^

According to the commit that introduced the "q" constraint, this was needed to fix miscompilation, but it gives no further detail.

Jan, do you have any memory why you added those 'q' constraints? The changelog of 3c598766a2ba is not really helpful.

Thanks,

tglx

Quoted 50 lines

Using the normal "=r" constraint seems to work so far.

Fixes 3c598766a2ba ("x86: fix percpu_{to,from}_op()")
Cc Jan Beulich <[email protected]>
arch/x86/include/asm/percpu.h +2 -2
index 2278797c769d..e791fbf4018f 100644
@@ -99,7 +99,7 @@ do {                                                  \
        case 1:                                         \
                asm qual (op "b %1,"__percpu_arg(0)     \
                    : "+m" (var)                        \
-                   : "qi" ((pto_T__)(val)));           \
+                   : "ri" ((pto_T__)(val)));           \
                break;                                  \
        case 2:                                         \
                asm qual (op "w %1,"__percpu_arg(0)     \
@@ -144,7 +144,7 @@ do {
                         \
                else                                                    \
                        asm qual ("addb %1, "__percpu_arg(0)            \
                            : "+m" (var)                                \
-                           : "qi" ((pao_T__)(val)));                   \
+                           : "ri" ((pao_T__)(val)));                   \
                break;                                                  \
        case 2:                                                         \
                if (pao_ID__ == 1)                                      \
arch/x86/include/asm/percpu.h +1 -1
@@ -186,7 +186,7 @@ do {
                         \
        switch (sizeof(var)) {                          \
        case 1:                                         \
                asm qual (op "b "__percpu_arg(1)",%0"   \
-                   : "=q" (pfo_ret__)                  \
+                   : "=r" (pfo_ret__)                  \
                    : "m" (var));                       \
                break;                                  \
        case 2:                                         \
arch/x86/include/asm/percpu.h +1 -1
@@ -215,7 +215,7 @@ do {
                         \
        switch (sizeof(var)) {                          \
        case 1:                                         \
                asm(op "b "__percpu_arg(P1)",%0"        \
-                   : "=q" (pfo_ret__)                  \
+                   : "=r" (pfo_ret__)                  \
                    : "p" (&(var)));                    \
                break;                                  \
        case 2:                                         \
#10 lore raw
Quoted 40 lines · Thomas Gleixner

On Thu, 11 Jul 2019, Arnd Bergmann wrote:

Trimmed CC list and added Jan

Quoted 36 lines

See below for the patch I am using locally to work around this. That patch is probably wrong, so I have not submitted it yet, but it gives you a clean build ;-)

Arnd
8<---
Subject: [PATCH] x86: percpu: fix clang 32-bit build

clang does not like an inline assembly with a "=q" contraint for a 64-bit output:

arch/x86/events/perf_event.h:824:21: error: invalid output size for constraint '=q'

         u64 disable_mask = __this_cpu_read(cpu_hw_events.perf_ctr_virt_mask);
                            ^
include/linux/percpu-defs.h:447:2: note: expanded from macro '__this_cpu_read'
         raw_cpu_read(pcp);                                              \
         ^
include/linux/percpu-defs.h:421:28: note: expanded from macro 'raw_cpu_read'
  #define raw_cpu_read(pcp)
__pcpu_size_call_return(raw_cpu_read_, pcp)
                                         ^
include/linux/percpu-defs.h:322:23: note: expanded from macro
'__pcpu_size_call_return'
         case 1: pscr_ret__ = stem##1(variable); break;                  \
                              ^

<scratch space>:357:1: note: expanded from here
raw_cpu_read_1
^
arch/x86/include/asm/percpu.h:394:30: note: expanded from macro 'raw_cpu_read_1'

  #define raw_cpu_read_1(pcp)             percpu_from_op(, "mov", pcp)
                                         ^
arch/x86/include/asm/percpu.h:189:15: note: expanded from macro 'percpu_from_op'
                     : "=q" (pfo_ret__)                  \
                             ^
Quoted 5 lines
Quoted 2 lines

According to the commit that introduced the "q" constraint, this was needed to fix miscompilation, but it gives no further detail.

Jan, do you have any memory why you added those 'q' constraints? The changelog of 3c598766a2ba is not really helpful.

"q" was used in that commit exclusively for byte sized operands, simply because that _is_ the constraint to use in such cases. Using "r" is wrong on 32-bit, as it would include inaccessible byte portions of %sp, %bp, %si, and %di. This is how it's described in gcc sources / docs:

"Any register accessible as @code{@var{r}l}. In 32-bit mode, @code{a},
@code{b}, @code{c}, and @code{d}; in 64-bit mode, any integer register."

What I'm struggling with is why clang would evaluate that asm() in the first place when a 64-bit field (perf_ctr_virt_mask) is being accessed.

Jan

author maintainer
#11 lore raw
Quoted 7 lines · Jan Beulich
Quoted 1 line · Thomas Gleixner

On Thu, 11 Jul 2019, Arnd Bergmann wrote:

"q" was used in that commit exclusively for byte sized operands, simply because that _is_ the constraint to use in such cases. Using "r" is wrong on 32-bit, as it would include inaccessible byte portions of %sp, %bp, %si, and %di. This is how it's described in gcc sources / docs:

Quoted 5 lines

"Any register accessible as @code{@var{r}l}. In 32-bit mode, @code{a},
@code{b}, @code{c}, and @code{d}; in 64-bit mode, any integer register."

What I'm struggling with is why clang would evaluate that asm() in the first place when a 64-bit field (perf_ctr_virt_mask) is being accessed.

clang does the optimization and warning checking in a different order, in this case the argument type checks for the inline assembly is done before it eliminates the dead code.

Arnd