bpf: enforce usage of __aligned_u64 in the UAPI header
Subsystems (3)
PATCH 2/2 bpf: enforce usage of __aligned_u64 in the UAPI header
Use __aligned_u64 instead of __u64 everywhere in the UAPI header, similarly to v4.17-rc1~94^2~58^2 "RDMA: Change all uapi headers to use __aligned_u64 instead of __u64".
This commit doesn't change structure layouts, but imposes additional alignment requirements on struct bpf_stack_build_id, struct bpf_sock_ops, struct bpf_perf_event_value, and struct bpf_raw_tracepoint_args; of them only struct bpf_sock_ops and struct bpf_perf_event_value have 64-bit fields that were present in a released kernel without this additional alignment requirement (bytes_received and bytes_acked were added to struct bpf_sock_ops in commit v4.16-rc1~123^2~33^2~5^2~4, struct bpf_perf_event_value was added in commit v4.15-rc1~84^2~532^2~3).
--- include/uapi/linux/bpf.h | 22 +++++++++++----------- tools/include/uapi/linux/bpf.h | 22 +++++++++++----------- 2 files changed, 22 insertions(+), 22 deletions(-)
include/uapi/linux/bpf.h +11 -11
@@ -259,8 +259,8 @@ struct bpf_stack_build_id {__s32 status;unsigned char build_id[BPF_BUILD_ID_SIZE ];union { -__u64 offset; -__u64 ip; +__aligned_u64 offset; +__aligned_u64 ip; }; };
@@ -288,7 +288,7 @@ union bpf_attr {__aligned_u64 value;__aligned_u64 next_key; }; -__u64 flags; +__aligned_u64 flags; };struct {/* anonymous struct used by BPF_PROG_LOAD command */
@@ -360,7 +360,7 @@ union bpf_attr { } query;struct { -__u64 name; +__aligned_u64 name;__u32 prog_fd; } raw_tracepoint; } __attribute__((aligned(8)));
@@ -1011,7 +1011,7 @@ struct bpf_prog_info {__u32 xlated_prog_len;__aligned_u64 jited_prog_insns;__aligned_u64 xlated_prog_insns; -__u64 load_time;/* ns since boottime */ +__aligned_u64 load_time;/* ns since boottime */ __u32 created_by_uid;__u32 nr_map_ids;__aligned_u64 map_ids;
@@ -1101,8 +1101,8 @@ struct bpf_sock_ops {__u32 lost_out;__u32 sacked_out;__u32 sk_txhash; -__u64 bytes_received; -__u64 bytes_acked; +__aligned_u64 bytes_received; +__aligned_u64 bytes_acked; };/* Definitions for bpf_sock_ops_cb_flags */
@@ -1189,9 +1189,9 @@ enum {#define TCP_BPF_SNDCWND_CLAMP 1002/* Set sndcwnd_clamp */ struct bpf_perf_event_value { -__u64 counter; -__u64 enabled; -__u64 running; +__aligned_u64 counter; +__aligned_u64 enabled; +__aligned_u64 running; };#define BPF_DEVCG_ACC_MKNOD (1ULL << 0)
@@ -1209,7 +1209,7 @@ struct bpf_cgroup_dev_ctx { };struct bpf_raw_tracepoint_args { -__u64 args[0 ]; +__aligned_u64 args[0 ]; };#endif /* _UAPI__LINUX_BPF_H__ */
tools/include/uapi/linux/bpf.h +11 -11
@@ -259,8 +259,8 @@ struct bpf_stack_build_id {__s32 status;unsigned char build_id[BPF_BUILD_ID_SIZE ];union { -__u64 offset; -__u64 ip; +__aligned_u64 offset; +__aligned_u64 ip; }; };
@@ -288,7 +288,7 @@ union bpf_attr {__aligned_u64 value;__aligned_u64 next_key; }; -__u64 flags; +__aligned_u64 flags; };struct {/* anonymous struct used by BPF_PROG_LOAD command */
@@ -360,7 +360,7 @@ union bpf_attr { } query;struct { -__u64 name; +__aligned_u64 name;__u32 prog_fd; } raw_tracepoint; } __attribute__((aligned(8)));
@@ -1011,7 +1011,7 @@ struct bpf_prog_info {__u32 xlated_prog_len;__aligned_u64 jited_prog_insns;__aligned_u64 xlated_prog_insns; -__u64 load_time;/* ns since boottime */ +__aligned_u64 load_time;/* ns since boottime */ __u32 created_by_uid;__u32 nr_map_ids;__aligned_u64 map_ids;
@@ -1101,8 +1101,8 @@ struct bpf_sock_ops {__u32 lost_out;__u32 sacked_out;__u32 sk_txhash; -__u64 bytes_received; -__u64 bytes_acked; +__aligned_u64 bytes_received; +__aligned_u64 bytes_acked; };/* Definitions for bpf_sock_ops_cb_flags */
@@ -1189,9 +1189,9 @@ enum {#define TCP_BPF_SNDCWND_CLAMP 1002/* Set sndcwnd_clamp */ struct bpf_perf_event_value { -__u64 counter; -__u64 enabled; -__u64 running; +__aligned_u64 counter; +__aligned_u64 enabled; +__aligned_u64 running; };#define BPF_DEVCG_ACC_MKNOD (1ULL << 0)
@@ -1209,7 +1209,7 @@ struct bpf_cgroup_dev_ctx { };struct bpf_raw_tracepoint_args { -__u64 args[0 ]; +__aligned_u64 args[0 ]; };#endif /* _UAPI__LINUX_BPF_H__ */
Signature · 1 line
2.1.4
Quoted 171 lines · Eugene Syromiatnikov
Use
__aligned_u64instead of__u64everywhere in the UAPI header, similarly to v4.17-rc1~94^2~58^2 "RDMA: Change all uapi headers to use__aligned_u64instead of__u64".This commit doesn't change structure layouts, but imposes additional alignment requirements on struct
bpf_stack_build_id, structbpf_sock_ops, structbpf_perf_event_value, and structbpf_raw_tracepoint_args; of them only structbpf_sock_opsand structbpf_perf_event_valuehave 64-bit fields that were present in a released kernel without this additional alignment requirement (bytes_receivedandbytes_ackedwere added to structbpf_sock_opsin commit v4.16-rc1~123^2~33^2~5^2~4, structbpf_perf_event_valuewas added in commit v4.15-rc1~84^2~532^2~3).Signed-off-by Eugene Syromiatnikov <[email protected]>--- include/uapi/linux/bpf.h | 22 +++++++++++----------- tools/include/uapi/linux/bpf.h | 22 +++++++++++----------- 2 files changed, 22 insertions(+), 22 deletions(-)include/uapi/linux/bpf.h +11
-11@@ -259,8 +259,8 @@ struct bpf_stack_build_id {__s32 status;unsigned char build_id[BPF_BUILD_ID_SIZE ];union { -__u64 offset; -__u64 ip; +__aligned_u64 offset; +__aligned_u64 ip; }; };@@ -288,7 +288,7 @@ union bpf_attr {__aligned_u64 value;__aligned_u64 next_key; }; -__u64 flags; +__aligned_u64 flags; };struct {/* anonymous struct used by BPF_PROG_LOAD command */ @@ -360,7 +360,7 @@ union bpf_attr { } query;struct { -__u64 name; +__aligned_u64 name;__u32 prog_fd; } raw_tracepoint; } __attribute__((aligned(8)));@@ -1011,7 +1011,7 @@ struct bpf_prog_info {__u32 xlated_prog_len;__aligned_u64 jited_prog_insns;__aligned_u64 xlated_prog_insns; -__u64 load_time;/* ns since boottime */ +__aligned_u64 load_time;/* ns since boottime */ __u32 created_by_uid;__u32 nr_map_ids;__aligned_u64 map_ids;@@ -1101,8 +1101,8 @@ struct bpf_sock_ops {__u32 lost_out;__u32 sacked_out;__u32 sk_txhash; -__u64 bytes_received; -__u64 bytes_acked; +__aligned_u64 bytes_received; +__aligned_u64 bytes_acked; };/* Definitions for bpf_sock_ops_cb_flags */ @@ -1189,9 +1189,9 @@ enum {#define TCP_BPF_SNDCWND_CLAMP 1002/* Set sndcwnd_clamp */ struct bpf_perf_event_value { -__u64 counter; -__u64 enabled; -__u64 running; +__aligned_u64 counter; +__aligned_u64 enabled; +__aligned_u64 running; };#define BPF_DEVCG_ACC_MKNOD (1ULL << 0)@@ -1209,7 +1209,7 @@ struct bpf_cgroup_dev_ctx { };struct bpf_raw_tracepoint_args { -__u64 args[0 ]; +__aligned_u64 args[0 ]; };#endif /* _UAPI__LINUX_BPF_H__ */ tools/include/uapi/linux/bpf.h +11
-12@@ -259,8 +259,8 @@ struct bpf_stack_build_id {__s32 status;unsigned char build_id[BPF_BUILD_ID_SIZE ];union { -__u64 offset; -__u64 ip; +__aligned_u64 offset; +__aligned_u64 ip; }; };@@ -288,7 +288,7 @@ union bpf_attr {__aligned_u64 value;__aligned_u64 next_key; }; -__u64 flags; +__aligned_u64 flags; };struct {/* anonymous struct used by BPF_PROG_LOAD command */ @@ -360,7 +360,7 @@ union bpf_attr { } query;struct { -__u64 name; +__aligned_u64 name;__u32 prog_fd; } raw_tracepoint; } __attribute__((aligned(8)));@@ -1011,7 +1011,7 @@ struct bpf_prog_info {__u32 xlated_prog_len;__aligned_u64 jited_prog_insns;__aligned_u64 xlated_prog_insns; -__u64 load_time;/* ns since boottime */ +__aligned_u64 load_time;/* ns since boottime */ __u32 created_by_uid;__u32 nr_map_ids;__aligned_u64 map_ids;@@ -1101,8 +1101,8 @@ struct bpf_sock_ops {__u32 lost_out;__u32 sacked_out;__u32 sk_txhash; -__u64 bytes_received; -__u64 bytes_acked; +__aligned_u64 bytes_received; +__aligned_u64 bytes_acked; };/* Definitions for bpf_sock_ops_cb_flags */ @@ -1189,9 +1189,9 @@ enum {#define TCP_BPF_SNDCWND_CLAMP 1002/* Set sndcwnd_clamp */ struct bpf_perf_event_value { -__u64 counter; -__u64 enabled; -__u64 running; +__aligned_u64 counter; +__aligned_u64 enabled; +__aligned_u64 running; };#define BPF_DEVCG_ACC_MKNOD (1ULL << 0)@@ -1209,7 +1209,7 @@ struct bpf_cgroup_dev_ctx { };struct bpf_raw_tracepoint_args { -__u64 args[0 ]; +__aligned_u64 args[0 ]; };#endif /* _UAPI__LINUX_BPF_H__ */ --
Quoted 2 lines
2.1.4
I think these changes are not necessary. Is it a general guidance to only use 64-bit aligned
variables in UAPI headers?
Thanks,
Song
Quoted 3 lines · Song Liu
I think these changes are not necessary. Is it a general guidance to only use 64-bit aligned
variables in UAPI headers?
Not really, but it allows avoiding most alignment issues like the one mentioned in the previous patch and in the referenced RDMA patch.