From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753928AbbJSQZL (ORCPT ); Mon, 19 Oct 2015 12:25:11 -0400 Received: from mail-db3on0081.outbound.protection.outlook.com ([157.55.234.81]:17312 "EHLO emea01-db3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751441AbbJSQZI (ORCPT ); Mon, 19 Oct 2015 12:25:08 -0400 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=cmetcalf@ezchip.com; Subject: Re: [PATCH] string: Improve the generic strlcpy() implementation To: Rasmus Villemoes , Ingo Molnar References: <55F1DD53.1070102@ezchip.com> <20151005112700.GA1096@gmail.com> <87r3krowqt.fsf@rasmusvillemoes.dk> CC: Linus Torvalds , open list , Peter Zijlstra , Thomas Gleixner , "H. Peter Anvin" , Borislav Petkov From: Chris Metcalf Message-ID: <5625194F.30408@ezchip.com> Date: Mon, 19 Oct 2015 12:24:47 -0400 User-Agent: Mozilla/5.0 (X11; Linux i686 on x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 MIME-Version: 1.0 In-Reply-To: <87r3krowqt.fsf@rasmusvillemoes.dk> Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [12.216.194.146] X-ClientProxiedBy: DM3PR13CA0034.namprd13.prod.outlook.com (25.164.193.44) To HE1PR02MB0777.eurprd02.prod.outlook.com (25.161.118.141) X-Microsoft-Exchange-Diagnostics: 1;HE1PR02MB0777;2:R+w7j+wXqIU+33j16l9Ve5lZgDVu4XADj200lx5jS2typkyiDX6k+JmbhpbsML7sIBltk+YjsTZ/+CHoLUsybWH++Pw7VRJvg4N40b01qQC36YDSuJs+YbApPjlCvzArNCW4xBM45fC5AaYFc7kazp1I3OTQz2HfhiL3vSci5So=;3:QH4bGRSd3ZxysAp5vVyuxqNrDGJ+vB21zWqZ/B0BTFbNvVvKZ4D5ATdedAoD2So1SdIPRjKT1JForFOXTf62dL5F8I2r9B1eUp5/ffM2YacBSkB28DtyhYatpEIGccTy89lv+OjqHoUho52gXHTJVA==;25:Q34/QMmyoAIFKQlg6+b+dBLD2dy3DPNr97eTU9B7qFIvc6Yzk7rs37n58p8e1fPW6SGVseksrLdodGSMfMRbNQZ8lJVjzFNHxA22QXlUoYsV7C8r6/IGTx2YfNtP364UorRgsiuRBwm9xBNwVK0cGGDHcCtrHIw5zP2tr7x7+i/2H6iQ2Tv0Z2T3y5h8dqXWZWES78DQ2EtmrG8FEeB+ZclbS1dRRtWSz2WnuHzQpNIft7nhybSoe5gpGLS8iiuP;20:cUwsRIJZzzdXI4WisnYKR8amjM4tHcQQdDuPrK4oOelYj71Bj7NglKleGNun4QOeeTq20MJQTBZn/5I6dB+FI18Wz+fT7xDbTFt5fERvzfB8Tmdn+wX+5/ikl15fIcMWLyM/QmZzXxC6IZVSXkf6YYSv3N8kZE6AZ+fOjpjnF3o= X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:HE1PR02MB0777; X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(121898900299872); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(601004)(2401047)(8121501046)(5005006)(520078)(3002001);SRVR:HE1PR02MB0777;BCL:0;PCL:0;RULEID:;SRVR:HE1PR02MB0777; X-Microsoft-Exchange-Diagnostics: 1;HE1PR02MB0777;4:jOfEN5clr8GZUa8oeFcId8neDv5ccNvFYWqzLbTLD77YTqQmIRUbSEmf6zdTH4k7bXUJ3n0xd1QDeX0ho0T/ENH8a+1d0m56rrx8db2rt9UYhQVxMbmzQW+Js5aoZXPnIdNW/e0OnC8npnLqVtGyhIYiDmgvj9CDkrBlwBqGUdBh0ma3TJFN0LcROw0KYlJUPo5DCODvjn+fS23j9csCEZFDOywwO+NWu23wVg/AmRaowhTrInRRMXTdbXg/GgWWUYA3YrTK4omyFOaDbQrvCRLVnr7VIgK9kTItBAcoE6rlmMS4WyVZnrbk6AYlrFEBvZVMmrV1gyyBDicDXNJuCOY/OhKBnRCPtIMPiQT5PKY= X-Forefront-PRVS: 07349BFAD2 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(6009001)(6049001)(189002)(479174004)(199003)(377454003)(24454002)(19580405001)(46102003)(93886004)(189998001)(64126003)(23746002)(19580395003)(77096005)(122386002)(2950100001)(5007970100001)(5004730100002)(101416001)(36756003)(106356001)(50466002)(105586002)(76176999)(5001960100002)(47776003)(65816999)(50986999)(83506001)(65806001)(81156007)(40100003)(42186005)(15975445007)(54356999)(4001350100001)(66066001)(92566002)(87976001)(5001920100001)(65956001)(5001770100001)(97736004)(5008740100001)(86362001)(64706001)(33656002)(41533002)(18886065003);DIR:OUT;SFP:1101;SCL:1;SRVR:HE1PR02MB0777;H:[10.7.0.41];FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?Windows-1252?Q?1;HE1PR02MB0777;23:kCBVFXEziQypy8xFhsnRLeAtxQyqJXG5kO+L4?= =?Windows-1252?Q?vDEI3odrOAk2vRAApAQvLEe+owmy41zhS8WDrIOPgWjdnlhEBrB2zuhC?= =?Windows-1252?Q?dRudAtI4YkxLDxOHuadN1eawzqmRzMZS52OuJtLZ2WjSWHUNH9IA/NKR?= =?Windows-1252?Q?JDQITPOu7v0YGZyGNXWSsDUBi/rkK4TN3/kYY+/zu1GrsrLoeFk/2os/?= =?Windows-1252?Q?qGaI4wbGtIcAp3TiTIkk41m4Hca0FrWjGsgNTCdZcLPSb9Bgm10cGT5Y?= =?Windows-1252?Q?iCev+jFxIn04rbn07+3HAuVa8bGce0Or+I2StgTpO+1DzxXqrbQlj9L6?= =?Windows-1252?Q?8JAQ2TWuGVv9v4AAhAn09wYEot/r8htWiUiAsNSpo1/l1Yf7hC4lZJtI?= =?Windows-1252?Q?OXEG52B1BUBCsEwpViBZ9X9JCxvL6P9BOcITk2ZZA22PRSbMp0EiKlp9?= =?Windows-1252?Q?Xpvmd8OfAf4UWrYV8X3VPfk7SJ4/8tj9oh8ZjLSTupDsDLu6c/y9BpQy?= =?Windows-1252?Q?H//rqxlPh88G5DCZw1Z7ZabcDK8E4kkevlWs4skaUc/iLx1KAtXW6TvD?= =?Windows-1252?Q?Ap11DRup6A0KVU+k4QrchWWdjN3N5SSjg1UbzcGDqYWYvN7AXPrj5ten?= =?Windows-1252?Q?clWofw5s//JbgVL3hvkBkVcdPTrXXnI585LQqVAebK9VU9gwOQwVGG55?= =?Windows-1252?Q?mz1DjY8ccFaeXS3gLN/SM5sxcOn3LL0IiOxvO/NF0AimrdCBBUkAzNxl?= =?Windows-1252?Q?m956r921qtjHfnpCDd8QktShKYjs7+9uGXBDQAXvxHdyGjieOgtZsGt3?= =?Windows-1252?Q?kKrJJRtSZyxQugi25vIl4QhUfAVc9/PWKaz/1BpAmtudNuICuYVczTHX?= =?Windows-1252?Q?FcpxLEgvkGYW4en6ofI9q9xuYtdLIlbDmOU844qnYkB3umtYaVUZBNC2?= =?Windows-1252?Q?D1TCbkzj6kSAx8/bbXB8c4IpCIvGF63TubGRo7D9aIIo5BjnWko+ti8V?= =?Windows-1252?Q?bR6bhP0/nJSzID59eWycDZC841qP6K2FDyuNU8AZ/qqMSzkVKn5V5W7g?= =?Windows-1252?Q?awAeAi7Lck5UnzAqROruxt9uHJ/aAyeVkhXFPdIO0U2xVQqWnzPB/EFJ?= =?Windows-1252?Q?x2aqN3Yxg7MByr25srZ0lY8cnCOOtaWdC9IwIF/6IEKxeSN/0UwtcKSw?= =?Windows-1252?Q?j6li08DmLTES2U7YQN+C2OGCl7UpwVkc+grWibPh3cGIs5jVa/4naWfz?= =?Windows-1252?Q?OFVaLCfqt5BQC4v9Wfx9Uy5nRWyAPgOYvID3mtmZ5CXs8hfTP/B88CBZ?= =?Windows-1252?Q?CHZp0MMoaR5kV7YakPlf39+FUVTsjhJDUFVpioZ4WQBQ1l8JHmv+vUf+?= =?Windows-1252?Q?FSwhAu3in+ggjdIWAHwsouAxAh3/7LLBxvpoaE24DAWis6bIDwPE0Zji?= =?Windows-1252?Q?390zKcAptell2Nm2+cI?= X-Microsoft-Exchange-Diagnostics: 1;HE1PR02MB0777;5:SgKhbZUiSYapOY5qz/cfeY94n5uySFl7CgxhIjEwKDVl6GJS5tPqbVuD/Vq8ePYQ3VRpraGjc4loverROF2Is62wx5/zGWYcHo0rZ+XrmyAdIuWrxdNuhEsFYHsNExTquPyzAfiv2DvKGidPq8MSFg==;24:SyLXAlLtXfgbSJim4lTJgALCPWY/CvCQX4v+1RTV1UOHaor46OJuODzsbzgKnEXpttNCy+X6KNqDgdMiYGT/DHRsKFggS2szUcdQduz3A54=;20:/P2DIhCnnFeoQJht67TXCJhDBVqrugxaOpcjYHqmKE7qJb5bagbTNCUs8JLrgNpbGja/88FCaxwVV0oM5wk8nQ== SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: ezchip.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Oct 2015 16:25:02.7278 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR02MB0777 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 10/19/2015 08:42 AM, Rasmus Villemoes wrote: > On Mon, Oct 05 2015, Ingo Molnar wrote: >> Interesting. I noticed that strscpy() says this in its comments: >> >> * In addition, the implementation is robust to the string changing out >> * from underneath it, unlike the current strlcpy() implementation. > Apologies for beating a dead horse, but: > > c = *(unsigned long *)(src+res); > if (has_zero(c, &data, &constants)) { > data = prep_zero_mask(c, data, &constants); > data = create_zero_mask(data); > *(unsigned long *)(dest+res) = c & zero_bytemask(data); > return res + find_zero(data); > } > *(unsigned long *)(dest+res) = c; > > I wonder whether an insane compiler might actually reload c before > storing to dest+res, so that we'd have exactly the same problem of > embedded nul characters? Using READ_ONCE() on x86_64 with gcc 4.8 adds a bit of overhead; for some reason the compiler converts some mov instructions to lea+mov, unnecessarily as far as I can see. And looking at older compilers, gcc 4.4 substantially pessimizes the output with READ_ONCE, though perhaps we don't care about compilers that old. Still, your argument is certainly plausible in terms of actually guaranteeing the semantics we are claiming rather than leaving it to the compiler to Do The Right Thing. Unsurprisingly, the current code is causing the compiler to do the right thing, but of course past performance is no guarantee of future results, as they say. So I'm kind of on the fence, maybe leaning slightly towards thinking the READ_ONCE semantics is worth it. Maybe someone with a tip gcc can see if the performance difference has been fixed there? -- Chris Metcalf, EZChip Semiconductor http://www.ezchip.com