Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758085AbcCaWM4 (ORCPT ); Thu, 31 Mar 2016 18:12:56 -0400 Received: from mail-bl2on0142.outbound.protection.outlook.com ([65.55.169.142]:35679 "EHLO na01-bl2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1757331AbcCaWMy (ORCPT ); Thu, 31 Mar 2016 18:12:54 -0400 Authentication-Results: infradead.org; dkim=none (message not signed) header.d=none;infradead.org; dmarc=none action=none header.from=hpe.com; Message-ID: <56FDA0D6.4090904@hpe.com> Date: Thu, 31 Mar 2016 18:12:38 -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: Peter Zijlstra CC: Ingo Molnar , , Scott J Norton , Douglas Hatch , Will Deacon Subject: Re: [PATCH] locking/qrwlock: Allow multiple spinning readers References: <1458444079-59601-1-git-send-email-Waiman.Long@hpe.com> <20160329202050.GN3408@twins.programming.kicks-ass.net> In-Reply-To: <20160329202050.GN3408@twins.programming.kicks-ass.net> Content-Type: text/plain; charset="ISO-8859-1"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [72.71.243.178] X-ClientProxiedBy: BY2PR12CA0005.namprd12.prod.outlook.com (10.160.121.15) To CS1PR84MB0311.NAMPRD84.PROD.OUTLOOK.COM (10.162.190.29) X-MS-Office365-Filtering-Correlation-Id: f1dd0b2e-c7f5-4932-dffa-08d359b19a2c X-Microsoft-Exchange-Diagnostics: 1;CS1PR84MB0311;2:eh736CVDuD6s1O0wLJQED7hxcTOXWoR7y4qFSx0BD4TJnJh9/fK/EQUOA/d2MqwsbNk0qCBMHfG4JhFVDf02Yv8uDqUJC/SsEt81T3AhkBzD42KqQZDsKAMNOUqESglVqELShkxTtdyTOBGuiAzs54QkOBVUY2Rz8TwORUtHq5HxoJqTKDNIjMBOGP4MVX8g;3:lwE95RHv0AU8UkDFrEEfdZIqthOLfWhF2r5BhUSrAxXe974PpO9WNEHypC3hujQY658L6RcwmB+ZvMudT1C953ekXkitEXP1eXVr6/4j7XZgI+R+Gjvp56piO7u7VHrq;25:PEzG5lFdl2bKvlzlDcwI2A5VSAPYsiTcbZNmXlVsBaRo2MGUbDWdAlP7PZnRFgjSKmwIwGQeNqicBqo54bXO0wo/FEtTPGddUM6ZI/RjOiMIXaT83K9XM/joF4qFz92FN7wEW/7xTio5HO2mI9fhYkT+uz3zh++J2t+Z8bu1r4toKIMiX0N10ZqhLESlZCvSmslwjnBG65ZI8ksbtqQrRxWskCzBLk5eW7DZVwMLCqtzJ2EQ7kHaF4dwFFEhGe0x9ZJ+j/Ib2xe+Vza74ireVaPvMUMnugmRxRktll5G+I6WroSFRk650viB3tMznjLRewHewdTmRbQFvpXhq+0n/s6YGzvHk+XAtu2tzyYzQcg= X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:CS1PR84MB0311; X-LD-Processed: 105b2061-b669-4b31-92ac-24d304d195dc,ExtAddr X-Microsoft-Exchange-Diagnostics: 1;CS1PR84MB0311;20:MJSQO8KtmsBf68hX2uHza+/mOKUw/eTPHOVpJrCalJvj7mpuNrgNOjAO4TszOErfALLwxOXuBGTtPxlyv3t2yCqUAfq8z0WpCiZiosmHsQOo/8Z0/zPI71z9VHgK1TIMB/9dA5NdS+jcAlKzbtLQOm38xaW0xknos27AN7QlDtejUNB/iHjWI4VOR94GOvmAFkfmcyS0qBInPgvYhThutSuTDYWOwnT9q1b/giYt+T+vGUjE/8p4eUFF/AXGUfDap5f2BRJyEmbpvdJ0HFyg4nMLhEf3gx3ktgDh9Z3zccmNsOVpYBJu0VP83JwX1UEEVlk7p7R8/sh6DmSDvRL0Fw==;4:NFZYfoAkemmwHS8ybEt1r5pcy+FeyR17B5YXencCgoZZ/wiCAZb2DtULuAxgRrMEdBwqa9yGfCJXSsJgCsmjjAIrAPZknJsKV+NQQ80LdyVgreth1elDR8hy/QoLiqq7ANPLGyso8L6awU3AxEn5SN8K2VWwWL/ptT/iLvxKpXTx7FXlQSJUwIBstm42kKeGfQvWSAL5gl0tRJHuY4vVuL9yDrHPu6W6MlHPXs6vdRTOW02Msd4a7SdLyNJcFBvlkLZH12uDDdC/7po5ZCF2M7yNClxqpO2uQqEvIgGaXRJl5yKGcisWyX2H6+eOjGtVU20K/KlyPLO7bXsWooF02Tcwe/SMliR7bEVUac64185JxlhnljxqTKO9YrzLP3MI X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(601004)(2401047)(8121501046)(5005006)(10201501046)(3002001);SRVR:CS1PR84MB0311;BCL:0;PCL:0;RULEID:;SRVR:CS1PR84MB0311; X-Forefront-PRVS: 0898A6E028 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(4630300001)(6009001)(6049001)(24454002)(54534003)(377454003)(3846002)(2906002)(6116002)(36756003)(4326007)(23756003)(117156001)(50466002)(586003)(2950100001)(81166005)(77096005)(66066001)(59896002)(5004730100002)(189998001)(64126003)(5008740100001)(110136002)(4001350100001)(50986999)(65816999)(76176999)(54356999)(87266999)(230700001)(47776003)(42186005)(65806001)(65956001)(1096002)(86362001)(83506001)(92566002)(33656002);DIR:OUT;SFP:1102;SCL:1;SRVR:CS1PR84MB0311;H:[192.168.142.148];FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: =?iso-8859-1?Q?1;CS1PR84MB0311;23:qiVnYq7OQR8a+aM8osCWiEKRbzP3BjYgaUlFh0A?= =?iso-8859-1?Q?s9IWpirxQk7vVugiO0IGjG+vm+FVVqeKDZdjiRTCztcgL+O6tTAv0xyllj?= =?iso-8859-1?Q?o0Vmr+uEJc4NH/Tz+2VQoRHDYmDn+uV/7Cs4tBoJ1uj27QAZr65WGa4thH?= =?iso-8859-1?Q?7qJ2KiP05v8Im+HeWMwbJdmhA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Q?+JLpCAxGdCZN1smxG27rKkbaCCWn1NXkkrEDvkNOy25NVnZKB4YL4MZqyQ?= =?iso-8859-1?Q?B7dSj?= X-Microsoft-Exchange-Diagnostics: 1;CS1PR84MB0311;5:34+gyCo69VSxvPwNdCM9lNLkLNJQDYRArw6aeOaOFntpRDlKIw6GXt2MsxEA8oPJFYTTD8F7xS/EcIP0tQZkC4cpPiAMY9re2q5IZ+n/DDvK0slyoyWUxr9jH9VsLpdh1S7uWvzFK7XaTZMboZsLAA==;24:4EqUbhR74JE0eNvtz0Y5yg+KoqiRBJTEqCSX/1ORXYs6xpf5EXAkPBXnepyH84rGztHLTqxlwxinvKLHSGvyzP6HvdA6FVr+VU508ljybPU= SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: hpe.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Mar 2016 22:12:50.9305 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: CS1PR84MB0311 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3875 Lines: 94 On 03/29/2016 04:20 PM, Peter Zijlstra wrote: > On Sat, Mar 19, 2016 at 11:21:19PM -0400, Waiman Long wrote: >> In qrwlock, the reader that is spining on the lock will need to notify >> the next reader in the queue when the lock is free. That introduces a >> reader-to-reader latency that is not present in the original rwlock. > How did you find this 'problem'? I am constantly on the lookout for twists that can make the code run faster. That change turn out to be good for reader performance and so I send it out to solicit feedback. >> That is the price for reducing lock cacheline contention. It also >> reduces the performance benefit of qrwlock on reader heavy workloads. >> >> However, if we allow a limited number of readers to spin on the >> lock simultaneously, we can eliminates some of the reader-to-reader >> latencies at the expense of a bit more cacheline contention and >> probably more power consumption. > So the embedded people might not like that much. It could be. It is always a compromise. >> This patch changes the reader slowpath to allow multiple readers to >> spin on the lock. The maximum number of concurrent readers allowed >> is currently set to 4 to limit the amount of additional cacheline >> contention while improving reader performance on most workloads. If >> a writer comes to the queue head, however, it will stop additional >> readers from coming out. >> >> Using a multi-threaded locking microbenchmark on a 4-socket 40-core >> Haswell-EX system, the locking throughput of 4.5-rc6 kernel with or >> without the patch were as follows: > Do you have an actual real world benchmark where this makes a > difference? Not yet. Will look out for some real world workload. >> /** >> * queued_read_lock_slowpath - acquire read lock of a queue rwlock >> * @lock: Pointer to queue rwlock structure >> * @cnts: Current qrwlock lock value >> */ >> void queued_read_lock_slowpath(struct qrwlock *lock, u32 cnts) >> { >> + bool locked = true; >> + >> /* >> * Readers come here when they cannot get the lock without waiting >> */ >> @@ -78,7 +71,10 @@ void queued_read_lock_slowpath(struct qrwlock *lock, u32 cnts) >> * semantics) until the lock is available without waiting in >> * the queue. >> */ >> + while ((cnts& _QW_WMASK) == _QW_LOCKED) { >> + cpu_relax_lowlatency(); >> + cnts = atomic_read_acquire(&lock->cnts); >> + } >> return; >> } >> atomic_sub(_QR_BIAS,&lock->cnts); >> @@ -92,14 +88,31 @@ void queued_read_lock_slowpath(struct qrwlock *lock, u32 cnts) >> * The ACQUIRE semantics of the following spinning code ensure >> * that accesses can't leak upwards out of our subsequent critical >> * section in the case that the lock is currently held for write. >> + * >> + * The reader increments the reader count& wait until the writer >> + * releases the lock. >> */ >> cnts = atomic_add_return_acquire(_QR_BIAS,&lock->cnts) - _QR_BIAS; >> + while ((cnts& _QW_WMASK) == _QW_LOCKED) { >> + if (locked&& ((cnts>> _QR_SHIFT)< MAX_SPINNING_READERS)) { >> + /* >> + * Unlock the wait queue so that more readers can >> + * come forward and waiting for the writer to exit >> + * as long as no more than MAX_SPINNING_READERS >> + * readers are present. >> + */ >> + arch_spin_unlock(&lock->wait_lock); >> + locked = false; > Only 1 more can come forward with this logic. How can you ever get to 4? Yes, each reader in the unlock path will release one in the queue. If the next one is also a reader, it will release one more and so on until the reader count reach 4 where the process will stop. > > Also, what says the next in queue is a reader? I did say in the changelog that the queue head could be a writer. In that case, the process will stop and the writer will wait until all the readers are gone. Cheers, Longman