diff mbox series

[bpf-next,04/13] bpf: Support bitfield read access in btf_struct_access

Message ID 20191214004746.1652586-1-kafai@fb.com
State Changes Requested
Delegated to: BPF Maintainers
Headers show
Series Introduce BPF STRUCT_OPS | expand

Commit Message

Martin KaFai Lau Dec. 14, 2019, 12:47 a.m. UTC
This patch allows bitfield access as a scalar.  It currently limits
the access to sizeof(u64) and upto the end of the struct.  It is needed
in a later bpf-tcp-cc example that reads bitfield from
inet_connection_sock and tcp_sock.

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
---
 kernel/bpf/btf.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

Comments

Yonghong Song Dec. 16, 2019, 10:05 p.m. UTC | #1
On 12/13/19 4:47 PM, Martin KaFai Lau wrote:
> This patch allows bitfield access as a scalar.  It currently limits
> the access to sizeof(u64) and upto the end of the struct.  It is needed
> in a later bpf-tcp-cc example that reads bitfield from
> inet_connection_sock and tcp_sock.
> 
> Signed-off-by: Martin KaFai Lau <kafai@fb.com>
> ---
>   kernel/bpf/btf.c | 13 +++++++++----
>   1 file changed, 9 insertions(+), 4 deletions(-)
> 
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index 6e652643849b..011194831499 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -3744,10 +3744,6 @@ int btf_struct_access(struct bpf_verifier_log *log,
>   	}
>   
>   	for_each_member(i, t, member) {
> -		if (btf_member_bitfield_size(t, member))
> -			/* bitfields are not supported yet */
> -			continue;
> -
>   		/* offset of the field in bytes */
>   		moff = btf_member_bit_offset(t, member) / 8;
>   		if (off + size <= moff)
> @@ -3757,6 +3753,15 @@ int btf_struct_access(struct bpf_verifier_log *log,
>   		if (off < moff)
>   			continue;
>   
> +		if (btf_member_bitfield_size(t, member)) {
> +			if (off == moff &&
> +			    !(btf_member_bit_offset(t, member) % 8) &&

This check '!(btf_member_bit_offset(t, member) % 8)' is not needed.

> +			    size <= sizeof(u64) &&

This one is not needed since verifier gets 'size' from load/store 
instructions which is guaranteed to be <= sizeof(u64).

> +			    off + size <= t->size)
> +				return SCALAR_VALUE;
> +			continue;
> +		}
> +
>   		/* type of the field */
>   		mtype = btf_type_by_id(btf_vmlinux, member->type);
>   		mname = __btf_name_by_offset(btf_vmlinux, member->name_off);
>
diff mbox series

Patch

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 6e652643849b..011194831499 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -3744,10 +3744,6 @@  int btf_struct_access(struct bpf_verifier_log *log,
 	}
 
 	for_each_member(i, t, member) {
-		if (btf_member_bitfield_size(t, member))
-			/* bitfields are not supported yet */
-			continue;
-
 		/* offset of the field in bytes */
 		moff = btf_member_bit_offset(t, member) / 8;
 		if (off + size <= moff)
@@ -3757,6 +3753,15 @@  int btf_struct_access(struct bpf_verifier_log *log,
 		if (off < moff)
 			continue;
 
+		if (btf_member_bitfield_size(t, member)) {
+			if (off == moff &&
+			    !(btf_member_bit_offset(t, member) % 8) &&
+			    size <= sizeof(u64) &&
+			    off + size <= t->size)
+				return SCALAR_VALUE;
+			continue;
+		}
+
 		/* type of the field */
 		mtype = btf_type_by_id(btf_vmlinux, member->type);
 		mname = __btf_name_by_offset(btf_vmlinux, member->name_off);