* [PATCH bpf-next] bpf, docs: Define signed modulo as using truncated division
@ 2023-10-17 20:30 Dave Thaler
2023-10-17 20:30 ` [Bpf] " Dave Thaler
` (3 more replies)
0 siblings, 4 replies; 10+ messages in thread
From: Dave Thaler @ 2023-10-17 20:30 UTC (permalink / raw)
To: bpf; +Cc: bpf, Dave Thaler
From: Dave Thaler <dthaler@microsoft.com>
There's different mathematical definitions (truncated, floored,
rounded, etc.) and different languages have chosen different
definitions [0][1]. E.g., languages/libraries that follow Knuth
use a different mathematical definition than C uses. This
patch specifies which definition BPF uses, as verified by
Eduard [2] and others.
[0]: https://en.wikipedia.org/wiki/Modulo#Variants_of_the_definition
[1]: https://torstencurdt.com/tech/posts/modulo-of-negative-numbers/
[2]: https://lore.kernel.org/bpf/57e6fefadaf3b2995bb259fa8e711c7220ce5290.camel@gmail.com/
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
---
Documentation/bpf/standardization/instruction-set.rst | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/Documentation/bpf/standardization/instruction-set.rst b/Documentation/bpf/standardization/instruction-set.rst
index c5d53a6e8c7..245b6defc29 100644
--- a/Documentation/bpf/standardization/instruction-set.rst
+++ b/Documentation/bpf/standardization/instruction-set.rst
@@ -283,6 +283,14 @@ For signed operations (``BPF_SDIV`` and ``BPF_SMOD``), for ``BPF_ALU``,
is first :term:`sign extended<Sign Extend>` from 32 to 64 bits, and then
interpreted as a 64-bit signed value.
+Note that there are varying definitions of the signed modulo operation
+when the dividend or divisor are negative, where implementations often
+vary by language such that Python, Ruby, etc. differ from C, Go, Java,
+etc. This specification requires that signed modulo use truncated division
+(where -13 % 3 == -1) as implemented in C, Go, etc.:
+
+ a % n = a - n * trunc(a / n)
+
The ``BPF_MOVSX`` instruction does a move operation with sign extension.
``BPF_ALU | BPF_MOVSX`` :term:`sign extends<Sign Extend>` 8-bit and 16-bit operands into 32
bit operands, and zeroes the remaining upper 32 bits.
--
2.33.4
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [Bpf] [PATCH bpf-next] bpf, docs: Define signed modulo as using truncated division
2023-10-17 20:30 [PATCH bpf-next] bpf, docs: Define signed modulo as using truncated division Dave Thaler
@ 2023-10-17 20:30 ` Dave Thaler
2023-10-18 22:28 ` David Vernet
` (2 subsequent siblings)
3 siblings, 0 replies; 10+ messages in thread
From: Dave Thaler @ 2023-10-17 20:30 UTC (permalink / raw)
To: bpf; +Cc: bpf, Dave Thaler
From: Dave Thaler <dthaler@microsoft.com>
There's different mathematical definitions (truncated, floored,
rounded, etc.) and different languages have chosen different
definitions [0][1]. E.g., languages/libraries that follow Knuth
use a different mathematical definition than C uses. This
patch specifies which definition BPF uses, as verified by
Eduard [2] and others.
[0]: https://en.wikipedia.org/wiki/Modulo#Variants_of_the_definition
[1]: https://torstencurdt.com/tech/posts/modulo-of-negative-numbers/
[2]: https://lore.kernel.org/bpf/57e6fefadaf3b2995bb259fa8e711c7220ce5290.camel@gmail.com/
Signed-off-by: Dave Thaler <dthaler@microsoft.com>
---
Documentation/bpf/standardization/instruction-set.rst | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/Documentation/bpf/standardization/instruction-set.rst b/Documentation/bpf/standardization/instruction-set.rst
index c5d53a6e8c7..245b6defc29 100644
--- a/Documentation/bpf/standardization/instruction-set.rst
+++ b/Documentation/bpf/standardization/instruction-set.rst
@@ -283,6 +283,14 @@ For signed operations (``BPF_SDIV`` and ``BPF_SMOD``), for ``BPF_ALU``,
is first :term:`sign extended<Sign Extend>` from 32 to 64 bits, and then
interpreted as a 64-bit signed value.
+Note that there are varying definitions of the signed modulo operation
+when the dividend or divisor are negative, where implementations often
+vary by language such that Python, Ruby, etc. differ from C, Go, Java,
+etc. This specification requires that signed modulo use truncated division
+(where -13 % 3 == -1) as implemented in C, Go, etc.:
+
+ a % n = a - n * trunc(a / n)
+
The ``BPF_MOVSX`` instruction does a move operation with sign extension.
``BPF_ALU | BPF_MOVSX`` :term:`sign extends<Sign Extend>` 8-bit and 16-bit operands into 32
bit operands, and zeroes the remaining upper 32 bits.
--
2.33.4
--
Bpf mailing list
Bpf@ietf.org
https://www.ietf.org/mailman/listinfo/bpf
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH bpf-next] bpf, docs: Define signed modulo as using truncated division
2023-10-17 20:30 [PATCH bpf-next] bpf, docs: Define signed modulo as using truncated division Dave Thaler
2023-10-17 20:30 ` [Bpf] " Dave Thaler
@ 2023-10-18 22:28 ` David Vernet
2023-10-18 22:28 ` [Bpf] " David Vernet
2023-10-18 22:34 ` Eduard Zingerman
2023-10-18 23:40 ` patchwork-bot+netdevbpf
3 siblings, 1 reply; 10+ messages in thread
From: David Vernet @ 2023-10-18 22:28 UTC (permalink / raw)
To: Dave Thaler; +Cc: bpf, bpf, Dave Thaler, eddyz87
On Tue, Oct 17, 2023 at 08:30:20PM +0000, Dave Thaler wrote:
> From: Dave Thaler <dthaler@microsoft.com>
>
> There's different mathematical definitions (truncated, floored,
> rounded, etc.) and different languages have chosen different
> definitions [0][1]. E.g., languages/libraries that follow Knuth
> use a different mathematical definition than C uses. This
> patch specifies which definition BPF uses, as verified by
> Eduard [2] and others.
>
> [0]: https://en.wikipedia.org/wiki/Modulo#Variants_of_the_definition
> [1]: https://torstencurdt.com/tech/posts/modulo-of-negative-numbers/
> [2]: https://lore.kernel.org/bpf/57e6fefadaf3b2995bb259fa8e711c7220ce5290.camel@gmail.com/
>
> Signed-off-by: Dave Thaler <dthaler@microsoft.com>
Acked-by: David Vernet <void@manifault.com>
+cc Eduard as well in case he wants to take a look.
> ---
> Documentation/bpf/standardization/instruction-set.rst | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/Documentation/bpf/standardization/instruction-set.rst b/Documentation/bpf/standardization/instruction-set.rst
> index c5d53a6e8c7..245b6defc29 100644
> --- a/Documentation/bpf/standardization/instruction-set.rst
> +++ b/Documentation/bpf/standardization/instruction-set.rst
> @@ -283,6 +283,14 @@ For signed operations (``BPF_SDIV`` and ``BPF_SMOD``), for ``BPF_ALU``,
> is first :term:`sign extended<Sign Extend>` from 32 to 64 bits, and then
> interpreted as a 64-bit signed value.
>
> +Note that there are varying definitions of the signed modulo operation
> +when the dividend or divisor are negative, where implementations often
> +vary by language such that Python, Ruby, etc. differ from C, Go, Java,
> +etc. This specification requires that signed modulo use truncated division
> +(where -13 % 3 == -1) as implemented in C, Go, etc.:
> +
> + a % n = a - n * trunc(a / n)
> +
> The ``BPF_MOVSX`` instruction does a move operation with sign extension.
> ``BPF_ALU | BPF_MOVSX`` :term:`sign extends<Sign Extend>` 8-bit and 16-bit operands into 32
> bit operands, and zeroes the remaining upper 32 bits.
> --
> 2.33.4
>
>
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Bpf] [PATCH bpf-next] bpf, docs: Define signed modulo as using truncated division
2023-10-18 22:28 ` David Vernet
@ 2023-10-18 22:28 ` David Vernet
0 siblings, 0 replies; 10+ messages in thread
From: David Vernet @ 2023-10-18 22:28 UTC (permalink / raw)
To: Dave Thaler; +Cc: bpf, bpf, Dave Thaler, eddyz87
On Tue, Oct 17, 2023 at 08:30:20PM +0000, Dave Thaler wrote:
> From: Dave Thaler <dthaler@microsoft.com>
>
> There's different mathematical definitions (truncated, floored,
> rounded, etc.) and different languages have chosen different
> definitions [0][1]. E.g., languages/libraries that follow Knuth
> use a different mathematical definition than C uses. This
> patch specifies which definition BPF uses, as verified by
> Eduard [2] and others.
>
> [0]: https://en.wikipedia.org/wiki/Modulo#Variants_of_the_definition
> [1]: https://torstencurdt.com/tech/posts/modulo-of-negative-numbers/
> [2]: https://lore.kernel.org/bpf/57e6fefadaf3b2995bb259fa8e711c7220ce5290.camel@gmail.com/
>
> Signed-off-by: Dave Thaler <dthaler@microsoft.com>
Acked-by: David Vernet <void@manifault.com>
+cc Eduard as well in case he wants to take a look.
> ---
> Documentation/bpf/standardization/instruction-set.rst | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/Documentation/bpf/standardization/instruction-set.rst b/Documentation/bpf/standardization/instruction-set.rst
> index c5d53a6e8c7..245b6defc29 100644
> --- a/Documentation/bpf/standardization/instruction-set.rst
> +++ b/Documentation/bpf/standardization/instruction-set.rst
> @@ -283,6 +283,14 @@ For signed operations (``BPF_SDIV`` and ``BPF_SMOD``), for ``BPF_ALU``,
> is first :term:`sign extended<Sign Extend>` from 32 to 64 bits, and then
> interpreted as a 64-bit signed value.
>
> +Note that there are varying definitions of the signed modulo operation
> +when the dividend or divisor are negative, where implementations often
> +vary by language such that Python, Ruby, etc. differ from C, Go, Java,
> +etc. This specification requires that signed modulo use truncated division
> +(where -13 % 3 == -1) as implemented in C, Go, etc.:
> +
> + a % n = a - n * trunc(a / n)
> +
> The ``BPF_MOVSX`` instruction does a move operation with sign extension.
> ``BPF_ALU | BPF_MOVSX`` :term:`sign extends<Sign Extend>` 8-bit and 16-bit operands into 32
> bit operands, and zeroes the remaining upper 32 bits.
> --
> 2.33.4
>
>
--
Bpf mailing list
Bpf@ietf.org
https://www.ietf.org/mailman/listinfo/bpf
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH bpf-next] bpf, docs: Define signed modulo as using truncated division
2023-10-17 20:30 [PATCH bpf-next] bpf, docs: Define signed modulo as using truncated division Dave Thaler
2023-10-17 20:30 ` [Bpf] " Dave Thaler
2023-10-18 22:28 ` David Vernet
@ 2023-10-18 22:34 ` Eduard Zingerman
2023-10-18 23:40 ` Daniel Borkmann
2023-10-18 23:40 ` patchwork-bot+netdevbpf
3 siblings, 1 reply; 10+ messages in thread
From: Eduard Zingerman @ 2023-10-18 22:34 UTC (permalink / raw)
To: Dave Thaler, bpf; +Cc: bpf, Dave Thaler
On Tue, 2023-10-17 at 20:30 +0000, Dave Thaler wrote:
> From: Dave Thaler <dthaler@microsoft.com>
>
> There's different mathematical definitions (truncated, floored,
> rounded, etc.) and different languages have chosen different
> definitions [0][1]. E.g., languages/libraries that follow Knuth
> use a different mathematical definition than C uses. This
> patch specifies which definition BPF uses, as verified by
> Eduard [2] and others.
>
> [0]: https://en.wikipedia.org/wiki/Modulo#Variants_of_the_definition
> [1]: https://torstencurdt.com/tech/posts/modulo-of-negative-numbers/
> [2]: https://lore.kernel.org/bpf/57e6fefadaf3b2995bb259fa8e711c7220ce5290.camel@gmail.com/
>
> Signed-off-by: Dave Thaler <dthaler@microsoft.com>
> ---
> Documentation/bpf/standardization/instruction-set.rst | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/Documentation/bpf/standardization/instruction-set.rst b/Documentation/bpf/standardization/instruction-set.rst
> index c5d53a6e8c7..245b6defc29 100644
> --- a/Documentation/bpf/standardization/instruction-set.rst
> +++ b/Documentation/bpf/standardization/instruction-set.rst
> @@ -283,6 +283,14 @@ For signed operations (``BPF_SDIV`` and ``BPF_SMOD``), for ``BPF_ALU``,
> is first :term:`sign extended<Sign Extend>` from 32 to 64 bits, and then
> interpreted as a 64-bit signed value.
>
> +Note that there are varying definitions of the signed modulo operation
> +when the dividend or divisor are negative, where implementations often
> +vary by language such that Python, Ruby, etc. differ from C, Go, Java,
> +etc. This specification requires that signed modulo use truncated division
> +(where -13 % 3 == -1) as implemented in C, Go, etc.:
> +
> + a % n = a - n * trunc(a / n)
> +
> The ``BPF_MOVSX`` instruction does a move operation with sign extension.
> ``BPF_ALU | BPF_MOVSX`` :term:`sign extends<Sign Extend>` 8-bit and 16-bit operands into 32
> bit operands, and zeroes the remaining upper 32 bits.
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH bpf-next] bpf, docs: Define signed modulo as using truncated division
2023-10-17 20:30 [PATCH bpf-next] bpf, docs: Define signed modulo as using truncated division Dave Thaler
` (2 preceding siblings ...)
2023-10-18 22:34 ` Eduard Zingerman
@ 2023-10-18 23:40 ` patchwork-bot+netdevbpf
3 siblings, 0 replies; 10+ messages in thread
From: patchwork-bot+netdevbpf @ 2023-10-18 23:40 UTC (permalink / raw)
To: Dave Thaler; +Cc: bpf, bpf, dthaler
Hello:
This patch was applied to bpf/bpf-next.git (master)
by Daniel Borkmann <daniel@iogearbox.net>:
On Tue, 17 Oct 2023 20:30:20 +0000 you wrote:
> From: Dave Thaler <dthaler@microsoft.com>
>
> There's different mathematical definitions (truncated, floored,
> rounded, etc.) and different languages have chosen different
> definitions [0][1]. E.g., languages/libraries that follow Knuth
> use a different mathematical definition than C uses. This
> patch specifies which definition BPF uses, as verified by
> Eduard [2] and others.
>
> [...]
Here is the summary with links:
- [bpf-next] bpf, docs: Define signed modulo as using truncated division
https://git.kernel.org/bpf/bpf-next/c/0e133a133703
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH bpf-next] bpf, docs: Define signed modulo as using truncated division
2023-10-18 22:34 ` Eduard Zingerman
@ 2023-10-18 23:40 ` Daniel Borkmann
2023-10-18 23:40 ` [Bpf] " Daniel Borkmann
2023-10-19 0:00 ` Eduard Zingerman
0 siblings, 2 replies; 10+ messages in thread
From: Daniel Borkmann @ 2023-10-18 23:40 UTC (permalink / raw)
To: Eduard Zingerman, Dave Thaler, bpf; +Cc: bpf, Dave Thaler
On 10/19/23 12:34 AM, Eduard Zingerman wrote:
> On Tue, 2023-10-17 at 20:30 +0000, Dave Thaler wrote:
>> From: Dave Thaler <dthaler@microsoft.com>
>>
>> There's different mathematical definitions (truncated, floored,
>> rounded, etc.) and different languages have chosen different
>> definitions [0][1]. E.g., languages/libraries that follow Knuth
>> use a different mathematical definition than C uses. This
>> patch specifies which definition BPF uses, as verified by
>> Eduard [2] and others.
>>
>> [0]: https://en.wikipedia.org/wiki/Modulo#Variants_of_the_definition
>> [1]: https://torstencurdt.com/tech/posts/modulo-of-negative-numbers/
>> [2]: https://lore.kernel.org/bpf/57e6fefadaf3b2995bb259fa8e711c7220ce5290.camel@gmail.com/
>>
>> Signed-off-by: Dave Thaler <dthaler@microsoft.com>
>> ---
>> Documentation/bpf/standardization/instruction-set.rst | 8 ++++++++
>> 1 file changed, 8 insertions(+)
>>
>> diff --git a/Documentation/bpf/standardization/instruction-set.rst b/Documentation/bpf/standardization/instruction-set.rst
>> index c5d53a6e8c7..245b6defc29 100644
>> --- a/Documentation/bpf/standardization/instruction-set.rst
>> +++ b/Documentation/bpf/standardization/instruction-set.rst
>> @@ -283,6 +283,14 @@ For signed operations (``BPF_SDIV`` and ``BPF_SMOD``), for ``BPF_ALU``,
>> is first :term:`sign extended<Sign Extend>` from 32 to 64 bits, and then
>> interpreted as a 64-bit signed value.
>>
>> +Note that there are varying definitions of the signed modulo operation
>> +when the dividend or divisor are negative, where implementations often
>> +vary by language such that Python, Ruby, etc. differ from C, Go, Java,
>> +etc. This specification requires that signed modulo use truncated division
>> +(where -13 % 3 == -1) as implemented in C, Go, etc.:
>> +
>> + a % n = a - n * trunc(a / n)
>> +
>> The ``BPF_MOVSX`` instruction does a move operation with sign extension.
>> ``BPF_ALU | BPF_MOVSX`` :term:`sign extends<Sign Extend>` 8-bit and 16-bit operands into 32
>> bit operands, and zeroes the remaining upper 32 bits.
>
> Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Eduard, do we have some test cases in BPF CI around this specifically (e.g. via test_verifier)?
Might be worth adding if not.
Thanks,
Daniel
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Bpf] [PATCH bpf-next] bpf, docs: Define signed modulo as using truncated division
2023-10-18 23:40 ` Daniel Borkmann
@ 2023-10-18 23:40 ` Daniel Borkmann
2023-10-19 0:00 ` Eduard Zingerman
1 sibling, 0 replies; 10+ messages in thread
From: Daniel Borkmann @ 2023-10-18 23:40 UTC (permalink / raw)
To: Eduard Zingerman, Dave Thaler, bpf; +Cc: bpf, Dave Thaler
On 10/19/23 12:34 AM, Eduard Zingerman wrote:
> On Tue, 2023-10-17 at 20:30 +0000, Dave Thaler wrote:
>> From: Dave Thaler <dthaler@microsoft.com>
>>
>> There's different mathematical definitions (truncated, floored,
>> rounded, etc.) and different languages have chosen different
>> definitions [0][1]. E.g., languages/libraries that follow Knuth
>> use a different mathematical definition than C uses. This
>> patch specifies which definition BPF uses, as verified by
>> Eduard [2] and others.
>>
>> [0]: https://en.wikipedia.org/wiki/Modulo#Variants_of_the_definition
>> [1]: https://torstencurdt.com/tech/posts/modulo-of-negative-numbers/
>> [2]: https://lore.kernel.org/bpf/57e6fefadaf3b2995bb259fa8e711c7220ce5290.camel@gmail.com/
>>
>> Signed-off-by: Dave Thaler <dthaler@microsoft.com>
>> ---
>> Documentation/bpf/standardization/instruction-set.rst | 8 ++++++++
>> 1 file changed, 8 insertions(+)
>>
>> diff --git a/Documentation/bpf/standardization/instruction-set.rst b/Documentation/bpf/standardization/instruction-set.rst
>> index c5d53a6e8c7..245b6defc29 100644
>> --- a/Documentation/bpf/standardization/instruction-set.rst
>> +++ b/Documentation/bpf/standardization/instruction-set.rst
>> @@ -283,6 +283,14 @@ For signed operations (``BPF_SDIV`` and ``BPF_SMOD``), for ``BPF_ALU``,
>> is first :term:`sign extended<Sign Extend>` from 32 to 64 bits, and then
>> interpreted as a 64-bit signed value.
>>
>> +Note that there are varying definitions of the signed modulo operation
>> +when the dividend or divisor are negative, where implementations often
>> +vary by language such that Python, Ruby, etc. differ from C, Go, Java,
>> +etc. This specification requires that signed modulo use truncated division
>> +(where -13 % 3 == -1) as implemented in C, Go, etc.:
>> +
>> + a % n = a - n * trunc(a / n)
>> +
>> The ``BPF_MOVSX`` instruction does a move operation with sign extension.
>> ``BPF_ALU | BPF_MOVSX`` :term:`sign extends<Sign Extend>` 8-bit and 16-bit operands into 32
>> bit operands, and zeroes the remaining upper 32 bits.
>
> Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Eduard, do we have some test cases in BPF CI around this specifically (e.g. via test_verifier)?
Might be worth adding if not.
Thanks,
Daniel
--
Bpf mailing list
Bpf@ietf.org
https://www.ietf.org/mailman/listinfo/bpf
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH bpf-next] bpf, docs: Define signed modulo as using truncated division
2023-10-18 23:40 ` Daniel Borkmann
2023-10-18 23:40 ` [Bpf] " Daniel Borkmann
@ 2023-10-19 0:00 ` Eduard Zingerman
2023-10-19 0:00 ` [Bpf] " Eduard Zingerman
1 sibling, 1 reply; 10+ messages in thread
From: Eduard Zingerman @ 2023-10-19 0:00 UTC (permalink / raw)
To: Daniel Borkmann, Dave Thaler, bpf; +Cc: bpf, Dave Thaler
On Thu, 2023-10-19 at 01:40 +0200, Daniel Borkmann wrote:
> On 10/19/23 12:34 AM, Eduard Zingerman wrote:
> > On Tue, 2023-10-17 at 20:30 +0000, Dave Thaler wrote:
> > > From: Dave Thaler <dthaler@microsoft.com>
> > >
> > > There's different mathematical definitions (truncated, floored,
> > > rounded, etc.) and different languages have chosen different
> > > definitions [0][1]. E.g., languages/libraries that follow Knuth
> > > use a different mathematical definition than C uses. This
> > > patch specifies which definition BPF uses, as verified by
> > > Eduard [2] and others.
> > >
> > > [0]: https://en.wikipedia.org/wiki/Modulo#Variants_of_the_definition
> > > [1]: https://torstencurdt.com/tech/posts/modulo-of-negative-numbers/
> > > [2]: https://lore.kernel.org/bpf/57e6fefadaf3b2995bb259fa8e711c7220ce5290.camel@gmail.com/
> > >
> > > Signed-off-by: Dave Thaler <dthaler@microsoft.com>
> > > ---
> > > Documentation/bpf/standardization/instruction-set.rst | 8 ++++++++
> > > 1 file changed, 8 insertions(+)
> > >
> > > diff --git a/Documentation/bpf/standardization/instruction-set.rst b/Documentation/bpf/standardization/instruction-set.rst
> > > index c5d53a6e8c7..245b6defc29 100644
> > > --- a/Documentation/bpf/standardization/instruction-set.rst
> > > +++ b/Documentation/bpf/standardization/instruction-set.rst
> > > @@ -283,6 +283,14 @@ For signed operations (``BPF_SDIV`` and ``BPF_SMOD``), for ``BPF_ALU``,
> > > is first :term:`sign extended<Sign Extend>` from 32 to 64 bits, and then
> > > interpreted as a 64-bit signed value.
> > >
> > > +Note that there are varying definitions of the signed modulo operation
> > > +when the dividend or divisor are negative, where implementations often
> > > +vary by language such that Python, Ruby, etc. differ from C, Go, Java,
> > > +etc. This specification requires that signed modulo use truncated division
> > > +(where -13 % 3 == -1) as implemented in C, Go, etc.:
> > > +
> > > + a % n = a - n * trunc(a / n)
> > > +
> > > The ``BPF_MOVSX`` instruction does a move operation with sign extension.
> > > ``BPF_ALU | BPF_MOVSX`` :term:`sign extends<Sign Extend>` 8-bit and 16-bit operands into 32
> > > bit operands, and zeroes the remaining upper 32 bits.
> >
> > Acked-by: Eduard Zingerman <eddyz87@gmail.com>
>
> Eduard, do we have some test cases in BPF CI around this specifically (e.g. via test_verifier)?
> Might be worth adding if not.
We do, e.g. see tools/testing/selftests/bpf/progs/verifier_sdiv.c:
SEC("socket")
__description("SMOD32, non-zero imm divisor, check 1")
__success __success_unpriv __retval(-1)
__naked void smod32_non_zero_imm_1(void)
{
asm volatile (" \
w0 = -41; \
w0 s%%= 2; \
exit; \
" ::: __clobber_all);
}
And I'm still surprised that this produces different results in C and
in Python :)
$ python3
Python 3.11.5 (main, Aug 31 2023, 07:57:41) [GCC] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> -41 % 2
1
$ clang-repl
clang-repl> #include <stdio.h>
clang-repl> printf("%d\n", -41 % 2);
-1
There are several such tests with different combination of signs,
both 32-bit and 64-bit.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Bpf] [PATCH bpf-next] bpf, docs: Define signed modulo as using truncated division
2023-10-19 0:00 ` Eduard Zingerman
@ 2023-10-19 0:00 ` Eduard Zingerman
0 siblings, 0 replies; 10+ messages in thread
From: Eduard Zingerman @ 2023-10-19 0:00 UTC (permalink / raw)
To: Daniel Borkmann, Dave Thaler, bpf; +Cc: bpf, Dave Thaler
On Thu, 2023-10-19 at 01:40 +0200, Daniel Borkmann wrote:
> On 10/19/23 12:34 AM, Eduard Zingerman wrote:
> > On Tue, 2023-10-17 at 20:30 +0000, Dave Thaler wrote:
> > > From: Dave Thaler <dthaler@microsoft.com>
> > >
> > > There's different mathematical definitions (truncated, floored,
> > > rounded, etc.) and different languages have chosen different
> > > definitions [0][1]. E.g., languages/libraries that follow Knuth
> > > use a different mathematical definition than C uses. This
> > > patch specifies which definition BPF uses, as verified by
> > > Eduard [2] and others.
> > >
> > > [0]: https://en.wikipedia.org/wiki/Modulo#Variants_of_the_definition
> > > [1]: https://torstencurdt.com/tech/posts/modulo-of-negative-numbers/
> > > [2]: https://lore.kernel.org/bpf/57e6fefadaf3b2995bb259fa8e711c7220ce5290.camel@gmail.com/
> > >
> > > Signed-off-by: Dave Thaler <dthaler@microsoft.com>
> > > ---
> > > Documentation/bpf/standardization/instruction-set.rst | 8 ++++++++
> > > 1 file changed, 8 insertions(+)
> > >
> > > diff --git a/Documentation/bpf/standardization/instruction-set.rst b/Documentation/bpf/standardization/instruction-set.rst
> > > index c5d53a6e8c7..245b6defc29 100644
> > > --- a/Documentation/bpf/standardization/instruction-set.rst
> > > +++ b/Documentation/bpf/standardization/instruction-set.rst
> > > @@ -283,6 +283,14 @@ For signed operations (``BPF_SDIV`` and ``BPF_SMOD``), for ``BPF_ALU``,
> > > is first :term:`sign extended<Sign Extend>` from 32 to 64 bits, and then
> > > interpreted as a 64-bit signed value.
> > >
> > > +Note that there are varying definitions of the signed modulo operation
> > > +when the dividend or divisor are negative, where implementations often
> > > +vary by language such that Python, Ruby, etc. differ from C, Go, Java,
> > > +etc. This specification requires that signed modulo use truncated division
> > > +(where -13 % 3 == -1) as implemented in C, Go, etc.:
> > > +
> > > + a % n = a - n * trunc(a / n)
> > > +
> > > The ``BPF_MOVSX`` instruction does a move operation with sign extension.
> > > ``BPF_ALU | BPF_MOVSX`` :term:`sign extends<Sign Extend>` 8-bit and 16-bit operands into 32
> > > bit operands, and zeroes the remaining upper 32 bits.
> >
> > Acked-by: Eduard Zingerman <eddyz87@gmail.com>
>
> Eduard, do we have some test cases in BPF CI around this specifically (e.g. via test_verifier)?
> Might be worth adding if not.
We do, e.g. see tools/testing/selftests/bpf/progs/verifier_sdiv.c:
SEC("socket")
__description("SMOD32, non-zero imm divisor, check 1")
__success __success_unpriv __retval(-1)
__naked void smod32_non_zero_imm_1(void)
{
asm volatile (" \
w0 = -41; \
w0 s%%= 2; \
exit; \
" ::: __clobber_all);
}
And I'm still surprised that this produces different results in C and
in Python :)
$ python3
Python 3.11.5 (main, Aug 31 2023, 07:57:41) [GCC] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> -41 % 2
1
$ clang-repl
clang-repl> #include <stdio.h>
clang-repl> printf("%d\n", -41 % 2);
-1
There are several such tests with different combination of signs,
both 32-bit and 64-bit.
--
Bpf mailing list
Bpf@ietf.org
https://www.ietf.org/mailman/listinfo/bpf
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2023-10-19 0:02 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-17 20:30 [PATCH bpf-next] bpf, docs: Define signed modulo as using truncated division Dave Thaler
2023-10-17 20:30 ` [Bpf] " Dave Thaler
2023-10-18 22:28 ` David Vernet
2023-10-18 22:28 ` [Bpf] " David Vernet
2023-10-18 22:34 ` Eduard Zingerman
2023-10-18 23:40 ` Daniel Borkmann
2023-10-18 23:40 ` [Bpf] " Daniel Borkmann
2023-10-19 0:00 ` Eduard Zingerman
2023-10-19 0:00 ` [Bpf] " Eduard Zingerman
2023-10-18 23:40 ` patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox