Skip to contentSkip to conversation

x86/hpet: Reduce HPET counter read contention

PATCH Superseded Waiman Long 2 participants 3 messages Apr 8, 2016 – Apr 11, 2016

Subsystems (3)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/hpet: Reduce HPET counter read contention

On a large system with many CPUs, using HPET as the clock source can have a significant impact on the overall system performance because of the following reasons:
1) There is a single HPET counter shared by all the CPUs.
2) HPET counter reading is a very slow operation.

Using HPET as the default clock source may happen when, for example, the TSC clock calibration exceeds the allowable tolerance. Something the performance slowdown can be so severe that the system may crash because of a NMI watchdog soft lockup, for example.

This patch attempts to reduce HPET read contention by using the fact that if more than one task are trying to access HPET at the same time, it will be more efficient if one task in the group reads the HPET counter and shares it with the rest of the group instead of each group member reads the HPET counter individually.

This is done by using a combination word with a sequence number and a bit lock. The task that gets the bit lock will be responsible for reading the HPET counter and update the sequence number. The others will monitor the change in sequence number and grab the HPET counter accordingly.

On a 4-socket Haswell-EX box with 72 cores (HT off), running the AIM7 compute workload (1500 users) on a 4.6-rc1 kernel (HZ=1000) with and without the patch has the following performance numbers (with HPET or TSC as clock source):

TSC = 646515 jobs/min
HPET w/o patch = 566708 jobs/min
HPET with patch = 638791 jobs/min

The perf profile showed a reduction of the %CPU time consumed by read_hpet from 4.99% without patch to 1.41% with patch.

On a 16-socket IvyBridge-EX system with 240 cores (HT on), on the other hand, the performance numbers of the same benchmark were:

TSC = 3145329 jobs/min
HPET w/o patch = 1108537 jobs/min
HPET with patch = 3019934 jobs/min

The corresponding perf profile showed a drop of CPU consumption of the read_hpet function from more than 34% to just 2.96%.

This optimization is enabled on systems with more than 32 CPUs. It can also be explicitly enabled or disabled by using the new opt_read_hpet kernel parameter.

---

v1->v2:
- Reduce the CPU threshold to 32.
- Add a kernel parameter to explicitly enable or disable hpet optimization.
- Change hpet_save.hpet type to u32 to make sure that read & write is atomic on i386.

 Documentation/kernel-parameters.txt |    4 +
 arch/x86/kernel/hpet.c              |  119 ++++++++++++++++++++++++++++++++++-
 2 files changed, 122 insertions(+), 1 deletions(-)
Documentation/kernel-parameters.txt +4 -0
index ecc74fa..9424c75 100644
@@ -2300,6 +2300,10 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 			lock	 - Configure if Flex-OneNAND boundary should be locked.
 				   Once locked, the boundary cannot be changed.
 				   1 indicates lock status, 0 indicates unlock status.
+	opt_read_hpet=	[X86]
+			0 to disable read_hpet optimization
+			1 to enable read_hpet optimization
+			See arch/x86/kernel/hpet.c.
 
 	mtdset=		[ARM]
 			ARM/S3C2412 JIVE boot control
arch/x86/kernel/hpet.c +118 -1

135 lines not on this page · raw message

Signature · 1 line
1.7.1
#1 lore raw
Quoted 5 lines · Waiman Long

This patch attempts to reduce HPET read contention by using the fact that if more than one task are trying to access HPET at the same time, it will be more efficient if one task in the group reads the HPET counter and shares it with the rest of the group instead of each group member reads the HPET counter individually.

That has nothing to do with tasks. clocksource reads can happen from almost any context. The problem is concurrent access on multiple cpus.

Quoted 3 lines

This optimization is enabled on systems with more than 32 CPUs. It can also be explicitly enabled or disabled by using the new opt_read_hpet kernel parameter.

Please not. What's wrong with enabling it unconditionally?

