1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
5 * LTTng Process ID tracking.
7 * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/err.h>
13 #include <linux/seq_file.h>
14 #include <linux/stringify.h>
15 #include <linux/hash.h>
16 #include <linux/rcupdate.h>
18 #include <wrapper/rcu.h>
19 #include <wrapper/list.h>
20 #include <lttng-events.h>
23 * Hash table is allocated and freed when there are no possible
24 * concurrent lookups (ensured by the alloc/free caller). However,
25 * there can be concurrent RCU lookups vs add/del operations.
27 * Concurrent updates of the PID hash table are forbidden: the caller
28 * must ensure mutual exclusion. This is currently done by holding the
29 * sessions_mutex across calls to create, destroy, add, and del
30 * functions of this API.
32 int lttng_id_tracker_get_node_id(const struct lttng_id_hash_node
*node
)
38 * Lookup performed from RCU read-side critical section (RCU sched),
39 * protected by preemption off at the tracepoint call site.
40 * Return true if found, false if not found.
42 bool lttng_id_tracker_lookup(struct lttng_id_tracker_rcu
*p
, int id
)
44 struct hlist_head
*head
;
45 struct lttng_id_hash_node
*e
;
46 uint32_t hash
= hash_32(id
, 32);
48 head
= &p
->id_hash
[hash
& (LTTNG_ID_TABLE_SIZE
- 1)];
49 lttng_hlist_for_each_entry_rcu(e
, head
, hlist
) {
51 return true; /* Found */
55 EXPORT_SYMBOL_GPL(lttng_id_tracker_lookup
);
57 static struct lttng_id_tracker_rcu
*lttng_id_tracker_rcu_create(void)
59 struct lttng_id_tracker_rcu
*tracker
;
61 tracker
= kzalloc(sizeof(struct lttng_id_tracker_rcu
), GFP_KERNEL
);
68 * Tracker add and del operations support concurrent RCU lookups.
70 int lttng_id_tracker_add(struct lttng_id_tracker
*lf
, int id
)
72 struct hlist_head
*head
;
73 struct lttng_id_hash_node
*e
;
74 struct lttng_id_tracker_rcu
*p
= lf
->p
;
75 uint32_t hash
= hash_32(id
, 32);
76 bool allocated
= false;
79 p
= lttng_id_tracker_rcu_create();
84 head
= &p
->id_hash
[hash
& (LTTNG_ID_TABLE_SIZE
- 1)];
85 lttng_hlist_for_each_entry(e
, head
, hlist
) {
89 e
= kmalloc(sizeof(struct lttng_id_hash_node
), GFP_KERNEL
);
93 hlist_add_head_rcu(&e
->hlist
, head
);
95 rcu_assign_pointer(lf
->p
, p
);
101 void id_tracker_del_node_rcu(struct lttng_id_hash_node
*e
)
103 hlist_del_rcu(&e
->hlist
);
105 * We choose to use a heavyweight synchronize on removal here,
106 * since removal of an ID from the tracker mask is a rare
107 * operation, and we don't want to use more cache lines than
108 * what we really need when doing the ID lookups, so we don't
109 * want to afford adding a rcu_head field to those pid hash
117 * This removal is only used on destroy, so it does not need to support
118 * concurrent RCU lookups.
121 void id_tracker_del_node(struct lttng_id_hash_node
*e
)
123 hlist_del(&e
->hlist
);
127 int lttng_id_tracker_del(struct lttng_id_tracker
*lf
, int id
)
129 struct hlist_head
*head
;
130 struct lttng_id_hash_node
*e
;
131 struct lttng_id_tracker_rcu
*p
= lf
->p
;
132 uint32_t hash
= hash_32(id
, 32);
136 head
= &p
->id_hash
[hash
& (LTTNG_ID_TABLE_SIZE
- 1)];
138 * No need of _safe iteration, because we stop traversal as soon
139 * as we remove the entry.
141 lttng_hlist_for_each_entry(e
, head
, hlist
) {
143 id_tracker_del_node_rcu(e
);
147 return -ENOENT
; /* Not found */
150 static void lttng_id_tracker_rcu_destroy(struct lttng_id_tracker_rcu
*p
)
156 for (i
= 0; i
< LTTNG_ID_TABLE_SIZE
; i
++) {
157 struct hlist_head
*head
= &p
->id_hash
[i
];
158 struct lttng_id_hash_node
*e
;
159 struct hlist_node
*tmp
;
161 lttng_hlist_for_each_entry_safe(e
, tmp
, head
, hlist
)
162 id_tracker_del_node(e
);
167 int lttng_id_tracker_empty_set(struct lttng_id_tracker
*lf
)
169 struct lttng_id_tracker_rcu
*p
, *oldp
;
171 p
= lttng_id_tracker_rcu_create();
175 rcu_assign_pointer(lf
->p
, p
);
177 lttng_id_tracker_rcu_destroy(oldp
);
181 void lttng_id_tracker_destroy(struct lttng_id_tracker
*lf
, bool rcu
)
183 struct lttng_id_tracker_rcu
*p
= lf
->p
;
187 rcu_assign_pointer(lf
->p
, NULL
);
190 lttng_id_tracker_rcu_destroy(p
);
This page took 0.036967 seconds and 4 git commands to generate.