From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752618AbbCVKOF (ORCPT ); Sun, 22 Mar 2015 06:14:05 -0400 Received: from terminus.zytor.com ([198.137.202.10]:57222 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752597AbbCVKOC (ORCPT ); Sun, 22 Mar 2015 06:14:02 -0400 Date: Sun, 22 Mar 2015 03:13:46 -0700 From: tip-bot for Milos Vyletel Message-ID: Cc: mingo@kernel.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, acme@redhat.com, hpa@zytor.com, milos@redhat.com, paulus@samba.org, tglx@linutronix.de, eranian@google.com, namhyung@kernel.org, a.p.zijlstra@chello.nl Reply-To: milos@redhat.com, hpa@zytor.com, acme@redhat.com, linux-kernel@vger.kernel.org, jolsa@kernel.org, mingo@kernel.org, eranian@google.com, a.p.zijlstra@chello.nl, namhyung@kernel.org, tglx@linutronix.de, paulus@samba.org In-Reply-To: <1426847846-11112-1-git-send-email-milos@redhat.com> References: <1426847846-11112-1-git-send-email-milos@redhat.com> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/core] perf tools: Fix race in build_id_cache__add_s() Git-Commit-ID: 0635b0f71424be7706793ac260d063491a2889a0 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: 0635b0f71424be7706793ac260d063491a2889a0 Gitweb: http://git.kernel.org/tip/0635b0f71424be7706793ac260d063491a2889a0 Author: Milos Vyletel AuthorDate: Fri, 20 Mar 2015 11:37:25 +0100 Committer: Arnaldo Carvalho de Melo CommitDate: Fri, 20 Mar 2015 17:49:50 -0300 perf tools: Fix race in build_id_cache__add_s() int build_id_cache__add_s(const char *sbuild_id, const char *debugdir, const char *name, bool is_kallsyms, bool is_vdso) { ... if (access(filename, F_OK)) { ^--------------------------------------------------------- [1] if (is_kallsyms) { if (copyfile("/proc/kallsyms", filename)) goto out_free; } else if (link(realname, filename) && copyfile(name, filename)) ^-----------------------------^------------- [2] \------------ [3] goto out_free; } ... When multiple instances of perf record get to [1] at more or less same time and run access() one or more may get failure because the file does not exist yet (since the first instance did not have chance to link it yet). At this point the race moves to link() at [2] where first thread to get there links file and goes on but second one gets -EEXIST so it runs copyfile [3] which truncates the file. reproducer: rm -rf /root/.debug for cpu in $(awk '/processor/ {print $3}' /proc/cpuinfo); do perf record -a -v -T -F 1000 -C $cpu \ -o perf-${cpu}.data sleep 5 2> /dev/null & done wait and simply search for empty files by: find /lib/modules/`uname -r`/kernel/* -size 0 Signed-off-by: Milos Vyletel Acked-by: Jiri Olsa Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Stephane Eranian Link: http://lkml.kernel.org/r/1426847846-11112-1-git-send-email-milos@redhat.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/build-id.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c index a196746..f7fb258 100644 --- a/tools/perf/util/build-id.c +++ b/tools/perf/util/build-id.c @@ -374,7 +374,8 @@ int build_id_cache__add_s(const char *sbuild_id, const char *name, if (is_kallsyms) { if (copyfile("/proc/kallsyms", filename)) goto out_free; - } else if (link(realname, filename) && copyfile(name, filename)) + } else if (link(realname, filename) && errno != EEXIST && + copyfile(name, filename)) goto out_free; }