Skip to contentSkip to conversation

x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

PATCH Superseded Guo Hui 4 participants 17 messages Jun 23, 2022 – Jun 28, 2022 2 messages not in the archive

Subsystems (4)
lore
Share & tools
lore

Guo Hui

author
#0 lore raw

PATCH 1/1 x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

The instructions assigned to the parameters of
the vcpu_is_preempted function in the X86 architecture physical machine are redundant instructions, which cause the multi-core performance of Unixbench to drop by about 300 to 500 points. The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter assignments in the function osq_lock that call the function vcpu_is_preempted are as follows:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment, causing high cache-misses and degrading performance.

This patch implements the replacement of the above instructions with the nop instruction at system startup.
When the assignment C code changes,
the above assignment instructions may also change accordingly. In order to flexibly replace the instructions generated by the C code, this patch defines the macro ALTERNATIVE_C_CODE
from the ALTERNATIVE macro. Different from the first parameter of the ALTERNATIVE macro, the ALTERNATIVE_C_CODE macro is the first parameter is a C code statement and cannot contain a return statement. To use the macro, for example,
replace the instructions generated by the
C code 'cpu = node->cpu - 1;' with the nop instruction:

OSQ_ALTERNATIVE_C_CODE(cpu = node->cpu - 1, "nop", 0);

The patch effect is as follows two machines,
Unixbench runs with full core score:

1. Machine configuration:
Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
CPU core: 40
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:

System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 944608003.9 80943.3
Double-Precision Whetstone 55.0 212045.8 38553.8
Execl Throughput 43.0 42772.3 9947.1

File Copy 1024 bufsize 2000 maxblocks          3960.0     462656.6   1168.3
File Copy 256 bufsize 500 maxblocks            1655.0     120043.6    725.3
File Copy 4096 bufsize 8000 maxblocks          5800.0    1544525.5   2663.0

Pipe Throughput 12440.0 47277698.5 38004.6
Pipe-based Context Switching 4000.0 1894556.8 4736.4
Process Creation 126.0 86077.0 6831.5
Shell Scripts (1 concurrent) 42.4 70236.3 16565.2
Shell Scripts (8 concurrent) 6.0 8978.1 14963.4
System Call Overhead 15000.0 4691260.0 3127.5

                                                                   ========

System Benchmarks Index Score 7980.9

After using the patch:

System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2253984916.9 193143.5
Double-Precision Whetstone 55.0 438940.3 79807.3
Execl Throughput 43.0 10720.3 2493.1

File Copy 1024 bufsize 2000 maxblocks          3960.0     312233.0    788.5
File Copy 256 bufsize 500 maxblocks            1655.0      80050.9    483.7
File Copy 4096 bufsize 8000 maxblocks          5800.0    1036101.7   1786.4

Pipe Throughput 12440.0 117700315.3 94614.4
Pipe-based Context Switching 4000.0 8421909.8 21054.8
Process Creation 126.0 36742.0 2916.0
Shell Scripts (1 concurrent) 42.4 52846.2 12463.7
Shell Scripts (8 concurrent) 6.0 7058.1 11763.6
System Call Overhead 15000.0 6791548.2 4527.7

                                                                   ========

System Benchmarks Index Score 8260.6

2. Machine configuration:
Hygon C86 7185 32-core Processor
CPU core: 128
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:

System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2256283941.6 193340.5
Double-Precision Whetstone 55.0 439577.3 79923.2
Execl Throughput 43.0 10013.6 2328.7

File Copy 1024 bufsize 2000 maxblocks          3960.0     278121.5    702.3
File Copy 256 bufsize 500 maxblocks            1655.0      71835.5    434.1
File Copy 4096 bufsize 8000 maxblocks          5800.0     905654.2   1561.5

Pipe Throughput 12440.0 117715166.2 94626.3
Pipe-based Context Switching 4000.0 7731331.7 19328.3
Process Creation 126.0 30157.8 2393.5
Shell Scripts (1 concurrent) 42.4 48670.8 11479.0
Shell Scripts (8 concurrent) 6.0 6595.6 10992.7
System Call Overhead 15000.0 6766475.9 4511.0

                                                                   ========

System Benchmarks Index Score 7688.7

After using the patch:

System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2253984916.9 193143.5
Double-Precision Whetstone 55.0 438940.3 79807.3
Execl Throughput 43.0 10720.3 2493.1

File Copy 1024 bufsize 2000 maxblocks          3960.0     312233.0    788.5
File Copy 256 bufsize 500 maxblocks            1655.0      80050.9    483.7
File Copy 4096 bufsize 8000 maxblocks          5800.0    1036101.7   1786.4

Pipe Throughput 12440.0 117700315.3 94614.4
Pipe-based Context Switching 4000.0 8421909.8 21054.8
Process Creation 126.0 36742.0 2916.0
Shell Scripts (1 concurrent) 42.4 52846.2 12463.7
Shell Scripts (8 concurrent) 6.0 7058.1 11763.6
System Call Overhead 15000.0 6791548.2 4527.7

                                                                   ========

System Benchmarks Index Score 8260.6

---
 arch/x86/include/asm/alternative.h | 15 +++++++++++++++
 arch/x86/kernel/alternative.c      | 11 +++++++++++
 include/linux/osq_lock.h           |  7 +++++++
 kernel/locking/osq_lock.c          |  6 +++++-
 4 files changed, 38 insertions(+), 1 deletion(-)
arch/x86/include/asm/alternative.h +15 -0
index 9b10c8c76..5979ebe89 100644
@@ -167,6 +167,21 @@ static inline int alternatives_text_reserved(void *start, void *end)
 	ALTINSTR_REPLACEMENT(newinstr, 1)				\
 	".popsection\n"
 
+/* alternative c code primitive: */
+#define ALTERNATIVE_C_CODE(oldinstr, newinstr, feature)			\
+do {									\
+	asm volatile("661:\n\t");					\
+	oldinstr;							\
+	asm volatile("\n662:\n"						\
+		alt_end_marker ":\n"					\
+		".pushsection .altinstructions,\"a\"\n"			\
+		ALTINSTR_ENTRY(feature, 1)				\
+		".popsection\n"						\
+		".pushsection .altinstr_replacement, \"ax\"\n"		\
+		ALTINSTR_REPLACEMENT(newinstr, 1)			\
+		".popsection\n");					\
+} while (0)
+
 #define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
 	OLDINSTR_2(oldinstr, 1, 2)					\
 	".pushsection .altinstructions,\"a\"\n"				\
arch/x86/kernel/alternative.c +11 -0
index e257f6c80..cf77be884 100644
@@ -251,6 +251,8 @@ static void __init_or_module noinline optimize_nops(u8 *instr, size_t len)
 	}
 }
 
+extern bool pv_is_native_vcpu_is_preempted(void);
+
 /*
  * Replace instructions with better alternatives for this CPU type. This runs
  * before SMP is initialized to avoid SMP problems with self modifying code.
@@ -285,6 +287,15 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
 
 		instr = (u8 *)&a->instr_offset + a->instr_offset;
 		replacement = (u8 *)&a->repl_offset + a->repl_offset;
+
+		if (*replacement == 0x90 && a->replacementlen == 1) {
+#if defined(CONFIG_PARAVIRT_SPINLOCKS)
+			if (pv_is_native_vcpu_is_preempted())
+				add_nops(instr, a->instrlen);
+#endif
+			continue;
+		}
+
 		BUG_ON(a->instrlen > sizeof(insn_buff));
 		BUG_ON(feature >= (NCAPINTS + NBUGINTS) * 32);
 
include/linux/osq_lock.h +7 -0
index 5581dbd3b..ee960e3aa 100644
@@ -38,4 +38,11 @@ static inline bool osq_is_locked(struct optimistic_spin_queue *lock)
 	return atomic_read(&lock->tail) != OSQ_UNLOCKED_VAL;
 }
 
+#ifdef ALTERNATIVE_C_CODE
+#define OSQ_ALTERNATIVE_C_CODE(oldinstr, newinstr, feature) \
+		ALTERNATIVE_C_CODE(oldinstr, newinstr, feature)
+#else
+#define OSQ_ALTERNATIVE_C_CODE(oldinstr, newinstr, feature) oldinstr
+#endif
+
 #endif
kernel/locking/osq_lock.c +5 -1
index d5610ad52..bbe7d640c 100644
@@ -24,7 +24,11 @@ static inline int encode_cpu(int cpu_nr)
 
 static inline int node_cpu(struct optimistic_spin_node *node)
 {
-	return node->cpu - 1;
+	int cpu = 0;
+
+	OSQ_ALTERNATIVE_C_CODE(cpu = node->cpu - 1, "nop", 0);
+
+	return cpu;
 }
 
 static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
Signature · 1 line
2.20.1
reviewer
#1 lore raw
Quoted 168 lines · Guo Hui

The instructions assigned to the parameters of
the vcpu_is_preempted function in the X86 architecture physical machine are redundant instructions, which cause the multi-core performance of Unixbench to drop by about 300 to 500 points. The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter assignments in the function osq_lock that call the function vcpu_is_preempted are as follows:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment, causing high cache-misses and degrading performance.

This patch implements the replacement of the above instructions with the nop instruction at system startup.
When the assignment C code changes,
the above assignment instructions may also change accordingly. In order to flexibly replace the instructions generated by the C code, this patch defines the macro ALTERNATIVE_C_CODE
from the ALTERNATIVE macro. Different from the first parameter of the ALTERNATIVE macro, the ALTERNATIVE_C_CODE macro is the first parameter is a C code statement and cannot contain a return statement. To use the macro, for example,
replace the instructions generated by the
C code 'cpu = node->cpu - 1;' with the nop instruction:

OSQ_ALTERNATIVE_C_CODE(cpu = node->cpu - 1, "nop", 0);

The patch effect is as follows two machines,
Unixbench runs with full core score:

1. Machine configuration:
Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
CPU core: 40
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:

System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 944608003.9 80943.3
Double-Precision Whetstone 55.0 212045.8 38553.8
Execl Throughput 43.0 42772.3 9947.1

File Copy 1024 bufsize 2000 maxblocks          3960.0     462656.6   1168.3
File Copy 256 bufsize 500 maxblocks            1655.0     120043.6    725.3
File Copy 4096 bufsize 8000 maxblocks          5800.0    1544525.5   2663.0

Pipe Throughput 12440.0 47277698.5 38004.6
Pipe-based Context Switching 4000.0 1894556.8 4736.4
Process Creation 126.0 86077.0 6831.5
Shell Scripts (1 concurrent) 42.4 70236.3 16565.2
Shell Scripts (8 concurrent) 6.0 8978.1 14963.4
System Call Overhead 15000.0 4691260.0 3127.5

                                                                    ========

System Benchmarks Index Score 7980.9

After using the patch:

System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2253984916.9 193143.5
Double-Precision Whetstone 55.0 438940.3 79807.3
Execl Throughput 43.0 10720.3 2493.1

File Copy 1024 bufsize 2000 maxblocks          3960.0     312233.0    788.5
File Copy 256 bufsize 500 maxblocks            1655.0      80050.9    483.7
File Copy 4096 bufsize 8000 maxblocks          5800.0    1036101.7   1786.4

Pipe Throughput 12440.0 117700315.3 94614.4
Pipe-based Context Switching 4000.0 8421909.8 21054.8
Process Creation 126.0 36742.0 2916.0
Shell Scripts (1 concurrent) 42.4 52846.2 12463.7
Shell Scripts (8 concurrent) 6.0 7058.1 11763.6
System Call Overhead 15000.0 6791548.2 4527.7

                                                                    ========

System Benchmarks Index Score 8260.6

2. Machine configuration:
Hygon C86 7185 32-core Processor
CPU core: 128
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:

System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2256283941.6 193340.5
Double-Precision Whetstone 55.0 439577.3 79923.2
Execl Throughput 43.0 10013.6 2328.7

File Copy 1024 bufsize 2000 maxblocks          3960.0     278121.5    702.3
File Copy 256 bufsize 500 maxblocks            1655.0      71835.5    434.1
File Copy 4096 bufsize 8000 maxblocks          5800.0     905654.2   1561.5

Pipe Throughput 12440.0 117715166.2 94626.3
Pipe-based Context Switching 4000.0 7731331.7 19328.3
Process Creation 126.0 30157.8 2393.5
Shell Scripts (1 concurrent) 42.4 48670.8 11479.0
Shell Scripts (8 concurrent) 6.0 6595.6 10992.7
System Call Overhead 15000.0 6766475.9 4511.0

                                                                    ========

System Benchmarks Index Score 7688.7

After using the patch:

System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2253984916.9 193143.5
Double-Precision Whetstone 55.0 438940.3 79807.3
Execl Throughput 43.0 10720.3 2493.1

File Copy 1024 bufsize 2000 maxblocks          3960.0     312233.0    788.5
File Copy 256 bufsize 500 maxblocks            1655.0      80050.9    483.7
File Copy 4096 bufsize 8000 maxblocks          5800.0    1036101.7   1786.4

Pipe Throughput 12440.0 117700315.3 94614.4
Pipe-based Context Switching 4000.0 8421909.8 21054.8
Process Creation 126.0 36742.0 2916.0
Shell Scripts (1 concurrent) 42.4 52846.2 12463.7
Shell Scripts (8 concurrent) 6.0 7058.1 11763.6
System Call Overhead 15000.0 6791548.2 4527.7

                                                                    ========

System Benchmarks Index Score 8260.6

---
  arch/x86/include/asm/alternative.h | 15 +++++++++++++++
  arch/x86/kernel/alternative.c      | 11 +++++++++++
  include/linux/osq_lock.h           |  7 +++++++
  kernel/locking/osq_lock.c          |  6 +++++-
  4 files changed, 38 insertions(+), 1 deletion(-)
arch/x86/include/asm/alternative.h +15 -0
index 9b10c8c76..5979ebe89 100644
@@ -167,6 +167,21 @@ static inline int alternatives_text_reserved(void *start, void *end)
  	ALTINSTR_REPLACEMENT(newinstr, 1)				\
  	".popsection\n"
 
+/* alternative c code primitive: */
+#define ALTERNATIVE_C_CODE(oldinstr, newinstr, feature)			\
+do {									\
+	asm volatile("661:\n\t");					\
+	oldinstr;							\
+	asm volatile("\n662:\n"						\
+		alt_end_marker ":\n"					\
+		".pushsection .altinstructions,\"a\"\n"			\
+		ALTINSTR_ENTRY(feature, 1)				\
+		".popsection\n"						\
+		".pushsection .altinstr_replacement, \"ax\"\n"		\
+		ALTINSTR_REPLACEMENT(newinstr, 1)			\
+		".popsection\n");					\
+} while (0)
+
  #define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
  	OLDINSTR_2(oldinstr, 1, 2)					\
  	".pushsection .altinstructions,\"a\"\n"				\
