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 <[email protected]>
---
 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

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to