Received: by 10.223.176.46 with SMTP id f43csp470540wra; Wed, 24 Jan 2018 00:48:59 -0800 (PST) X-Google-Smtp-Source: AH8x227rzro+0TSh0GLixRCSpUK4o0SyASxG8jdVuMcYWEA686qhI+FSRN51aOyEYJRwUnSnJUOt X-Received: by 10.99.111.68 with SMTP id k65mr10381967pgc.446.1516783738972; Wed, 24 Jan 2018 00:48:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516783738; cv=none; d=google.com; s=arc-20160816; b=iuaaFP5pz2c4a0WhwfDHbTT8lqNMICvhFWxvc0rezyima4W/xImb32kNrrzyntuCoz 5zw0uixFyLcA2En9kWO0p2VtfXtDsJoVVx6ZWpHkzDVY9C7iCjns+Rl/BlaeyvLrlovU L4C9xaorX/f1mdsfSw2kbFou8oZjhxtWQ6I/by4XnMg/xPCRZUEVSsoZgpeL5oHfWTrk mo8p9zHR5Wvyc/wJd1NfGbdyKAFiznJI+zQGCJ6eMNqsxbHtNXO6H73t9i+2dSpoVzH9 +LutTFpJL7D7E8UHoMoaZsWo3UsYhNSCvmpNbouXpZwILRXtKrvQPUDlMfNxHcyVXTjQ aJIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature :arc-authentication-results; bh=xMCW2/9OlMv64dp3i/lVDcl9/3MdGCIRjmtNVdgrVkQ=; b=kQnAvOi8FPwh42QQIGGCPz1nrfbDFEf3gMagF/5++DA/WcrLz2RhamqGdNb4cb2v7d 9HKQfAvr8Rgcy9tXe11ThNEBVj7cF+CJ094rpsjeRKSuEtKqL3hcZekxDxu/aVRAQM3a ch4Bc4+EJ5rRsm8itqOxf1BhopD9Jh7uUdJo3eH2buredPtJnHRhGr4FzEWsN4nwoXn6 mbpxqeXZcAGzfGZVvMu/leJfi/yx5VaDJ4w3c0pniynGJnWUz54IexBMl4xQqzCmqvSZ GbXX4r7Jce3pkZuEu3llp+vfxYw+ytsJ3S9isCARt+EjUzROgnuRt3UbuQiVTCneRwpF mCCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@prevas.dk header.s=ironport1 header.b=J9xZy+Cn; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l67si2606873pfa.321.2018.01.24.00.48.45; Wed, 24 Jan 2018 00:48:58 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@prevas.dk header.s=ironport1 header.b=J9xZy+Cn; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932515AbeAXIrp (ORCPT + 99 others); Wed, 24 Jan 2018 03:47:45 -0500 Received: from mail01.prevas.se ([62.95.78.3]:19248 "EHLO mail01.prevas.se" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752624AbeAXIrn (ORCPT ); Wed, 24 Jan 2018 03:47:43 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=prevas.dk; i=@prevas.dk; l=1619; q=dns/txt; s=ironport1; t=1516783663; x=1548319663; h=subject:to:cc:references:from:message-id:date: mime-version:in-reply-to:content-transfer-encoding; bh=byU9BCQlsJ5L8fEE/egd2euBkwFgXWKpihHs6Vb1cZg=; b=J9xZy+CnXF6vsbwHxdDctnAsOOu13fTv6xKZfLEyITDMv+xFkhAbSTIZ 7hvsYBRYoCGCr2EmM6gHZjUUWmMxmAGTqqa9n91Gsgu6N9RK0JpYYhgUg R5m52VogAjt7haKQKFH0upRumKtenp31dAK1dxjjBPbV3B3xoulS8d1Mm I=; X-IronPort-AV: E=Sophos;i="5.46,406,1511823600"; d="scan'208";a="3119452" Received: from vmprevas4.prevas.se (HELO smtp.prevas.se) ([172.16.8.104]) by ironport1.prevas.se with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Jan 2018 09:47:42 +0100 Received: from [172.16.11.49] (172.16.8.31) by smtp.prevas.se (172.16.8.104) with Microsoft SMTP Server (TLS) id 14.3.361.1; Wed, 24 Jan 2018 09:47:41 +0100 Subject: Re: [PATCH] lib/strscpy: remove word-at-a-time optimization. To: Andrey Ryabinin , Andrew Morton , Linus Torvalds CC: , Kees Cook , Eryu Guan , Alexander Potapenko , Chris Metcalf , David Laight , Dmitry Vyukov , References: <20180109163745.3692-1-aryabinin@virtuozzo.com> From: Rasmus Villemoes Message-ID: <317d8c15-b4af-c0d6-31c9-30e311327b3f@prevas.dk> Date: Wed, 24 Jan 2018 09:47:41 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 In-Reply-To: <20180109163745.3692-1-aryabinin@virtuozzo.com> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [172.16.8.31] Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2018-01-09 17:37, Andrey Ryabinin wrote: > strscpy() performs the word-at-a-time optimistic reads. So it may > may access the memory past the end of the object, which is perfectly fine > since strscpy() doesn't use that (past-the-end) data and makes sure the > optimistic read won't cross a page boundary. > > But KASAN doesn't know anything about that so it will complain. > There are several possible ways to address this issue, but none > are perfect. See https://lkml.kernel.org/r/9f0a9cf6-51f7-cd1f-5dc6-6d510a7b8ec4@virtuozzo.com > > It seems the best solution is to simply disable word-at-a-time > optimization. My trivial testing shows that byte-at-a-time > could be up to x4.3 times slower than word-at-a-time. > It may seems like a lot, but it's actually ~1.2e-10 sec per symbol vs > ~4.8e-10 sec per symbol on modern hardware. And we don't use strscpy() > in a performance critical paths to copy large amounts of data, > so it shouldn't matter anyway. > > Fixes: 30035e45753b7 ("string: provide strscpy()") > Signed-off-by: Andrey Ryabinin > Cc: > Acked-by: Rasmus Villemoes Your microbenchmark even favours word-at-a-time slightly, since in practice I think at least one of src or dst will be unaligned a lot of the time, and while x86 may HAVE_EFFICIENT_UNALIGNED_ACCESS, it's still a little more expensive than doing aligned access. And since strscpy is not called that often, I expect some of the ~300 bytes of instruction cache it occupies can be put to better use elsewhere. Rasmus