minor bpf improvements
cover minor bpf improvements
Minor update to BPF. Add allocation annotation and remove VLA.
Stephen Hemminger (2):
bpf: add allocation annotations to functions
bpf: remove dependency on vla
lib/bpf/bpf_pkt.c | 16 +++++++++------- lib/bpf/meson.build | 2 -- lib/bpf/rte_bpf.h | 11 ++++++++--- 3 files changed, 17 insertions(+), 12 deletions(-)
Signature · 1 line
2.51.0
PATCH 1/2 bpf: add allocation annotations to functions
In commit 80da7efbb4c4 ("eal: annotate allocation functions") helper macros were added to provide compiler information to detect misuse of alloc/free combinations. This covered many of the functions in DPDK but missed the case of data allocated by BPF load functions.
--- lib/bpf/rte_bpf.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
lib/bpf/rte_bpf.h +8 -3
@@ -18,6 +18,7 @@#include <rte_common.h> #include <rte_mbuf.h> +#include <rte_malloc.h> #include <bpf_def.h> #ifdef __cplusplus
@@ -128,7 +129,8 @@ rte_bpf_destroy(struct rte_bpf *bpf);* - ENOMEM - can't reserve enough memory */ struct rte_bpf * -rte_bpf_load (const struct rte_bpf_prm *prm); +rte_bpf_load (const struct rte_bpf_prm *prm) + __rte_malloc__rte_dealloc (rte_bpf_destroy,1 );/** * Create a new eBPF execution context and load BPF code from given ELF
@@ -152,7 +154,9 @@ rte_bpf_load(const struct rte_bpf_prm *prm);*/ struct rte_bpf *rte_bpf_elf_load (const struct rte_bpf_prm *prm,const char *fname, -const char *sname); +const char *sname) + __rte_malloc__rte_dealloc (rte_bpf_destroy,1 ); +/** * Execute given BPF bytecode. *
@@ -228,7 +232,8 @@ struct bpf_program;* - ENOTSUP - operation not supported */ struct rte_bpf_prm * -rte_bpf_convert (const struct bpf_program *prog); +rte_bpf_convert (const struct bpf_program *prog) + __rte_malloc __rte_dealloc_free; #ifdef __cplusplus }
Signature · 1 line
2.51.0
PATCH 2/2 bpf: remove dependency on vla
In reply to Stephen HemmingerThe code for ethdev callbacks was using variable length arrays which is a feature not supported on MSVC and later C standards. Replace with alloca().
--- lib/bpf/bpf_pkt.c | 16 +++++++++------- lib/bpf/meson.build | 2 -- 2 files changed, 9 insertions(+), 9 deletions(-)
lib/bpf/bpf_pkt.c +9 -7
@@ -6,6 +6,7 @@#include <string.h> #include <errno.h> #include <stdint.h> +#include <alloca.h> #include <sys/queue.h>
@@ -163,10 +164,11 @@ apply_filter(struct rte_mbuf *mb[], const uint64_t rc[], uint32_t num, uint32_t drop) {uint32_t i, j, k; -struct rte_mbuf *dr[num]; +struct rte_mbuf **dr; -for (i =0 , j =0 , k =0 ; i != num; i++) { + dr =alloca (sizeof (struct rte_mbuf *) * num); +for (i =0 , j =0 , k =0 ; i != num; i++) {/* filter matches */ if (rc[i] !=0 ) mb[j++] = mb[i];
@@ -193,8 +195,8 @@ pkt_filter_vm(const struct rte_bpf *bpf, struct rte_mbuf *mb[], uint32_t num, uint32_t drop) {uint32_t i; -void *dp[num]; -uint64_t rc[num]; +void **dp =alloca (sizeof (void *) * num); +uint64_t *rc =alloca (sizeof (uint64_t ) * num);for (i =0 ; i != num; i++) dp[i] = rte_pktmbuf_mtod(mb[i], void *);
@@ -209,7 +211,7 @@ pkt_filter_jit(const struct rte_bpf_jit *jit, struct rte_mbuf *mb[], {uint32_t i, n;void *dp; -uint64_t rc[num]; +uint64_t *rc =alloca (sizeof (uint64_t ) * num); n =0 ;for (i =0 ; i != num; i++) {
@@ -228,7 +230,7 @@ static inline uint32_t pkt_filter_mb_vm(const struct rte_bpf *bpf, struct rte_mbuf *mb[], uint32_t num, uint32_t drop) { - uint64_t rc[num]; + uint64_t *rc = alloca(sizeof(uint64_t) * num);rte_bpf_exec_burst (bpf, (void **)mb, rc, num);return apply_filter (mb, rc, num, drop);
@@ -239,7 +241,7 @@ pkt_filter_mb_jit(const struct rte_bpf_jit *jit, struct rte_mbuf *mb[], uint32_t num, uint32_t drop) { uint32_t i, n; -uint64_t rc[num]; +uint64_t *rc =alloca (sizeof (uint64_t ) * num); n =0 ;for (i =0 ; i != num; i++) {
lib/bpf/meson.build +0 -2
@@ -7,8 +7,6 @@ if is_windows subdir_done() endif -cflags += no_wvla_cflag - if arch_subdir == 'x86' and dpdk_conf.get('RTE_ARCH_32') build = false reason = 'not supported on 32-bit x86'
Signature · 1 line
2.51.0
v2 cover minor bpf improvements
In reply to Stephen HemmingerMinor update to BPF. Add allocation annotation and remove VLA.
v2 - fix build on FreeBsd
Stephen Hemminger (2):
bpf: add allocation annotations to functions
bpf: remove dependency on vla
lib/bpf/bpf_pkt.c | 16 +++++++++------- lib/bpf/meson.build | 2 -- lib/bpf/rte_bpf.h | 11 ++++++++--- 3 files changed, 17 insertions(+), 12 deletions(-)
Signature · 1 line
2.51.0
PATCH v2 1/2 bpf: add allocation annotations to functions
In commit 80da7efbb4c4 ("eal: annotate allocation functions") helper macros were added to provide compiler information to detect misuse of alloc/free combinations. This covered many of the functions in DPDK but missed the case of data allocated by BPF load functions.
--- lib/bpf/rte_bpf.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
lib/bpf/rte_bpf.h +8 -3
@@ -18,6 +18,7 @@#include <rte_common.h> #include <rte_mbuf.h> +#include <rte_malloc.h> #include <bpf_def.h> #ifdef __cplusplus
@@ -128,7 +129,8 @@ rte_bpf_destroy(struct rte_bpf *bpf);* - ENOMEM - can't reserve enough memory */ struct rte_bpf * -rte_bpf_load (const struct rte_bpf_prm *prm); +rte_bpf_load (const struct rte_bpf_prm *prm) + __rte_malloc__rte_dealloc (rte_bpf_destroy,1 );/** * Create a new eBPF execution context and load BPF code from given ELF
@@ -152,7 +154,9 @@ rte_bpf_load(const struct rte_bpf_prm *prm);*/ struct rte_bpf *rte_bpf_elf_load (const struct rte_bpf_prm *prm,const char *fname, -const char *sname); +const char *sname) + __rte_malloc__rte_dealloc (rte_bpf_destroy,1 ); +/** * Execute given BPF bytecode. *
@@ -228,7 +232,8 @@ struct bpf_program;* - ENOTSUP - operation not supported */ struct rte_bpf_prm * -rte_bpf_convert (const struct bpf_program *prog); +rte_bpf_convert (const struct bpf_program *prog) + __rte_malloc __rte_dealloc_free; #ifdef __cplusplus }
Signature · 1 line
2.51.0
RE: [PATCH v2 1/2] bpf: add allocation annotations to functions
Quoted 55 lines
-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Sunday, October 19, 2025 9:40 PM
To: [email protected]
Cc: Stephen Hemminger <[email protected]>; Konstantin Ananyev <[email protected]>
Subject: [PATCH v2 1/2] bpf: add allocation annotations to functionsIn commit
80da7efbb4c4("eal: annotate allocation functions") helper macros were added to provide compiler information to detect misuse of alloc/free combinations. This covered many of the functions in DPDK but missed the case of data allocated by BPF load functions.Signed-off-by Stephen Hemminger <[email protected]>--- lib/bpf/rte_bpf.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)lib/bpf/rte_bpf.h +6
-2@@ -18,6 +18,7 @@#include <rte_common.h> #include <rte_mbuf.h> +#include <rte_malloc.h> #include <bpf_def.h> #ifdef __cplusplus@@ -128,7 +129,8 @@ rte_bpf_destroy(struct rte_bpf *bpf);* - ENOMEM - can't reserve enough memory */ struct rte_bpf * -rte_bpf_load (const struct rte_bpf_prm *prm); +rte_bpf_load (const struct rte_bpf_prm *prm) + __rte_malloc__rte_dealloc (rte_bpf_destroy,1 );/** * Create a new eBPF execution context and load BPF code from given ELF @@ -152,7 +154,9 @@ rte_bpf_load(const struct rte_bpf_prm *prm);*/ struct rte_bpf *rte_bpf_elf_load (const struct rte_bpf_prm *prm,const char *fname, -const char *sname); +const char *sname) + __rte_malloc__rte_dealloc (rte_bpf_destroy,1 ); +/** * Execute given BPF bytecode. *
lib/bpf/rte_bpf.h struct bpf_program;
Quoted 10 lines
lib/bpf/rte_bpf.h +2
-2@@ -228,7 +232,8 @@ struct bpf_program;* - ENOTSUP - operation not supported */ struct rte_bpf_prm * -rte_bpf_convert (const struct bpf_program *prog); +rte_bpf_convert (const struct bpf_program *prog) + __rte_malloc __rte_dealloc_free; #ifdef __cplusplus } --
Quoted 1 line
2.51.0
PATCH v2 2/2 bpf: remove dependency on vla
In reply to Stephen HemmingerThe code for ethdev callbacks was using variable length arrays which is a feature not supported on MSVC and later C standards. Replace with alloca().
--- lib/bpf/bpf_pkt.c | 16 +++++++++------- lib/bpf/meson.build | 2 -- 2 files changed, 9 insertions(+), 9 deletions(-)
lib/bpf/bpf_pkt.c +9 -7
@@ -6,6 +6,7 @@#include <string.h> #include <errno.h> #include <stdint.h> +#include <alloca.h> #include <sys/queue.h>
@@ -163,10 +164,11 @@ apply_filter(struct rte_mbuf *mb[], const uint64_t rc[], uint32_t num, uint32_t drop) {uint32_t i, j, k; -struct rte_mbuf *dr[num]; +struct rte_mbuf **dr; -for (i =0 , j =0 , k =0 ; i != num; i++) { + dr =alloca (sizeof (struct rte_mbuf *) * num); +for (i =0 , j =0 , k =0 ; i != num; i++) {/* filter matches */ if (rc[i] !=0 ) mb[j++] = mb[i];
@@ -193,8 +195,8 @@ pkt_filter_vm(const struct rte_bpf *bpf, struct rte_mbuf *mb[], uint32_t num, uint32_t drop) {uint32_t i; -void *dp[num]; -uint64_t rc[num]; +void **dp =alloca (sizeof (void *) * num); +uint64_t *rc =alloca (sizeof (uint64_t ) * num);for (i =0 ; i != num; i++) dp[i] = rte_pktmbuf_mtod(mb[i], void *);
@@ -209,7 +211,7 @@ pkt_filter_jit(const struct rte_bpf_jit *jit, struct rte_mbuf *mb[], {uint32_t i, n;void *dp; -uint64_t rc[num]; +uint64_t *rc =alloca (sizeof (uint64_t ) * num); n =0 ;for (i =0 ; i != num; i++) {
@@ -228,7 +230,7 @@ static inline uint32_t pkt_filter_mb_vm(const struct rte_bpf *bpf, struct rte_mbuf *mb[], uint32_t num, uint32_t drop) { - uint64_t rc[num]; + uint64_t *rc = alloca(sizeof(uint64_t) * num);rte_bpf_exec_burst (bpf, (void **)mb, rc, num);return apply_filter (mb, rc, num, drop);
@@ -239,7 +241,7 @@ pkt_filter_mb_jit(const struct rte_bpf_jit *jit, struct rte_mbuf *mb[], uint32_t num, uint32_t drop) { uint32_t i, n; -uint64_t rc[num]; +uint64_t *rc =alloca (sizeof (uint64_t ) * num); n =0 ;for (i =0 ; i != num; i++) {
lib/bpf/meson.build +0 -2
@@ -7,8 +7,6 @@ if is_windows subdir_done() endif -cflags += no_wvla_cflag - if arch_subdir == 'x86' and dpdk_conf.get('RTE_ARCH_32') build = false reason = 'not supported on 32-bit x86'
Signature · 1 line
2.51.0
RE: [PATCH v2 2/2] bpf: remove dependency on vla
Quoted 5 lines
The code for ethdev callbacks was using variable length arrays which is a feature not supported on MSVC and later C standards. Replace with
alloca().
That looks exactly the same as first version of the patch. So I can only repeat my comment for previous version:
I am not a big fun of such mechanical replacement of vla with alloca() Specially in that particular case, we can have internal function that uses fixed size array and in public one just call it several times in a loop. Again, using VLA here is probably a real security breach,
as we put some assumptions on size of input arrays provided by user, which we probably shouldn't.
Konstantin
Quoted 90 lines
Signed-off-by Stephen Hemminger <[email protected]>--- lib/bpf/bpf_pkt.c | 16 +++++++++------- lib/bpf/meson.build | 2 -- 2 files changed, 9 insertions(+), 9 deletions(-)lib/bpf/bpf_pkt.c +4
-2@@ -6,6 +6,7 @@#include <string.h> #include <errno.h> #include <stdint.h> +#include <alloca.h> #include <sys/queue.h> @@ -163,10 +164,11 @@ apply_filter(struct rte_mbuf *mb[], const uint64_t rc[],uint32_t num, uint32_t drop) { uint32_t i, j, k; -struct rte_mbuf *dr[num]; +struct rte_mbuf **dr; -for (i =0 , j =0 , k =0 ; i != num; i++) { + dr =alloca (sizeof (struct rte_mbuf *) * num); +for (i =0 , j =0 , k =0 ; i != num; i++) {/* filter matches */ if (rc[i] !=0 )lib/bpf/bpf_pkt.c +2
-2mb[j++] = mb[i];@@ -193,8 +195,8 @@ pkt_filter_vm(const struct rte_bpf *bpf, struct rte_mbuf *mb[], uint32_t num, uint32_t drop) {uint32_t i; -void *dp[num]; -uint64_t rc[num]; +void **dp =alloca (sizeof (void *) * num); +uint64_t *rc =alloca (sizeof (uint64_t ) * num);for (i =0 ; i != num; i++)lib/bpf/bpf_pkt.c +1
-1dp[i] = rte_pktmbuf_mtod(mb[i], void *);@@ -209,7 +211,7 @@ pkt_filter_jit(const struct rte_bpf_jit *jit, struct rte_mbuf *mb[], {uint32_t i, n;void *dp; -uint64_t rc[num]; +uint64_t *rc =alloca (sizeof (uint64_t ) * num); n =0 ;lib/bpf/bpf_pkt.c +1
-1for (i =0 ; i != num; i++) {@@ -228,7 +230,7 @@ static inline uint32_t pkt_filter_mb_vm(const struct rte_bpf *bpf, struct rte_mbuf *mb[], uint32_t num, uint32_t drop) { - uint64_t rc[num]; + uint64_t *rc = alloca(sizeof(uint64_t) * num);rte_bpf_exec_burst (bpf, (void **)mb, rc, num);lib/bpf/bpf_pkt.c +1
-1return apply_filter (mb, rc, num, drop);@@ -239,7 +241,7 @@ pkt_filter_mb_jit(const struct rte_bpf_jit *jit, structrte_mbuf *mb[], uint32_t num, uint32_t drop) { uint32_t i, n; -uint64_t rc[num]; +uint64_t *rc =alloca (sizeof (uint64_t ) * num); n =0 ;lib/bpf/bpf_pkt.c +0
-0for (i =0 ; i != num; i++) {lib/bpf/meson.build +0
-3@@ -7,8 +7,6 @@ if is_windows subdir_done() endif -cflags += no_wvla_cflag - if arch_subdir == 'x86' and dpdk_conf.get('RTE_ARCH_32') build = false reason = 'not supported on 32-bit x86' --2.51.0