Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1356551pxu; Sat, 5 Dec 2020 13:01:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJyKc5BpGkjPZET7iRx6MLy4MXd28cwikhAD2tgbNdiAQ9K78xFWCGh99RB34n/7RMyY1kij X-Received: by 2002:a17:906:a982:: with SMTP id jr2mr13256904ejb.292.1607202072411; Sat, 05 Dec 2020 13:01:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607202072; cv=none; d=google.com; s=arc-20160816; b=mU/+laL0ls2o95xTL9b2eQKe2fZX9AMAAyAllSRAARvvldhN6UbiMubtPDEQzw/s2Y xetBmuoXkMSl30fAGgY32Cq+8482nKlOnY5UxyQBXjoTnMY++i0Fo2Qey3WgPzBsMAS0 l2+NIlMx5yvQL749y6qcgNSC6I8/703DrXZoAx33Y/ILv58Xi+uLZD59VFfF0itIQro1 WuOTAXO6x2WW+v2SvJcDOl7+FA2g9pFyz6/SHv3ixlErDTYBOPYwP/ZBvRCxFB5JFPwp oH0oS6xoCcusy8QuB7rtT1+f8djm7DMdEB1JMehMIAsDEQ7iLxZLUQ9QwqYF9b+mvtoy DSZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=mOpIJBRgmuWvVNZYLIua/JzCWg+15U5th6++AtTsSK0=; b=FyJr5VDwZ98HFUp5ELTUOCKI/9x/SLOJNZseajtFWHwQDpNX7DS4hGDkNh7ijWxtBa hL146g1u+IldNQPNeyrCszR9e2g9928gJ0xY6iyExgn151X7gHKLX3f/88KnoENAcS84 UK5ZVsbIldk1/bUWjy8s1GNH6lrHV4n6dyBaowZlJBEY+chaSFOTlAVRm8JGcWEnqllb u/VDPCUo9Ku1fdJGkr0QDYuNv21uFiKQNIbvBqVX3/wHGS9K2XwJ0WqXxP/EPJsjOw+/ H5ot2cXjFhogoaoPRoGvNHsG6a25Xv4qw/NTWj+pFIrQdGllZfczNeWL+/a3bdfYFfVZ EFvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=khfVwD0L; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id zn8si3589990ejb.16.2020.12.05.13.00.48; Sat, 05 Dec 2020 13:01:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=khfVwD0L; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726878AbgLEU6T (ORCPT + 99 others); Sat, 5 Dec 2020 15:58:19 -0500 Received: from mail.kernel.org ([198.145.29.99]:55930 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725270AbgLEU6S (ORCPT ); Sat, 5 Dec 2020 15:58:18 -0500 X-Gm-Message-State: AOAM533TiQpXkFR/loqScTk9b+TQGvZ4+PzM3RUoMP1urVktIwIyCZ8N E009eyiy/Pm2cNCUrVCaNXE8pK49O89CbN8Ghf4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1607201857; bh=6uR1ro3FB6gF6Hso009vX3qFoBF8H98C3imWvG68XRg=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=khfVwD0LglSW4w+rMzBOp+lYdau1zTJfVpqupVQ2dWKgA5YbANCzrZoHWlPgymzki n5TAJV6DpFp0ZhSYnvRCw01BMbd4GzJk86mrRe31ffWCv/SHLBraSLriB2lDIlFY2O vrWdFvPLdK4CbspcJ+VxhEdw+RRbfIbjDmHzM08JYz2zsDp3ksEwdxUCxHo09dTm4X 0hT+T47vZJVzneNd4S5xaAjEG/o8V8XF/5GXwIszSzIo38If2WWJyX3IlyaZQotEwz 6bSodRRyCEmOcK4F6Un5k81d0qLzKcQnbtEk6KnMdcWMwBJeaYyeAUJbh8uq6HSZTW iEMEyWLNCkuIA== X-Received: by 2002:aca:dd0b:: with SMTP id u11mr7493751oig.47.1607201857245; Sat, 05 Dec 2020 12:57:37 -0800 (PST) MIME-Version: 1.0 References: <20201204170319.20383-1-laniel_francis@privacyrequired.com> <20201204170319.20383-8-laniel_francis@privacyrequired.com> <3161fc13d69c388b1f51f59c6ecea48dcd0a7856.camel@HansenPartnership.com> In-Reply-To: <3161fc13d69c388b1f51f59c6ecea48dcd0a7856.camel@HansenPartnership.com> From: Ard Biesheuvel Date: Sat, 5 Dec 2020 21:57:25 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC PATCH v1 07/12] efi: Replace strstarts() by str_has_prefix(). To: James Bottomley Cc: laniel_francis@privacyrequired.com, linux-efi , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, 5 Dec 2020 at 21:24, James Bottomley wrote: > > On Sat, 2020-12-05 at 20:36 +0100, Ard Biesheuvel wrote: > > On Fri, 4 Dec 2020 at 19:02, James Bottomley > > wrote: > > > On Fri, 2020-12-04 at 18:07 +0100, Ard Biesheuvel wrote: > > > > On Fri, 4 Dec 2020 at 18:06, > > > > wrote: > > > > > From: Francis Laniel > > > > > > > > > > The two functions indicates if a string begins with a given > > > > > prefix. The only difference is that strstarts() returns a bool > > > > > while str_has_prefix() returns the length of the prefix if the > > > > > string begins with it or 0 otherwise. > > > > > > > > > > > > > Why? > > > > > > I think I can answer that. If the conversion were done properly > > > (which it's not) you could get rid of the double strings in the > > > code which are error prone if you update one and forget > > > another. This gives a good example: 3d739c1f6156 ("tracing: Use > > > the return of str_has_prefix() to remove open coded numbers"). so > > > in your code you'd replace things like > > > > > > if (strstarts(option, "rgb")) { > > > option += strlen("rgb"); > > > ... > > > > > > with > > > > > > len = str_has_prefix(option, "rgb"); > > > if (len) { > > > option += len > > > ... > > > > > > Obviously you also have cases where strstart is used as a boolean > > > with no need to know the length ... I think there's no value to > > > converting those. > > > > > > > This will lead to worse code being generated. strlen() is evaluated > > at build time by the compiler if the argument is a string literal, so > > your 'before' version gets turned into 'option += 3', whereas the > > latter needs to use a runtime variable. > > str_has_prefix() is an always_inline function so it should be build > time evaluated as well. I think most compilers see len as being a > constant and unchanged, so elide the variable. This means the code > generated should be the same. > Fair enough. I wasn't aware str_has_prefix() was __always_inline. > > So I don't object to using str_has_prefix() in new code in this way, > > but I really don't see the point of touching existing code. > > That's your prerogative as a Maintainer ... I was just explaining what > the original author had in mind when str_has_prefix() was created. > Sure, I fully understand you are not the one proposing these changes. But if the pattern in question is so common, couldn't we go one step further and define something like static inline const u8 *skip_prefix_or_null(const u8 *str, const u8 *prefix) { } which returns a pointer into the original string, or NULL if the prefix is not present. The current patch as proposed has no benefit whatsoever, but even the meaningful alternative you are proposing is not actually an improvement, given that it is not self-explanatory from the name 'str_has_prefix' what it returns, and so the code becomes more difficult to understand.