All of lore.kernel.org
 help / color / mirror / Atom feed
* Range checking on r1 in function reg_set_seen in arch/s390/net/bpf_jit_comp.c
@ 2021-07-15 12:02 Colin Ian King
  2021-07-15 12:09 ` Ilya Leoshkevich
  0 siblings, 1 reply; 3+ messages in thread
From: Colin Ian King @ 2021-07-15 12:02 UTC (permalink / raw)
  To: Michael Holzheu, Martin Schwidefsky
  Cc: Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
	Ilya Leoshkevich, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, linux-s390, netdev, bpf, linux-kernel

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



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Range checking on r1 in function reg_set_seen in arch/s390/net/bpf_jit_comp.c
  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
  0 siblings, 1 reply; 3+ messages in thread
From: Ilya Leoshkevich @ 2021-07-15 12:09 UTC (permalink / raw)
  To: Colin Ian King, Michael Holzheu, Martin Schwidefsky
  Cc: Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
	Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, linux-s390,
	netdev, bpf, linux-kernel

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?

Best regards,
Ilya


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Range checking on r1 in function reg_set_seen in arch/s390/net/bpf_jit_comp.c
  2021-07-15 12:09 ` Ilya Leoshkevich
@ 2021-07-15 12:40   ` Colin Ian King
  0 siblings, 0 replies; 3+ messages in thread
From: Colin Ian King @ 2021-07-15 12:40 UTC (permalink / raw)
  To: Ilya Leoshkevich, Michael Holzheu, Martin Schwidefsky
  Cc: Heiko Carstens, Vasily Gorbik, Christian Borntraeger,
	Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, linux-s390,
	netdev, bpf, linux-kernel

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
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-07-15 12:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.