From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S964770AbcFOLnY (ORCPT ); Wed, 15 Jun 2016 07:43:24 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:9935 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753113AbcFOLnW (ORCPT ); Wed, 15 Jun 2016 07:43:22 -0400 X-IBM-Helo: d23dlp02.au.ibm.com X-IBM-MailFrom: maddy@linux.vnet.ibm.com X-IBM-RcptTo: linux-kernel@vger.kernel.org From: Madhavan Srinivasan To: linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Cc: Madhavan Srinivasan , Arnaldo Carvalho de Melo , Adrian Hunter , Borislav Petkov , David Ahern , George Spelvin , Jiri Olsa , Namhyung Kim , Rasmus Villemoes , Wang Nan , Yury Norov , Michael Ellerman Subject: [PATCH] tools/perf: fix the word selected in find_*_bit Date: Wed, 15 Jun 2016 17:12:53 +0530 X-Mailer: git-send-email 1.9.1 X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16061511-0016-0000-0000-000001A5E1FF X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 16061511-0017-0000-0000-000004D3EC83 Message-Id: <1465990973-31483-1-git-send-email-maddy@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2016-06-15_07:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1604210000 definitions=main-1606150127 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When decoding the perf_regs mask in regs_dump__printf(), we loop through the mask using find_first_bit and find_next_bit functions. And mask is of type "u64". But "u64" is send as a "unsigned long *" to lib functions along with sizeof(). While the exisitng code works fine in most of the case, when using a 32bit perf on a 64bit kernel (Big Endian), we end reading the wrong word. In find_first_bit(), one word at a time (based on BITS_PER_LONG) is loaded and checked for any bit set. In 32bit BE userspace, BITS_PER_LONG turns out to be 32, and for a mask value of "0x00000000000000ff", find_first_bit will return 32, instead of 0. Reason for this is that, value in the word0 is all zeros and value in word1 is 0xff. Ideally, second word in the mask should be loaded and searched. Patch swaps the word to look incase of 32bit BE. Cc: Arnaldo Carvalho de Melo Cc: Adrian Hunter Cc: Borislav Petkov Cc: David Ahern Cc: George Spelvin Cc: Jiri Olsa Cc: Namhyung Kim Cc: Rasmus Villemoes Cc: Wang Nan Cc: Yury Norov Cc: Michael Ellerman Signed-off-by: Madhavan Srinivasan --- tools/lib/find_bit.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tools/lib/find_bit.c b/tools/lib/find_bit.c index 9122a9e80046..996b3e04324f 100644 --- a/tools/lib/find_bit.c +++ b/tools/lib/find_bit.c @@ -37,7 +37,12 @@ static unsigned long _find_next_bit(const unsigned long *addr, if (!nbits || start >= nbits) return nbits; +#if (__BYTE_ORDER == __BIG_ENDIAN) && (BITS_PER_LONG != 64) + tmp = addr[(((nbits - 1)/BITS_PER_LONG) - (start / BITS_PER_LONG))] + ^ invert; +#else tmp = addr[start / BITS_PER_LONG] ^ invert; +#endif /* Handle 1st word. */ tmp &= BITMAP_FIRST_WORD_MASK(start); @@ -48,7 +53,12 @@ static unsigned long _find_next_bit(const unsigned long *addr, if (start >= nbits) return nbits; +#if (__BYTE_ORDER == __BIG_ENDIAN) && (BITS_PER_LONG != 64) + tmp = addr[(((nbits - 1)/BITS_PER_LONG) - (start / BITS_PER_LONG))] + ^ invert; +#else tmp = addr[start / BITS_PER_LONG] ^ invert; +#endif } return min(start + __ffs(tmp), nbits); @@ -75,8 +85,15 @@ unsigned long find_first_bit(const unsigned long *addr, unsigned long size) unsigned long idx; for (idx = 0; idx * BITS_PER_LONG < size; idx++) { +#if (__BYTE_ORDER == __BIG_ENDIAN) && (BITS_PER_LONG != 64) + if (addr[(((size-1)/BITS_PER_LONG) - idx)]) + return min(idx * BITS_PER_LONG + + __ffs(addr[(((size-1)/BITS_PER_LONG) - idx)]), + size); +#else if (addr[idx]) return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size); +#endif } return size; -- 1.9.1