arch/x86/kernel/alternative.c +2 -0
index e257f6c80..cf77be884 100644
@@ -251,6 +251,8 @@ static void __init_or_module noinline optimize_nops(u8 *instr, size_t len)
  	}
  }
 
+extern bool pv_is_native_vcpu_is_preempted(void);
+
  /*
   * Replace instructions with better alternatives for this CPU type. This runs
   * before SMP is initialized to avoid SMP problems with self modifying code.
@@ -285,6 +287,15 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
 
  		instr = (u8 *)&a->instr_offset + a->instr_offset;

arch/x86/kernel/alternative.c void __init_or_module noinline apply_alternatives(struct alt_instr *start, · line 298

Quoted 10 lines
arch/x86/kernel/alternative.c +9 -0
  		replacement = (u8 *)&a->repl_offset + a->repl_offset;
+
+		if (*replacement == 0x90 && a->replacementlen == 1) {
+#if defined(CONFIG_PARAVIRT_SPINLOCKS)
+			if (pv_is_native_vcpu_is_preempted())
+				add_nops(instr, a->instrlen);
+#endif
+			continue;
+		}
+

This is hacky and it may incorrectly affect other alternatives that patches thing to nop.

Quoted 24 lines
arch/x86/kernel/alternative.c +0 -0
  		BUG_ON(a->instrlen > sizeof(insn_buff));
  		BUG_ON(feature >= (NCAPINTS + NBUGINTS) * 32);
 
include/linux/osq_lock.h +7 -0
index 5581dbd3b..ee960e3aa 100644
@@ -38,4 +38,11 @@ static inline bool osq_is_locked(struct optimistic_spin_queue *lock)
  	return atomic_read(&lock->tail) != OSQ_UNLOCKED_VAL;
  }
 
+#ifdef ALTERNATIVE_C_CODE
+#define OSQ_ALTERNATIVE_C_CODE(oldinstr, newinstr, feature) \
+		ALTERNATIVE_C_CODE(oldinstr, newinstr, feature)
+#else
+#define OSQ_ALTERNATIVE_C_CODE(oldinstr, newinstr, feature) oldinstr
+#endif
+
  #endif
kernel/locking/osq_lock.c +0 -0
index d5610ad52..bbe7d640c 100644

kernel/locking/osq_lock.c static inline int encode_cpu(int cpu_nr) · line 32

Quoted 10 lines
kernel/locking/osq_lock.c +5 -1
@@ -24,7 +24,11 @@ static inline int encode_cpu(int cpu_nr)
 
  static inline int node_cpu(struct optimistic_spin_node *node)
  {
-	return node->cpu - 1;
+	int cpu = 0;
+
+	OSQ_ALTERNATIVE_C_CODE(cpu = node->cpu - 1, "nop", 0);
+
+	return cpu;
  }

Why don't you use static_key to control the alternative and add a late_initcall() to set it. I think that will be simpler and you can throw away ALTERNATIVE_C_CODE().

Cheers,
Longman

author
#2 lore raw

PATCH v2 1/1 x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

The instructions assigned to the vcpu_is_preempted function parameter in the X86 architecture physical machine are redundant instructions, causing the multi-core performance of Unixbench to drop by about 4% to 5%. The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter 'vcpu' in the function osq_lock
that calls the function vcpu_is_preempted is assigned as follows:

The C code is in the function node_cpu:
cpu = node->cpu - 1;

The instructions corresponding to the C code are:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment, causing high cache-misses and degrading performance.

This patch uses static_key to not execute this instruction in the Native runtime environment.

The code is as follows:

DEFINE_STATIC_KEY_FALSE(preemted_key);

static inline int node_cpu(struct optimistic_spin_node *node)

{
     int cpu = 0;

     if (!static_branch_unlikely(&preemted_key))
             cpu = node->cpu - 1;

     return cpu;
}

The patch effect is as follows two machines,
Unixbench runs with full core score:

1. Machine configuration:
Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
CPU core: 40
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  948326591.2  81261.9
Double-Precision Whetstone                       55.0     211986.3  38543.0
Execl Throughput                                 43.0      43453.2  10105.4
File Copy 1024 bufsize 2000 maxblocks          3960.0     438936.2   1108.4
File Copy 256 bufsize 500 maxblocks            1655.0     118197.4    714.2
File Copy 4096 bufsize 8000 maxblocks          5800.0    1534674.7   2646.0

Pipe Throughput 12440.0 46482107.6 37365.0
Pipe-based Context Switching 4000.0 1915094.2 4787.7
Process Creation 126.0 85442.2 6781.1
Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
System Call Overhead 15000.0 4714906.1 3143.3

                                                                   ========

System Benchmarks Index Score 7923.3

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  947032915.5  81151.1
Double-Precision Whetstone                       55.0     211971.2  38540.2
Execl Throughput                                 43.0      45054.8  10477.9
File Copy 1024 bufsize 2000 maxblocks          3960.0     515024.9   1300.6
File Copy 256 bufsize 500 maxblocks            1655.0     146354.6    884.3
File Copy 4096 bufsize 8000 maxblocks          5800.0    1679995.9   2896.5

Pipe Throughput 12440.0 46466394.2 37352.4
Pipe-based Context Switching 4000.0 1898221.4 4745.6
Process Creation 126.0 85653.1 6797.9
Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
System Call Overhead 15000.0 4658746.7 3105.8

                                                                   ========

System Benchmarks Index Score 8248.8

2. Machine configuration:
Hygon C86 7185 32-core Processor
CPU core: 128
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
Double-Precision Whetstone 55.0 438969.9 79812.7
Execl Throughput 43.0 10108.6 2350.8

File Copy 1024 bufsize 2000 maxblocks          3960.0     275892.8    696.7
File Copy 256 bufsize 500 maxblocks            1655.0      72082.7    435.5
File Copy 4096 bufsize 8000 maxblocks          5800.0     925043.4   1594.9

Pipe Throughput 12440.0 118905512.5 95583.2
Pipe-based Context Switching 4000.0 7820945.7 19552.4
Process Creation 126.0 31233.3 2478.8
Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
System Call Overhead 15000.0 6816047.5 4544.0

                                                                   ========

System Benchmarks Index Score 7756.6

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
Double-Precision Whetstone 55.0 451847.2 82154.0
Execl Throughput 43.0 10595.1 2464.0

File Copy 1024 bufsize 2000 maxblocks          3960.0     301279.3    760.8
File Copy 256 bufsize 500 maxblocks            1655.0      79291.3    479.1
File Copy 4096 bufsize 8000 maxblocks          5800.0    1039755.2   1792.7

Pipe Throughput 12440.0 118701468.1 95419.2
Pipe-based Context Switching 4000.0 8073453.3 20183.6
Process Creation 126.0 33440.9 2654.0
Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
System Call Overhead 15000.0 6834371.5 4556.2

                                                                   ========

System Benchmarks Index Score 8157.8

---
 arch/x86/kernel/paravirt-spinlocks.c | 4 ++++
 kernel/locking/osq_lock.c            | 9 ++++++++-
 2 files changed, 12 insertions(+), 1 deletion(-)
arch/x86/kernel/paravirt-spinlocks.c +4 -0
index 9e1ea99ad..7a55f8407 100644
@@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
 		__raw_callee_save___native_vcpu_is_preempted;
 }
 
+DECLARE_STATIC_KEY_FALSE(preemted_key);
+
 void __init paravirt_set_cap(void)
 {
 	if (!pv_is_native_spin_unlock())
@@ -40,4 +42,6 @@ void __init paravirt_set_cap(void)
 
 	if (!pv_is_native_vcpu_is_preempted())
 		setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
+	else
+		static_branch_enable(&preemted_key);
 }
kernel/locking/osq_lock.c +8 -1
index d5610ad52..a8798e701 100644
@@ -22,9 +22,16 @@ static inline int encode_cpu(int cpu_nr)
 	return cpu_nr + 1;
 }
 
+DEFINE_STATIC_KEY_FALSE(preemted_key);
+
 static inline int node_cpu(struct optimistic_spin_node *node)
 {
-	return node->cpu - 1;
+	int cpu = 0;
+
+	if (!static_branch_unlikely(&preemted_key))
+		cpu = node->cpu - 1;
+
+	return cpu;
 }
 
 static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
Signature · 1 line
2.20.1
reviewer
#3 lore raw
Quoted 158 lines · Guo Hui

The instructions assigned to the vcpu_is_preempted function parameter in the X86 architecture physical machine are redundant instructions, causing the multi-core performance of Unixbench to drop by about 4% to 5%. The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter 'vcpu' in the function osq_lock
that calls the function vcpu_is_preempted is assigned as follows:

The C code is in the function node_cpu:
cpu = node->cpu - 1;

The instructions corresponding to the C code are:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment, causing high cache-misses and degrading performance.

This patch uses static_key to not execute this instruction in the Native runtime environment.

The code is as follows:

DEFINE_STATIC_KEY_FALSE(preemted_key);

static inline int node_cpu(struct optimistic_spin_node *node)

{
      int cpu = 0;

      if (!static_branch_unlikely(&preemted_key))
              cpu = node->cpu - 1;

      return cpu;
}

The patch effect is as follows two machines,
Unixbench runs with full core score:

1. Machine configuration:
Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
CPU core: 40
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  948326591.2  81261.9
Double-Precision Whetstone                       55.0     211986.3  38543.0
Execl Throughput                                 43.0      43453.2  10105.4
File Copy 1024 bufsize 2000 maxblocks          3960.0     438936.2   1108.4
File Copy 256 bufsize 500 maxblocks            1655.0     118197.4    714.2
File Copy 4096 bufsize 8000 maxblocks          5800.0    1534674.7   2646.0

Pipe Throughput 12440.0 46482107.6 37365.0
Pipe-based Context Switching 4000.0 1915094.2 4787.7
Process Creation 126.0 85442.2 6781.1
Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
System Call Overhead 15000.0 4714906.1 3143.3

                                                                    ========

System Benchmarks Index Score 7923.3

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  947032915.5  81151.1
Double-Precision Whetstone                       55.0     211971.2  38540.2
Execl Throughput                                 43.0      45054.8  10477.9
File Copy 1024 bufsize 2000 maxblocks          3960.0     515024.9   1300.6
File Copy 256 bufsize 500 maxblocks            1655.0     146354.6    884.3
File Copy 4096 bufsize 8000 maxblocks          5800.0    1679995.9   2896.5

Pipe Throughput 12440.0 46466394.2 37352.4
Pipe-based Context Switching 4000.0 1898221.4 4745.6
Process Creation 126.0 85653.1 6797.9
Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
System Call Overhead 15000.0 4658746.7 3105.8

                                                                    ========

System Benchmarks Index Score 8248.8

2. Machine configuration:
Hygon C86 7185 32-core Processor
CPU core: 128
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
Double-Precision Whetstone 55.0 438969.9 79812.7
Execl Throughput 43.0 10108.6 2350.8

File Copy 1024 bufsize 2000 maxblocks          3960.0     275892.8    696.7
File Copy 256 bufsize 500 maxblocks            1655.0      72082.7    435.5
File Copy 4096 bufsize 8000 maxblocks          5800.0     925043.4   1594.9

Pipe Throughput 12440.0 118905512.5 95583.2
Pipe-based Context Switching 4000.0 7820945.7 19552.4
Process Creation 126.0 31233.3 2478.8
Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
System Call Overhead 15000.0 6816047.5 4544.0

                                                                    ========

System Benchmarks Index Score 7756.6

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
Double-Precision Whetstone 55.0 451847.2 82154.0
Execl Throughput 43.0 10595.1 2464.0

File Copy 1024 bufsize 2000 maxblocks          3960.0     301279.3    760.8
File Copy 256 bufsize 500 maxblocks            1655.0      79291.3    479.1
File Copy 4096 bufsize 8000 maxblocks          5800.0    1039755.2   1792.7

Pipe Throughput 12440.0 118701468.1 95419.2
Pipe-based Context Switching 4000.0 8073453.3 20183.6
Process Creation 126.0 33440.9 2654.0
Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
System Call Overhead 15000.0 6834371.5 4556.2

                                                                    ========

System Benchmarks Index Score 8157.8

---
  arch/x86/kernel/paravirt-spinlocks.c | 4 ++++
  kernel/locking/osq_lock.c            | 9 ++++++++-
  2 files changed, 12 insertions(+), 1 deletion(-)
arch/x86/kernel/paravirt-spinlocks.c +4 -0
index 9e1ea99ad..7a55f8407 100644
@@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
  		__raw_callee_save___native_vcpu_is_preempted;
  }
 
+DECLARE_STATIC_KEY_FALSE(preemted_key);
+
  void __init paravirt_set_cap(void)
  {
  	if (!pv_is_native_spin_unlock())
@@ -40,4 +42,6 @@ void __init paravirt_set_cap(void)
 
  	if (!pv_is_native_vcpu_is_preempted())
  		setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
+	else
+		static_branch_enable(&preemted_key);
  }
kernel/locking/osq_lock.c +2 -0
index d5610ad52..a8798e701 100644
@@ -22,9 +22,16 @@ static inline int encode_cpu(int cpu_nr)
  	return cpu_nr + 1;
  }
 
+DEFINE_STATIC_KEY_FALSE(preemted_key);
+
  static inline int node_cpu(struct optimistic_spin_node *node)
  {

kernel/locking/osq_lock.c static inline int encode_cpu(int cpu_nr) · line 37

Quoted 10 lines
kernel/locking/osq_lock.c +6 -1
-	return node->cpu - 1;
+	int cpu = 0;
+
+	if (!static_branch_unlikely(&preemted_key))
+		cpu = node->cpu - 1;
+
+	return cpu;
  }
 
  static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)

This looks much better now.

However vcpu_is_preempted() is not just used by x86, it is also used in arm64, powerpc and s390. If you want to default disable node_cpu(), you have to either make the necessary changes in all those places or you have to change it a way that won't affect other arches while disabling it for the x86 case.

Cheers,
Longman

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

A message that is not in the archive (sent off-list or not yet received): [email protected]

reviewer
#5 lore raw
Quoted 13 lines · Guo Hui

Thank you very much Longman, my patch is as you said, only disable node_cpu on X86, enable node_cpu on arm64, powerpc, s390 architectures; the code is in file arch/x86/kernel/paravirt-spinlocks.c:
    DECLARE_STATIC_KEY_FALSE(preemted_key);
    static_branch_enable(&preemted_key);

the default value of preemted_key is false and the if conditional statement is reversed, the code is in file kernel/locking/osq_lock.c:
    DEFINE_STATIC_KEY_FALSE(preemted_key);

    static inline int node_cpu(struct optimistic_spin_node *node) {
        int cpu = 0;

Quoted 7 lines

        if (!static_branch_unlikely(&preemted_key)) cpu = node->cpu - 1;

        return cpu;
  }

In this way, only one nop instruction is added to architectures arm64, powerpc and s390, including virtual machines, without any other changes.

You are right. I am probably too tired last night to read the patch more carefully.

Cheers,
Longman

maintainer
#6 lore raw
In reply to Guo Hui
Quoted 131 lines · Guo Hui

The instructions assigned to the vcpu_is_preempted function parameter in the X86 architecture physical machine are redundant instructions, causing the multi-core performance of Unixbench to drop by about 4% to 5%. The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter 'vcpu' in the function osq_lock
that calls the function vcpu_is_preempted is assigned as follows:

The C code is in the function node_cpu:
cpu = node->cpu - 1;

The instructions corresponding to the C code are:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment, causing high cache-misses and degrading performance.

This patch uses static_key to not execute this instruction in the Native runtime environment.

The code is as follows:

DEFINE_STATIC_KEY_FALSE(preemted_key);

static inline int node_cpu(struct optimistic_spin_node *node)

{
      int cpu = 0;

      if (!static_branch_unlikely(&preemted_key))
              cpu = node->cpu - 1;

      return cpu;
}

The patch effect is as follows two machines,
Unixbench runs with full core score:

1. Machine configuration:
Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
CPU core: 40
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  948326591.2  81261.9
Double-Precision Whetstone                       55.0     211986.3  38543.0
Execl Throughput                                 43.0      43453.2  10105.4
File Copy 1024 bufsize 2000 maxblocks          3960.0     438936.2   1108.4
File Copy 256 bufsize 500 maxblocks            1655.0     118197.4    714.2
File Copy 4096 bufsize 8000 maxblocks          5800.0    1534674.7   2646.0

Pipe Throughput 12440.0 46482107.6 37365.0
Pipe-based Context Switching 4000.0 1915094.2 4787.7
Process Creation 126.0 85442.2 6781.1
Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
System Call Overhead 15000.0 4714906.1 3143.3

                                                                    ========

System Benchmarks Index Score 7923.3

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  947032915.5  81151.1
Double-Precision Whetstone                       55.0     211971.2  38540.2
Execl Throughput                                 43.0      45054.8  10477.9
File Copy 1024 bufsize 2000 maxblocks          3960.0     515024.9   1300.6
File Copy 256 bufsize 500 maxblocks            1655.0     146354.6    884.3
File Copy 4096 bufsize 8000 maxblocks          5800.0    1679995.9   2896.5

Pipe Throughput 12440.0 46466394.2 37352.4
Pipe-based Context Switching 4000.0 1898221.4 4745.6
Process Creation 126.0 85653.1 6797.9
Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
System Call Overhead 15000.0 4658746.7 3105.8

                                                                    ========

System Benchmarks Index Score 8248.8

2. Machine configuration:
Hygon C86 7185 32-core Processor
CPU core: 128
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
Double-Precision Whetstone 55.0 438969.9 79812.7
Execl Throughput 43.0 10108.6 2350.8

File Copy 1024 bufsize 2000 maxblocks          3960.0     275892.8    696.7
File Copy 256 bufsize 500 maxblocks            1655.0      72082.7    435.5
File Copy 4096 bufsize 8000 maxblocks          5800.0     925043.4   1594.9

Pipe Throughput 12440.0 118905512.5 95583.2
Pipe-based Context Switching 4000.0 7820945.7 19552.4
Process Creation 126.0 31233.3 2478.8
Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
System Call Overhead 15000.0 6816047.5 4544.0

                                                                    ========

System Benchmarks Index Score 7756.6

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
Double-Precision Whetstone 55.0 451847.2 82154.0
Execl Throughput 43.0 10595.1 2464.0

File Copy 1024 bufsize 2000 maxblocks          3960.0     301279.3    760.8
File Copy 256 bufsize 500 maxblocks            1655.0      79291.3    479.1
File Copy 4096 bufsize 8000 maxblocks          5800.0    1039755.2   1792.7

Pipe Throughput 12440.0 118701468.1 95419.2
Pipe-based Context Switching 4000.0 8073453.3 20183.6
Process Creation 126.0 33440.9 2654.0
Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
System Call Overhead 15000.0 6834371.5 4556.2

                                                                    ========

System Benchmarks Index Score 8157.8

---
  arch/x86/kernel/paravirt-spinlocks.c | 4 ++++
  kernel/locking/osq_lock.c            | 9 ++++++++-
  2 files changed, 12 insertions(+), 1 deletion(-)
arch/x86/kernel/paravirt-spinlocks.c +0 -0
index 9e1ea99ad..7a55f8407 100644

arch/x86/kernel/paravirt-spinlocks.c bool pv_is_native_vcpu_is_preempted(void) · line 36

Quoted 4 lines
arch/x86/kernel/paravirt-spinlocks.c +1 -0
@@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
  		__raw_callee_save___native_vcpu_is_preempted;
  }
 
+DECLARE_STATIC_KEY_FALSE(preemted_key);

Please name it "preempted_key".

Juergen

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#7 lore raw

PATCH v3 1/1 x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

The instructions assigned to the vcpu_is_preempted function parameter in the X86 architecture physical machine are redundant instructions, causing the multi-core performance of Unixbench to drop by about 4% to 5%. The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter 'vcpu' in the function osq_lock
that calls the function vcpu_is_preempted is assigned as follows:

The C code is in the function node_cpu:
cpu = node->cpu - 1;

The instructions corresponding to the C code are:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment, causing high cache-misses and degrading performance.

This patch uses static_key to not execute this instruction in the Native runtime environment.

The code is as follows:

DEFINE_STATIC_KEY_FALSE(preempted_key);

static inline int node_cpu(struct optimistic_spin_node *node)

{
     int cpu = 0;

     if (!static_branch_unlikely(&preempted_key))
             cpu = node->cpu - 1;

     return cpu;
}

The patch effect is as follows two machines,
Unixbench runs with full core score:

1. Machine configuration:
Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
CPU core: 40
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  948326591.2  81261.9
Double-Precision Whetstone                       55.0     211986.3  38543.0
Execl Throughput                                 43.0      43453.2  10105.4
File Copy 1024 bufsize 2000 maxblocks          3960.0     438936.2   1108.4
File Copy 256 bufsize 500 maxblocks            1655.0     118197.4    714.2
File Copy 4096 bufsize 8000 maxblocks          5800.0    1534674.7   2646.0

Pipe Throughput 12440.0 46482107.6 37365.0
Pipe-based Context Switching 4000.0 1915094.2 4787.7
Process Creation 126.0 85442.2 6781.1
Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
System Call Overhead 15000.0 4714906.1 3143.3

                                                                   ========

System Benchmarks Index Score 7923.3

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  947032915.5  81151.1
Double-Precision Whetstone                       55.0     211971.2  38540.2
Execl Throughput                                 43.0      45054.8  10477.9
File Copy 1024 bufsize 2000 maxblocks          3960.0     515024.9   1300.6
File Copy 256 bufsize 500 maxblocks            1655.0     146354.6    884.3
File Copy 4096 bufsize 8000 maxblocks          5800.0    1679995.9   2896.5

Pipe Throughput 12440.0 46466394.2 37352.4
Pipe-based Context Switching 4000.0 1898221.4 4745.6
Process Creation 126.0 85653.1 6797.9
Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
System Call Overhead 15000.0 4658746.7 3105.8

                                                                   ========

System Benchmarks Index Score 8248.8

2. Machine configuration:
Hygon C86 7185 32-core Processor
CPU core: 128
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
Double-Precision Whetstone 55.0 438969.9 79812.7
Execl Throughput 43.0 10108.6 2350.8

File Copy 1024 bufsize 2000 maxblocks          3960.0     275892.8    696.7
File Copy 256 bufsize 500 maxblocks            1655.0      72082.7    435.5
File Copy 4096 bufsize 8000 maxblocks          5800.0     925043.4   1594.9

Pipe Throughput 12440.0 118905512.5 95583.2
Pipe-based Context Switching 4000.0 7820945.7 19552.4
Process Creation 126.0 31233.3 2478.8
Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
System Call Overhead 15000.0 6816047.5 4544.0

                                                                   ========

System Benchmarks Index Score 7756.6

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
Double-Precision Whetstone 55.0 451847.2 82154.0
Execl Throughput 43.0 10595.1 2464.0

File Copy 1024 bufsize 2000 maxblocks          3960.0     301279.3    760.8
File Copy 256 bufsize 500 maxblocks            1655.0      79291.3    479.1
File Copy 4096 bufsize 8000 maxblocks          5800.0    1039755.2   1792.7

Pipe Throughput 12440.0 118701468.1 95419.2
Pipe-based Context Switching 4000.0 8073453.3 20183.6
Process Creation 126.0 33440.9 2654.0
Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
System Call Overhead 15000.0 6834371.5 4556.2

                                                                   ========

System Benchmarks Index Score 8157.8

---
 arch/x86/kernel/paravirt-spinlocks.c | 4 ++++
 kernel/locking/osq_lock.c            | 9 ++++++++-
 2 files changed, 12 insertions(+), 1 deletion(-)
arch/x86/kernel/paravirt-spinlocks.c +4 -0
index 9e1ea99ad..7a55f8407 100644
@@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
 		__raw_callee_save___native_vcpu_is_preempted;
 }
 
+DECLARE_STATIC_KEY_FALSE(preempted_key);
+
 void __init paravirt_set_cap(void)
 {
 	if (!pv_is_native_spin_unlock())
@@ -40,4 +42,6 @@ void __init paravirt_set_cap(void)
 
 	if (!pv_is_native_vcpu_is_preempted())
 		setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
+	else
+		static_branch_enable(&preempted_key);
 }
kernel/locking/osq_lock.c +8 -1
index d5610ad52..a8798e701 100644
@@ -22,9 +22,16 @@ static inline int encode_cpu(int cpu_nr)
 	return cpu_nr + 1;
 }
 
+DEFINE_STATIC_KEY_FALSE(preempted_key);
+
 static inline int node_cpu(struct optimistic_spin_node *node)
 {
-	return node->cpu - 1;
+	int cpu = 0;
+
+	if (!static_branch_unlikely(&preempted_key))
+		cpu = node->cpu - 1;
+
+	return cpu;
 }
 
 static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
Signature · 1 line
2.20.1
maintainer
#8 lore raw
In reply to Guo Hui
Quoted 12 lines · Guo Hui

The instructions assigned to the vcpu_is_preempted function parameter in the X86 architecture physical machine are redundant instructions, causing the multi-core performance of Unixbench to drop by about 4% to 5%. The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter 'vcpu' in the function osq_lock
that calls the function vcpu_is_preempted is assigned as follows:

The C code is in the function node_cpu:
cpu = node->cpu - 1;

Quoted 7 lines

The instructions corresponding to the C code are:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment, causing high cache-misses and degrading performance.

The above basically says that argument setup is not patched out and causes significant pain due to a cache-miss.

Quoted 20 lines
---
 arch/x86/kernel/paravirt-spinlocks.c | 4 ++++
 kernel/locking/osq_lock.c            | 9 ++++++++-
 2 files changed, 12 insertions(+), 1 deletion(-)
arch/x86/kernel/paravirt-spinlocks.c +2 -0
index 9e1ea99ad..7a55f8407 100644
@@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
 		__raw_callee_save___native_vcpu_is_preempted;
 }
 
+DECLARE_STATIC_KEY_FALSE(preemted_key);
+
 void __init paravirt_set_cap(void)
 {
 	if (!pv_is_native_spin_unlock())

arch/x86/kernel/paravirt-spinlocks.c void __init paravirt_set_cap(void) · line 47

Quoted 6 lines
arch/x86/kernel/paravirt-spinlocks.c +2 -0
@@ -40,4 +42,6 @@ void __init paravirt_set_cap(void)
 
 	if (!pv_is_native_vcpu_is_preempted())
 		setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
+	else
+		static_branch_enable(&preemted_key);
 }

At least for x86 it makes sense to have the static_key default the other way around. That is, enable it along with vcpu_is_preempted().

Quoted 10 lines
kernel/locking/osq_lock.c +2 -0
index d5610ad52..a8798e701 100644
@@ -22,9 +22,16 @@ static inline int encode_cpu(int cpu_nr)
 	return cpu_nr + 1;
 }
 
+DEFINE_STATIC_KEY_FALSE(preemted_key);
+

kernel/locking/osq_lock.c static inline int encode_cpu(int cpu_nr) · line 35

Quoted 10 lines
kernel/locking/osq_lock.c +6 -1
 static inline int node_cpu(struct optimistic_spin_node *node)
 {
-	return node->cpu - 1;
+	int cpu = 0;
+
+	if (!static_branch_unlikely(&preemted_key))
+		cpu = node->cpu - 1;
+
+	return cpu;
 }

Would not something like:

static inline bool
vcpu_is_preempted_node(struct optimistic_spin_node *node)

{
	if (!static_branch_unlikely(&vcpu_has_preemption))
		return false;

	return vcpu_is_preempted(node_cpu(node->prev));
}

And then use that like:

	if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
				  vcpu_is_preempted_node(node)))

Not generate better code still?
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#11 lore raw

PATCH v4 1/1 x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

In reply to Peter Zijlstra

The instructions assigned to the vcpu_is_preempted function parameter in the X86 architecture physical machine are redundant instructions, causing the multi-core performance of Unixbench to drop by about 4% to 5%. The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter 'vcpu' in the function osq_lock
that calls the function vcpu_is_preempted is assigned as follows:

The C code is in the function node_cpu:
cpu = node->cpu - 1;

The instructions corresponding to the C code are:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment, causing high cache-misses and degrading performance.

This patch uses static_key to not execute this instruction in the Native runtime environment.

The patch effect is as follows two machines,
Unixbench runs with full core score:

1. Machine configuration:
Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
CPU core: 40
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  948326591.2  81261.9
Double-Precision Whetstone                       55.0     211986.3  38543.0
Execl Throughput                                 43.0      43453.2  10105.4
File Copy 1024 bufsize 2000 maxblocks          3960.0     438936.2   1108.4
File Copy 256 bufsize 500 maxblocks            1655.0     118197.4    714.2
File Copy 4096 bufsize 8000 maxblocks          5800.0    1534674.7   2646.0

Pipe Throughput 12440.0 46482107.6 37365.0
Pipe-based Context Switching 4000.0 1915094.2 4787.7
Process Creation 126.0 85442.2 6781.1
Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
System Call Overhead 15000.0 4714906.1 3143.3

                                                                   ========

System Benchmarks Index Score 7923.3

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  947032915.5  81151.1
Double-Precision Whetstone                       55.0     211971.2  38540.2
Execl Throughput                                 43.0      45054.8  10477.9
File Copy 1024 bufsize 2000 maxblocks          3960.0     515024.9   1300.6
File Copy 256 bufsize 500 maxblocks            1655.0     146354.6    884.3
File Copy 4096 bufsize 8000 maxblocks          5800.0    1679995.9   2896.5

Pipe Throughput 12440.0 46466394.2 37352.4
Pipe-based Context Switching 4000.0 1898221.4 4745.6
Process Creation 126.0 85653.1 6797.9
Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
System Call Overhead 15000.0 4658746.7 3105.8

                                                                   ========

System Benchmarks Index Score 8248.8

2. Machine configuration:
Hygon C86 7185 32-core Processor
CPU core: 128
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
Double-Precision Whetstone 55.0 438969.9 79812.7
Execl Throughput 43.0 10108.6 2350.8

File Copy 1024 bufsize 2000 maxblocks          3960.0     275892.8    696.7
File Copy 256 bufsize 500 maxblocks            1655.0      72082.7    435.5
File Copy 4096 bufsize 8000 maxblocks          5800.0     925043.4   1594.9

Pipe Throughput 12440.0 118905512.5 95583.2
Pipe-based Context Switching 4000.0 7820945.7 19552.4
Process Creation 126.0 31233.3 2478.8
Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
System Call Overhead 15000.0 6816047.5 4544.0

                                                                   ========

System Benchmarks Index Score 7756.6

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
Double-Precision Whetstone 55.0 451847.2 82154.0
Execl Throughput 43.0 10595.1 2464.0

File Copy 1024 bufsize 2000 maxblocks          3960.0     301279.3    760.8
File Copy 256 bufsize 500 maxblocks            1655.0      79291.3    479.1
File Copy 4096 bufsize 8000 maxblocks          5800.0    1039755.2   1792.7

Pipe Throughput 12440.0 118701468.1 95419.2
Pipe-based Context Switching 4000.0 8073453.3 20183.6
Process Creation 126.0 33440.9 2654.0
Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
System Call Overhead 15000.0 6834371.5 4556.2

                                                                   ========

System Benchmarks Index Score 8157.8

---
 arch/x86/kernel/paravirt-spinlocks.c |  4 ++++
 kernel/locking/osq_lock.c            | 12 +++++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)
arch/x86/kernel/paravirt-spinlocks.c +4 -0
index 9e1ea99ad..a2eb375e2 100644
@@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
 		__raw_callee_save___native_vcpu_is_preempted;
 }
 
+DECLARE_STATIC_KEY_TRUE(vcpu_has_preemption);
+
 void __init paravirt_set_cap(void)
 {
 	if (!pv_is_native_spin_unlock())
@@ -40,4 +42,6 @@ void __init paravirt_set_cap(void)
 
 	if (!pv_is_native_vcpu_is_preempted())
 		setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
+	else
+		static_branch_disable(&vcpu_has_preemption);
 }
kernel/locking/osq_lock.c +11 -1
index d5610ad52..adb41080d 100644
@@ -27,6 +27,16 @@ static inline int node_cpu(struct optimistic_spin_node *node)
 	return node->cpu - 1;
 }
 
+DEFINE_STATIC_KEY_TRUE(vcpu_has_preemption);
+
+static inline bool vcpu_is_preempted_node(struct optimistic_spin_node *node)
+{
+	if (!static_branch_unlikely(&vcpu_has_preemption))
+		return false;
+
+	return vcpu_is_preempted(node_cpu(node->prev));
+}
+
 static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
 {
 	int cpu_nr = encoded_cpu_val - 1;
@@ -141,7 +151,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
 	 * polling, be careful.
 	 */
 	if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
-				  vcpu_is_preempted(node_cpu(node->prev))))
+						vcpu_is_preempted_node(node)))
 		return true;
 
 	/* unqueue */
Signature · 1 line
2.20.1
maintainer Reviewed-by on v4 1/1
#12 lore raw
Quoted 103 lines · Guo Hui

The instructions assigned to the vcpu_is_preempted function parameter in the X86 architecture physical machine are redundant instructions, causing the multi-core performance of Unixbench to drop by about 4% to 5%. The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter 'vcpu' in the function osq_lock
that calls the function vcpu_is_preempted is assigned as follows:

The C code is in the function node_cpu:
cpu = node->cpu - 1;

The instructions corresponding to the C code are:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment, causing high cache-misses and degrading performance.

This patch uses static_key to not execute this instruction in the Native runtime environment.

The patch effect is as follows two machines,
Unixbench runs with full core score:

1. Machine configuration:
Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
CPU core: 40
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  948326591.2  81261.9
Double-Precision Whetstone                       55.0     211986.3  38543.0
Execl Throughput                                 43.0      43453.2  10105.4
File Copy 1024 bufsize 2000 maxblocks          3960.0     438936.2   1108.4
File Copy 256 bufsize 500 maxblocks            1655.0     118197.4    714.2
File Copy 4096 bufsize 8000 maxblocks          5800.0    1534674.7   2646.0

