All of lore.kernel.org
 help / color / mirror / Atom feed
From: Milos Vyletel <milos@redhat.com>
To: Peter Zijlstra <a.p.zijlstra@chello.nl>,
	Paul Mackerras <paulus@samba.org>, Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>,
	Namhyung Kim <namhyung@kernel.org>,
	Milos Vyletel <milos@redhat.com>, Jiri Olsa <jolsa@kernel.org>,
	He Kuang <hekuang@huawei.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	linux-kernel@vger.kernel.org (open list:PERFORMANCE EVENT...)
Subject: [PATCH 2/2] perf/tools: put new buildid locks to use
Date: Thu, 14 May 2015 09:28:11 +0200	[thread overview]
Message-ID: <1431588493-1474-3-git-send-email-milos@redhat.com> (raw)
In-Reply-To: <1431588493-1474-1-git-send-email-milos@redhat.com>

Use new read/write locks when accesing buildid directory on places where
we may race if multiple instances are run simultaneously.

Signed-off-by: Milos Vyletel <milos@redhat.com>
---
 tools/perf/builtin-buildid-cache.c | 12 +++++++++++
 tools/perf/util/build-id.c         | 41 ++++++++++++++++++++++++++++++--------
 2 files changed, 45 insertions(+), 8 deletions(-)

diff --git a/tools/perf/builtin-buildid-cache.c b/tools/perf/builtin-buildid-cache.c
index d47a0cd..4cf0a1d 100644
--- a/tools/perf/builtin-buildid-cache.c
+++ b/tools/perf/builtin-buildid-cache.c
@@ -94,8 +94,13 @@ static int build_id_cache__kcore_existing(const char *from_dir, char *to_dir,
 	char to_subdir[PATH_MAX];
 	struct dirent *dent;
 	int ret = -1;
+	int lockfd;
 	DIR *d;
 
+	buildid_dir_read_lock(&lockfd);
+	if (lockfd == -1)
+		return -1;
+
 	d = opendir(to_dir);
 	if (!d)
 		return -1;
@@ -121,6 +126,7 @@ static int build_id_cache__kcore_existing(const char *from_dir, char *to_dir,
 	}
 
 	closedir(d);
+	buildid_dir_read_unlock(lockfd);
 
 	return ret;
 }
@@ -130,6 +136,7 @@ static int build_id_cache__add_kcore(const char *filename, bool force)
 	char dir[32], sbuildid[BUILD_ID_SIZE * 2 + 1];
 	char from_dir[PATH_MAX], to_dir[PATH_MAX];
 	char *p;
+	int lockfd;
 
 	strlcpy(from_dir, filename, sizeof(from_dir));
 
@@ -156,6 +163,10 @@ static int build_id_cache__add_kcore(const char *filename, bool force)
 	scnprintf(to_dir, sizeof(to_dir), "%s/[kernel.kcore]/%s/%s",
 		  buildid_dir, sbuildid, dir);
 
+	buildid_dir_write_lock(&lockfd);
+	if (lockfd == -1)
+		return -1;
+
 	if (mkdir_p(to_dir, 0755))
 		return -1;
 
@@ -176,6 +187,7 @@ static int build_id_cache__add_kcore(const char *filename, bool force)
 		}
 		return -1;
 	}
+	buildid_dir_write_unlock(lockfd);
 
 	pr_debug("kcore added to build-id cache directory %s\n", to_dir);
 
diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c
index 46c41e1..13d23aa 100644
--- a/tools/perf/util/build-id.c
+++ b/tools/perf/util/build-id.c
@@ -17,6 +17,7 @@
 #include "tool.h"
 #include "header.h"
 #include "vdso.h"
+#include <sys/file.h>
 
 
 static bool no_buildid_cache;
