blob: 7798a2cc8a867420a468ffc530b444a1fd26f1fe [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Frederic Weisbecker1902efe2013-09-11 16:56:44 +02002#include "comm.h"
3#include "util.h"
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03004#include <errno.h>
Frederic Weisbecker1902efe2013-09-11 16:56:44 +02005#include <stdlib.h>
6#include <stdio.h>
Arnaldo Carvalho de Melo72f7c4d2017-04-19 19:06:30 -03007#include <string.h>
Elena Reshetova6df74bc2017-02-21 17:34:57 +02008#include <linux/refcount.h>
Kan Liangf988e712017-09-29 07:47:53 -07009#include "rwsem.h"
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020010
11struct comm_str {
12 char *str;
13 struct rb_node rb_node;
Elena Reshetova6df74bc2017-02-21 17:34:57 +020014 refcount_t refcnt;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020015};
16
17/* Should perhaps be moved to struct machine */
18static struct rb_root comm_str_root;
Kan Liangf988e712017-09-29 07:47:53 -070019static struct rw_semaphore comm_str_lock = {.lock = PTHREAD_RWLOCK_INITIALIZER,};
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020020
Arnaldo Carvalho de Melo86c19522015-05-19 19:07:42 -030021static struct comm_str *comm_str__get(struct comm_str *cs)
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020022{
Arnaldo Carvalho de Melo86c19522015-05-19 19:07:42 -030023 if (cs)
Elena Reshetova6df74bc2017-02-21 17:34:57 +020024 refcount_inc(&cs->refcnt);
Arnaldo Carvalho de Melo86c19522015-05-19 19:07:42 -030025 return cs;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020026}
27
28static void comm_str__put(struct comm_str *cs)
29{
Elena Reshetova6df74bc2017-02-21 17:34:57 +020030 if (cs && refcount_dec_and_test(&cs->refcnt)) {
Kan Liangf988e712017-09-29 07:47:53 -070031 down_write(&comm_str_lock);
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020032 rb_erase(&cs->rb_node, &comm_str_root);
Kan Liangf988e712017-09-29 07:47:53 -070033 up_write(&comm_str_lock);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -030034 zfree(&cs->str);
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020035 free(cs);
36 }
37}
38
39static struct comm_str *comm_str__alloc(const char *str)
40{
41 struct comm_str *cs;
42
43 cs = zalloc(sizeof(*cs));
44 if (!cs)
45 return NULL;
46
47 cs->str = strdup(str);
48 if (!cs->str) {
49 free(cs);
50 return NULL;
51 }
52
Elena Reshetova6df74bc2017-02-21 17:34:57 +020053 refcount_set(&cs->refcnt, 1);
Arnaldo Carvalho de Melo86c19522015-05-19 19:07:42 -030054
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020055 return cs;
56}
57
Kan Liangf988e712017-09-29 07:47:53 -070058static
59struct comm_str *__comm_str__findnew(const char *str, struct rb_root *root)
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020060{
61 struct rb_node **p = &root->rb_node;
62 struct rb_node *parent = NULL;
63 struct comm_str *iter, *new;
64 int cmp;
65
66 while (*p != NULL) {
67 parent = *p;
68 iter = rb_entry(parent, struct comm_str, rb_node);
69
70 cmp = strcmp(str, iter->str);
71 if (!cmp)
Elena Reshetova6df74bc2017-02-21 17:34:57 +020072 return comm_str__get(iter);
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020073
74 if (cmp < 0)
75 p = &(*p)->rb_left;
76 else
77 p = &(*p)->rb_right;
78 }
79
80 new = comm_str__alloc(str);
81 if (!new)
82 return NULL;
83
84 rb_link_node(&new->rb_node, parent, p);
85 rb_insert_color(&new->rb_node, root);
86
87 return new;
88}
89
Kan Liangf988e712017-09-29 07:47:53 -070090static struct comm_str *comm_str__findnew(const char *str, struct rb_root *root)
91{
92 struct comm_str *cs;
93
94 down_write(&comm_str_lock);
95 cs = __comm_str__findnew(str, root);
96 up_write(&comm_str_lock);
97
98 return cs;
99}
100
Adrian Hunter65de51f2014-07-31 09:00:44 +0300101struct comm *comm__new(const char *str, u64 timestamp, bool exec)
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200102{
103 struct comm *comm = zalloc(sizeof(*comm));
104
105 if (!comm)
106 return NULL;
107
108 comm->start = timestamp;
Adrian Hunter65de51f2014-07-31 09:00:44 +0300109 comm->exec = exec;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200110
111 comm->comm_str = comm_str__findnew(str, &comm_str_root);
112 if (!comm->comm_str) {
113 free(comm);
114 return NULL;
115 }
116
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200117 return comm;
118}
119
Adrian Hunter65de51f2014-07-31 09:00:44 +0300120int comm__override(struct comm *comm, const char *str, u64 timestamp, bool exec)
Namhyung Kim4dfced32013-09-13 16:28:57 +0900121{
Frederic Weisbecker3178f582014-01-14 16:37:14 +0100122 struct comm_str *new, *old = comm->comm_str;
Namhyung Kim4dfced32013-09-13 16:28:57 +0900123
Frederic Weisbecker3178f582014-01-14 16:37:14 +0100124 new = comm_str__findnew(str, &comm_str_root);
125 if (!new)
126 return -ENOMEM;
Namhyung Kim4dfced32013-09-13 16:28:57 +0900127
Namhyung Kim4dfced32013-09-13 16:28:57 +0900128 comm_str__put(old);
Frederic Weisbecker3178f582014-01-14 16:37:14 +0100129 comm->comm_str = new;
130 comm->start = timestamp;
Adrian Hunter65de51f2014-07-31 09:00:44 +0300131 if (exec)
132 comm->exec = true;
Frederic Weisbecker3178f582014-01-14 16:37:14 +0100133
134 return 0;
Namhyung Kim4dfced32013-09-13 16:28:57 +0900135}
136
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200137void comm__free(struct comm *comm)
138{
139 comm_str__put(comm->comm_str);
140 free(comm);
141}
142
143const char *comm__str(const struct comm *comm)
144{
145 return comm->comm_str->str;
146}