Skip to contentSkip to conversation

ARM: net: JIT compiler for packet filters

PATCH Superseded Mircea Gherzan 6 participants 15 messages Dec 19, 2011 – Dec 23, 2011

Subsystems (5)

v2 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ARM: net: JIT compiler for packet filters

Based of Matt Evans's PPC64 implementation.

Supports only ARM mode with EABI.

Supports both little and big endian. Depends on the support for unaligned loads on ARMv7. Does not support all the BPF opcodes that deal with ancillary data. The scratch memory of the filter lives on the stack.

Enabled in the same way as for x86-64 and PPC64:

echo 1 > /proc/sys/net/core/bpf_jit_enable

A value greater than 1 enables opcode output.

---

Changes in v2:
* enable the compiler ony for ARMv5+ because of the BLX instruction
* use the same comparison for the ARM version checks
* use misaligned accesses on ARMv6
* fix the SEEN_MEM
* fix the mem_words_used()

 arch/arm/Kconfig          |    1 +
 arch/arm/Makefile         |    1 +
 arch/arm/net/Makefile     |    3 +
 arch/arm/net/bpf_jit_32.c |  838 +++++++++++++++++++++++++++++++++++++++++++++
 arch/arm/net/bpf_jit_32.h |  174 ++++++++++
 5 files changed, 1017 insertions(+), 0 deletions(-)
 create mode 100644 arch/arm/net/Makefile
 create mode 100644 arch/arm/net/bpf_jit_32.c
 create mode 100644 arch/arm/net/bpf_jit_32.h
arch/arm/Kconfig +1 -0
index abba5b8..ea65c41 100644
@@ -30,6 +30,7 @@ config ARM
 	select HAVE_SPARSE_IRQ
 	select GENERIC_IRQ_SHOW
 	select CPU_PM if (SUSPEND || CPU_IDLE)
+	select HAVE_BPF_JIT if (!THUMB2_KERNEL && AEABI)
 	help
 	  The ARM series is a line of low-power-consumption RISC chip designs
 	  licensed by ARM Ltd and targeted at embedded applications and
arch/arm/Makefile +1 -0
index dfcf3b0..8810a10 100644
@@ -255,6 +255,7 @@ core-$(CONFIG_VFP)		+= arch/arm/vfp/
 
 # If we have a machine-specific directory, then include it in the build.
 core-y				+= arch/arm/kernel/ arch/arm/mm/ arch/arm/common/
+core-y				+= arch/arm/net/
 core-y				+= $(machdirs) $(platdirs)
 
 drivers-$(CONFIG_OPROFILE)      += arch/arm/oprofile/
arch/arm/net/Makefile new file +3 -0
new file mode 100644
index 0000000..c2c1084
@@ -0,0 +1,3 @@
+# ARM-specific networking code
+
+obj-$(CONFIG_BPF_JIT) += bpf_jit_32.o
arch/arm/net/bpf_jit_32.c new file +838 -0

838 lines not on this page · raw message

arch/arm/net/bpf_jit_32.h new file +174 -0

174 lines not on this page · raw message

Signature · 1 line
1.7.7.3
#1 lore raw
Quoted 9 lines · Mircea Gherzan

Based of Matt Evans's PPC64 implementation.

Supports only ARM mode with EABI.

Supports both little and big endian. Depends on the support for unaligned loads on ARMv7. Does not support all the BPF opcodes that deal with ancillary data. The scratch memory of the filter lives on the stack.

Quoted 8 lines

Enabled in the same way as for x86-64 and PPC64:

echo 1 > /proc/sys/net/core/bpf_jit_enable

A value greater than 1 enables opcode output.

---

Interesting patch... I haven't reviewed in detail, but I have a few quick comments.

Quoted 5 lines

Changes in v2:
* enable the compiler ony for ARMv5+ because of the BLX instruction
* use the same comparison for the ARM version checks
* use misaligned accesses on ARMv6

You probably want to change the commit message now to reflect this.

Quoted 18 lines

* fix the SEEN_MEM
* fix the mem_words_used()

 arch/arm/Kconfig          |    1 +
 arch/arm/Makefile         |    1 +
 arch/arm/net/Makefile     |    3 +
 arch/arm/net/bpf_jit_32.c |  838 +++++++++++++++++++++++++++++++++++++++++++++
 arch/arm/net/bpf_jit_32.h |  174 ++++++++++
 5 files changed, 1017 insertions(+), 0 deletions(-)
 create mode 100644 arch/arm/net/Makefile
 create mode 100644 arch/arm/net/bpf_jit_32.c
 create mode 100644 arch/arm/net/bpf_jit_32.h
arch/arm/Kconfig +0 -0
index abba5b8..ea65c41 100644

arch/arm/Kconfig config ARM · line 33

Quoted 4 lines
arch/arm/Kconfig +1 -0
@@ -30,6 +30,7 @@ config ARM
 	select HAVE_SPARSE_IRQ
 	select GENERIC_IRQ_SHOW
 	select CPU_PM if (SUSPEND || CPU_IDLE)
+	select HAVE_BPF_JIT if (!THUMB2_KERNEL && AEABI)

Have to tried your code with a Thumb-2 kernel?

Quickly skimming though your patch, I don't see an obvious reason why we can't have that working, though I haven't tried it yet.