Pipe Throughput 12440.0 46482107.6 37365.0
Pipe-based Context Switching 4000.0 1915094.2 4787.7
Process Creation 126.0 85442.2 6781.1
Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
System Call Overhead 15000.0 4714906.1 3143.3

                                                                    ========

System Benchmarks Index Score 7923.3

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  947032915.5  81151.1
Double-Precision Whetstone                       55.0     211971.2  38540.2
Execl Throughput                                 43.0      45054.8  10477.9
File Copy 1024 bufsize 2000 maxblocks          3960.0     515024.9   1300.6
File Copy 256 bufsize 500 maxblocks            1655.0     146354.6    884.3
File Copy 4096 bufsize 8000 maxblocks          5800.0    1679995.9   2896.5

Pipe Throughput 12440.0 46466394.2 37352.4
Pipe-based Context Switching 4000.0 1898221.4 4745.6
Process Creation 126.0 85653.1 6797.9
Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
System Call Overhead 15000.0 4658746.7 3105.8

                                                                    ========

System Benchmarks Index Score 8248.8

2. Machine configuration:
Hygon C86 7185 32-core Processor
CPU core: 128
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
Double-Precision Whetstone 55.0 438969.9 79812.7
Execl Throughput 43.0 10108.6 2350.8

File Copy 1024 bufsize 2000 maxblocks          3960.0     275892.8    696.7
File Copy 256 bufsize 500 maxblocks            1655.0      72082.7    435.5
File Copy 4096 bufsize 8000 maxblocks          5800.0     925043.4   1594.9

Pipe Throughput 12440.0 118905512.5 95583.2
Pipe-based Context Switching 4000.0 7820945.7 19552.4
Process Creation 126.0 31233.3 2478.8
Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
System Call Overhead 15000.0 6816047.5 4544.0

                                                                    ========

System Benchmarks Index Score 7756.6

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
Double-Precision Whetstone 55.0 451847.2 82154.0
Execl Throughput 43.0 10595.1 2464.0

File Copy 1024 bufsize 2000 maxblocks          3960.0     301279.3    760.8
File Copy 256 bufsize 500 maxblocks            1655.0      79291.3    479.1
File Copy 4096 bufsize 8000 maxblocks          5800.0    1039755.2   1792.7

Pipe Throughput 12440.0 118701468.1 95419.2
Pipe-based Context Switching 4000.0 8073453.3 20183.6
Process Creation 126.0 33440.9 2654.0
Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
System Call Overhead 15000.0 6834371.5 4556.2

Quoted 4 lines
                                                                    ========

System Benchmarks Index Score 8157.8

Juergen

reviewer
#13 lore raw
In reply to Guo Hui
Quoted 154 lines · Guo Hui

The instructions assigned to the vcpu_is_preempted function parameter in the X86 architecture physical machine are redundant instructions, causing the multi-core performance of Unixbench to drop by about 4% to 5%. The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter 'vcpu' in the function osq_lock
that calls the function vcpu_is_preempted is assigned as follows:

The C code is in the function node_cpu:
cpu = node->cpu - 1;

The instructions corresponding to the C code are:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment, causing high cache-misses and degrading performance.

This patch uses static_key to not execute this instruction in the Native runtime environment.

The patch effect is as follows two machines,
Unixbench runs with full core score:

1. Machine configuration:
Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
CPU core: 40
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  948326591.2  81261.9
Double-Precision Whetstone                       55.0     211986.3  38543.0
Execl Throughput                                 43.0      43453.2  10105.4
File Copy 1024 bufsize 2000 maxblocks          3960.0     438936.2   1108.4
File Copy 256 bufsize 500 maxblocks            1655.0     118197.4    714.2
File Copy 4096 bufsize 8000 maxblocks          5800.0    1534674.7   2646.0

Pipe Throughput 12440.0 46482107.6 37365.0
Pipe-based Context Switching 4000.0 1915094.2 4787.7
Process Creation 126.0 85442.2 6781.1
Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
System Call Overhead 15000.0 4714906.1 3143.3

                                                                    ========

System Benchmarks Index Score 7923.3

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  947032915.5  81151.1
Double-Precision Whetstone                       55.0     211971.2  38540.2
Execl Throughput                                 43.0      45054.8  10477.9
File Copy 1024 bufsize 2000 maxblocks          3960.0     515024.9   1300.6
File Copy 256 bufsize 500 maxblocks            1655.0     146354.6    884.3
File Copy 4096 bufsize 8000 maxblocks          5800.0    1679995.9   2896.5

Pipe Throughput 12440.0 46466394.2 37352.4
Pipe-based Context Switching 4000.0 1898221.4 4745.6
Process Creation 126.0 85653.1 6797.9
Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
System Call Overhead 15000.0 4658746.7 3105.8

                                                                    ========

System Benchmarks Index Score 8248.8

2. Machine configuration:
Hygon C86 7185 32-core Processor
CPU core: 128
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
Double-Precision Whetstone 55.0 438969.9 79812.7
Execl Throughput 43.0 10108.6 2350.8

File Copy 1024 bufsize 2000 maxblocks          3960.0     275892.8    696.7
File Copy 256 bufsize 500 maxblocks            1655.0      72082.7    435.5
File Copy 4096 bufsize 8000 maxblocks          5800.0     925043.4   1594.9

Pipe Throughput 12440.0 118905512.5 95583.2
Pipe-based Context Switching 4000.0 7820945.7 19552.4
Process Creation 126.0 31233.3 2478.8
Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
System Call Overhead 15000.0 6816047.5 4544.0

                                                                    ========

System Benchmarks Index Score 7756.6

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
Double-Precision Whetstone 55.0 451847.2 82154.0
Execl Throughput 43.0 10595.1 2464.0

File Copy 1024 bufsize 2000 maxblocks          3960.0     301279.3    760.8
File Copy 256 bufsize 500 maxblocks            1655.0      79291.3    479.1
File Copy 4096 bufsize 8000 maxblocks          5800.0    1039755.2   1792.7

Pipe Throughput 12440.0 118701468.1 95419.2
Pipe-based Context Switching 4000.0 8073453.3 20183.6
Process Creation 126.0 33440.9 2654.0
Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
System Call Overhead 15000.0 6834371.5 4556.2

                                                                    ========

System Benchmarks Index Score 8157.8

---
  arch/x86/kernel/paravirt-spinlocks.c |  4 ++++
  kernel/locking/osq_lock.c            | 12 +++++++++++-
  2 files changed, 15 insertions(+), 1 deletion(-)
arch/x86/kernel/paravirt-spinlocks.c +4 -0
index 9e1ea99ad..a2eb375e2 100644
@@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
  		__raw_callee_save___native_vcpu_is_preempted;
  }
 
+DECLARE_STATIC_KEY_TRUE(vcpu_has_preemption);
+
  void __init paravirt_set_cap(void)
  {
  	if (!pv_is_native_spin_unlock())
@@ -40,4 +42,6 @@ void __init paravirt_set_cap(void)
 
  	if (!pv_is_native_vcpu_is_preempted())
  		setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
+	else
+		static_branch_disable(&vcpu_has_preemption);
  }
kernel/locking/osq_lock.c +10 -0
index d5610ad52..adb41080d 100644
@@ -27,6 +27,16 @@ static inline int node_cpu(struct optimistic_spin_node *node)
  	return node->cpu - 1;
  }
 
+DEFINE_STATIC_KEY_TRUE(vcpu_has_preemption);
+
+static inline bool vcpu_is_preempted_node(struct optimistic_spin_node *node)
+{
+	if (!static_branch_unlikely(&vcpu_has_preemption))
+		return false;
+
+	return vcpu_is_preempted(node_cpu(node->prev));
+}
+
  static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
  {
  	int cpu_nr = encoded_cpu_val - 1;

kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 157

Quoted 8 lines
kernel/locking/osq_lock.c +1 -1
@@ -141,7 +151,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
  	 * polling, be careful.
  	 */
  	if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
-				  vcpu_is_preempted(node_cpu(node->prev))))
+						vcpu_is_preempted_node(node)))
  		return true;
 
  	/* unqueue */

The patch looks good. I do have a minor nit though.

Usually, DEFINE_STATIC_KEY_TRUE() is paired with static_branch_likely() and DEFINE_STATIC_KEY_FALSE() is paired with static_branch_unlikely(). I think what Peter meant is to use DEFINE_STATIC_KEY_FALSE() and enable vcpu_has_preemption together with X86_FEATURE_VCPUPREEMPT in the same if block.

Cheers,
Longman

author
#14 lore raw

PATCH v5 1/1 x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

The instructions assigned to the vcpu_is_preempted function parameter in the X86 architecture physical machine are redundant instructions, causing the multi-core performance of Unixbench to drop by about 4% to 5%. The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter 'vcpu' in the function osq_lock
that calls the function vcpu_is_preempted is assigned as follows:

The C code is in the function node_cpu:
cpu = node->cpu - 1;

The instructions corresponding to the C code are:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment, causing high cache-misses and degrading performance.

This patch uses static_key to not execute this instruction in the Native runtime environment.

The patch effect is as follows two machines,
Unixbench runs with full core score:

1. Machine configuration:
Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
CPU core: 40
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  948326591.2  81261.9
Double-Precision Whetstone                       55.0     211986.3  38543.0
Execl Throughput                                 43.0      43453.2  10105.4
File Copy 1024 bufsize 2000 maxblocks          3960.0     438936.2   1108.4
File Copy 256 bufsize 500 maxblocks            1655.0     118197.4    714.2
File Copy 4096 bufsize 8000 maxblocks          5800.0    1534674.7   2646.0

Pipe Throughput 12440.0 46482107.6 37365.0
Pipe-based Context Switching 4000.0 1915094.2 4787.7
Process Creation 126.0 85442.2 6781.1
Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
System Call Overhead 15000.0 4714906.1 3143.3

                                                                   ========

System Benchmarks Index Score 7923.3

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  947032915.5  81151.1
Double-Precision Whetstone                       55.0     211971.2  38540.2
Execl Throughput                                 43.0      45054.8  10477.9
File Copy 1024 bufsize 2000 maxblocks          3960.0     515024.9   1300.6
File Copy 256 bufsize 500 maxblocks            1655.0     146354.6    884.3
File Copy 4096 bufsize 8000 maxblocks          5800.0    1679995.9   2896.5

Pipe Throughput 12440.0 46466394.2 37352.4
Pipe-based Context Switching 4000.0 1898221.4 4745.6
Process Creation 126.0 85653.1 6797.9
Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
System Call Overhead 15000.0 4658746.7 3105.8

                                                                   ========

System Benchmarks Index Score 8248.8

2. Machine configuration:
Hygon C86 7185 32-core Processor
CPU core: 128
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
Double-Precision Whetstone 55.0 438969.9 79812.7
Execl Throughput 43.0 10108.6 2350.8

File Copy 1024 bufsize 2000 maxblocks          3960.0     275892.8    696.7
File Copy 256 bufsize 500 maxblocks            1655.0      72082.7    435.5
File Copy 4096 bufsize 8000 maxblocks          5800.0     925043.4   1594.9

Pipe Throughput 12440.0 118905512.5 95583.2
Pipe-based Context Switching 4000.0 7820945.7 19552.4
Process Creation 126.0 31233.3 2478.8
Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
System Call Overhead 15000.0 6816047.5 4544.0

                                                                   ========

System Benchmarks Index Score 7756.6

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
Double-Precision Whetstone 55.0 451847.2 82154.0
Execl Throughput 43.0 10595.1 2464.0

File Copy 1024 bufsize 2000 maxblocks          3960.0     301279.3    760.8
File Copy 256 bufsize 500 maxblocks            1655.0      79291.3    479.1
File Copy 4096 bufsize 8000 maxblocks          5800.0    1039755.2   1792.7

Pipe Throughput 12440.0 118701468.1 95419.2
Pipe-based Context Switching 4000.0 8073453.3 20183.6
Process Creation 126.0 33440.9 2654.0
Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
System Call Overhead 15000.0 6834371.5 4556.2

                                                                   ========

System Benchmarks Index Score 8157.8

---
 arch/x86/kernel/paravirt-spinlocks.c |  6 +++++-
 kernel/locking/osq_lock.c            | 12 +++++++++++-
 2 files changed, 16 insertions(+), 2 deletions(-)
arch/x86/kernel/paravirt-spinlocks.c +5 -1
index 9e1ea99..00f6059 100644
@@ -33,11 +33,15 @@ bool pv_is_native_vcpu_is_preempted(void)
 		__raw_callee_save___native_vcpu_is_preempted;
 }
 
+DECLARE_STATIC_KEY_FALSE(vcpu_has_preemption);
+
 void __init paravirt_set_cap(void)
 {
 	if (!pv_is_native_spin_unlock())
 		setup_force_cpu_cap(X86_FEATURE_PVUNLOCK);
 
-	if (!pv_is_native_vcpu_is_preempted())
+	if (!pv_is_native_vcpu_is_preempted()) {
 		setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
+		static_branch_enable(&vcpu_has_preemption);
+	}
 }
kernel/locking/osq_lock.c +11 -1
index d5610ad..2909fad 100644
@@ -27,6 +27,16 @@ static inline int node_cpu(struct optimistic_spin_node *node)
 	return node->cpu - 1;
 }
 
+DEFINE_STATIC_KEY_FALSE(vcpu_has_preemption);
+
+static inline bool vcpu_is_preempted_node(struct optimistic_spin_node *node)
+{
+	if (!static_branch_unlikely(&vcpu_has_preemption))
+		return false;
+
+	return vcpu_is_preempted(node_cpu(node->prev));
+}
+
 static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
 {
 	int cpu_nr = encoded_cpu_val - 1;
@@ -141,7 +151,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
 	 * polling, be careful.
 	 */
 	if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
-				  vcpu_is_preempted(node_cpu(node->prev))))
+						vcpu_is_preempted_node(node)))
 		return true;
 
 	/* unqueue */
Signature · 1 line
2.20.1
author
#15 lore raw

PATCH v6 1/1 x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

In reply to Waiman Long

The instructions assigned to the vcpu_is_preempted function parameter in the X86 architecture physical machine are redundant instructions, causing the multi-core performance of Unixbench to drop by about 4% to 5%. The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter 'vcpu' in the function osq_lock
that calls the function vcpu_is_preempted is assigned as follows:

The C code is in the function node_cpu:
cpu = node->cpu - 1;

The instructions corresponding to the C code are:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment, causing high cache-misses and degrading performance.

This patch uses static_key to not execute this instruction in the Native runtime environment.

The patch effect is as follows two machines,
Unixbench runs with full core score:

1. Machine configuration:
Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
CPU core: 40
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  948326591.2  81261.9
Double-Precision Whetstone                       55.0     211986.3  38543.0
Execl Throughput                                 43.0      43453.2  10105.4
File Copy 1024 bufsize 2000 maxblocks          3960.0     438936.2   1108.4
File Copy 256 bufsize 500 maxblocks            1655.0     118197.4    714.2
File Copy 4096 bufsize 8000 maxblocks          5800.0    1534674.7   2646.0

Pipe Throughput 12440.0 46482107.6 37365.0
Pipe-based Context Switching 4000.0 1915094.2 4787.7
Process Creation 126.0 85442.2 6781.1
Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
System Call Overhead 15000.0 4714906.1 3143.3

                                                                   ========

System Benchmarks Index Score 7923.3

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  947032915.5  81151.1
Double-Precision Whetstone                       55.0     211971.2  38540.2
Execl Throughput                                 43.0      45054.8  10477.9
File Copy 1024 bufsize 2000 maxblocks          3960.0     515024.9   1300.6
File Copy 256 bufsize 500 maxblocks            1655.0     146354.6    884.3
File Copy 4096 bufsize 8000 maxblocks          5800.0    1679995.9   2896.5

Pipe Throughput 12440.0 46466394.2 37352.4
Pipe-based Context Switching 4000.0 1898221.4 4745.6
Process Creation 126.0 85653.1 6797.9
Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
System Call Overhead 15000.0 4658746.7 3105.8

                                                                   ========

System Benchmarks Index Score 8248.8

2. Machine configuration:
Hygon C86 7185 32-core Processor
CPU core: 128
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
Double-Precision Whetstone 55.0 438969.9 79812.7
Execl Throughput 43.0 10108.6 2350.8

