public inbox for linux-s390@vger.kernel.org
 help / color / mirror / Atom feed
From: Colin Ian King <colin.king@canonical.com>
To: Ilya Leoshkevich <iii@linux.ibm.com>,
	Michael Holzheu <holzheu@linux.vnet.ibm.com>,
	Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Heiko Carstens <hca@linux.ibm.com>,
	Vasily Gorbik <gor@linux.ibm.com>,
	Christian Borntraeger <borntraeger@de.ibm.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	linux-s390@vger.kernel.org,
	"netdev@vger.kernel.org" <netdev@vger.kernel.org>,
	bpf@vger.kernel.org,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: Range checking on r1 in function reg_set_seen in arch/s390/net/bpf_jit_comp.c
Date: Thu, 15 Jul 2021 13:40:20 +0100	[thread overview]
Message-ID: <96c114c8-1369-05d3-6b44-78ac4e5e73fb@canonical.com> (raw)
In-Reply-To: <8b280523cf98294bee897615de84546e241b4e11.camel@linux.ibm.com>

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
> 


      reply	other threads:[~2021-07-15 12:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-15 12:02 Range checking on r1 in function reg_set_seen in arch/s390/net/bpf_jit_comp.c Colin Ian King
2021-07-15 12:09 ` Ilya Leoshkevich
2021-07-15 12:40   ` Colin Ian King [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=96c114c8-1369-05d3-6b44-78ac4e5e73fb@canonical.com \
    --to=colin.king@canonical.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=borntraeger@de.ibm.com \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=holzheu@linux.vnet.ibm.com \
    --cc=iii@linux.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=schwidefsky@de.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox