linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf v2 0/1] bpf: btf: Fix endianness of bitfields
@ 2018-07-10 21:33 Okash Khawaja
  2018-07-10 21:33 ` [PATCH bpf v2 1/1] bpf: btf: Fix bitfield extraction for big endian Okash Khawaja
  0 siblings, 1 reply; 4+ messages in thread
From: Okash Khawaja @ 2018-07-10 21:33 UTC (permalink / raw)
  To: Daniel Borkmann, Martin KaFai Lau, Alexei Starovoitov,
	Yonghong Song, Jakub Kicinski, David S. Miller
  Cc: netdev, kernel-team, linux-kernel

Hi,

Following are the changes from v1:

- use compile time check for endianness
- factor out right_shift_bits from the check as it is same in both
- change size of bit shift variables to match length of bit offset in
BTF specification

Please note that I have considered non-big endians to be little endians
for our purposes. Let me know if you think that's a problem. We need to
then decide what to do in case neither of macros are defined.

Thanks,
Okash

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH bpf v2 1/1] bpf: btf: Fix bitfield extraction for big endian
  2018-07-10 21:33 [PATCH bpf v2 0/1] bpf: btf: Fix endianness of bitfields Okash Khawaja
@ 2018-07-10 21:33 ` Okash Khawaja
  2018-07-10 23:46   ` Martin KaFai Lau
  0 siblings, 1 reply; 4+ messages in thread
From: Okash Khawaja @ 2018-07-10 21:33 UTC (permalink / raw)
  To: Daniel Borkmann, Martin KaFai Lau, Alexei Starovoitov,
	Yonghong Song, Jakub Kicinski, David S. Miller
  Cc: netdev, kernel-team, linux-kernel

[-- Attachment #1: fix-btf-bitfields.patch --]
[-- Type: text/plain, Size: 2222 bytes --]

When extracting bitfield from a number, btf_int_bits_seq_show() builds
a mask and accesses least significant byte of the number in a way
specific to little-endian. This patch fixes that by checking endianness
of the machine and then shifting left and right the unneeded bits.

Thanks to Martin Lau for the help in navigating potential pitfalls when
dealing with endianess and for the final solution.

Fixes: b00b8daec828 ("bpf: btf: Add pretty print capability for data with BTF type info")
Signed-off-by: Okash Khawaja <osk@fb.com>

---
 kernel/bpf/btf.c |   30 +++++++++++++-----------------
 1 file changed, 13 insertions(+), 17 deletions(-)

--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -991,16 +991,13 @@ static void btf_int_bits_seq_show(const
 				  void *data, u8 bits_offset,
 				  struct seq_file *m)
 {
+	u16 left_shift_bits, right_shift_bits;
 	u32 int_data = btf_type_int(t);
 	u16 nr_bits = BTF_INT_BITS(int_data);
 	u16 total_bits_offset;
 	u16 nr_copy_bytes;
 	u16 nr_copy_bits;
-	u8 nr_upper_bits;
-	union {
-		u64 u64_num;
-		u8  u8_nums[8];
-	} print_num;
+	u64 print_num;
 
 	total_bits_offset = bits_offset + BTF_INT_OFFSET(int_data);
 	data += BITS_ROUNDDOWN_BYTES(total_bits_offset);
@@ -1008,21 +1005,20 @@ static void btf_int_bits_seq_show(const
 	nr_copy_bits = nr_bits + bits_offset;
 	nr_copy_bytes = BITS_ROUNDUP_BYTES(nr_copy_bits);
 
-	print_num.u64_num = 0;
-	memcpy(&print_num.u64_num, data, nr_copy_bytes);
+	print_num = 0;
+	memcpy(&print_num, data, nr_copy_bytes);
 
-	/* Ditch the higher order bits */
-	nr_upper_bits = BITS_PER_BYTE_MASKED(nr_copy_bits);
-	if (nr_upper_bits) {
-		/* We need to mask out some bits of the upper byte. */
-		u8 mask = (1 << nr_upper_bits) - 1;
+#ifdef __BIG_ENDIAN_BITFIELD
+	left_shift_bits = bits_offset;
+#else
+	left_shift_bits = BITS_PER_U64 - nr_copy_bits;
+#endif
+	right_shift_bits = BITS_PER_U64 - nr_bits;
 
-		print_num.u8_nums[nr_copy_bytes - 1] &= mask;
-	}
+	print_num <<= left_shift_bits;
+	print_num >>= right_shift_bits;
 
-	print_num.u64_num >>= bits_offset;
-
-	seq_printf(m, "0x%llx", print_num.u64_num);
+	seq_printf(m, "0x%llx", print_num);
 }
 
 static void btf_int_seq_show(const struct btf *btf, const struct btf_type *t,


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf v2 1/1] bpf: btf: Fix bitfield extraction for big endian
  2018-07-10 21:33 ` [PATCH bpf v2 1/1] bpf: btf: Fix bitfield extraction for big endian Okash Khawaja
@ 2018-07-10 23:46   ` Martin KaFai Lau
  2018-07-11 22:12     ` Daniel Borkmann
  0 siblings, 1 reply; 4+ messages in thread
From: Martin KaFai Lau @ 2018-07-10 23:46 UTC (permalink / raw)
  To: Okash Khawaja
  Cc: Daniel Borkmann, Alexei Starovoitov, Yonghong Song,
	Jakub Kicinski, David S. Miller, netdev, kernel-team,
	linux-kernel

On Tue, Jul 10, 2018 at 02:33:07PM -0700, Okash Khawaja wrote:
> When extracting bitfield from a number, btf_int_bits_seq_show() builds
> a mask and accesses least significant byte of the number in a way
> specific to little-endian. This patch fixes that by checking endianness
> of the machine and then shifting left and right the unneeded bits.
> 
> Thanks to Martin Lau for the help in navigating potential pitfalls when
> dealing with endianess and for the final solution.
> 
> Fixes: b00b8daec828 ("bpf: btf: Add pretty print capability for data with BTF type info")
> Signed-off-by: Okash Khawaja <osk@fb.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf v2 1/1] bpf: btf: Fix bitfield extraction for big endian
  2018-07-10 23:46   ` Martin KaFai Lau
@ 2018-07-11 22:12     ` Daniel Borkmann
  0 siblings, 0 replies; 4+ messages in thread
From: Daniel Borkmann @ 2018-07-11 22:12 UTC (permalink / raw)
  To: Martin KaFai Lau, Okash Khawaja
  Cc: Alexei Starovoitov, Yonghong Song, Jakub Kicinski,
	David S. Miller, netdev, kernel-team, linux-kernel

On 07/11/2018 01:46 AM, Martin KaFai Lau wrote:
> On Tue, Jul 10, 2018 at 02:33:07PM -0700, Okash Khawaja wrote:
>> When extracting bitfield from a number, btf_int_bits_seq_show() builds
>> a mask and accesses least significant byte of the number in a way
>> specific to little-endian. This patch fixes that by checking endianness
>> of the machine and then shifting left and right the unneeded bits.
>>
>> Thanks to Martin Lau for the help in navigating potential pitfalls when
>> dealing with endianess and for the final solution.
>>
>> Fixes: b00b8daec828 ("bpf: btf: Add pretty print capability for data with BTF type info")
>> Signed-off-by: Okash Khawaja <osk@fb.com>
> Acked-by: Martin KaFai Lau <kafai@fb.com>

Applied to bpf, thanks Okash!

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-07-11 22:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-10 21:33 [PATCH bpf v2 0/1] bpf: btf: Fix endianness of bitfields Okash Khawaja
2018-07-10 21:33 ` [PATCH bpf v2 1/1] bpf: btf: Fix bitfield extraction for big endian Okash Khawaja
2018-07-10 23:46   ` Martin KaFai Lau
2018-07-11 22:12     ` Daniel Borkmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).