Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965896AbcDLTLd (ORCPT ); Tue, 12 Apr 2016 15:11:33 -0400 Received: from mail-bl2on0139.outbound.protection.outlook.com ([65.55.169.139]:65375 "EHLO na01-bl2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S965342AbcDLTLb (ORCPT ); Tue, 12 Apr 2016 15:11:31 -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: <570D4491.3020408@hpe.com> Date: Tue, 12 Apr 2016 14:55:13 -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> <570D245A.3020306@hpe.com> In-Reply-To: <570D245A.3020306@hpe.com> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [72.71.243.229] X-ClientProxiedBy: BY1PR0201CA0033.namprd02.prod.outlook.com (10.160.191.171) To DF4PR84MB0315.NAMPRD84.PROD.OUTLOOK.COM (10.162.193.29) X-MS-Office365-Filtering-Correlation-Id: f2416dbb-c27d-4c81-f9c2-08d3630402c8 X-Microsoft-Exchange-Diagnostics: 1;DF4PR84MB0315;2:bgzKe15mFV2xxU+27wxQUmoVyvEIP2AHtEheZ9od7IFw1fA8ILhPfZ5ZmB/HzoCeHAgbZCWeNyOVvpyfOVZO57ryyR2al0NixG6mDVGHFlfGeWZ8/RjrwLabIKemUqkZ1DtnznlMjS4XoPWcm0hUrmtuTUyPOvK1+2wKCLw6PZExuuSuRc1jtzeQK2K0r3k4;3:bEWD4RtuJUw1qXFsaMqjXRr8fY0X5mAs17tBl6GGLlkYoE9Wei2C6RVwawDk+oL+1UEbUP2cY2eKxZqMAF34dEcaeq0kkjQ0GRMeKQHuwYyarRTYpGAY7LC0AtJ4oDF2;25:Wbim1uuPvsA0NnzYaPpX89uO/1uflpminNIkYD6SeB3HtB89kHyEYySMzFto48p1H51utZ3uiyVYlMAxYaeYthYnBt5hAlT5BSbptWirhpSl2X3Hj+38tnBfpM39kQpMe5IVhsRAdacLs9+v1sCc6N4ZpX8p9cgGs3ZtiO8Hj4pwxAvD6Q3k3GPQdLapULX4msiG13U+b8HDkICjC/iHiPE051MK29mliERqUum3SWt44FCtsnWZoh+rZDk2PASIu+tOr8A7zl7P7GRfbGQ4tqY3dgQNrC9R5Go4oy8M0SA2bgwr2AA9BRzR47ctvMyqwTUNlJvy5ugitLVgfRyMlQ== X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:DF4PR84MB0315; X-LD-Processed: 105b2061-b669-4b31-92ac-24d304d195dc,ExtAddr X-Microsoft-Exchange-Diagnostics: 1;DF4PR84MB0315;20:71wY1wuwgX7CV87TUCUG6gH+CWYWErFUtfwfUkvImUBEdBEMO1UdxyENwwOBjUiS709gf3HcnF32TeCkGHaNlJlU8Ho/coLo0smhhIMYHHI5qF5UFFcIBzl6ifr7a+KmhiFq8peLlykvbtuXbqbz5Fuf82CIgoYLcKl3poa+is2VXnTFYpvLVsVSpyIkfNoUdk+PhhqV5g2fGleARTDXk/7BJ6XAL/AI10r9WFagU9nuPythMhZuF13NECmxzt4R+EzstJb5vMj4ViRntTzyvONYSRunfNUQYmTHmi/Qfedm+1EhqWXI0xnIP94O0giQZnaD4NCfz4/ZnBg6Xut2vA==;4:cDn0UnKMRsF+mkCIf/U0V+Ky0kQ4FZwpib/18OiaiJRxNCyJTTlq00+P+0jT00oi95huJ0hQ0z26uMo3dCeoNtSW3ojKowYLwi+ohafQvyi36zVsZyil7ws/BweOTSS+zR15TcSaJ6vHh0ys9VjFWP15zbYm/Mt4JuGPDNHGQXT6Gwg4QAWFi82dwoMQfYdKygVFUQPZkqn6eugvyRpGX5j3PDXqJ0Xq++60wtUidEZRubFQTYRYOPys9r+8OXqfJ3P7qotWTtkV5CEMARieNGDG97begEQmSYkpGQRNKcvbSqPXJYKEfYSxPe8yL8aFQXjD9aSwBillQKR8xVar73Rtt0S4aZ7Thfgymxvwg0diRZFhJEHE6HDDTOo+q/fqzzihtgI0aaayXl/HcAk4HA== 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)(10201501046)(3002001)(6055026);SRVR:DF4PR84MB0315;BCL:0;PCL:0;RULEID:;SRVR:DF4PR84MB0315; X-Forefront-PRVS: 0910AAF391 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(4630300001)(6049001)(6009001)(54094003)(24454002)(377454003)(4326007)(586003)(3846002)(5004730100002)(50466002)(77096005)(5008740100001)(80316001)(1096002)(19580395003)(189998001)(230700001)(6116002)(86362001)(81166005)(50986999)(36756003)(23676002)(83506001)(2950100001)(2906002)(87266999)(64126003)(76176999)(110136002)(54356999)(92566002)(33656002)(42186005)(66066001)(65956001)(65816999)(47776003)(117156001)(65806001);DIR:OUT;SFP:1102;SCL:1;SRVR:DF4PR84MB0315;H:[192.168.142.155];FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtERjRQUjg0TUIwMzE1OzIzOnQwb05LMGRmbWR3VWIxZFd4VitJWktBa0JP?= =?utf-8?B?b1p6YzFIRGh3TFN3U3dDVEthU3p6dTVjTWxSMUhCLzBFTndzZTNWZG1YU2Vt?= =?utf-8?B?QlJvbDM3YWxJTyt4T2g4d1ZGM2hzSDZIZkV5NWwycit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VlFyS3JoL21HbGg0em42eWpVQloy?= =?utf-8?B?NXlOSlkyUHFlZGpISEI3T3doNktXdEF3R3RiaFF5MkhjWjByTG40aDNDeUky?= =?utf-8?B?TWN1MHRBaVpWbDk3UDVVNVMxeXFMNWZyNTlCdStodUsvTFNDaHZMR1d0dDQ4?= =?utf-8?B?ZFM5YTVKb3dhWU56aldCNHFJdFliTnRrL08vRFczdlNpN3JRZHBMTFhwUmlK?= =?utf-8?B?MHo0TVlZbi9TenJjWjcxZC9ld2h4MXNqa0hoc09rSmJaTXlRUldMOWU3OG5h?= =?utf-8?B?VUFPbG0vc1prT2FFTm9Cd2IvOFdzK0R2MENWbU8yYzRDY3h0dnZCOCs4eVhn?= =?utf-8?B?dnZKVVNqYzFNVUIvVGVEaTJpTGtYKzlobVFad0hoM2N3bFdBa3V0Z0hYV0dI?= =?utf-8?Q?RP6GwDdvi/0qExtzQ4lplLBbegC?= X-Microsoft-Exchange-Diagnostics: 1;DF4PR84MB0315;5:2mF9BuN4+hxyYfYB5uGRLAWWBWEJHUPUgbbJHAiYB415K0u6xytjyIaObcQMXlJmsKrcngbJQS9iRgk0OUdZ3Z67O3K+f+I/oeAgkONOWLKVBu2roP4oDUzhQC9ei8lsnMNU4VGQcWLoux4TDQurlg==;24:TxbQmH4+4ADtDHLkZjJQbLlRWVUI3Ocm4tnHWf2vL/PB3WJ0rrx3tyHWe5jwH6Vfhf/TVGPtRg4fILhLKfGk+sXyopWo7yURT6hnu7mvvuo= SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: hpe.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Apr 2016 18:55:24.4146 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DF4PR84MB0315 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2827 Lines: 81 On 04/12/2016 12:37 PM, Waiman Long wrote: > On 04/11/2016 04:21 PM, Andy Lutomirski wrote: >> >>> + >>> + /* 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. > Moreover, if the timing is such that right after one CPU release the lock, the next one get it immediately and there is a continuous stream of incoming CPUs. It is possible that the ones that are waiting for the lock to be free will see the lock being acquired for an indefinite period of time. This is certainly not something we want to have and this is what the sequence number is for. Cheers, Longman