Skip to content

Commit 364fed3

Browse files
ereshetovaacmel
authored andcommitted
perf thread_map: Convert thread_map.refcnt from atomic_t to refcount_t
The refcount_t type and corresponding API should be used instead of atomic_t when the variable is used as a reference counter. This allows to avoid accidental refcounter overflows that might lead to use-after-free situations. Signed-off-by: Elena Reshetova <elena.reshetova@intel.com> Signed-off-by: David Windsor <dwindsor@gmail.com> Signed-off-by: Hans Liljestrand <ishkamiel@gmail.com> Signed-off-by: Kees Kook <keescook@chromium.org> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: David Windsor <dwindsor@gmail.com> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Hans Liljestrand <ishkamiel@gmail.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Kees Kook <keescook@chromium.org> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Matija Glavinic Pecotic <matija.glavinic-pecotic.ext@nokia.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: alsa-devel@alsa-project.org Link: http://lkml.kernel.org/r/1487691303-31858-10-git-send-email-elena.reshetova@intel.com [ Did missing tests/thread-map.c conversion ] Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent e34f5b1 commit 364fed3

3 files changed

Lines changed: 15 additions & 15 deletions

File tree

‎tools/perf/tests/thread-map.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ int test__thread_map(int subtest __maybe_unused)
2929
thread_map__comm(map, 0) &&
3030
!strcmp(thread_map__comm(map, 0), NAME));
3131
TEST_ASSERT_VAL("wrong refcnt",
32-
atomic_read(&map->refcnt) == 1);
32+
refcount_read(&map->refcnt) == 1);
3333
thread_map__put(map);
3434

3535
/* test dummy pid */
@@ -44,7 +44,7 @@ int test__thread_map(int subtest __maybe_unused)
4444
thread_map__comm(map, 0) &&
4545
!strcmp(thread_map__comm(map, 0), "dummy"));
4646
TEST_ASSERT_VAL("wrong refcnt",
47-
atomic_read(&map->refcnt) == 1);
47+
refcount_read(&map->refcnt) == 1);
4848
thread_map__put(map);
4949
return 0;
5050
}
@@ -71,7 +71,7 @@ static int process_event(struct perf_tool *tool __maybe_unused,
7171
thread_map__comm(threads, 0) &&
7272
!strcmp(thread_map__comm(threads, 0), NAME));
7373
TEST_ASSERT_VAL("wrong refcnt",
74-
atomic_read(&threads->refcnt) == 1);
74+
refcount_read(&threads->refcnt) == 1);
7575
thread_map__put(threads);
7676
return 0;
7777
}

‎tools/perf/util/thread_map.c‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ struct thread_map *thread_map__new_by_pid(pid_t pid)
6666
for (i = 0; i < items; i++)
6767
thread_map__set_pid(threads, i, atoi(namelist[i]->d_name));
6868
threads->nr = items;
69-
atomic_set(&threads->refcnt, 1);
69+
refcount_set(&threads->refcnt, 1);
7070
}
7171

7272
for (i=0; i<items; i++)
@@ -83,7 +83,7 @@ struct thread_map *thread_map__new_by_tid(pid_t tid)
8383
if (threads != NULL) {
8484
thread_map__set_pid(threads, 0, tid);
8585
threads->nr = 1;
86-
atomic_set(&threads->refcnt, 1);
86+
refcount_set(&threads->refcnt, 1);
8787
}
8888

8989
return threads;
@@ -105,7 +105,7 @@ struct thread_map *thread_map__new_by_uid(uid_t uid)
105105
goto out_free_threads;
106106

107107
threads->nr = 0;
108-
atomic_set(&threads->refcnt, 1);
108+
refcount_set(&threads->refcnt, 1);
109109

110110
while ((dirent = readdir(proc)) != NULL) {
111111
char *end;
@@ -235,7 +235,7 @@ static struct thread_map *thread_map__new_by_pid_str(const char *pid_str)
235235
out:
236236
strlist__delete(slist);
237237
if (threads)
238-
atomic_set(&threads->refcnt, 1);
238+
refcount_set(&threads->refcnt, 1);
239239
return threads;
240240

241241
out_free_namelist:
@@ -255,7 +255,7 @@ struct thread_map *thread_map__new_dummy(void)
255255
if (threads != NULL) {
256256
thread_map__set_pid(threads, 0, -1);
257257
threads->nr = 1;
258-
atomic_set(&threads->refcnt, 1);
258+
refcount_set(&threads->refcnt, 1);
259259
}
260260
return threads;
261261
}
@@ -300,7 +300,7 @@ struct thread_map *thread_map__new_by_tid_str(const char *tid_str)
300300
}
301301
out:
302302
if (threads)
303-
atomic_set(&threads->refcnt, 1);
303+
refcount_set(&threads->refcnt, 1);
304304
return threads;
305305

306306
out_free_threads:
@@ -326,7 +326,7 @@ static void thread_map__delete(struct thread_map *threads)
326326
if (threads) {
327327
int i;
328328

329-
WARN_ONCE(atomic_read(&threads->refcnt) != 0,
329+
WARN_ONCE(refcount_read(&threads->refcnt) != 0,
330330
"thread map refcnt unbalanced\n");
331331
for (i = 0; i < threads->nr; i++)
332332
free(thread_map__comm(threads, i));
@@ -337,13 +337,13 @@ static void thread_map__delete(struct thread_map *threads)
337337
struct thread_map *thread_map__get(struct thread_map *map)
338338
{
339339
if (map)
340-
atomic_inc(&map->refcnt);
340+
refcount_inc(&map->refcnt);
341341
return map;
342342
}
343343

344344
void thread_map__put(struct thread_map *map)
345345
{
346-
if (map && atomic_dec_and_test(&map->refcnt))
346+
if (map && refcount_dec_and_test(&map->refcnt))
347347
thread_map__delete(map);
348348
}
349349

@@ -423,7 +423,7 @@ static void thread_map__copy_event(struct thread_map *threads,
423423
threads->map[i].comm = strndup(event->entries[i].comm, 16);
424424
}
425425

426-
atomic_set(&threads->refcnt, 1);
426+
refcount_set(&threads->refcnt, 1);
427427
}
428428

429429
struct thread_map *thread_map__new_event(struct thread_map_event *event)

‎tools/perf/util/thread_map.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
#include <sys/types.h>
55
#include <stdio.h>
6-
#include <linux/atomic.h>
6+
#include <linux/refcount.h>
77

88
struct thread_map_data {
99
pid_t pid;
1010
char *comm;
1111
};
1212

1313
struct thread_map {
14-
atomic_t refcnt;
14+
refcount_t refcnt;
1515
int nr;
1616
struct thread_map_data map[];
1717
};

0 commit comments

Comments
 (0)