Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp839802imm; Mon, 9 Jul 2018 11:34:40 -0700 (PDT) X-Google-Smtp-Source: AAOMgpeIXw91iblx5pMJ2OuiKWqrzF37CwueOqYvOtjAwuRYw76k+JV/VPH2rRUTCgznqmQNFCr9 X-Received: by 2002:a62:850d:: with SMTP id u13-v6mr22597985pfd.131.1531161280111; Mon, 09 Jul 2018 11:34:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531161280; cv=none; d=google.com; s=arc-20160816; b=c7kAn874xa4YOFSpOILMWNRn4jkQ5kptQR8e72/l+Uj7OKW/Iul8gkp5wAkBfPcu2G wCp0JEY2K9b8C1Q6S4uVa+23aoGW9X0DvoO9RpbQDtYOieHxg8XQsX3rKVxxAKM7thlf V9wC8bxI8jmlXiO3ozWpbf570NwqeLdKmC3jInciLyooXtSrdwymGMx7B6aSvdb9I5ow wl46Ijz4x/Y2cNOL33RQSfwhKBnZuXMXqrD7Kth2J0aBnMQ7Ypvc1eS29FiSvi9738KW 870YHUh3Yt/nqyBwuJl0GqJouXi2rdBwMeOeFJ06euvVry6LgARLiEdsZKdxp6y0BYG4 ngQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=fNNOgFojnedJE7GGD+xQ7gJ/zbfWrMS82aNKGJa6ZgM=; b=qTGz2xX5ood+TYsBNwUeF0B6XokIlTZ6fvOtorWYlhvZMkg0mtZucwHZ+TrENS+n1g M/J9QokCGLMSGI0Ts08zvs+aatdyq/yvCWN+f21rIm2esa+GbpFJWpDrtegrHh76jYxb 5fHnB0wPI3eoCUVO1UqcS5RxyZIhNCVz7wNbo8CkH/GTMk5HX3jGMkBOUoPJtK7ARczG FzVLhgUA2UMUBkVhwhpyiZIxFx4NMHAs4Bdu3m3O5iQggxqB1TKWG7dA6lfZxU6wzaLf V4HuwHQWNv1IB1n1aS1Dqzkaownp1y1bWck+Qzbx7uc8tAz9Nhy66dqZ9csgmkBPJUJo PB7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=nFXWv2gc; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a1-v6si3564991pgq.387.2018.07.09.11.34.25; Mon, 09 Jul 2018 11:34:40 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=nFXWv2gc; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933538AbeGISdR (ORCPT + 99 others); Mon, 9 Jul 2018 14:33:17 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:42500 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S933315AbeGISdO (ORCPT ); Mon, 9 Jul 2018 14:33:14 -0400 Received: from pps.filterd (m0089730.ppops.net [127.0.0.1]) by m0089730.ppops.net (8.16.0.22/8.16.0.22) with SMTP id w69ITMa3000420; Mon, 9 Jul 2018 11:32:50 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=date : from : to : cc : subject : message-id : references : mime-version : content-type : in-reply-to; s=facebook; bh=fNNOgFojnedJE7GGD+xQ7gJ/zbfWrMS82aNKGJa6ZgM=; b=nFXWv2gcKnMbh06RLbyiX4tMqMFbJCA1dxOJ+u4VAwCOqddpMeBBSvNSbIumPo86puYT 3/ElKSTvcVLh3z8OUu0MeUX0MOvIuM/VEKY/Mk/HNNJXn8L4bNz/6dwHjAWTgY+9RFsp Pp19NIpgEI4WQea9tfuG6QKKRIX9MnuZMM8= Received: from mail.thefacebook.com ([199.201.64.23]) by m0089730.ppops.net with ESMTP id 2k4a5agq7s-1 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Mon, 09 Jul 2018 11:32:50 -0700 Received: from kafai-mbp.dhcp.thefacebook.com (192.168.52.123) by mail.thefacebook.com (192.168.16.22) with Microsoft SMTP Server (TLS) id 14.3.319.2; Mon, 9 Jul 2018 11:32:40 -0700 Date: Mon, 9 Jul 2018 11:32:36 -0700 From: Martin KaFai Lau To: Okash Khawaja CC: Daniel Borkmann , Alexei Starovoitov , Yonghong Song , Jakub Kicinski , "David S. Miller" , , , Subject: Re: [PATCH bpf 1/1] bpf: btf: Fix bitfield extraction for big endian Message-ID: <20180709183236.r4b7gzmev5h4lcbw@kafai-mbp.dhcp.thefacebook.com> References: <20180709002202.019053555@fb.com> <20180709004002.440153594@fb.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline In-Reply-To: <20180709004002.440153594@fb.com> User-Agent: NeoMutt/20180512 X-Originating-IP: [192.168.52.123] X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-07-09_07:,, signatures=0 X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Jul 08, 2018 at 05:22:03PM -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 > > --- > kernel/bpf/btf.c | 32 +++++++++++++++----------------- > 1 file changed, 15 insertions(+), 17 deletions(-) > > --- a/kernel/bpf/btf.c > +++ b/kernel/bpf/btf.c > @@ -162,6 +162,8 @@ > #define BITS_ROUNDDOWN_BYTES(bits) ((bits) >> 3) > #define BITS_ROUNDUP_BYTES(bits) \ > (BITS_ROUNDDOWN_BYTES(bits) + !!BITS_PER_BYTE_MASKED(bits)) > +const int one = 1; > +#define is_big_endian() ((*(char *)&one) == 0) > > #define BTF_INFO_MASK 0x0f00ffff > #define BTF_INT_MASK 0x0fffffff > @@ -991,16 +993,13 @@ static void btf_int_bits_seq_show(const > void *data, u8 bits_offset, > struct seq_file *m) > { > + u8 left_shift_bits, right_shift_bits; Nit. Although only max 64 bit int is allowed now (ensured by btf_int_check_meta), it is better to use u16 such that it will be consistent to BTF_INT_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 +1007,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); > - > - /* 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; > - > - print_num.u8_nums[nr_copy_bytes - 1] &= mask; > + print_num = 0; > + memcpy(&print_num, data, nr_copy_bytes); > + if (is_big_endian()) { > + left_shift_bits = bits_offset; > + right_shift_bits = BITS_PER_U64 - nr_bits; > + } else { > + left_shift_bits = BITS_PER_U64 - nr_copy_bits; > + right_shift_bits = BITS_PER_U64 - nr_bits; Nit. right_shift_bits is the same for both cases. Lets simplify it. > } > > - print_num.u64_num >>= bits_offset; > + print_num <<= left_shift_bits; > + print_num >>= right_shift_bits; > > - 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, >