Note that it's fine to have the JIT generating ARM code, even if the rest if the kernel is Thumb-2. This would only start to cause problems if we want to do things like set kprobes in the JITted code, or unwind out of the JITted code.

It's just necessary to make sure that calls/returns into/out of the JITted code are handled correctly. You don't seem to do any scary arithmetic or mov to or from pc or lr, and it doesn't look like you ever call back into the kernel from JITted code, so the implementation is probably safe for ARM/Thumb interworking already (if I've understood correctly).

It doesn't look hard to port the JIT to generate Thumb-2 code directly either -- but I suggest not to worry about that initially. So long as the ARM-based JIT works in a Thumb-2 kernel, it will be useful.

[...]

Quoted 5 lines
arch/arm/net/bpf_jit_32.c new file +0 -0
new file mode 100644
index 0000000..d2b86fa

[...]

Quoted 2 lines
arch/arm/net/bpf_jit_32.c new file +2 -0
+static int build_body(struct jit_ctx *ctx)
+{

[...]

Quoted 7 lines
arch/arm/net/bpf_jit_32.c new file +7 -0
+		case BPF_S_ALU_DIV_K:
+			/* K is not zero, it was previously checked */
+			emit_mov_i(ARM_R1, k, ctx);
+			goto div;
+		case BPF_S_ALU_DIV_X:
+			ctx->seen |= SEEN_X;
+			emit(ARM_CMP_I(r_X, 0), ctx);

arch/arm/net/bpf_jit_32.c line 563

Quoted 10 lines
arch/arm/net/bpf_jit_32.c new file +10 -0
+			emit_err_ret(ARM_COND_EQ, ctx);
+			emit(ARM_MOV_R(ARM_R1, r_X), ctx);
+div:
+			ctx->seen |= SEEN_CALL;
+
+			emit(ARM_MOV_R(ARM_R0, r_A), ctx);
+			emit_mov_i(r_scratch, (u32)__aeabi_uidiv, ctx);
+			emit(ARM_BLX_R(r_scratch), ctx);
+			emit(ARM_MOV_R(r_A, ARM_R0), ctx);
+			break;

I don't know how much division is used by the packet filter JIT. If it gets used a significant amount, you might want to support hardware divide for CPUs that have it:

Cortex-A15 and later processors may have hardware integer divide support. You can check for its availability at runtime using by testing the HWCAP_IDIVA (for ARM) or HWCAP_IDIVT (for Thumb) bits in elf_hwcap (see arch/arm/include/asm/hwcap.h).

Cheers
---Dave

#2 lore raw
Quoted 55 lines · Dave Martin
Quoted 9 lines · Mircea Gherzan

Based of Matt Evans's PPC64 implementation.

Supports only ARM mode with EABI.

Supports both little and big endian. Depends on the support for unaligned loads on ARMv7. Does not support all the BPF opcodes that deal with ancillary data. The scratch memory of the filter lives on the stack.

Quoted 8 lines

Enabled in the same way as for x86-64 and PPC64:

echo 1 > /proc/sys/net/core/bpf_jit_enable

A value greater than 1 enables opcode output.

---

Interesting patch... I haven't reviewed in detail, but I have a few quick comments.

Quoted 5 lines

Changes in v2:
* enable the compiler ony for ARMv5+ because of the BLX instruction
* use the same comparison for the ARM version checks
* use misaligned accesses on ARMv6

You probably want to change the commit message now to reflect this.

Quoted 18 lines

* fix the SEEN_MEM
* fix the mem_words_used()

 arch/arm/Kconfig          |    1 +
 arch/arm/Makefile         |    1 +
 arch/arm/net/Makefile     |    3 +
 arch/arm/net/bpf_jit_32.c |  838 +++++++++++++++++++++++++++++++++++++++++++++
 arch/arm/net/bpf_jit_32.h |  174 ++++++++++
 5 files changed, 1017 insertions(+), 0 deletions(-)
 create mode 100644 arch/arm/net/Makefile
 create mode 100644 arch/arm/net/bpf_jit_32.c
 create mode 100644 arch/arm/net/bpf_jit_32.h
arch/arm/Kconfig +0 -0
index abba5b8..ea65c41 100644

arch/arm/Kconfig config ARM · line 33

Quoted 4 lines
arch/arm/Kconfig +1 -0
@@ -30,6 +30,7 @@ config ARM
 	select HAVE_SPARSE_IRQ
 	select GENERIC_IRQ_SHOW
 	select CPU_PM if (SUSPEND || CPU_IDLE)
+	select HAVE_BPF_JIT if (!THUMB2_KERNEL && AEABI)

Have to tried your code with a Thumb-2 kernel?

Quoted 7 lines

Quickly skimming though your patch, I don't see an obvious reason why we can't have that working, though I haven't tried it yet.

Note that it's fine to have the JIT generating ARM code, even if the rest if the kernel is Thumb-2. This would only start to cause problems if we want to do things like set kprobes in the JITted code, or unwind out of the JITted code.

or use a CPU that doesn't speak ARM (e.g. v7M)

Quoted 10 lines

It's just necessary to make sure that calls/returns into/out of the JITted code are handled correctly. You don't seem to do any scary arithmetic or mov to or from pc or lr, and it doesn't look like you ever call back into the kernel from JITted code, so the implementation is probably safe for ARM/Thumb interworking already (if I've understood correctly).

