Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933250AbcDLQxB (ORCPT ); Tue, 12 Apr 2016 12:53:01 -0400 Received: from mail-by2on0110.outbound.protection.outlook.com ([207.46.100.110]:25600 "EHLO na01-by2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1757206AbcDLQw7 (ORCPT ); Tue, 12 Apr 2016 12:52:59 -0400 Authentication-Results: amacapital.net; dkim=none (message not signed) header.d=none;amacapital.net; dmarc=none action=none header.from=hpe.com; Message-ID: <570D245A.3020306@hpe.com> Date: Tue, 12 Apr 2016 12:37:46 -0400 From: Waiman Long User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12) Gecko/20130109 Thunderbird/10.0.12 MIME-Version: 1.0 To: Andy Lutomirski CC: Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , Jonathan Corbet , "linux-kernel@vger.kernel.org" , , X86 ML , Jiang Liu , Borislav Petkov , Andy Lutomirski , Scott J Norton , Douglas Hatch , Randy Wright Subject: Re: [PATCH v3] x86/hpet: Reduce HPET counter read contention References: <1460405379-25294-1-git-send-email-Waiman.Long@hpe.com> In-Reply-To: Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [72.71.243.229] X-ClientProxiedBy: CO2PR18CA0010.namprd18.prod.outlook.com (10.161.80.20) To AT5PR84MB0307.NAMPRD84.PROD.OUTLOOK.COM (10.162.138.29) X-MS-Office365-Filtering-Correlation-Id: aa2bf5a2-17aa-4aba-5f35-08d362f0cdac X-Microsoft-Exchange-Diagnostics: 1;AT5PR84MB0307;2:Q1fYhI6HgQsnPViy4EQOThFyND4iQFLXpMYiJBCqTsQgjfFc4FyCNveO1Lc2sacxiT9nveB3IA1AvbAsqqc97SDMOJHBnljy1KMMeY61XET+okwZlllXuxUD0GpBP5emNQMllAcUwZsPhIFcBDDhiMpc5egUlQxKCA+OVvz0dE8rO2po9z/mT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JYbNDTDTGbMke3v/wZ5KERJDGcpG/H8+N8QOCYLx/M7hECKk3R/vL6KyHZOMDIQV7NHXTHz08+pmAIKh/1kFJYbx5RMBoFrWtBcLr+rxQKHpI1Jhnf66LAG5HFLlFpsHT09hoKcLlukO0Zuzb/IYeX34bOQY5Z6e0Nd8xgTQzMjClF4frrukKc7JzT3Kr9+30G/zBHiDYU7qQH/ik2xkHdxZAuduvHqvVjrD7j/I/0IyRncEDBbfhORtQXjjpA7jAbkXCKUatRwHklzMRUGrm2dICeEUZb4osnuY33v9+KOvU6HXefM/lbQ== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(601004)(2401047)(5005006)(8121501046)(3002001)(10201501046)(6055026);SRVR:AT5PR84MB0307;BCL:0;PCL:0;RULEID:;SRVR:AT5PR84MB0307; X-Forefront-PRVS: 0910AAF391 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(4630300001)(6009001)(6049001)(54094003)(377454003)(24454002)(23676002)(117156001)(83506001)(19580395003)(2906002)(19580405001)(42186005)(230700001)(80316001)(36756003)(4326007)(64126003)(1096002)(110136002)(50466002)(189998001)(66066001)(65816999)(65806001)(54356999)(2950100001)(87266999)(65956001)(76176999)(50986999)(5008740100001)(33656002)(3846002)(5004730100002)(575784001)(86362001)(6116002)(586003)(47776003)(81166005)(92566002)(77096005);DIR:OUT;SFP:1102;SCL:1;SRVR:AT5PR84MB0307;H:[192.168.142.155];FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtBVDVQUjg0TUIwMzA3OzIzOnpFWjRCWklhdkxrdWVxQmNUejhNd2FDcitr?= =?utf-8?B?VWw4ZHJ2Mk9GKzFyZWtteW4weGVINFpNd2hDWk1hUks5emFnNHlJNEprVkVL?= =?utf-8?B?QllOY1pCcTN3bFEzUnZkZWZYQ2RvSVh3MGRGRUJ3d3laTW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MXpya0JwUG5Qb2t6TFdPMEZ2Q2JHVDZu?= =?utf-8?B?NUUvZVErMitqVFRnWEdkbmJTN0tmQml6S1pSeVpNa0FvbFVMMnZKczFnWDky?= =?utf-8?B?Yy9nTWhQTnhWUURoWjNMMzV6SXNXd1NYVzJiVldWZkMvVWxiM0J2TUVVUmln?= =?utf-8?B?TThvRm5VMTFld1ZESEI5TUZxVktjWTdtZVlXc0dXcGNlSldHdUV4MDVOWUVO?= =?utf-8?B?dnRqQS9hQkJaaTcyM3poWHFVTXR5WWJBYnFvTE1jUHBaZ2tZUWh3emxYMmZj?= =?utf-8?B?Q3N5TEF4R0poaitXa2lVWS96WExhZE0waHB5YTZGcldGQlJJc2J1TkI4eE80?= =?utf-8?B?SVcvV0J4QTJLd3dhZmR5MVhzOTYvMVpHaHBiU2lCVE1ibFR5MFRmQ1VBTDN5?= =?utf-8?B?TlJCalNkTk51U25rYmFMSjhYbER6MkhNdHUrRzF6WWx3MXlyblBkM3pieUhW?= =?utf-8?Q?cc96N89eERBPckSK9skbmgxo0=3D?= X-Microsoft-Exchange-Diagnostics: 1;AT5PR84MB0307;5:y/J8R6H5ij8RWbAOzPf2JHekr4XP2nr04JqsPLcm7yNDA26FWQPwLPvvW183sUjV8RtmfHR+SpUVSSD4GDEWVHxyC6zFlbIgLeOXn+oCxWHcYd+3+4iSsumUsnpEki2QuR6x5bK1r6N/OU6qEnnZZQ==;24:pfLHpLUCVCv/p36O+Wologdh4tjsYRwk2SM3iay5SunJwON2b6X/MRy/t47cJo5o99x0awjceaqJoexiudxzjTrTZdwbHrBiHib4MSb6LK4= SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: hpe.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Apr 2016 16:37:54.3075 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: AT5PR84MB0307 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 8078 Lines: 203 On 04/11/2016 04:21 PM, Andy Lutomirski wrote: > On Mon, Apr 11, 2016 at 1:09 PM, Waiman Long wrote: >> 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 CPUs are trying to access HPET at the same time, >> it will be more efficient if one CPU 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 CPU 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. This change is enabled on SMP configuration. >> >> 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%. >> >> Signed-off-by: Waiman Long >> --- >> v2->v3: >> - Make the hpet optimization the default for SMP configuration. So >> no documentation change is needed. >> - Remove threshold checking code as it should not be necessary and >> can be potentially unsafe. >> >> 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. >> >> arch/x86/kernel/hpet.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ >> 1 files changed, 83 insertions(+), 0 deletions(-) >> >> diff --git a/arch/x86/kernel/hpet.c b/arch/x86/kernel/hpet.c >> index a1f0e4a..ff1f140 100644 >> --- a/arch/x86/kernel/hpet.c >> +++ b/arch/x86/kernel/hpet.c >> @@ -759,12 +759,95 @@ static int hpet_cpuhp_notify(struct notifier_block *n, >> #endif >> >> /* >> + * Reading the HPET counter is a very slow operation. If a large number of >> + * CPUs are trying to access the HPET counter simultaneously, it can cause >> + * massive delay and slow down system performance dramatically. This may >> + * happen when HPET is the default clock source instead of TSC. For a >> + * really large system with hundreds of CPUs, the slowdown may be so >> + * severe that it may actually crash the system because of a NMI watchdog >> + * soft lockup, for example. >> + * >> + * If multiple CPUs are trying to access the HPET counter at the same time, >> + * we don't actually need to read the counter multiple times. Instead, the >> + * other CPUs can use the counter value read by the first CPU in the group. >> + * >> + * A sequence number whose lsb is a lock bit is used to control which CPU >> + * has the right to read the HPET counter directly and which CPUs are going >> + * to get the indirect value read by the lock holder. For the later group, >> + * if the sequence number differs from the expected locked value, they >> + * can assume that the saved HPET value is up-to-date and return it. >> + */ >> +static struct { >> + /* Sequence number + bit lock */ >> + int seq ____cacheline_aligned_in_smp; >> + >> + /* Current HPET value */ >> + u32 hpet ____cacheline_aligned_in_smp; >> +} hpet_save; >> +#define HPET_SEQ_LOCKED(seq) ((seq)& 1) /* Odd == locked */ >> + >> +/* >> * Clock source related code >> */ >> +#ifdef CONFIG_SMP >> +static cycle_t read_hpet(struct clocksource *cs) >> +{ >> + int seq; >> + >> + 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) { >> + u32 time = hpet_readl(HPET_COUNTER); >> + >> + WRITE_ONCE(hpet_save.hpet, time); > I think a plain store is okay due to the smp_store_release below. You were right. A plain store should be good enough. > >> + >> + /* 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(); >> + } >> + >> + return (cycle_t)READ_ONCE(hpet_save.hpet); >> +} > I wonder if this could be simplified. Pseudocode: > > u32 time; > unsigned long flags; > > local_irq_save(flags); > > if (spin_trylock(&hpet_lock)) { > time = hpet_readl(HPET_COUNTER); > WRITE_ONCE(last_hpet_counter, time); You will need a spin_unlock(&hpet_lock) here. > } else { > spin_unlock_wait(&hpet_lock); > /* When this function started, hpet_lock was locked. Now it's > unlocked, which means that time is at least as new as whatever the > lock holder returned. */ > time = READ_ONCE(last_hpet_counter); > } > > local_irq_restore(flags); > return time; > > Should be fasterunder heavy contention, too: spinlocks are very nicely > optimized. I don't think it will be faster. The current spinlock code isn't more optimized than what you can do with a cmpxchg and smp_store_release. In fact, it is what the spinlock code is actually doing. Other differences includes: 1) A CPU will not do local_irq_save/local_irq_restore when the lock is not free. 2) My patch also use a change a sequence number to indicate an updated time stamp is available. So there will be cases where CPUs running your code will have to wait while the those running my code can grab the time stamp and return immediately. > I *think* this is sufficiently atomic. > > --Andy Cheers, Longman