1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21902efe7SFrederic Weisbecker #include "comm.h"
3a43783aeSArnaldo Carvalho de Melo #include <errno.h>
41902efe7SFrederic Weisbecker #include <stdlib.h>
51902efe7SFrederic Weisbecker #include <stdio.h>
672f7c4d2SArnaldo Carvalho de Melo #include <string.h>
76df74bc0SElena Reshetova #include <linux/refcount.h>
8f0049f2cSArnaldo Carvalho de Melo #include <linux/rbtree.h>
9*7f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
10f988e71bSKan Liang #include "rwsem.h"
111902efe7SFrederic Weisbecker
121902efe7SFrederic Weisbecker struct comm_str {
131902efe7SFrederic Weisbecker char *str;
141902efe7SFrederic Weisbecker struct rb_node rb_node;
156df74bc0SElena Reshetova refcount_t refcnt;
161902efe7SFrederic Weisbecker };
171902efe7SFrederic Weisbecker
181902efe7SFrederic Weisbecker /* Should perhaps be moved to struct machine */
191902efe7SFrederic Weisbecker static struct rb_root comm_str_root;
20f988e71bSKan Liang static struct rw_semaphore comm_str_lock = {.lock = PTHREAD_RWLOCK_INITIALIZER,};
211902efe7SFrederic Weisbecker
comm_str__get(struct comm_str * cs)2286c19525SArnaldo Carvalho de Melo static struct comm_str *comm_str__get(struct comm_str *cs)
231902efe7SFrederic Weisbecker {
2446b3722cSJiri Olsa if (cs && refcount_inc_not_zero(&cs->refcnt))
2586c19525SArnaldo Carvalho de Melo return cs;
2646b3722cSJiri Olsa
2746b3722cSJiri Olsa return NULL;
281902efe7SFrederic Weisbecker }
291902efe7SFrederic Weisbecker
comm_str__put(struct comm_str * cs)301902efe7SFrederic Weisbecker static void comm_str__put(struct comm_str *cs)
311902efe7SFrederic Weisbecker {
326df74bc0SElena Reshetova if (cs && refcount_dec_and_test(&cs->refcnt)) {
33f988e71bSKan Liang down_write(&comm_str_lock);
341902efe7SFrederic Weisbecker rb_erase(&cs->rb_node, &comm_str_root);
35f988e71bSKan Liang up_write(&comm_str_lock);
3674cf249dSArnaldo Carvalho de Melo zfree(&cs->str);
371902efe7SFrederic Weisbecker free(cs);
381902efe7SFrederic Weisbecker }
391902efe7SFrederic Weisbecker }
401902efe7SFrederic Weisbecker
comm_str__alloc(const char * str)411902efe7SFrederic Weisbecker static struct comm_str *comm_str__alloc(const char *str)
421902efe7SFrederic Weisbecker {
431902efe7SFrederic Weisbecker struct comm_str *cs;
441902efe7SFrederic Weisbecker
451902efe7SFrederic Weisbecker cs = zalloc(sizeof(*cs));
461902efe7SFrederic Weisbecker if (!cs)
471902efe7SFrederic Weisbecker return NULL;
481902efe7SFrederic Weisbecker
491902efe7SFrederic Weisbecker cs->str = strdup(str);
501902efe7SFrederic Weisbecker if (!cs->str) {
511902efe7SFrederic Weisbecker free(cs);
521902efe7SFrederic Weisbecker return NULL;
531902efe7SFrederic Weisbecker }
541902efe7SFrederic Weisbecker
556df74bc0SElena Reshetova refcount_set(&cs->refcnt, 1);
5686c19525SArnaldo Carvalho de Melo
571902efe7SFrederic Weisbecker return cs;
581902efe7SFrederic Weisbecker }
591902efe7SFrederic Weisbecker
60f988e71bSKan Liang static
__comm_str__findnew(const char * str,struct rb_root * root)61f988e71bSKan Liang struct comm_str *__comm_str__findnew(const char *str, struct rb_root *root)
621902efe7SFrederic Weisbecker {
631902efe7SFrederic Weisbecker struct rb_node **p = &root->rb_node;
641902efe7SFrederic Weisbecker struct rb_node *parent = NULL;
651902efe7SFrederic Weisbecker struct comm_str *iter, *new;
661902efe7SFrederic Weisbecker int cmp;
671902efe7SFrederic Weisbecker
681902efe7SFrederic Weisbecker while (*p != NULL) {
691902efe7SFrederic Weisbecker parent = *p;
701902efe7SFrederic Weisbecker iter = rb_entry(parent, struct comm_str, rb_node);
711902efe7SFrederic Weisbecker
7246b3722cSJiri Olsa /*
7346b3722cSJiri Olsa * If we race with comm_str__put, iter->refcnt is 0
7446b3722cSJiri Olsa * and it will be removed within comm_str__put call
7546b3722cSJiri Olsa * shortly, ignore it in this search.
7646b3722cSJiri Olsa */
771902efe7SFrederic Weisbecker cmp = strcmp(str, iter->str);
7846b3722cSJiri Olsa if (!cmp && comm_str__get(iter))
7946b3722cSJiri Olsa return iter;
801902efe7SFrederic Weisbecker
811902efe7SFrederic Weisbecker if (cmp < 0)
821902efe7SFrederic Weisbecker p = &(*p)->rb_left;
831902efe7SFrederic Weisbecker else
841902efe7SFrederic Weisbecker p = &(*p)->rb_right;
851902efe7SFrederic Weisbecker }
861902efe7SFrederic Weisbecker
871902efe7SFrederic Weisbecker new = comm_str__alloc(str);
881902efe7SFrederic Weisbecker if (!new)
891902efe7SFrederic Weisbecker return NULL;
901902efe7SFrederic Weisbecker
911902efe7SFrederic Weisbecker rb_link_node(&new->rb_node, parent, p);
921902efe7SFrederic Weisbecker rb_insert_color(&new->rb_node, root);
931902efe7SFrederic Weisbecker
941902efe7SFrederic Weisbecker return new;
951902efe7SFrederic Weisbecker }
961902efe7SFrederic Weisbecker
comm_str__findnew(const char * str,struct rb_root * root)97f988e71bSKan Liang static struct comm_str *comm_str__findnew(const char *str, struct rb_root *root)
98f988e71bSKan Liang {
99f988e71bSKan Liang struct comm_str *cs;
100f988e71bSKan Liang
101f988e71bSKan Liang down_write(&comm_str_lock);
102f988e71bSKan Liang cs = __comm_str__findnew(str, root);
103f988e71bSKan Liang up_write(&comm_str_lock);
104f988e71bSKan Liang
105f988e71bSKan Liang return cs;
106f988e71bSKan Liang }
107f988e71bSKan Liang
comm__new(const char * str,u64 timestamp,bool exec)10865de51f9SAdrian Hunter struct comm *comm__new(const char *str, u64 timestamp, bool exec)
1091902efe7SFrederic Weisbecker {
1101902efe7SFrederic Weisbecker struct comm *comm = zalloc(sizeof(*comm));
1111902efe7SFrederic Weisbecker
1121902efe7SFrederic Weisbecker if (!comm)
1131902efe7SFrederic Weisbecker return NULL;
1141902efe7SFrederic Weisbecker
1151902efe7SFrederic Weisbecker comm->start = timestamp;
11665de51f9SAdrian Hunter comm->exec = exec;
1171902efe7SFrederic Weisbecker
1181902efe7SFrederic Weisbecker comm->comm_str = comm_str__findnew(str, &comm_str_root);
1191902efe7SFrederic Weisbecker if (!comm->comm_str) {
1201902efe7SFrederic Weisbecker free(comm);
1211902efe7SFrederic Weisbecker return NULL;
1221902efe7SFrederic Weisbecker }
1231902efe7SFrederic Weisbecker
1241902efe7SFrederic Weisbecker return comm;
1251902efe7SFrederic Weisbecker }
1261902efe7SFrederic Weisbecker
comm__override(struct comm * comm,const char * str,u64 timestamp,bool exec)12765de51f9SAdrian Hunter int comm__override(struct comm *comm, const char *str, u64 timestamp, bool exec)
1284dfced35SNamhyung Kim {
1293178f58bSFrederic Weisbecker struct comm_str *new, *old = comm->comm_str;
1304dfced35SNamhyung Kim
1313178f58bSFrederic Weisbecker new = comm_str__findnew(str, &comm_str_root);
1323178f58bSFrederic Weisbecker if (!new)
1333178f58bSFrederic Weisbecker return -ENOMEM;
1344dfced35SNamhyung Kim
1354dfced35SNamhyung Kim comm_str__put(old);
1363178f58bSFrederic Weisbecker comm->comm_str = new;
1373178f58bSFrederic Weisbecker comm->start = timestamp;
13865de51f9SAdrian Hunter if (exec)
13965de51f9SAdrian Hunter comm->exec = true;
1403178f58bSFrederic Weisbecker
1413178f58bSFrederic Weisbecker return 0;
1424dfced35SNamhyung Kim }
1434dfced35SNamhyung Kim
comm__free(struct comm * comm)1441902efe7SFrederic Weisbecker void comm__free(struct comm *comm)
1451902efe7SFrederic Weisbecker {
1461902efe7SFrederic Weisbecker comm_str__put(comm->comm_str);
1471902efe7SFrederic Weisbecker free(comm);
1481902efe7SFrederic Weisbecker }
1491902efe7SFrederic Weisbecker
comm__str(const struct comm * comm)1501902efe7SFrederic Weisbecker const char *comm__str(const struct comm *comm)
1511902efe7SFrederic Weisbecker {
1521902efe7SFrederic Weisbecker return comm->comm_str->str;
1531902efe7SFrederic Weisbecker }
154