It doesn't look hard to port the JIT to generate Thumb-2 code directly either -- but I suggest not to worry about that initially. So long as the ARM-based JIT works in a Thumb-2 kernel, it will be useful.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-K?nig            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#3 lore raw
Quoted 56 lines · Uwe Kleine-K?nig
Quoted 55 lines · Dave Martin
Quoted 9 lines · Mircea Gherzan

Based of Matt Evans's PPC64 implementation.

Supports only ARM mode with EABI.

Supports both little and big endian. Depends on the support for unaligned loads on ARMv7. Does not support all the BPF opcodes that deal with ancillary data. The scratch memory of the filter lives on the stack.

Quoted 8 lines

Enabled in the same way as for x86-64 and PPC64:

echo 1 > /proc/sys/net/core/bpf_jit_enable

A value greater than 1 enables opcode output.

---

Interesting patch... I haven't reviewed in detail, but I have a few quick comments.

Quoted 5 lines

Changes in v2:
* enable the compiler ony for ARMv5+ because of the BLX instruction
* use the same comparison for the ARM version checks
* use misaligned accesses on ARMv6

You probably want to change the commit message now to reflect this.

Quoted 18 lines

* fix the SEEN_MEM
* fix the mem_words_used()

 arch/arm/Kconfig          |    1 +
 arch/arm/Makefile         |    1 +
 arch/arm/net/Makefile     |    3 +
 arch/arm/net/bpf_jit_32.c |  838 +++++++++++++++++++++++++++++++++++++++++++++
 arch/arm/net/bpf_jit_32.h |  174 ++++++++++
 5 files changed, 1017 insertions(+), 0 deletions(-)
 create mode 100644 arch/arm/net/Makefile
 create mode 100644 arch/arm/net/bpf_jit_32.c
 create mode 100644 arch/arm/net/bpf_jit_32.h
arch/arm/Kconfig +0 -0
index abba5b8..ea65c41 100644

arch/arm/Kconfig config ARM · line 33

Quoted 4 lines
arch/arm/Kconfig +1 -0
@@ -30,6 +30,7 @@ config ARM
 	select HAVE_SPARSE_IRQ
 	select GENERIC_IRQ_SHOW
 	select CPU_PM if (SUSPEND || CPU_IDLE)
+	select HAVE_BPF_JIT if (!THUMB2_KERNEL && AEABI)

Have to tried your code with a Thumb-2 kernel?

Quoted 8 lines
Quoted 7 lines

Quickly skimming though your patch, I don't see an obvious reason why we can't have that working, though I haven't tried it yet.

Note that it's fine to have the JIT generating ARM code, even if the rest if the kernel is Thumb-2. This would only start to cause problems if we want to do things like set kprobes in the JITted code, or unwind out of the JITted code.

or use a CPU that doesn't speak ARM (e.g. v7M)

Indeed... I was assuming though that that would be out of scope for the first iteration of this code.

Cheers
---Dave

#4 lore raw

Hello,

Quoted 4 lines · Dave Martin
Quoted 3 lines · Uwe Kleine-K?nig
Quoted 2 lines · Dave Martin
Quoted 1 line · Mircea Gherzan

Based of Matt Evans's PPC64 implementation.

Quoted 8 lines
Quoted 5 lines
Quoted 4 lines

Note that it's fine to have the JIT generating ARM code, even if the rest if the kernel is Thumb-2. This would only start to cause problems if we want to do things like set kprobes in the JITted code, or unwind out of the JITted code.

or use a CPU that doesn't speak ARM (e.g. v7M)

Indeed... I was assuming though that that would be out of scope for the first iteration of this code.

Right, depending on !THUMB2 is fine. Only generating ARM code with THUMB2=y is not.

Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-K?nig            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#5 lore raw
Quoted 11 lines · Uwe Kleine-K?nig

Hello,

Quoted 8 lines · Dave Martin
Quoted 7 lines · Uwe Kleine-K?nig
Quoted 6 lines · Dave Martin
Quoted 1 line · Mircea Gherzan

Based of Matt Evans's PPC64 implementation.

Note that it's fine to have the JIT generating ARM code, even if the rest if the kernel is Thumb-2. This would only start to cause problems if we want to do things like set kprobes in the JITted code, or unwind out of the JITted code.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

or use a CPU that doesn't speak ARM (e.g. v7M)

Indeed... I was assuming though that that would be out of scope for the first iteration of this code.

Right, depending on !THUMB2 is fine. Only generating ARM code with THUMB2=y is not.

The kernel doesn't support v7-M upstream though. CONFIG_THUMB2_KERNEL=y doesn't mean that there is no ARM code present -- there _is_ ARM code in the kernel image even with this option (though not very much).

IMHO, CONFIG_THUMB2_KERNEL should be viewed as similiar to an optimisation option or other code gen options -- it's a hint to use the Thumb-2 instruction set in preference to ARM, but doesn't make a 100% guarantee. Some parts of the kernel do contain ARM code even in this configuration.

