From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.3 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 63AA4C47E4D for ; Thu, 15 Jul 2021 12:40:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 463BF613C1 for ; Thu, 15 Jul 2021 12:40:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237390AbhGOMnQ (ORCPT ); Thu, 15 Jul 2021 08:43:16 -0400 Received: from smtp-relay-canonical-1.canonical.com ([185.125.188.121]:59086 "EHLO smtp-relay-canonical-1.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229946AbhGOMnP (ORCPT ); Thu, 15 Jul 2021 08:43:15 -0400 Received: from [10.172.193.212] (1.general.cking.uk.vpn [10.172.193.212]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-canonical-1.canonical.com (Postfix) with ESMTPSA id BBB854057E; Thu, 15 Jul 2021 12:40:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1626352821; bh=AmOVYckpwNAaXTV3DzCNna6wdwLn6NLZ6K6/8u4iK8w=; h=Subject:To:Cc:References:From:Message-ID:Date:MIME-Version: In-Reply-To:Content-Type; b=WiC1/QqjZB3fmo4rjb9Pq0sxQyEydMClS3x8p1GFnd22mZswav06O+Wt9S0RZH3CG +qgkm6TOibZc+2TKs5P52KDj1qtLeQW09skug3Z0vkcxsXUm7/Lt5eCz69MX8OVJ/1 LKmkMvUKqQmn63fBR9/ymj0m2f041x4ogXzLJqpWEP6QsIjcyri5UO9DyEZ8/hfM0n mZN8M615+d3TUEeXMO0FVp3CHNe/mYotHK0YTpuvZHji+ZEllJLI4qpYZvsMTX0XOe 2dBoCmFgEV/9xW0HxBRK4WrqU761i4n9Lpz3W3236RT1lvkLs/kenyB31WXTI9eNE+ BJNuTM6RL3hYg== Subject: Re: Range checking on r1 in function reg_set_seen in arch/s390/net/bpf_jit_comp.c To: Ilya Leoshkevich , Michael Holzheu , Martin Schwidefsky Cc: Heiko Carstens , Vasily Gorbik , Christian Borntraeger , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , linux-s390@vger.kernel.org, "netdev@vger.kernel.org" , bpf@vger.kernel.org, "linux-kernel@vger.kernel.org" References: <845025d4-11b9-b16d-1dd6-1e0bd66b0e20@canonical.com> <8b280523cf98294bee897615de84546e241b4e11.camel@linux.ibm.com> From: Colin Ian King Message-ID: <96c114c8-1369-05d3-6b44-78ac4e5e73fb@canonical.com> Date: Thu, 15 Jul 2021 13:40:20 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 MIME-Version: 1.0 In-Reply-To: <8b280523cf98294bee897615de84546e241b4e11.camel@linux.ibm.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 15/07/2021 13:09, Ilya Leoshkevich wrote: > On Thu, 2021-07-15 at 13:02 +0100, Colin Ian King wrote: >> Hi >> >> Static analysis with cppcheck picked up an interesting issue with the >> following inline helper function in arch/s390/net/bpf_jit_comp.c : >> >> static inline void reg_set_seen(struct bpf_jit *jit, u32 b1) >> { >>         u32 r1 = reg2hex[b1]; >> >>         if (!jit->seen_reg[r1] && r1 >= 6 && r1 <= 15) >>                 jit->seen_reg[r1] = 1; >> } >> >> Although I believe r1 is always within range, the range check on r1 >> is >> being performed before the more cache/memory expensive lookup on >> jit->seen_reg[r1].  I can't see why the range change is being >> performed >> after the access of jit->seen_reg[r1]. The following seems more >> correct: >> >>         if (r1 >= 6 && r1 <= 15 && !jit->seen_reg[r1]) >>                 jit->seen_reg[r1] = 1; >> >> ..since the check on r1 are less expensive than !jit->seen_reg[r1] >> and >> also the range check ensures the array access is not out of bounds. I >> was just wondering if I'm missing something deeper to why the order >> is >> the way it is. >> >> Colin > > Hi, > > I think your analysis is correct, thanks for spotting this! > Even though I don't think the performance difference would be  > measurable here, not confusing future readers is a good reason > to make a change that you suggest. > Do you plan to send a patch? I'll send a patch later today. Colin > > Best regards, > Ilya >