Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 2 | #include "comm.h" |
| 3 | #include "util.h" |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 4 | #include <errno.h> |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 5 | #include <stdlib.h> |
| 6 | #include <stdio.h> |
Arnaldo Carvalho de Melo | 72f7c4d | 2017-04-19 19:06:30 -0300 | [diff] [blame] | 7 | #include <string.h> |
Elena Reshetova | 6df74bc | 2017-02-21 17:34:57 +0200 | [diff] [blame] | 8 | #include <linux/refcount.h> |
Kan Liang | f988e71 | 2017-09-29 07:47:53 -0700 | [diff] [blame] | 9 | #include "rwsem.h" |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 10 | |
| 11 | struct comm_str { |
| 12 | char *str; |
| 13 | struct rb_node rb_node; |
Elena Reshetova | 6df74bc | 2017-02-21 17:34:57 +0200 | [diff] [blame] | 14 | refcount_t refcnt; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 15 | }; |
| 16 | |
| 17 | /* Should perhaps be moved to struct machine */ |
| 18 | static struct rb_root comm_str_root; |
Kan Liang | f988e71 | 2017-09-29 07:47:53 -0700 | [diff] [blame] | 19 | static struct rw_semaphore comm_str_lock = {.lock = PTHREAD_RWLOCK_INITIALIZER,}; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 20 | |
Arnaldo Carvalho de Melo | 86c1952 | 2015-05-19 19:07:42 -0300 | [diff] [blame] | 21 | static struct comm_str *comm_str__get(struct comm_str *cs) |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 22 | { |
Arnaldo Carvalho de Melo | 86c1952 | 2015-05-19 19:07:42 -0300 | [diff] [blame] | 23 | if (cs) |
Elena Reshetova | 6df74bc | 2017-02-21 17:34:57 +0200 | [diff] [blame] | 24 | refcount_inc(&cs->refcnt); |
Arnaldo Carvalho de Melo | 86c1952 | 2015-05-19 19:07:42 -0300 | [diff] [blame] | 25 | return cs; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | static void comm_str__put(struct comm_str *cs) |
| 29 | { |
Elena Reshetova | 6df74bc | 2017-02-21 17:34:57 +0200 | [diff] [blame] | 30 | if (cs && refcount_dec_and_test(&cs->refcnt)) { |
Kan Liang | f988e71 | 2017-09-29 07:47:53 -0700 | [diff] [blame] | 31 | down_write(&comm_str_lock); |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 32 | rb_erase(&cs->rb_node, &comm_str_root); |
Kan Liang | f988e71 | 2017-09-29 07:47:53 -0700 | [diff] [blame] | 33 | up_write(&comm_str_lock); |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 34 | zfree(&cs->str); |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 35 | free(cs); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | static 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 Reshetova | 6df74bc | 2017-02-21 17:34:57 +0200 | [diff] [blame] | 53 | refcount_set(&cs->refcnt, 1); |
Arnaldo Carvalho de Melo | 86c1952 | 2015-05-19 19:07:42 -0300 | [diff] [blame] | 54 | |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 55 | return cs; |
| 56 | } |
| 57 | |
Kan Liang | f988e71 | 2017-09-29 07:47:53 -0700 | [diff] [blame] | 58 | static |
| 59 | struct comm_str *__comm_str__findnew(const char *str, struct rb_root *root) |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 60 | { |
| 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 Reshetova | 6df74bc | 2017-02-21 17:34:57 +0200 | [diff] [blame] | 72 | return comm_str__get(iter); |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 73 | |
| 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 Liang | f988e71 | 2017-09-29 07:47:53 -0700 | [diff] [blame] | 90 | static 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 Hunter | 65de51f | 2014-07-31 09:00:44 +0300 | [diff] [blame] | 101 | struct comm *comm__new(const char *str, u64 timestamp, bool exec) |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 102 | { |
| 103 | struct comm *comm = zalloc(sizeof(*comm)); |
| 104 | |
| 105 | if (!comm) |
| 106 | return NULL; |
| 107 | |
| 108 | comm->start = timestamp; |
Adrian Hunter | 65de51f | 2014-07-31 09:00:44 +0300 | [diff] [blame] | 109 | comm->exec = exec; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 110 | |
| 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 Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 117 | return comm; |
| 118 | } |
| 119 | |
Adrian Hunter | 65de51f | 2014-07-31 09:00:44 +0300 | [diff] [blame] | 120 | int comm__override(struct comm *comm, const char *str, u64 timestamp, bool exec) |
Namhyung Kim | 4dfced3 | 2013-09-13 16:28:57 +0900 | [diff] [blame] | 121 | { |
Frederic Weisbecker | 3178f58 | 2014-01-14 16:37:14 +0100 | [diff] [blame] | 122 | struct comm_str *new, *old = comm->comm_str; |
Namhyung Kim | 4dfced3 | 2013-09-13 16:28:57 +0900 | [diff] [blame] | 123 | |
Frederic Weisbecker | 3178f58 | 2014-01-14 16:37:14 +0100 | [diff] [blame] | 124 | new = comm_str__findnew(str, &comm_str_root); |
| 125 | if (!new) |
| 126 | return -ENOMEM; |
Namhyung Kim | 4dfced3 | 2013-09-13 16:28:57 +0900 | [diff] [blame] | 127 | |
Namhyung Kim | 4dfced3 | 2013-09-13 16:28:57 +0900 | [diff] [blame] | 128 | comm_str__put(old); |
Frederic Weisbecker | 3178f58 | 2014-01-14 16:37:14 +0100 | [diff] [blame] | 129 | comm->comm_str = new; |
| 130 | comm->start = timestamp; |
Adrian Hunter | 65de51f | 2014-07-31 09:00:44 +0300 | [diff] [blame] | 131 | if (exec) |
| 132 | comm->exec = true; |
Frederic Weisbecker | 3178f58 | 2014-01-14 16:37:14 +0100 | [diff] [blame] | 133 | |
| 134 | return 0; |
Namhyung Kim | 4dfced3 | 2013-09-13 16:28:57 +0900 | [diff] [blame] | 135 | } |
| 136 | |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 137 | void comm__free(struct comm *comm) |
| 138 | { |
| 139 | comm_str__put(comm->comm_str); |
| 140 | free(comm); |
| 141 | } |
| 142 | |
| 143 | const char *comm__str(const struct comm *comm) |
| 144 | { |
| 145 | return comm->comm_str->str; |
| 146 | } |