]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blobdiff - tools/perf/util/cgroup.c
perf cgroup: Fix refcount usage
[mirror_ubuntu-artful-kernel.git] / tools / perf / util / cgroup.c
index 03347748f3fadc43f070fa0b6ef789f071c30d65..0e77bc9e5f3c3b354b023e5d26efd08ead615104 100644 (file)
@@ -98,8 +98,10 @@ static int add_cgroup(struct perf_evlist *evlist, char *str)
                cgrp = counter->cgrp;
                if (!cgrp)
                        continue;
-               if (!strcmp(cgrp->name, str))
+               if (!strcmp(cgrp->name, str)) {
+                       refcount_inc(&cgrp->refcnt);
                        break;
+               }
 
                cgrp = NULL;
        }
@@ -110,6 +112,7 @@ static int add_cgroup(struct perf_evlist *evlist, char *str)
                        return -1;
 
                cgrp->name = str;
+               refcount_set(&cgrp->refcnt, 1);
 
                cgrp->fd = open_cgroup(str);
                if (cgrp->fd == -1) {
@@ -128,12 +131,11 @@ static int add_cgroup(struct perf_evlist *evlist, char *str)
                        goto found;
                n++;
        }
-       if (refcount_read(&cgrp->refcnt) == 0)
+       if (refcount_dec_and_test(&cgrp->refcnt))
                free(cgrp);
 
        return -1;
 found:
-       refcount_inc(&cgrp->refcnt);
        counter->cgrp = cgrp;
        return 0;
 }