Debian Codegraph @

runtime/linux/task_finder_map.c

raw
1 #include <linux/list.h>
2 #include <linux/jhash.h>
3
4 #include "stp_helper_lock.h"
5
6 // When handling mmap()/munmap()/mprotect() syscall tracing to notice
7 // memory map changes, we need to cache syscall entry parameter values
8 // for processing at syscall exit.
9
10 // __stp_tf_map_lock protects the hash table.
11 // Documentation/spinlocks.txt suggest we can be a bit more clever
12 // if we guarantee that in interrupt context we only read, not write
13 // the datastructures. We should never change the hash table or the
14 // contents in interrupt context (which should only ever call
15 // stap_find_map_map_info for getting stored info). So we might
16 // want to look into that if this seems a bottleneck.
17 static STP_DEFINE_RWLOCK(__stp_tf_map_lock);
18
19 #define __STP_TF_HASH_BITS 4
20 #define __STP_TF_TABLE_SIZE (1 << __STP_TF_HASH_BITS)
21
22 #ifndef TASK_FINDER_MAP_ENTRY_ITEMS
23 #define TASK_FINDER_MAP_ENTRY_ITEMS 100
24 #endif
25
26 struct __stp_tf_map_entry {
27 /* private: */
28 struct hlist_node hlist;
29 int usage;
30
31 /* public: */
32 pid_t pid;
33 long syscall_no;
34 unsigned long arg0;
35 unsigned long arg1;
36 unsigned long arg2;
37 };
38
39 static struct __stp_tf_map_entry
40 __stp_tf_map_free_list_items[TASK_FINDER_MAP_ENTRY_ITEMS];
41
42 static struct hlist_head __stp_tf_map_free_list[1];
43
44 static struct hlist_head __stp_tf_map_table[__STP_TF_TABLE_SIZE];
45
46 // __stp_tf_map_initialize(): Initialize the free list. Grabs the
47 // lock.
48 static void
49 __stp_tf_map_initialize(void)
50 {
51 int i;
52 struct hlist_head *head = &__stp_tf_map_free_list[0];
53
54 unsigned long flags;
55 stp_write_lock_irqsave(&__stp_tf_map_lock, flags);
56 for (i = 0; i < TASK_FINDER_MAP_ENTRY_ITEMS; i++) {
57 hlist_add_head(&__stp_tf_map_free_list_items[i].hlist, head);
58 }
59 stp_write_unlock_irqrestore(&__stp_tf_map_lock, flags);
60 }
61
62
63 // __stp_tf_map_get_free_entry(): Returns an entry from the free list
64 // or NULL. The __stp_tf_map_lock must be write locked before calling this
65 // function.
66 static struct __stp_tf_map_entry *
67 __stp_tf_map_get_free_entry(void)
68 {
69 struct hlist_head *head = &__stp_tf_map_free_list[0];
70 struct hlist_node *node;
71 struct __stp_tf_map_entry *entry = NULL;
72
73 if (hlist_empty(head))
74 return NULL;
75 stap_hlist_for_each_entry(entry, node, head, hlist) {
76 break;
77 }
78 if (entry != NULL)
79 hlist_del(&entry->hlist);
80 return entry;
81 }
82
83
84 // __stp_tf_map_put_free_entry(): Puts an entry back on the free
85 // list. The __stp_tf_map_lock must be write locked before calling this
86 // function.
87 static void
88 __stp_tf_map_put_free_entry(struct __stp_tf_map_entry *entry)
89 {
90 struct hlist_head *head = &__stp_tf_map_free_list[0];
91 hlist_add_head(&entry->hlist, head);
92 }
93
94
95 // __stp_tf_map_hash(): Compute the map hash.
96 static inline u32
97 __stp_tf_map_hash(struct task_struct *tsk)
98 {
99 return (jhash_1word(tsk->pid, 0) & (__STP_TF_TABLE_SIZE - 1));
100 }
101
102
103 // Get map_entry if the map is present in the map hash table.
104 // Returns NULL if not present. Takes a read lock on __stp_tf_map_lock.
105 static struct __stp_tf_map_entry *
106 __stp_tf_get_map_entry(struct task_struct *tsk)
107 {
108 struct hlist_head *head;
109 struct hlist_node *node;
110 struct __stp_tf_map_entry *entry;
111
112 unsigned long flags;
113 stp_read_lock_irqsave(&__stp_tf_map_lock, flags);
114 head = &__stp_tf_map_table[__stp_tf_map_hash(tsk)];
115 stap_hlist_for_each_entry(entry, node, head, hlist) {
116 if (tsk->pid == entry->pid) {
117 stp_read_unlock_irqrestore(&__stp_tf_map_lock, flags);
118 return entry;
119 }
120 }
121 stp_read_unlock_irqrestore(&__stp_tf_map_lock, flags);
122 return NULL;
123 }
124
125
126 // Add the map info to the map hash table. Takes a write lock on
127 // __stp_tf_map_lock.
128 static int
129 __stp_tf_add_map(struct task_struct *tsk, long syscall_no, unsigned long arg0,
130 unsigned long arg1, unsigned long arg2)
131 {
132 struct hlist_head *head;
133 struct hlist_node *node;
134 struct __stp_tf_map_entry *entry;
135 unsigned long flags;
136
137 stp_write_lock_irqsave(&__stp_tf_map_lock, flags);
138 head = &__stp_tf_map_table[__stp_tf_map_hash(tsk)];
139 stap_hlist_for_each_entry(entry, node, head, hlist) {
140 // If we find an existing entry, just increment the
141 // usage count.
142 if (tsk->pid == entry->pid) {
143 entry->usage++;
144 stp_write_unlock_irqrestore(&__stp_tf_map_lock, flags);
145 return 0;
146 }
147 }
148
149 // Get an element from the free list.
150 entry = __stp_tf_map_get_free_entry();
151 if (!entry) {
152 stp_write_unlock_irqrestore(&__stp_tf_map_lock, flags);
153 return -ENOMEM;
154 }
155 entry->usage = 1;
156 entry->pid = tsk->pid;
157 entry->syscall_no = syscall_no;
158 entry->arg0 = arg0;
159 entry->arg1 = arg1;
160 entry->arg2 = arg2;
161 hlist_add_head(&entry->hlist, head);
162 stp_write_unlock_irqrestore(&__stp_tf_map_lock, flags);
163 return 0;
164 }
165
166
167 // Remove the map entry from the map hash table. Takes a write lock on
168 // __stp_tf_map_lock.
169 static int
170 __stp_tf_remove_map_entry(struct __stp_tf_map_entry *entry)
171 {
172 struct hlist_head *head;
173 struct hlist_node *node;
174 int found = 0;
175
176 if (entry != NULL) {
177 unsigned long flags;
178 stp_write_lock_irqsave(&__stp_tf_map_lock, flags);
179
180 // Decrement the usage count.
181 entry->usage--;
182
183 // If the entry is unused, put it back on the free
184 // list.
185 if (entry->usage == 0) {
186 hlist_del(&entry->hlist);
187 __stp_tf_map_put_free_entry(entry);
188 }
189 stp_write_unlock_irqrestore(&__stp_tf_map_lock, flags);
190 }
191 return 0;
192 }