File Copy 1024 bufsize 2000 maxblocks          3960.0     275892.8    696.7
File Copy 256 bufsize 500 maxblocks            1655.0      72082.7    435.5
File Copy 4096 bufsize 8000 maxblocks          5800.0     925043.4   1594.9

Pipe Throughput 12440.0 118905512.5 95583.2
Pipe-based Context Switching 4000.0 7820945.7 19552.4
Process Creation 126.0 31233.3 2478.8
Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
System Call Overhead 15000.0 6816047.5 4544.0

                                                                   ========

System Benchmarks Index Score 7756.6

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
Double-Precision Whetstone 55.0 451847.2 82154.0
Execl Throughput 43.0 10595.1 2464.0

File Copy 1024 bufsize 2000 maxblocks          3960.0     301279.3    760.8
File Copy 256 bufsize 500 maxblocks            1655.0      79291.3    479.1
File Copy 4096 bufsize 8000 maxblocks          5800.0    1039755.2   1792.7

Pipe Throughput 12440.0 118701468.1 95419.2
Pipe-based Context Switching 4000.0 8073453.3 20183.6
Process Creation 126.0 33440.9 2654.0
Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
System Call Overhead 15000.0 6834371.5 4556.2

                                                                   ========

System Benchmarks Index Score 8157.8

---
 arch/x86/kernel/paravirt-spinlocks.c |  4 ++++
 kernel/locking/osq_lock.c            | 12 +++++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)
arch/x86/kernel/paravirt-spinlocks.c +4 -0
index 9e1ea99..a2eb375 100644
@@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
 		__raw_callee_save___native_vcpu_is_preempted;
 }
 
+DECLARE_STATIC_KEY_TRUE(vcpu_has_preemption);
+
 void __init paravirt_set_cap(void)
 {
 	if (!pv_is_native_spin_unlock())
@@ -40,4 +42,6 @@ void __init paravirt_set_cap(void)
 
 	if (!pv_is_native_vcpu_is_preempted())
 		setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
+	else
+		static_branch_disable(&vcpu_has_preemption);
 }
kernel/locking/osq_lock.c +11 -1
index d5610ad..883e815 100644
@@ -27,6 +27,16 @@ static inline int node_cpu(struct optimistic_spin_node *node)
 	return node->cpu - 1;
 }
 
+DEFINE_STATIC_KEY_TRUE(vcpu_has_preemption);
+
+static inline bool vcpu_is_preempted_node(struct optimistic_spin_node *node)
+{
+	if (static_branch_likely(&vcpu_has_preemption))
+		return vcpu_is_preempted(node_cpu(node->prev));
+
+	return false;
+}
+
 static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
 {
 	int cpu_nr = encoded_cpu_val - 1;
@@ -141,7 +151,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
 	 * polling, be careful.
 	 */
 	if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
-				  vcpu_is_preempted(node_cpu(node->prev))))
+						vcpu_is_preempted_node(node)))
 		return true;
 
 	/* unqueue */
Signature · 1 line
2.20.1
reviewer
#16 lore raw
Quoted 154 lines · Guo Hui

The instructions assigned to the vcpu_is_preempted function parameter in the X86 architecture physical machine are redundant instructions, causing the multi-core performance of Unixbench to drop by about 4% to 5%. The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter 'vcpu' in the function osq_lock
that calls the function vcpu_is_preempted is assigned as follows:

The C code is in the function node_cpu:
cpu = node->cpu - 1;

The instructions corresponding to the C code are:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment, causing high cache-misses and degrading performance.

This patch uses static_key to not execute this instruction in the Native runtime environment.

The patch effect is as follows two machines,
Unixbench runs with full core score:

1. Machine configuration:
Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
CPU core: 40
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  948326591.2  81261.9
Double-Precision Whetstone                       55.0     211986.3  38543.0
Execl Throughput                                 43.0      43453.2  10105.4
File Copy 1024 bufsize 2000 maxblocks          3960.0     438936.2   1108.4
File Copy 256 bufsize 500 maxblocks            1655.0     118197.4    714.2
File Copy 4096 bufsize 8000 maxblocks          5800.0    1534674.7   2646.0

Pipe Throughput 12440.0 46482107.6 37365.0
Pipe-based Context Switching 4000.0 1915094.2 4787.7
Process Creation 126.0 85442.2 6781.1
Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
System Call Overhead 15000.0 4714906.1 3143.3

                                                                    ========

System Benchmarks Index Score 7923.3

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  947032915.5  81151.1
Double-Precision Whetstone                       55.0     211971.2  38540.2
Execl Throughput                                 43.0      45054.8  10477.9
File Copy 1024 bufsize 2000 maxblocks          3960.0     515024.9   1300.6
File Copy 256 bufsize 500 maxblocks            1655.0     146354.6    884.3
File Copy 4096 bufsize 8000 maxblocks          5800.0    1679995.9   2896.5

Pipe Throughput 12440.0 46466394.2 37352.4
Pipe-based Context Switching 4000.0 1898221.4 4745.6
Process Creation 126.0 85653.1 6797.9
Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
System Call Overhead 15000.0 4658746.7 3105.8

                                                                    ========

System Benchmarks Index Score 8248.8

2. Machine configuration:
Hygon C86 7185 32-core Processor
CPU core: 128
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
Double-Precision Whetstone 55.0 438969.9 79812.7
Execl Throughput 43.0 10108.6 2350.8

File Copy 1024 bufsize 2000 maxblocks          3960.0     275892.8    696.7
File Copy 256 bufsize 500 maxblocks            1655.0      72082.7    435.5
File Copy 4096 bufsize 8000 maxblocks          5800.0     925043.4   1594.9

Pipe Throughput 12440.0 118905512.5 95583.2
Pipe-based Context Switching 4000.0 7820945.7 19552.4
Process Creation 126.0 31233.3 2478.8
Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
System Call Overhead 15000.0 6816047.5 4544.0

                                                                    ========

System Benchmarks Index Score 7756.6

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
Double-Precision Whetstone 55.0 451847.2 82154.0
Execl Throughput 43.0 10595.1 2464.0

File Copy 1024 bufsize 2000 maxblocks          3960.0     301279.3    760.8
File Copy 256 bufsize 500 maxblocks            1655.0      79291.3    479.1
File Copy 4096 bufsize 8000 maxblocks          5800.0    1039755.2   1792.7

Pipe Throughput 12440.0 118701468.1 95419.2
Pipe-based Context Switching 4000.0 8073453.3 20183.6
Process Creation 126.0 33440.9 2654.0
Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
System Call Overhead 15000.0 6834371.5 4556.2

                                                                    ========

System Benchmarks Index Score 8157.8

---
  arch/x86/kernel/paravirt-spinlocks.c |  4 ++++
  kernel/locking/osq_lock.c            | 12 +++++++++++-
  2 files changed, 15 insertions(+), 1 deletion(-)
arch/x86/kernel/paravirt-spinlocks.c +4 -0
index 9e1ea99..a2eb375 100644
@@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
  		__raw_callee_save___native_vcpu_is_preempted;
  }
 
+DECLARE_STATIC_KEY_TRUE(vcpu_has_preemption);
+
  void __init paravirt_set_cap(void)
  {
  	if (!pv_is_native_spin_unlock())
@@ -40,4 +42,6 @@ void __init paravirt_set_cap(void)
 
  	if (!pv_is_native_vcpu_is_preempted())
  		setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
+	else
+		static_branch_disable(&vcpu_has_preemption);
  }
kernel/locking/osq_lock.c +10 -0
index d5610ad..883e815 100644
@@ -27,6 +27,16 @@ static inline int node_cpu(struct optimistic_spin_node *node)
  	return node->cpu - 1;
  }
 
+DEFINE_STATIC_KEY_TRUE(vcpu_has_preemption);
+
+static inline bool vcpu_is_preempted_node(struct optimistic_spin_node *node)
+{
+	if (static_branch_likely(&vcpu_has_preemption))
+		return vcpu_is_preempted(node_cpu(node->prev));
+
+	return false;
+}
+
  static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
  {
  	int cpu_nr = encoded_cpu_val - 1;

kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 157

Quoted 8 lines
kernel/locking/osq_lock.c +1 -1
@@ -141,7 +151,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
  	 * polling, be careful.
  	 */
  	if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
-				  vcpu_is_preempted(node_cpu(node->prev))))
+						vcpu_is_preempted_node(node)))
  		return true;
 
  	/* unqueue */

How about a further improvement on configurations that don't use vcpu_is_preempted() at all?

+#ifdef vcpu_is_preempted +DEFINE_STATIC_KEY_TRUE(vcpu_has_preemption);
+
 static inline int node_cpu(struct optimistic_spin_node *node) {
        return node->cpu - 1;
 }

+static inline bool vcpu_is_preempted_node(struct optimistic_spin_node *node) +{
+       if (static_branch_likely(&vcpu_has_preemption))
+               return vcpu_is_preempted(node_cpu(node->prev));
+
+       return false;
+}
+#else
+static inline bool vcpu_is_preempted_node(struct optimistic_spin_node *node) +{
+       return false;
+}
+#endif
+
 static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val) {
        int cpu_nr = encoded_cpu_val - 1;
@@ -141,7 +158,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
         * polling, be careful.
         */
        if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
- vcpu_is_preempted(node_cpu(node->prev))))
+                                 vcpu_is_preempted_node(node))) return true;

        /* unqueue */

For those configurations, vcpu_is_preempted_node() will just get compiled out.

Cheers,
Longman

author
#17 lore raw

PATCH v7 1/1 x86/paravirt: useless assignment instructions cause Unixbench full core performance degradation

The instructions assigned to the vcpu_is_preempted function parameter in the X86 architecture physical machine are redundant instructions, causing the multi-core performance of Unixbench to drop by about 4% to 5%. The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter 'vcpu' in the function osq_lock
that calls the function vcpu_is_preempted is assigned as follows:

The C code is in the function node_cpu:
cpu = node->cpu - 1;

The instructions corresponding to the C code are:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment, causing high cache-misses and degrading performance.

This patch uses static_key to not execute this instruction in the Native runtime environment.

The patch effect is as follows two machines,
Unixbench runs with full core score:

1. Machine configuration:
Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
CPU core: 40
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  948326591.2  81261.9
Double-Precision Whetstone                       55.0     211986.3  38543.0
Execl Throughput                                 43.0      43453.2  10105.4
File Copy 1024 bufsize 2000 maxblocks          3960.0     438936.2   1108.4
File Copy 256 bufsize 500 maxblocks            1655.0     118197.4    714.2
File Copy 4096 bufsize 8000 maxblocks          5800.0    1534674.7   2646.0

Pipe Throughput 12440.0 46482107.6 37365.0
Pipe-based Context Switching 4000.0 1915094.2 4787.7
Process Creation 126.0 85442.2 6781.1
Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
System Call Overhead 15000.0 4714906.1 3143.3

                                                                   ========