arch/x86/kernel/hpet.c static int hpet_cpuhp_notify(struct notifier_block *n, · line 813

Quoted 11 lines
arch/x86/kernel/hpet.c +6 -1
+/*
  * Clock source related code
  */
 static cycle_t read_hpet(struct clocksource *cs)
 {
-	return (cycle_t)hpet_readl(HPET_COUNTER);
+	int seq, cnt = 0;
+	u32 time;
+
+	if (opt_read_hpet <= 0)
+		return (cycle_t)hpet_readl(HPET_COUNTER);

This wants to be conditional on CONFIG_SMP. No point in having all that muck around for an UP kernel.

Quoted 47 lines
arch/x86/kernel/hpet.c +47 -0
+	seq = READ_ONCE(hpet_save.seq);
+	if (!HPET_SEQ_LOCKED(seq)) {
+		int old, new = seq + 1;
+		unsigned long flags;
+
+		local_irq_save(flags);
+		/*
+		 * Set the lock bit (lsb) to get the right to read HPET
+		 * counter directly. If successful, read the counter, save
+		 * its value, and increment the sequence number. Otherwise,
+		 * increment the sequnce number to the expected locked value
+		 * for comparison later on.
+		 */
+		old = cmpxchg(&hpet_save.seq, seq, new);
+		if (old == seq) {
+			time = hpet_readl(HPET_COUNTER);
+			WRITE_ONCE(hpet_save.hpet, time);
+
+			/* Unlock */
+			smp_store_release(&hpet_save.seq, new + 1);
+			local_irq_restore(flags);
+			return (cycle_t)time;
+		}
+		local_irq_restore(flags);
+		seq = new;
+	}
+
+	/*
+	 * Wait until the locked sequence number changes which indicates
+	 * that the saved HPET value is up-to-date.
+	 */
+	while (READ_ONCE(hpet_save.seq) == seq) {
+		/*
+		 * Since reading the HPET is much slower than a single
+		 * cpu_relax() instruction, we use two here in an attempt
+		 * to reduce the amount of cacheline contention in the
+		 * hpet_save.seq cacheline.
+		 */
+		cpu_relax();
+		cpu_relax();
+
+		if (likely(++cnt <= HPET_RESET_THRESHOLD))
+			continue;
+
+		/*
+		 * In the unlikely event that it takes too long for the lock
+		 * holder to read the HPET, we do it ourselves and try to

arch/x86/kernel/hpet.c static int hpet_cpuhp_notify(struct notifier_block *n, · line 871

Quoted 10 lines
arch/x86/kernel/hpet.c +10 -0
+		 * reset the lock. This will also break a deadlock if it
+		 * happens, for example, when the process context lock holder
+		 * gets killed in the middle of reading the HPET counter.
+		 */
+		time = hpet_readl(HPET_COUNTER);
+		WRITE_ONCE(hpet_save.hpet, time);
+		if (READ_ONCE(hpet_save.seq) == seq) {
+			if (cmpxchg(&hpet_save.seq, seq, seq + 1) == seq)
+				pr_warn("read_hpet: reset hpet seq to 0x%x\n",
+					seq + 1);

This is voodoo programming and actively dangerous.

CPU0 CPU1 CPU2
lock_hpet()
T1=read_hpet() wait_for_unlock()
store_hpet(T1)

		....			
		T2 = read_hpet()
unlock_hpet()				
					lock_hpet()
					T3 = read_hpet()
					store_hpet(T3)
					unlock_hpet()
					return T3
lock_hpet()
T4 = read_hpet()			wait_for_unlock()
store_hpet(T4)	
		store_hpet(T2)			

unlock_hpet() return T2

CPU2 will observe time going backwards.

Thanks,

tglx

#2 lore raw
Quoted 8 lines · Thomas Gleixner
Quoted 5 lines · Waiman Long

This patch attempts to reduce HPET read contention by using the fact that if more than one task are trying to access HPET at the same time, it will be more efficient if one task in the group reads the HPET counter and shares it with the rest of the group instead of each group member reads the HPET counter individually.

That has nothing to do with tasks. clocksource reads can happen from almost any context. The problem is concurrent access on multiple cpus.

You are right. I should have used CPU instead.

Quoted 5 lines
Quoted 3 lines

This optimization is enabled on systems with more than 32 CPUs. It can also be explicitly enabled or disabled by using the new opt_read_hpet kernel parameter.

Please not. What's wrong with enabling it unconditionally?

That is nothing wrong to enable it unconditionally. I am just not sure if that is the right thing to do. Since both you and Andy said we should enable it unconditionally, I will do so in the next version of the patch.

Quoted 5 lines
Quoted 5 lines

+/*
* Clock source related code
*/
static cycle_t read_hpet(struct clocksource *cs) {

Quoted 8 lines

arch/x86/kernel/hpet.c static int hpet_cpuhp_notify(struct notifier_block *n, · line 813

Quoted 6 lines
arch/x86/kernel/hpet.c +5 -1
-	return (cycle_t)hpet_readl(HPET_COUNTER);
+	int seq, cnt = 0;
+	u32 time;
+
+	if (opt_read_hpet<= 0)
+		return (cycle_t)hpet_readl(HPET_COUNTER);

This wants to be conditional on CONFIG_SMP. No point in having all that muck around for an UP kernel.

Will do so.

Quoted 75 lines
Quoted 47 lines
arch/x86/kernel/hpet.c +47 -0
+	seq = READ_ONCE(hpet_save.seq);
+	if (!HPET_SEQ_LOCKED(seq)) {
+		int old, new = seq + 1;
+		unsigned long flags;
+
+		local_irq_save(flags);
+		/*
+		 * Set the lock bit (lsb) to get the right to read HPET
+		 * counter directly. If successful, read the counter, save
+		 * its value, and increment the sequence number. Otherwise,
+		 * increment the sequnce number to the expected locked value
+		 * for comparison later on.
+		 */
+		old = cmpxchg(&hpet_save.seq, seq, new);
+		if (old == seq) {
+			time = hpet_readl(HPET_COUNTER);
+			WRITE_ONCE(hpet_save.hpet, time);
+
+			/* Unlock */
+			smp_store_release(&hpet_save.seq, new + 1);
+			local_irq_restore(flags);
+			return (cycle_t)time;
+		}
+		local_irq_restore(flags);
+		seq = new;
+	}
+
+	/*
+	 * Wait until the locked sequence number changes which indicates
+	 * that the saved HPET value is up-to-date.
+	 */
+	while (READ_ONCE(hpet_save.seq) == seq) {
+		/*
+		 * Since reading the HPET is much slower than a single
+		 * cpu_relax() instruction, we use two here in an attempt
+		 * to reduce the amount of cacheline contention in the
+		 * hpet_save.seq cacheline.
+		 */
+		cpu_relax();
+		cpu_relax();
+
+		if (likely(++cnt<= HPET_RESET_THRESHOLD))
+			continue;
+
+		/*
+		 * In the unlikely event that it takes too long for the lock
+		 * holder to read the HPET, we do it ourselves and try to

arch/x86/kernel/hpet.c static int hpet_cpuhp_notify(struct notifier_block *n, · line 871

Quoted 10 lines
arch/x86/kernel/hpet.c +10 -0
+		 * reset the lock. This will also break a deadlock if it
+		 * happens, for example, when the process context lock holder
+		 * gets killed in the middle of reading the HPET counter.
+		 */
+		time = hpet_readl(HPET_COUNTER);
+		WRITE_ONCE(hpet_save.hpet, time);
+		if (READ_ONCE(hpet_save.seq) == seq) {
+			if (cmpxchg(&hpet_save.seq, seq, seq + 1) == seq)
+				pr_warn("read_hpet: reset hpet seq to 0x%x\n",
+					seq + 1);

This is voodoo programming and actively dangerous.

CPU0 CPU1 CPU2
lock_hpet()
T1=read_hpet() wait_for_unlock()
store_hpet(T1)

		....			
		T2 = read_hpet()
unlock_hpet()				
					lock_hpet()
					T3 = read_hpet()
					store_hpet(T3)
					unlock_hpet()
					return T3
lock_hpet()
T4 = read_hpet()			wait_for_unlock()
store_hpet(T4)	
		store_hpet(T2)			
Quoted 7 lines

unlock_hpet() return T2

CPU2 will observe time going backwards.

Thanks,

tglx

That part is leftover code from my testing and debugging effort. I think using local_irq_save() should allow the critical section to be executed without interruption. In this case, I should be able to remove the threshold checking code without harm.

Thanks for the review.

Cheers,
Longman