The real problem we have here is that we do not distinguish between v7-A and v7-M in Kconfig (this is not the same thing as CONFIG_MMU, since there's no special reason why it should be impossible to build a ucLinux kernel for v7-A -- in which case the ARM instruction is available)

In the meantime, we could temporarily make the JIT dependent on MMU || !THUMB2_KERNEL, but that doesn't seem strictly correct.

If we had dedicated Kconfig variables describing the (non-) availability of the two instruction sets, that might enable us to get this right.

Cheers
---Dave

#6 lore raw

Hi,

Quoted 14 lines · Dave Martin
Quoted 13 lines · Uwe Kleine-K?nig

Hello,

Quoted 10 lines · Dave Martin
Quoted 8 lines · Uwe Kleine-K?nig
Quoted 6 lines · Dave Martin
Quoted 1 line · Mircea Gherzan

Based of Matt Evans's PPC64 implementation.

Note that it's fine to have the JIT generating ARM code, even if the rest if the kernel is Thumb-2. This would only start to cause problems if we want to do things like set kprobes in the JITted code, or unwind out of the JITted code.

or use a CPU that doesn't speak ARM (e.g. v7M)

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Indeed... I was assuming though that that would be out of scope for the first iteration of this code.

Right, depending on !THUMB2 is fine. Only generating ARM code with THUMB2=y is not.

The kernel doesn't support v7-M upstream though. CONFIG_THUMB2_KERNEL=y doesn't mean that there is no ARM code present -- there _is_ ARM code in the kernel image even with this option (though not very much).

What about relying at runtime on bits[3:0] of ID_PFR0? And this only for ARMv7.

Are there any other ARM cores of interest that do _not_ support the ARM instruction set?

Thanks,
Mircea

#7 lore raw
In reply to Dave Martin
Quoted 27 lines · Dave Martin
Quoted 9 lines · Uwe Kleine-K?nig
Quoted 8 lines · Dave Martin
Quoted 7 lines · Uwe Kleine-K?nig
Quoted 6 lines · Dave Martin
Quoted 1 line · Mircea Gherzan

Based of Matt Evans's PPC64 implementation.

Note that it's fine to have the JIT generating ARM code, even if the rest if the kernel is Thumb-2. ?This would only start to cause problems if we want to do things like set kprobes in the JITted code, or unwind out of the JITted code.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

or use a CPU that doesn't speak ARM (e.g. v7M)

Indeed... I was assuming though that that would be out of scope for the first iteration of this code.

Right, depending on !THUMB2 is fine. Only generating ARM code with THUMB2=y is not.

The kernel doesn't support v7-M upstream though. ?CONFIG_THUMB2_KERNEL=y doesn't mean that there is no ARM code present -- there _is_ ARM code in the kernel image even with this option (though not very much).

IMHO, CONFIG_THUMB2_KERNEL should be viewed as similiar to an optimisation option or other code gen options -- it's a hint to use the Thumb-2 instruction set in preference to ARM, but doesn't make a 100% guarantee. Some parts of the kernel do contain ARM code even in this configuration.

Quoted 4 lines

The real problem we have here is that we do not distinguish between v7-A and v7-M in Kconfig (this is not the same thing as CONFIG_MMU, since there's no special reason why it should be impossible to build a ucLinux kernel for v7-A -- in which case the ARM instruction is available)

If we ever take the time to push ARMv7-M into mainline, my patches actually introduce CONFIG_CPU_V7M:

http://www.linux-arm.org/git?p=linux-2.6-stable.git;a=commitdiff;h=53a9ca6eccb3e14c7d9045870464cbfb37b0bb3b

Signature · 1 line
Catalin
#8 lore raw
In reply to Dave Martin

Hi,

Quoted 23 lines · Dave Martin
Quoted 9 lines · Mircea Gherzan

Based of Matt Evans's PPC64 implementation.

Supports only ARM mode with EABI.

Supports both little and big endian. Depends on the support for unaligned loads on ARMv7. Does not support all the BPF opcodes that deal with ancillary data. The scratch memory of the filter lives on the stack.

Quoted 8 lines

Enabled in the same way as for x86-64 and PPC64:

echo 1 > /proc/sys/net/core/bpf_jit_enable

A value greater than 1 enables opcode output.

---

Interesting patch... I haven't reviewed in detail, but I have a few quick comments.

Quoted 1 line
Quoted 6 lines
Quoted 4 lines

Changes in v2:
* enable the compiler ony for ARMv5+ because of the BLX instruction
* use the same comparison for the ARM version checks
* use misaligned accesses on ARMv6

You probably want to change the commit message now to reflect this.

Will do in the next version.

Quoted 14 lines
Quoted 14 lines

* fix the SEEN_MEM
* fix the mem_words_used()

 arch/arm/Kconfig          |    1 +
 arch/arm/Makefile         |    1 +
 arch/arm/net/Makefile     |    3 +
 arch/arm/net/bpf_jit_32.c |  838 +++++++++++++++++++++++++++++++++++++++++++++
 arch/arm/net/bpf_jit_32.h |  174 ++++++++++
 5 files changed, 1017 insertions(+), 0 deletions(-)
 create mode 100644 arch/arm/net/Makefile
 create mode 100644 arch/arm/net/bpf_jit_32.c
 create mode 100644 arch/arm/net/bpf_jit_32.h
Quoted 11 lines

arch/arm/Kconfig config ARM · line 33

Quoted 9 lines
arch/arm/Kconfig +1 -0
index abba5b8..ea65c41 100644
@@ -30,6 +30,7 @@ config ARM
 	select HAVE_SPARSE_IRQ
 	select GENERIC_IRQ_SHOW
 	select CPU_PM if (SUSPEND || CPU_IDLE)
+	select HAVE_BPF_JIT if (!THUMB2_KERNEL && AEABI)

Have to tried your code with a Thumb-2 kernel?

Not yet.

Quoted 8 lines

Quickly skimming though your patch, I don't see an obvious reason why we can't have that working, though I haven't tried it yet.

Note that it's fine to have the JIT generating ARM code, even if the rest if the kernel is Thumb-2. This would only start to cause problems if we want to do things like set kprobes in the JITted code, or unwind out of the JITted code.

Quoted 6 lines

It's just necessary to make sure that calls/returns into/out of the JITted code are handled correctly. You don't seem to do any scary arithmetic or mov to or from pc or lr, and it doesn't look like you ever call back into the kernel from JITted code, so the implementation is probably safe for ARM/Thumb interworking already (if I've understood correctly).

The JITed code calls back to the kernel for the load helpers. So setting bit 0 is required.

Quoted 32 lines

It doesn't look hard to port the JIT to generate Thumb-2 code directly either -- but I suggest not to worry about that initially. So long as the ARM-based JIT works in a Thumb-2 kernel, it will be useful.

[...]

Quoted 5 lines
arch/arm/net/bpf_jit_32.c new file +0 -0
new file mode 100644
index 0000000..d2b86fa

[...]

Quoted 2 lines
arch/arm/net/bpf_jit_32.c new file +2 -0
+static int build_body(struct jit_ctx *ctx)
+{

[...]

Quoted 13 lines
arch/arm/net/bpf_jit_32.c new file +13 -0
+		case BPF_S_ALU_DIV_K:
+			/* K is not zero, it was previously checked */
+			emit_mov_i(ARM_R1, k, ctx);
+			goto div;
+		case BPF_S_ALU_DIV_X:
+			ctx->seen |= SEEN_X;
+			emit(ARM_CMP_I(r_X, 0), ctx);
+			emit_err_ret(ARM_COND_EQ, ctx);
+			emit(ARM_MOV_R(ARM_R1, r_X), ctx);
+div:
+			ctx->seen |= SEEN_CALL;
+
+			emit(ARM_MOV_R(ARM_R0, r_A), ctx);
Quoted 8 lines

arch/arm/net/bpf_jit_32.c line 563

Quoted 4 lines
arch/arm/net/bpf_jit_32.c new file +4 -0
+			emit_mov_i(r_scratch, (u32)__aeabi_uidiv, ctx);
+			emit(ARM_BLX_R(r_scratch), ctx);
+			emit(ARM_MOV_R(r_A, ARM_R0), ctx);
+			break;

I don't know how much division is used by the packet filter JIT. If it gets used a significant amount, you might want to support hardware divide for CPUs that have it:

Division rarely appears in "normal" BPF filters: it must be an explicit part of the human-readable filter expression (the BPF compiler does not generate division opcodes in other cases, AFAICT). Nonetheless, support for hardware division would spare a bit of stack space for filters like "len / 100 == 1".

Quoted 4 lines

Cortex-A15 and later processors may have hardware integer divide support. You can check for its availability at runtime using by testing the HWCAP_IDIVA (for ARM) or HWCAP_IDIVT (for Thumb) bits in elf_hwcap (see arch/arm/include/asm/hwcap.h).

I will include this in the next version of the patch.

Cheers,
Mircea

#9 lore raw
Quoted 78 lines · Mircea Gherzan

Hi,

Quoted 23 lines · Dave Martin
Quoted 9 lines · Mircea Gherzan

Based of Matt Evans's PPC64 implementation.

Supports only ARM mode with EABI.

Supports both little and big endian. Depends on the support for unaligned loads on ARMv7. Does not support all the BPF opcodes that deal with ancillary data. The scratch memory of the filter lives on the stack.

Quoted 8 lines

Enabled in the same way as for x86-64 and PPC64:

echo 1 > /proc/sys/net/core/bpf_jit_enable

A value greater than 1 enables opcode output.

---

Interesting patch... I haven't reviewed in detail, but I have a few quick comments.

Quoted 1 line
Quoted 6 lines
Quoted 4 lines

Changes in v2:
* enable the compiler ony for ARMv5+ because of the BLX instruction
* use the same comparison for the ARM version checks
* use misaligned accesses on ARMv6

You probably want to change the commit message now to reflect this.

Will do in the next version.

Quoted 14 lines
Quoted 14 lines

* fix the SEEN_MEM
* fix the mem_words_used()

 arch/arm/Kconfig          |    1 +
 arch/arm/Makefile         |    1 +
 arch/arm/net/Makefile     |    3 +
 arch/arm/net/bpf_jit_32.c |  838 +++++++++++++++++++++++++++++++++++++++++++++
 arch/arm/net/bpf_jit_32.h |  174 ++++++++++
 5 files changed, 1017 insertions(+), 0 deletions(-)
 create mode 100644 arch/arm/net/Makefile
 create mode 100644 arch/arm/net/bpf_jit_32.c
 create mode 100644 arch/arm/net/bpf_jit_32.h
Quoted 11 lines

arch/arm/Kconfig config ARM · line 33

Quoted 9 lines
arch/arm/Kconfig +1 -0
index abba5b8..ea65c41 100644
@@ -30,6 +30,7 @@ config ARM
 	select HAVE_SPARSE_IRQ
 	select GENERIC_IRQ_SHOW
 	select CPU_PM if (SUSPEND || CPU_IDLE)
+	select HAVE_BPF_JIT if (!THUMB2_KERNEL && AEABI)

Have to tried your code with a Thumb-2 kernel?

Not yet.

Quoted 8 lines

Quickly skimming though your patch, I don't see an obvious reason why we can't have that working, though I haven't tried it yet.

Note that it's fine to have the JIT generating ARM code, even if the rest if the kernel is Thumb-2. This would only start to cause problems if we want to do things like set kprobes in the JITted code, or unwind out of the JITted code.

Quoted 6 lines

It's just necessary to make sure that calls/returns into/out of the JITted code are handled correctly. You don't seem to do any scary arithmetic or mov to or from pc or lr, and it doesn't look like you ever call back into the kernel from JITted code, so the implementation is probably safe for ARM/Thumb interworking already (if I've understood correctly).

Quoted 2 lines

The JITed code calls back to the kernel for the load helpers. So setting bit 0 is required.

When you take the address of a link-time external function symbol, bit[0] in the address will automatically be set appropriately by the linker to indicate the target instruction set -- you already use BX/BLX to jump to such symbols, so you should switch correctly when calling _to_ the kernel.

Returns should also work, except for old-style "mov pc,lr" returns made in Thumb code (from ARM code, this magically works for >= v7). Such returns only happen in hand-written assembler: for C code, the compiler always generates proper AEABI-compliant return sequences.

So, for calling load_func[], jit_get_skb_b etc. (which are C functions), there should be no problem.

I think the only code which you call from the JIT output but which does not return compliantly is __aeabi_uidiv() in arch/arm/lib/lib1funcs.S.

I have a quick hacked-up patch (below) which attempts to fix this; I'd be interested if this works for you -- but finalising your ARM-only version of the patch should still be the priority.

If this fix does work, I'll turn it into a proper patch, as we can maybe use it more widely.

[...]

Quoted 24 lines
Quoted 9 lines
Quoted 9 lines
arch/arm/Kconfig +9 -0
+		case BPF_S_ALU_DIV_X:
+			ctx->seen |= SEEN_X;
+			emit(ARM_CMP_I(r_X, 0), ctx);
+			emit_err_ret(ARM_COND_EQ, ctx);
+			emit(ARM_MOV_R(ARM_R1, r_X), ctx);
+div:
+			ctx->seen |= SEEN_CALL;
+
+			emit(ARM_MOV_R(ARM_R0, r_A), ctx);
Quoted 8 lines

arch/arm/Kconfig

Quoted 4 lines
arch/arm/Kconfig +4 -0
+			emit_mov_i(r_scratch, (u32)__aeabi_uidiv, ctx);
+			emit(ARM_BLX_R(r_scratch), ctx);
+			emit(ARM_MOV_R(r_A, ARM_R0), ctx);
+			break;

I don't know how much division is used by the packet filter JIT. If it gets used a significant amount, you might want to support hardware divide for CPUs that have it:

Division rarely appears in "normal" BPF filters: it must be an explicit part of the human-readable filter expression (the BPF compiler does not generate division opcodes in other cases, AFAICT). Nonetheless, support for hardware division would spare a bit of stack space for filters like "len / 100 == 1".

Quoted 6 lines
Quoted 4 lines

Cortex-A15 and later processors may have hardware integer divide support. You can check for its availability at runtime using by testing the HWCAP_IDIVA (for ARM) or HWCAP_IDIVT (for Thumb) bits in elf_hwcap (see arch/arm/include/asm/hwcap.h).

I will include this in the next version of the patch.

Ok, cool

Cheers
---Dave

#10 lore raw
Quoted 1 line · Dave Martin

On Mon, Dec 19, 2011 at 06:45:13PM +0200, Mircea Gherzan wrote:

[...]

Quoted 21 lines
Quoted 2 lines

The JITed code calls back to the kernel for the load helpers. So setting bit 0 is required.

When you take the address of a link-time external function symbol, bit[0] in the address will automatically be set appropriately by the linker to indicate the target instruction set -- you already use BX/BLX to jump to such symbols, so you should switch correctly when calling _to_ the kernel.

Returns should also work, except for old-style "mov pc,lr" returns made in Thumb code (from ARM code, this magically works for >= v7). Such returns only happen in hand-written assembler: for C code, the compiler always generates proper AEABI-compliant return sequences.

So, for calling load_func[], jit_get_skb_b etc. (which are C functions), there should be no problem.

I think the only code which you call from the JIT output but which does not return compliantly is __aeabi_uidiv() in arch/arm/lib/lib1funcs.S.

Quoted 6 lines

I have a quick hacked-up patch (below) which attempts to fix this; I'd be interested if this works for you -- but finalising your ARM-only version of the patch should still be the priority.

If this fix does work, I'll turn it into a proper patch, as we can maybe use it more widely.

Oops, I forgot to paste in my patch ... here it is.

Cheers
---Dave

arch/arm/include/asm/assembler.h +35 -0
index b6e65de..013bfaf 100644
@@ -313,4 +313,39 @@
 	.size \name , . - \name
 	.endm
 
+/*
+ * Helper macro to abstract a function return, where the return address is
+ * held in a register:
+ */
+.macro __def_bret cc
+	.macro bret\cc Rm:req
+#if __LINUX_ARM_ARCH__ < 7
+		mov\cc	pc, \Rm
+#else
+		bx\cc	\Rm
+#endif
+	
+	.endm
+.endm
+
+__def_bret
+__def_bret eq
+__def_bret ne
+__def_bret hs
+__def_bret cs	
+__def_bret lo
+__def_bret cc
+__def_bret vs
+__def_bret vc
+__def_bret mi
+__def_bret pl
+__def_bret hi
+__def_bret ls
+__def_bret ge
+__def_bret lt
+__def_bret gt
+__def_bret le
+
+.purgem __def_bret
+
 #endif /* __ASM_ASSEMBLER_H__ */
arch/arm/lib/lib1funcs.S +13 -13
index c562f64..c3a4dbf 100644
@@ -210,7 +210,7 @@ ENTRY(__aeabi_uidiv)
 UNWIND(.fnstart)
 
 	subs	r2, r1, #1
-	moveq	pc, lr
+	breteq	lr
 	bcc	Ldiv0
 	cmp	r0, r1
 	bls	11f
@@ -220,16 +220,16 @@ UNWIND(.fnstart)
 	ARM_DIV_BODY r0, r1, r2, r3
 
 	mov	r0, r2
-	mov	pc, lr
+	bret	lr
 
 11:	moveq	r0, #1
 	movne	r0, #0
-	mov	pc, lr
+	bret	lr
 
 12:	ARM_DIV2_ORDER r1, r2
 
 	mov	r0, r0, lsr r2
-	mov	pc, lr
+	bret	lr
 
 UNWIND(.fnend)
 ENDPROC(__udivsi3)
@@ -244,11 +244,11 @@ UNWIND(.fnstart)
 	moveq   r0, #0
 	tsthi	r1, r2				@ see if divisor is power of 2
 	andeq	r0, r0, r2
-	movls	pc, lr
+	bretls	lr
 
 	ARM_MOD_BODY r0, r1, r2, r3
 
-	mov	pc, lr
+	bret	lr
 
 UNWIND(.fnend)
 ENDPROC(__umodsi3)
@@ -274,23 +274,23 @@ UNWIND(.fnstart)
 
 	cmp	ip, #0
 	rsbmi	r0, r0, #0
-	mov	pc, lr
+	bret	lr
 
 10:	teq	ip, r0				@ same sign ?
 	rsbmi	r0, r0, #0
-	mov	pc, lr
+	bret	lr
 
 11:	movlo	r0, #0
 	moveq	r0, ip, asr #31
 	orreq	r0, r0, #1
-	mov	pc, lr
+	bret	lr
 
 12:	ARM_DIV2_ORDER r1, r2
 
 	cmp	ip, #0
 	mov	r0, r3, lsr r2
 	rsbmi	r0, r0, #0
-	mov	pc, lr
+	bret	lr
 
 UNWIND(.fnend)
 ENDPROC(__divsi3)
@@ -315,7 +315,7 @@ UNWIND(.fnstart)
 
 10:	cmp	ip, #0
 	rsbmi	r0, r0, #0
-	mov	pc, lr
+	bret	lr
 
 UNWIND(.fnend)
 ENDPROC(__modsi3)
@@ -331,7 +331,7 @@ UNWIND(.save {r0, r1, ip, lr}	)
 	ldmfd	sp!, {r1, r2, ip, lr}
 	mul	r3, r0, r2
 	sub	r1, r1, r3
-	mov	pc, lr
+	bret	lr
 
 UNWIND(.fnend)
 ENDPROC(__aeabi_uidivmod)
@@ -344,7 +344,7 @@ UNWIND(.save {r0, r1, ip, lr}	)
 	ldmfd	sp!, {r1, r2, ip, lr}
 	mul	r3, r0, r2
 	sub	r1, r1, r3
-	mov	pc, lr
+	bret	lr
 
 UNWIND(.fnend)
 ENDPROC(__aeabi_idivmod)
#11 lore raw
Quoted 47 lines · Dave Martin
Quoted 1 line · Dave Martin

On Mon, Dec 19, 2011 at 06:45:13PM +0200, Mircea Gherzan wrote:

[...]

Quoted 21 lines
Quoted 2 lines

The JITed code calls back to the kernel for the load helpers. So setting bit 0 is required.

When you take the address of a link-time external function symbol, bit[0] in the address will automatically be set appropriately by the linker to indicate the target instruction set -- you already use BX/BLX to jump to such symbols, so you should switch correctly when calling _to_ the kernel.

Returns should also work, except for old-style "mov pc,lr" returns made in Thumb code (from ARM code, this magically works for >= v7). Such returns only happen in hand-written assembler: for C code, the compiler always generates proper AEABI-compliant return sequences.

So, for calling load_func[], jit_get_skb_b etc. (which are C functions), there should be no problem.

I think the only code which you call from the JIT output but which does not return compliantly is __aeabi_uidiv() in arch/arm/lib/lib1funcs.S.

Quoted 6 lines

I have a quick hacked-up patch (below) which attempts to fix this; I'd be interested if this works for you -- but finalising your ARM-only version of the patch should still be the priority.

If this fix does work, I'll turn it into a proper patch, as we can maybe use it more widely.

Oops, I forgot to paste in my patch ... here it is.

Cheers
---Dave

arch/arm/include/asm/assembler.h +1 -0
index b6e65de..013bfaf 100644
@@ -313,4 +313,39 @@
 	.size \name , . - \name
 	.endm
 
+/*

arch/arm/include/asm/assembler.h line 326

Quoted 10 lines
arch/arm/include/asm/assembler.h +10 -0
+ * Helper macro to abstract a function return, where the return address is
+ * held in a register:
+ */
+.macro __def_bret cc
+	.macro bret\cc Rm:req
+#if __LINUX_ARM_ARCH__ < 7
+		mov\cc	pc, \Rm
+#else
+		bx\cc	\Rm
+#endif

This patch worked for me, because I'm using ARMv7 exclusively. But I'm inclined to think that your patch will fail on ARMv6T (for example) with a Thumb2 kernel, because the "mov pc, Rm" is a simple branch on pre-v7 cores.

Note however that v3 of my patch no longer requires any changes to the assembly code: the C trampoline/wrapper for integer division ensures a proper return. And this one is used only for the DIV_X BPF opcode, which appears also quite rarely.

[...]

Cheers,
Mircea

#12 lore raw
Quoted 47 lines · Mircea Gherzan
Quoted 46 lines · Dave Martin
Quoted 1 line · Dave Martin

On Mon, Dec 19, 2011 at 06:45:13PM +0200, Mircea Gherzan wrote:

[...]

Quoted 21 lines
Quoted 2 lines

The JITed code calls back to the kernel for the load helpers. So setting bit 0 is required.

When you take the address of a link-time external function symbol, bit[0] in the address will automatically be set appropriately by the linker to indicate the target instruction set -- you already use BX/BLX to jump to such symbols, so you should switch correctly when calling _to_ the kernel.

Returns should also work, except for old-style "mov pc,lr" returns made in Thumb code (from ARM code, this magically works for >= v7). Such returns only happen in hand-written assembler: for C code, the compiler always generates proper AEABI-compliant return sequences.

So, for calling load_func[], jit_get_skb_b etc. (which are C functions), there should be no problem.

I think the only code which you call from the JIT output but which does not return compliantly is __aeabi_uidiv() in arch/arm/lib/lib1funcs.S.

Quoted 6 lines

I have a quick hacked-up patch (below) which attempts to fix this; I'd be interested if this works for you -- but finalising your ARM-only version of the patch should still be the priority.

If this fix does work, I'll turn it into a proper patch, as we can maybe use it more widely.

Oops, I forgot to paste in my patch ... here it is.

Cheers
---Dave

arch/arm/include/asm/assembler.h +0 -0
index b6e65de..013bfaf 100644
@@ -313,4 +313,39 @@
 	.size \name , . - \name
 	.endm
 
Quoted 16 lines

arch/arm/include/asm/assembler.h line 326

Quoted 11 lines
arch/arm/include/asm/assembler.h +11 -0
+/*
+ * Helper macro to abstract a function return, where the return address is
+ * held in a register:
+ */
+.macro __def_bret cc
+	.macro bret\cc Rm:req
+#if __LINUX_ARM_ARCH__ < 7
+		mov\cc	pc, \Rm
+#else
+		bx\cc	\Rm
+#endif

This patch worked for me, because I'm using ARMv7 exclusively. But I'm inclined to think that your patch will fail on ARMv6T (for example) with a Thumb2 kernel, because the "mov pc, Rm" is a simple branch on pre-v7 cores.

Thumb-2 kernels are not currently supported on ARMv6T2, and that's probably not going to change for the foreseeable future. There are not many v6T2 CPUs out there.

In principle, the condition could be __LINUX_ARM_ARCH__ < 5, since the bx instruction is guaranteed to be available on all ARMv5 CPUs and later (ARMv5T is not required).

For this sketch of the patch though, I just went for the principle of least impact, and changed the behaviour only for CPUs where it's definitely needed (i.e., v7+).

Thanks for trying it out, though.

Quoted 4 lines

Note however that v3 of my patch no longer requires any changes to the assembly code: the C trampoline/wrapper for integer division ensures a proper return. And this one is used only for the DIV_X BPF opcode, which appears also quite rarely.

I may try to push my patch independently -- this way we might make a lot of the existing assembler files properly EABI compliant at low effort; as and when that happens, your C helper wouldn't be needed any more, but it makes sense for you to keep if for now.

Cheers
---Dave

#13 lore raw
Quoted 3 lines

In principle, the condition could be __LINUX_ARM_ARCH__ < 5, since the bx instruction is guaranteed to be available on all ARMv5 CPUs and later (ARMv5T is not required).

Can't this code look at the actual cpu being used, and generate the relevant 'return' instruction? (Instead of trying to use some compile-time value.)

David

#14 lore raw
Quoted 7 lines · David Laight
Quoted 3 lines

In principle, the condition could be __LINUX_ARM_ARCH__ < 5, since the bx instruction is guaranteed to be available on all ARMv5 CPUs and later (ARMv5T is not required).

Can't this code look at the actual cpu being used, and generate the relevant 'return' instruction? (Instead of trying to use some compile-time value.)

There is really no point doing so. We intend to ultimately have a kernel that may support ARMv5 and lower, and another kernel to support ARMv6 and ARMv7. So in a given context, either bx is always available (ARMv6+) or never required (ARMv5-).

Nicolas