From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754009AbcCUJzO (ORCPT ); Mon, 21 Mar 2016 05:55:14 -0400 Received: from [198.137.202.10] ([198.137.202.10]:45456 "EHLO terminus.zytor.com" rhost-flags-FAIL-FAIL-OK-OK) by vger.kernel.org with ESMTP id S1752002AbcCUJzJ (ORCPT ); Mon, 21 Mar 2016 05:55:09 -0400 Date: Mon, 21 Mar 2016 02:51:03 -0700 From: tip-bot for Peter Zijlstra Message-ID: Cc: vincent.weaver@maine.edu, sasha.levin@oracle.com, acme@redhat.com, bp@alien8.de, acme@kernel.org, luto@amacapital.net, namhyung@kernel.org, eranian@google.com, peterz@infradead.org, tglx@linutronix.de, jolsa@redhat.com, brgerst@gmail.com, hpa@zytor.com, alexander.shishkin@linux.intel.com, dsahern@gmail.com, linux-kernel@vger.kernel.org, mingo@kernel.org, dvlasenk@redhat.com, ryabinin.a.a@gmail.com, torvalds@linux-foundation.org Reply-To: bp@alien8.de, acme@redhat.com, acme@kernel.org, luto@amacapital.net, vincent.weaver@maine.edu, sasha.levin@oracle.com, linux-kernel@vger.kernel.org, mingo@kernel.org, dsahern@gmail.com, hpa@zytor.com, alexander.shishkin@linux.intel.com, torvalds@linux-foundation.org, dvlasenk@redhat.com, ryabinin.a.a@gmail.com, peterz@infradead.org, tglx@linutronix.de, eranian@google.com, namhyung@kernel.org, brgerst@gmail.com, jolsa@redhat.com In-Reply-To: <20160129141751.GA407@worktop> References: <20160129141751.GA407@worktop> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/urgent] perf/core: Fix Undefined behaviour in rb_alloc() Git-Commit-ID: 8184059e93c200757f5c0805dae0f14e880eab5d X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: 8184059e93c200757f5c0805dae0f14e880eab5d Gitweb: http://git.kernel.org/tip/8184059e93c200757f5c0805dae0f14e880eab5d Author: Peter Zijlstra AuthorDate: Fri, 29 Jan 2016 15:17:51 +0100 Committer: Ingo Molnar CommitDate: Mon, 21 Mar 2016 09:08:18 +0100 perf/core: Fix Undefined behaviour in rb_alloc() Sasha reported: [ 3494.030114] UBSAN: Undefined behaviour in kernel/events/ring_buffer.c:685:22 [ 3494.030647] shift exponent -1 is negative Andrey spotted that this is because: It happens if nr_pages = 0: rb->page_order = ilog2(nr_pages); Fix it by making both assignments conditional on nr_pages; since otherwise they should both be 0 anyway, and will be because of the kzalloc() used to allocate the structure. Reported-by: Sasha Levin Reported-by: Andrey Ryabinin Signed-off-by: Peter Zijlstra (Intel) Cc: Alexander Shishkin Cc: Andy Lutomirski Cc: Arnaldo Carvalho de Melo Cc: Arnaldo Carvalho de Melo Cc: Borislav Petkov Cc: Brian Gerst Cc: David Ahern Cc: Denys Vlasenko Cc: H. Peter Anvin Cc: Jiri Olsa Cc: Linus Torvalds Cc: Namhyung Kim Cc: Peter Zijlstra Cc: Stephane Eranian Cc: Thomas Gleixner Cc: Vince Weaver Link: http://lkml.kernel.org/r/20160129141751.GA407@worktop Signed-off-by: Ingo Molnar --- kernel/events/ring_buffer.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c index 1faad2c..c61f0cb 100644 --- a/kernel/events/ring_buffer.c +++ b/kernel/events/ring_buffer.c @@ -746,8 +746,10 @@ struct ring_buffer *rb_alloc(int nr_pages, long watermark, int cpu, int flags) rb->user_page = all_buf; rb->data_pages[0] = all_buf + PAGE_SIZE; - rb->page_order = ilog2(nr_pages); - rb->nr_pages = !!nr_pages; + if (nr_pages) { + rb->nr_pages = 1; + rb->page_order = ilog2(nr_pages); + } ring_buffer_init(rb, watermark, flags);