@@ -308,6 +309,7 @@ int build_id_cache__list_build_ids(const char *pathname,
 	DIR *dir;
 	struct dirent *d;
 	int ret = 0;
+	int lockfd = -1;
 
 	list = strlist__new(true, NULL);
 	dir_name = build_id_cache__dirname_from_path(pathname, false, false);
@@ -316,11 +318,17 @@ int build_id_cache__list_build_ids(const char *pathname,
 		goto out;
 	}
 
+	buildid_dir_read_lock(&lockfd);
+	if (lockfd == -1) {
+		ret = -errno;
+		goto out;
+	}
+
 	/* List up all dirents */
 	dir = opendir(dir_name);
 	if (!dir) {
 		ret = -errno;
-		goto out;
+		goto out_unlock;
 	}
 
 	while ((d = readdir(dir)) != NULL) {
@@ -330,6 +338,8 @@ int build_id_cache__list_build_ids(const char *pathname,
 	}
 	closedir(dir);
 
+out_unlock:
+	buildid_dir_read_unlock(lockfd);
 out:
 	free(dir_name);
 	if (ret)
@@ -347,6 +357,7 @@ int build_id_cache__add_s(const char *sbuild_id, const char *name,
 	char *realname = NULL, *filename = NULL, *dir_name = NULL,
 	     *linkname = zalloc(size), *targetname, *tmp;
 	int err = -1;
+	int lockfd;
 
 	if (!is_kallsyms) {
 		realname = realpath(name, NULL);
@@ -358,30 +369,34 @@ int build_id_cache__add_s(const char *sbuild_id, const char *name,
 	if (!dir_name)
 		goto out_free;
 
-	if (mkdir_p(dir_name, 0755))
+	buildid_dir_write_lock(&lockfd);
+	if (lockfd == -1)
 		goto out_free;
 
+	if (mkdir_p(dir_name, 0755))
+		goto out_unlock;
+
 	if (asprintf(&filename, "%s/%s", dir_name, sbuild_id) < 0) {
 		filename = NULL;
-		goto out_free;
+		goto out_unlock;
 	}
 
 	if (access(filename, F_OK)) {
 		if (is_kallsyms) {
 			 if (copyfile("/proc/kallsyms", filename))
-				goto out_free;
+				goto out_unlock;
 		} else if (link(realname, filename) && errno != EEXIST &&
 				copyfile(name, filename))
-			goto out_free;
+			goto out_unlock;
 	}
 
 	if (!build_id__filename(sbuild_id, linkname, size))
-		goto out_free;
+		goto out_unlock;
 	tmp = strrchr(linkname, '/');
 	*tmp = '\0';
 
 	if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
-		goto out_free;
+		goto out_unlock;
 
 	*tmp = '/';
 	targetname = filename + strlen(buildid_dir) - 5;
@@ -389,6 +404,8 @@ int build_id_cache__add_s(const char *sbuild_id, const char *name,
 
 	if (symlink(targetname, linkname) == 0)
 		err = 0;
+out_unlock:
+	buildid_dir_write_unlock(lockfd);
 out_free:
 	if (!is_kallsyms)
 		free(realname);
@@ -427,6 +444,7 @@ int build_id_cache__remove_s(const char *sbuild_id)
 	char *filename = zalloc(size),
 	     *linkname = zalloc(size), *tmp;
 	int err = -1;
+	int lockfd = -1;
 
 	if (filename == NULL || linkname == NULL)
 		goto out_free;
@@ -449,10 +467,17 @@ int build_id_cache__remove_s(const char *sbuild_id)
 	tmp = strrchr(linkname, '/') + 1;
 	snprintf(tmp, size - (tmp - linkname), "%s", filename);
 
-	if (unlink(linkname))
+	buildid_dir_write_lock(&lockfd);
+	if (lockfd == -1)
 		goto out_free;
 
+	if (unlink(linkname))
+		goto out_unlock;
+
+
 	err = 0;
+out_unlock:
+	buildid_dir_write_unlock(lockfd);
 out_free:
 	free(filename);
 	free(linkname);
-- 
2.4.0


  parent reply	other threads:[~2015-05-14  7:29 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-14  7:28 [PATCH 0/2] Add perf debug dir locking Milos Vyletel
2015-05-14  7:28 ` [PATCH 1/2] perf/tools: add read/write buildid dir locks Milos Vyletel
2015-05-14  7:28 ` Milos Vyletel [this message]
2015-05-14 10:40   ` [PATCH 2/2] perf/tools: put new buildid locks to use Ingo Molnar
2015-05-14 11:38     ` Jiri Olsa
2015-05-14 15:44       ` Milos Vyletel
2015-05-14 17:38         ` Ingo Molnar
2015-05-14 17:38       ` Ingo Molnar
2015-05-20  7:27         ` Milos Vyletel
2015-06-03 11:13         ` Milos Vyletel
2015-06-03 11:21           ` Ingo Molnar
2015-06-03 11:27             ` Milos Vyletel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1431588493-1474-3-git-send-email-milos@redhat.com \
    --to=milos@redhat.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=hekuang@huawei.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=masami.hiramatsu.pt@hitachi.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=paulus@samba.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.