Debian Codegraph @

runtime/linux/runtime_context.h

raw
1 /* -*- linux-c -*-
2 * Context Runtime Functions
3 * Copyright (C) 2014 Red Hat Inc.
4 *
5 * This file is part of systemtap, and is free software. You can
6 * redistribute it and/or modify it under the terms of the GNU General
7 * Public License (GPL); either version 2, or (at your option) any
8 * later version.
9 */
10
11 #ifndef _LINUX_RUNTIME_CONTEXT_H_
12 #define _LINUX_RUNTIME_CONTEXT_H_
13
14 /* Can't use a lock primitive for this because lock_acquire() has tracepoints */
15 static atomic_t _stp_contexts_busy_ctr = ATOMIC_INIT(0);
16
17 /* Per-cpu data is always initialized with zero filled. */
18 static DEFINE_PER_CPU(struct context *, contexts);
19
20 static int _stp_runtime_contexts_alloc(void)
21 {
22 unsigned int cpu;
23
24 /* We don't use for_aech_possible_cpu() here since the number of possible
25 * CPUs may be very large even though there are many fewere online CPUs.
26 * For example, VMWare guests usually have 128 possible CPUs while only
27 * have a few online CPUs. Once the context structs were
28 * allocated for online CPUs at this point, we will discard any context
29 * fetching operations on any future online CPUs dynamically added
30 * through the kernel's CPU hotplug feature. Memory allocations of the
31 * context structs can only happen right here.
32 */
33 for_each_online_cpu(cpu) {
34 /* Module init, so in user context, safe to use
35 * "sleeping" allocation. */
36 struct context *c = _stp_vzalloc_node(sizeof (struct context),
37 cpu_to_node(cpu));
38 if (unlikely(c == NULL)) {
39 _stp_error ("context (size %lu per cpu) allocation failed",
40 (unsigned long) sizeof (struct context));
41 return -ENOMEM;
42 }
43 per_cpu(contexts, cpu) = c;
44 }
45 return 0;
46 }
47
48 static bool _stp_runtime_context_trylock(void)
49 {
50 bool locked;
51
52 /* Need to disable preemption because of the smp_processor_id() call
53 in _stp_runtime_get_context(). */
54 preempt_disable();
55
56 /* fast path to ignore new online CPUs without percpu context memory
57 * allocations. this also serves as an extra safe guard for NULL context
58 * pointers. */
59 if (unlikely(_stp_runtime_get_context() == NULL)) {
60 preempt_enable_no_resched();
61 return false;
62 }
63
64 locked = atomic_add_unless(&_stp_contexts_busy_ctr, 1, INT_MAX);
65 if (!locked)
66 preempt_enable_no_resched();
67
68 return locked;
69 }
70
71 static void _stp_runtime_context_unlock(void)
72 {
73 atomic_dec(&_stp_contexts_busy_ctr);
74 preempt_enable_no_resched();
75 }
76
77 /* We should be free of all probes by this time, but for example the timer for
78 * _stp_ctl_work_callback may still be running and looking for contexts. We
79 * use _stp_contexts_busy_ctr to be sure its safe to free them. */
80 static void _stp_runtime_contexts_free(void)
81 {
82 unsigned int cpu;
83
84 /* Sync to make sure existing readers are done */
85 while (atomic_cmpxchg(&_stp_contexts_busy_ctr, 0, INT_MAX))
86 cpu_relax();
87
88 /* Now we can actually free the contexts */
89
90 /* NB We cannot use the for_each_online_cpu() here since online
91 * CPUs may get changed on-the-fly through the CPU hotplug feature
92 * of the kernel. We only allocated the context structs on original
93 * online CPUs when _stp_runtime_contexts_alloc() was called. And we
94 * cannot allocate new memory from within this context. */
95 for_each_possible_cpu(cpu) {
96 struct context *c = per_cpu(contexts, cpu);
97 if (likely(c))
98 _stp_vfree(c);
99 }
100 }
101
102 static inline struct context * _stp_runtime_get_context(void)
103 {
104 return per_cpu(contexts, smp_processor_id());
105 }
106
107 static struct context * _stp_runtime_entryfn_get_context(void)
108 {
109 struct context* __restrict__ c = NULL;
110
111 if (!_stp_runtime_context_trylock())
112 return NULL;
113
114 c = _stp_runtime_get_context();
115 if (c != NULL) {
116 if (!atomic_cmpxchg(&c->busy, 0, 1)) {
117 // NB: Notice we're not releasing _stp_contexts_busy_ctr
118 // here. We exepect the calling code to call
119 // _stp_runtime_entryfn_get_context() and
120 // _stp_runtime_entryfn_put_context() as a
121 // pair.
122 return c;
123 }
124 }
125 _stp_runtime_context_unlock();
126 return NULL;
127 }
128
129 static inline void _stp_runtime_entryfn_put_context(struct context *c)
130 {
131 if (c) {
132 atomic_set(&c->busy, 0);
133 _stp_runtime_context_unlock();
134 }
135 }
136
137 static void _stp_runtime_context_wait(void)
138 {
139 int holdon;
140 unsigned long hold_start;
141 int hold_index;
142
143 hold_start = jiffies;
144 hold_index = -1;
145 do {
146 int i;
147
148 holdon = 0;
149 if (!_stp_runtime_context_trylock())
150 break;
151
152 for_each_possible_cpu(i) {
153 struct context *c = per_cpu(contexts, i);
154 if (c != NULL
155 && atomic_read (& c->busy)) {
156 holdon = 1;
157
158 /* Just in case things are really
159 * stuck, let's print some diagnostics. */
160 if (time_after(jiffies, hold_start + HZ) // > 1 second
161 && (i > hold_index)) { // not already printed
162 hold_index = i;
163 printk(KERN_ERR "%s context[%d] stuck: %s\n", THIS_MODULE->name, i, c->probe_point);
164 }
165 }
166 }
167 _stp_runtime_context_unlock();
168
169 /*
170 * Just in case things are really really stuck, a
171 * handler probably suffered a fault, and the kernel
172 * probably killed a task/thread already. We can't be
173 * quite sure in what state everything is in, however
174 * auxiliary stuff like kprobes / uprobes / locks have
175 * already been unregistered. So it's *probably* safe
176 * to pretend/assume/hope everything is OK, and let
177 * the cleanup finish.
178 *
179 * In the worst case, there may occur a fault, as a
180 * genuinely running probe handler tries to access
181 * script globals (about to be freed), or something
182 * accesses module memory (about to be unloaded).
183 * This is sometimes stinky, so the alternative
184 * (default) is to change from a livelock to a
185 * livelock that sleeps awhile.
186 */
187 #ifdef STAP_OVERRIDE_STUCK_CONTEXT
188 if (time_after(jiffies, hold_start + HZ*10)) { // > 10 seconds
189 printk(KERN_ERR "%s overriding stuck context to allow module shutdown.", THIS_MODULE->name);
190 holdon = 0; // allow loop to exit
191 }
192 #else
193 /* at least stop sucking down the staprun cpu */
194 msleep(250);
195 #endif
196
197 /* NB: we run at least one of these during the
198 * shutdown sequence: */
199 yield(); /* aka schedule() and then some */
200 } while (holdon);
201 }
202
203 #endif /* _LINUX_RUNTIME_CONTEXT_H_ */