System Benchmarks Index Score 7923.3

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  947032915.5  81151.1
Double-Precision Whetstone                       55.0     211971.2  38540.2
Execl Throughput                                 43.0      45054.8  10477.9
File Copy 1024 bufsize 2000 maxblocks          3960.0     515024.9   1300.6
File Copy 256 bufsize 500 maxblocks            1655.0     146354.6    884.3
File Copy 4096 bufsize 8000 maxblocks          5800.0    1679995.9   2896.5

Pipe Throughput 12440.0 46466394.2 37352.4
Pipe-based Context Switching 4000.0 1898221.4 4745.6
Process Creation 126.0 85653.1 6797.9
Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
System Call Overhead 15000.0 4658746.7 3105.8

                                                                   ========

System Benchmarks Index Score 8248.8

2. Machine configuration:
Hygon C86 7185 32-core Processor
CPU core: 128
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
Double-Precision Whetstone 55.0 438969.9 79812.7
Execl Throughput 43.0 10108.6 2350.8

File Copy 1024 bufsize 2000 maxblocks          3960.0     275892.8    696.7
File Copy 256 bufsize 500 maxblocks            1655.0      72082.7    435.5
File Copy 4096 bufsize 8000 maxblocks          5800.0     925043.4   1594.9

Pipe Throughput 12440.0 118905512.5 95583.2
Pipe-based Context Switching 4000.0 7820945.7 19552.4
Process Creation 126.0 31233.3 2478.8
Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
System Call Overhead 15000.0 6816047.5 4544.0

                                                                   ========

System Benchmarks Index Score 7756.6

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
Double-Precision Whetstone 55.0 451847.2 82154.0
Execl Throughput 43.0 10595.1 2464.0

File Copy 1024 bufsize 2000 maxblocks          3960.0     301279.3    760.8
File Copy 256 bufsize 500 maxblocks            1655.0      79291.3    479.1
File Copy 4096 bufsize 8000 maxblocks          5800.0    1039755.2   1792.7

Pipe Throughput 12440.0 118701468.1 95419.2
Pipe-based Context Switching 4000.0 8073453.3 20183.6
Process Creation 126.0 33440.9 2654.0
Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
System Call Overhead 15000.0 6834371.5 4556.2

                                                                   ========

System Benchmarks Index Score 8157.8

---
 arch/x86/kernel/paravirt-spinlocks.c |  4 ++++
 kernel/locking/osq_lock.c            | 19 ++++++++++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)
arch/x86/kernel/paravirt-spinlocks.c +4 -0
index 9e1ea99ad..a2eb375e2 100644
@@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
 		__raw_callee_save___native_vcpu_is_preempted;
 }
 
+DECLARE_STATIC_KEY_TRUE(vcpu_has_preemption);
+
 void __init paravirt_set_cap(void)
 {
 	if (!pv_is_native_spin_unlock())
@@ -40,4 +42,6 @@ void __init paravirt_set_cap(void)
 
 	if (!pv_is_native_vcpu_is_preempted())
 		setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
+	else
+		static_branch_disable(&vcpu_has_preemption);
 }
kernel/locking/osq_lock.c +18 -1
index d5610ad52..f521b0f6d 100644
@@ -27,6 +27,23 @@ static inline int node_cpu(struct optimistic_spin_node *node)
 	return node->cpu - 1;
 }
 
+#ifdef vcpu_is_preempted
+DEFINE_STATIC_KEY_TRUE(vcpu_has_preemption);
+
+static inline bool vcpu_is_preempted_node(struct optimistic_spin_node *node)
+{
+	if (static_branch_likely(&vcpu_has_preemption))
+		return vcpu_is_preempted(node_cpu(node->prev));
+
+	return false;
+}
+#else
+static inline bool vcpu_is_preempted_node(struct optimistic_spin_node *node)
+{
+	return false;
+}
+#endif
+
 static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
 {
 	int cpu_nr = encoded_cpu_val - 1;
@@ -141,7 +158,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
 	 * polling, be careful.
 	 */
 	if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
-				  vcpu_is_preempted(node_cpu(node->prev))))
+						vcpu_is_preempted_node(node)))
 		return true;
 
 	/* unqueue */
Signature · 1 line
2.20.1
reviewer Reviewed-by on v7 1/1
#18 lore raw
Quoted 161 lines · Guo Hui

The instructions assigned to the vcpu_is_preempted function parameter in the X86 architecture physical machine are redundant instructions, causing the multi-core performance of Unixbench to drop by about 4% to 5%. The C function is as follows:
static bool vcpu_is_preempted(long vcpu);

The parameter 'vcpu' in the function osq_lock
that calls the function vcpu_is_preempted is assigned as follows:

The C code is in the function node_cpu:
cpu = node->cpu - 1;

The instructions corresponding to the C code are:
mov 0x14(%rax),%edi
sub $0x1,%edi

The above instructions are unnecessary
in the X86 Native operating environment, causing high cache-misses and degrading performance.

This patch uses static_key to not execute this instruction in the Native runtime environment.

The patch effect is as follows two machines,
Unixbench runs with full core score:

1. Machine configuration:
Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
CPU core: 40
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  948326591.2  81261.9
Double-Precision Whetstone                       55.0     211986.3  38543.0
Execl Throughput                                 43.0      43453.2  10105.4
File Copy 1024 bufsize 2000 maxblocks          3960.0     438936.2   1108.4
File Copy 256 bufsize 500 maxblocks            1655.0     118197.4    714.2
File Copy 4096 bufsize 8000 maxblocks          5800.0    1534674.7   2646.0

Pipe Throughput 12440.0 46482107.6 37365.0
Pipe-based Context Switching 4000.0 1915094.2 4787.7
Process Creation 126.0 85442.2 6781.1
Shell Scripts (1 concurrent) 42.4 69400.7 16368.1
Shell Scripts (8 concurrent) 6.0 8877.2 14795.3
System Call Overhead 15000.0 4714906.1 3143.3

                                                                    ========

System Benchmarks Index Score 7923.3

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX

Dhrystone 2 using register variables         116700.0  947032915.5  81151.1
Double-Precision Whetstone                       55.0     211971.2  38540.2
Execl Throughput                                 43.0      45054.8  10477.9
File Copy 1024 bufsize 2000 maxblocks          3960.0     515024.9   1300.6
File Copy 256 bufsize 500 maxblocks            1655.0     146354.6    884.3
File Copy 4096 bufsize 8000 maxblocks          5800.0    1679995.9   2896.5

Pipe Throughput 12440.0 46466394.2 37352.4
Pipe-based Context Switching 4000.0 1898221.4 4745.6
Process Creation 126.0 85653.1 6797.9
Shell Scripts (1 concurrent) 42.4 69437.3 16376.7
Shell Scripts (8 concurrent) 6.0 8898.9 14831.4
System Call Overhead 15000.0 4658746.7 3105.8

                                                                    ========

System Benchmarks Index Score 8248.8

2. Machine configuration:
Hygon C86 7185 32-core Processor
CPU core: 128
Memory: 256G
OS Kernel: 5.19-rc3

Before using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2256644068.3 193371.4
Double-Precision Whetstone 55.0 438969.9 79812.7
Execl Throughput 43.0 10108.6 2350.8

File Copy 1024 bufsize 2000 maxblocks          3960.0     275892.8    696.7
File Copy 256 bufsize 500 maxblocks            1655.0      72082.7    435.5
File Copy 4096 bufsize 8000 maxblocks          5800.0     925043.4   1594.9

Pipe Throughput 12440.0 118905512.5 95583.2
Pipe-based Context Switching 4000.0 7820945.7 19552.4
Process Creation 126.0 31233.3 2478.8
Shell Scripts (1 concurrent) 42.4 49042.8 11566.7
Shell Scripts (8 concurrent) 6.0 6656.0 11093.3
System Call Overhead 15000.0 6816047.5 4544.0

                                                                    ========

System Benchmarks Index Score 7756.6

After using the patch:
System Benchmarks Index Values BASELINE RESULT INDEX
Dhrystone 2 using register variables 116700.0 2252272929.4 192996.8
Double-Precision Whetstone 55.0 451847.2 82154.0
Execl Throughput 43.0 10595.1 2464.0

File Copy 1024 bufsize 2000 maxblocks          3960.0     301279.3    760.8
File Copy 256 bufsize 500 maxblocks            1655.0      79291.3    479.1
File Copy 4096 bufsize 8000 maxblocks          5800.0    1039755.2   1792.7

Pipe Throughput 12440.0 118701468.1 95419.2
Pipe-based Context Switching 4000.0 8073453.3 20183.6
Process Creation 126.0 33440.9 2654.0
Shell Scripts (1 concurrent) 42.4 52722.6 12434.6
Shell Scripts (8 concurrent) 6.0 7050.4 11750.6
System Call Overhead 15000.0 6834371.5 4556.2

                                                                    ========

System Benchmarks Index Score 8157.8

---
  arch/x86/kernel/paravirt-spinlocks.c |  4 ++++
  kernel/locking/osq_lock.c            | 19 ++++++++++++++++++-
  2 files changed, 22 insertions(+), 1 deletion(-)
arch/x86/kernel/paravirt-spinlocks.c +4 -0
index 9e1ea99ad..a2eb375e2 100644
@@ -33,6 +33,8 @@ bool pv_is_native_vcpu_is_preempted(void)
  		__raw_callee_save___native_vcpu_is_preempted;
  }
 
+DECLARE_STATIC_KEY_TRUE(vcpu_has_preemption);
+
  void __init paravirt_set_cap(void)
  {
  	if (!pv_is_native_spin_unlock())
@@ -40,4 +42,6 @@ void __init paravirt_set_cap(void)
 
  	if (!pv_is_native_vcpu_is_preempted())
  		setup_force_cpu_cap(X86_FEATURE_VCPUPREEMPT);
+	else
+		static_branch_disable(&vcpu_has_preemption);
  }
kernel/locking/osq_lock.c +17 -0
index d5610ad52..f521b0f6d 100644
@@ -27,6 +27,23 @@ static inline int node_cpu(struct optimistic_spin_node *node)
  	return node->cpu - 1;
  }
 
+#ifdef vcpu_is_preempted
+DEFINE_STATIC_KEY_TRUE(vcpu_has_preemption);
+
+static inline bool vcpu_is_preempted_node(struct optimistic_spin_node *node)
+{
+	if (static_branch_likely(&vcpu_has_preemption))
+		return vcpu_is_preempted(node_cpu(node->prev));
+
+	return false;
+}
+#else
+static inline bool vcpu_is_preempted_node(struct optimistic_spin_node *node)
+{
+	return false;
+}
+#endif
+
  static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
  {
  	int cpu_nr = encoded_cpu_val - 1;

kernel/locking/osq_lock.c bool osq_lock(struct optimistic_spin_queue *lock) · line 164

Quoted 8 lines
kernel/locking/osq_lock.c +1 -1
@@ -141,7 +158,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
  	 * polling, be careful.
  	 */
  	if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
-				  vcpu_is_preempted(node_cpu(node->prev))))
+						vcpu_is_preempted_node(node)))
  		return true;
 
  	/* unqueue */