From patchwork Tue Nov 15 04:05:44 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 82253 Delivered-To: patch@linaro.org Received: by 10.140.97.165 with SMTP id m34csp1342644qge; Mon, 14 Nov 2016 20:16:09 -0800 (PST) X-Received: by 10.36.192.213 with SMTP id u204mr1345612itf.54.1479183369695; Mon, 14 Nov 2016 20:16:09 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u78si876915ita.9.2016.11.14.20.16.09; Mon, 14 Nov 2016 20:16:09 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S966598AbcKOEQF (ORCPT + 26 others); Mon, 14 Nov 2016 23:16:05 -0500 Received: from szxga02-in.huawei.com ([119.145.14.65]:54623 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755267AbcKOEIF (ORCPT ); Mon, 14 Nov 2016 23:08:05 -0500 Received: from 172.24.1.47 (EHLO SZXEML429-HUB.china.huawei.com) ([172.24.1.47]) by szxrg02-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id DQQ94741; Tue, 15 Nov 2016 12:07:59 +0800 (CST) Received: from linux-4hy3.site (10.107.193.248) by SZXEML429-HUB.china.huawei.com (10.82.67.184) with Microsoft SMTP Server id 14.3.235.1; Tue, 15 Nov 2016 12:07:50 +0800 From: Wang Nan To: , CC: , , , , Wang Nan Subject: [PATCH 01/34] perf tools: Fix kernel version error in ubuntu Date: Tue, 15 Nov 2016 04:05:44 +0000 Message-ID: <20161115040617.69788-2-wangnan0@huawei.com> X-Mailer: git-send-email 2.10.1 In-Reply-To: <20161115040617.69788-1-wangnan0@huawei.com> References: <20161115040617.69788-1-wangnan0@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.107.193.248] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On ubuntu the internal kernel version code is different from what can be retrived from uname: $ uname -r 4.4.0-47-generic $ cat /lib/modules/`uname -r`/build/include/generated/uapi/linux/version.h #define LINUX_VERSION_CODE 263192 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c)) $ cat /lib/modules/`uname -r`/build/include/generated/utsrelease.h #define UTS_RELEASE "4.4.0-47-generic" #define UTS_UBUNTU_RELEASE_ABI 47 $ cat /proc/version_signature Ubuntu 4.4.0-47.68-generic 4.4.24 The macro LINUX_VERSION_CODE is set to 4.4.24 (263192 == 0x40418), but `uname -r` reports 4.4.0. This mismatch causes LINUX_VERSION_CODE macro passed to BPF script become an incorrect value, results in magic failure in BPF loading: $ sudo ./buildperf/perf record -e ./tools/perf/tests/bpf-script-example.c ls event syntax error: './tools/perf/tests/bpf-script-example.c' \___ Failed to load program for unknown reason According to Ubuntu document (https://wiki.ubuntu.com/Kernel/FAQ), the correct kernel version can be retrived through /proc/version_signature, which is ubuntu specific. This patch checks the existance of /proc/version_signature, and returns version number through parsing this file instead of uname. Version string is untouched (value returns from uname) because `uname -r` is required to be consistence with path of kbuild directory in /lib/module. Signed-off-by: Wang Nan Cc: Arnaldo Carvalho de Melo --- tools/perf/util/util.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) -- 2.10.1 diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index 5bbd1f6..67ac765 100644 --- a/tools/perf/util/util.c +++ b/tools/perf/util/util.c @@ -637,12 +637,63 @@ bool find_process(const char *name) return ret ? false : true; } +static int +fetch_ubuntu_kernel_version(unsigned int *puint) +{ + ssize_t len; + size_t line_len = 0; + char *ptr, *line = NULL; + int version, patchlevel, sublevel, err; + FILE *vsig = fopen("/proc/version_signature", "r"); + + if (!vsig) { + pr_debug("Open /proc/version_signature failed: %s\n", + strerror(errno)); + return -1; + } + + len = getline(&line, &line_len, vsig); + fclose(vsig); + err = -1; + if (len <= 0) { + pr_debug("Reading from /proc/version_signature failed: %s\n", + strerror(errno)); + goto errout; + } + + ptr = strrchr(line, ' '); + if (!ptr) { + pr_debug("Parsing /proc/version_signature failed: %s\n", line); + goto errout; + } + + err = sscanf(ptr + 1, "%d.%d.%d", + &version, &patchlevel, &sublevel); + if (err != 3) { + pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n", + line); + goto errout; + } + + if (puint) + *puint = (version << 16) + (patchlevel << 8) + sublevel; + err = 0; +errout: + free(line); + return err; +} + int fetch_kernel_version(unsigned int *puint, char *str, size_t str_size) { struct utsname utsname; int version, patchlevel, sublevel, err; + bool int_ver_ready = false; + + if (access("/proc/version_signature", R_OK) == 0) + if (!fetch_ubuntu_kernel_version(puint)) + int_ver_ready = true; if (uname(&utsname)) return -1; @@ -656,12 +707,12 @@ fetch_kernel_version(unsigned int *puint, char *str, &version, &patchlevel, &sublevel); if (err != 3) { - pr_debug("Unablt to get kernel version from uname '%s'\n", + pr_debug("Unable to get kernel version from uname '%s'\n", utsname.release); return -1; } - if (puint) + if (puint && !int_ver_ready) *puint = (version << 16) + (patchlevel << 8) + sublevel; return 0; }