Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp643823yba; Mon, 1 Apr 2019 13:44:13 -0700 (PDT) X-Google-Smtp-Source: APXvYqwnLvxBU2eze4NZ8sr0R4BRr5zD25Jk5jM8LRSiJ3FZdPdB+5XaJn9LHqn49LgQKjAQM501 X-Received: by 2002:a17:902:70cc:: with SMTP id l12mr48194329plt.10.1554151453473; Mon, 01 Apr 2019 13:44:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554151453; cv=none; d=google.com; s=arc-20160816; b=INSqjIH0DWskdFShsCDmggPTVCN/S0U4BnIiEKhCkmy1X/fpQXrB5sr8zs2XUopOj7 P2DjY0E392yYEMEg33XeKdYqKWqLhWcl9C1WyN1jdODqgJ9o3rpRi8qRgyPzRyerAA3J GBTZhbPGD6Jw/PXvuABLpf5XBy3xKRcjNFnhN0Non6B3ffflzIucAqjoEWRTUvIMg1DA mL8IlIt1UTPYLXl3vsNwQzkv8MBNk+l1Ffq173z1h2KPy1zElKnu+ijSvPPe8WcEOp5u AT+ojTfQt8V3Wbj9ryVU+dhHvSKdx24NhwmvybyoZAh0xBFoqqcQCeNXjj1C7EnN/Rce wL5g== 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; bh=IPp5UBhjmauumhzMvJEgg7H5y+lW26wX4Uq5ovId0EI=; b=mvAUaZlOcFbS67ReVXXQANdRbu4Bnpk0cAUFF5zDjmYCW7KfXS5jbUn6iJCR02K8i+ HCFipoMACItf6HRN8VrnwAnL/xAxWkNDdNKjtz7tHrPEmx9masYT3sGHnczxxVdAi30D UZBu1CkRWQgu+unOhQkAfygsfVLhrDTCCxyjLIjzxvk6vDoR0Pler3gn2RMDZ1hJHe0D rBX1YjdOD6K+FIi5xBjamyrAA74NnCd+h3nikap2PHgFxHjO2pxw/CeBSXA+RBiSpoZh 4PljhJe/z2JfvBthBpWA7YAFEbo8ECXaaZ5NG6VzcOO9GlPgI3OFrOvSJBRfcuzS2C+P xpig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=JxeqGjQH; 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 a11si418383plp.306.2019.04.01.13.43.58; Mon, 01 Apr 2019 13:44:13 -0700 (PDT) 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=pass header.i=@rasmusvillemoes.dk header.s=google header.b=JxeqGjQH; 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 S1727019AbfDAUnR (ORCPT + 99 others); Mon, 1 Apr 2019 16:43:17 -0400 Received: from mail-ed1-f67.google.com ([209.85.208.67]:32895 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726109AbfDAUnQ (ORCPT ); Mon, 1 Apr 2019 16:43:16 -0400 Received: by mail-ed1-f67.google.com with SMTP id q3so9642926edg.0 for ; Mon, 01 Apr 2019 13:43:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=IPp5UBhjmauumhzMvJEgg7H5y+lW26wX4Uq5ovId0EI=; b=JxeqGjQHlNjMVebBP21HH0HbGpAX8q8RRJIjq+SuSnodAVVKetBbnHSHnHqhTY2FwL jnIMyVyl81Bj4dQeTkjWUEkMDGEeRLPbJDDdkLyUQt+GIzDqlUVIrWhUcM9dvKTvV/Nl eYaar5LPJjaxisTiHuGely22xjaKnV+fjUgt4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=IPp5UBhjmauumhzMvJEgg7H5y+lW26wX4Uq5ovId0EI=; b=aYZj6cRMOIWgrYDza+ohi0xiKVR9uUFCmreB5suvmauZkIkpjuZgb5lLq9zVPHVFZH iMoWywRao03kAntzUgfGyw23DTqqtGAiXRpvRFIl9wDYYt5ShvFQ2E8m9QtU4SkZ3cpU 8lN1ucbh9ITRHdx5q+qE5DgXcub7QYupcSoTmKxpcVrDqC0p6XQFTlBBnqCZBjIdba0t CU+t10q1nEOZbo4CK0TBYG75ne0DITTAcxuSZ39VS1ozAbzRLR5sxsQEivUNMG6EMGkH 2FG6hbM4lmWiN1SFnUy9GjrTAcLTsVtlpAaqkj5pOizsl11O3AAlVPxDo3zowKXUlEIF T+/w== X-Gm-Message-State: APjAAAXIakJTKiCNcWeSk7aHQrj9M6i0kg+5fxekAjVms/mF7PHZKASr KY/FExRp3zVVsMzgrbKlW+JpWw== X-Received: by 2002:a50:8864:: with SMTP id c33mr44834654edc.110.1554151394947; Mon, 01 Apr 2019 13:43:14 -0700 (PDT) Received: from [192.168.1.149] (ip-5-186-118-63.cgn.fibianet.dk. [5.186.118.63]) by smtp.gmail.com with ESMTPSA id 4sm3455392eds.74.2019.04.01.13.43.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 01 Apr 2019 13:43:14 -0700 (PDT) Subject: Re: [RFCv2] string: Use faster alternatives when constant arguments are used To: Sultan Alsawaf Cc: akpm@linux-foundation.org, linux-kernel@vger.kernel.org, Nathan Chancellor References: <20190324014445.28688-1-sultan@kerneltoast.com> <20190324022406.GA18988@sultan-box.localdomain> <2293c54f-40b1-1e59-665a-bd8f2cb957d2@rasmusvillemoes.dk> <20190324223202.GA875@sultan-box.localdomain> <8672b98c-bf71-7b5b-625e-2f241807d46c@rasmusvillemoes.dk> <20190330225941.GA7456@sultan-box.localdomain> From: Rasmus Villemoes Message-ID: Date: Mon, 1 Apr 2019 22:43:13 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <20190330225941.GA7456@sultan-box.localdomain> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 30/03/2019 23.59, Sultan Alsawaf wrote: > How can the memcmps cross a page boundary when memcmp itself will > only read in large buffers of data at word boundaries? Consider your patch replacing !strcmp(buf, "123") by !memcmp(buf, "123", 4). buf is known to point to a nul-terminated string. But it may point at, say, the second-last byte in a page, with the last byte in that page being a nul byte, and the following page being MMIO or unmapped or all kinds of bad things. On e.g. x86 where unaligned accesses are cheap, and seeing that you're only comparing for equality, gcc is likely to compile the memcmp version into *(u32*)buf == 0x00333231 because you've told the compiler that there's no problem accessing four bytes starting at buf. Boom. Even without unaligned access being cheap this can happen; suppose the length is 8 instead, and gcc somehow knows that buf is four-byte aligned (and in this case it happens to point four bytes before a page boundary), so it could compile the memcmp(,,8) into *(u32*)(buf+4) == secondword && *(u32*)buf == firstword (or do the comparisons in the "natural" order, but it might still do both loads first). > And if there are concerns for some arches but not others, then couldn't this be > a feasible optimization for those which would work well with it? No. First, these are concerns for all arches. Second, if you can find some particular place where string parsing/matching is in any way performance relevant and not just done once during driver init or whatnot, maybe the maintainers of that file would take a patch hand-optimizing some strcmps to memcmps, or, depending on what the code does, perhaps replacing the whole *cmp logic with a custom hash table. But a patch implicitly and silently touching thousands of lines of code, without an analysis of why none of the above is a problem for any of those lines, for any .config, arch, compiler version? No. Rasmus