Repository navigation
Expand file tree
/
Copy pathtaskq.c
More file actions
367 lines (331 loc) · 14.2 KB
/
Copy pathtaskq.c
File metadata and controls
367 lines (331 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/**********************************************************************/
/* Emulation of the Solaris taskq interface. A taskq corresponds */
/* to a Linux work_queue interface. Sometimes we need to schedule */
/* something to happen outside of an interrupt and in the context */
/* of a process - not any process - but a process which is */
/* independent of all other processes. */
/* */
/* This is needed by the garbage collection code for fasttrap. We */
/* want to remove the probes when the original dtrace owner dies, */
/* but he may be taking time, so, to avoid mutex deadlock, dtrace */
/* uses the taskq mechanism to periodically retry, since its not */
/* time critical. */
/* */
/* (Of course the implication is that a pathological dtrace */
/* come-and-go could use up a lot of memory and not allow the */
/* garbage collector to kick in). */
/* */
/* Unfortunately, the workqueue interface is GPL, but even if we */
/* state this file is GPL, doesnt really help. We have to use */
/* function pointers to get around this, which means things could */
/* be brittle over kernel releases. __alloc_workqueue_key() */
/* significantly changed parameter ordering so we need to be */
/* careful to handle this. */
/* */
/* Author: Paul D Fox */
/* Date: July 2012 */
/* */
/* This is GPL code - its only a piece of glue and doesnt need to */
/* come under the CDDL. */
/**********************************************************************/
/**********************************************************************/
/* Dont try to fix the gcc redefinition warning unless you truly */
/* know what you are doing. */
/**********************************************************************/
#define __alloc_workqueue_key local__alloc_workqueue_key
#define lockdep_init_map local_lockdep_init_map
#include <dtrace_linux.h>
#include <sys/dtrace_impl.h>
#include <dtrace_proto.h>
#include "../port.h"
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/workqueue.h>
#include <taskq.h>
//typedef struct taskq taskq_t;
/**********************************************************************/
/* Handle older kernels which do not separate out the */
/* work/delayed-work structs. */
/**********************************************************************/
#if !defined(INIT_DELAYED_WORK)
/***********************************************/
/* On older kernel, INIT_WORK takes three */
/* args and the last is the callback (void */
/* *)ptr, so be explicit and pass it in. */
/***********************************************/
# define INIT_DELAYED_WORK(a, b) INIT_WORK(a, b, a)
# define delayed_work work_struct
# define cancel_delayed_work_sync cancel_delayed_work
#else
#undef __INIT_DELAYED_WORK
# define __INIT_DELAYED_WORK(_work, _func, _tflags) \
do { \
INIT_WORK(&(_work)->work, (_func)); \
__setup_timer(&(_work)->timer, delayed_work_timer_fn_ptr, \
(unsigned long)(_work), \
(_tflags) | TIMER_IRQSAFE); \
} while (0)
#endif
/**********************************************************************/
/* Kernels >= 2.6.37 changed the interface. Work around GPL issue. */
/**********************************************************************/
# if !defined(WR_MEM_RECLAIM) /* Introduced in 2.6.37 */
struct workqueue_struct *
__create_workqueue_key(const char *name, int singlethread,
int freezeable, int rt, struct lock_class_key *key,
const char *lock_name)
{ static void *(*func)(const char *name, int singlethread,
int freezeable, int rt, struct lock_class_key *key,
const char *lock_name);
if (func == NULL)
func = get_proc_addr("__create_workqueue_key");
if (func == NULL)
return NULL;
return func(name, singlethread, freezeable, rt, key, lock_name);
}
# endif
/**********************************************************************/
/* Following is horrible. For older kernels, eg centos5 */
/* 2.6.18-128.el5, the create_workqueue expands into a call to */
/* this GPL function. So we get the macro to call locally, then */
/* indirect to the real function. */
/**********************************************************************/
#if !defined(__create_workqueue)
struct workqueue_struct *__create_workqueue(const char *name, int singlethread)
{ static struct workqueue_struct *(*func)(const char *name, int singlethread);
if (func == NULL)
func = get_proc_addr("__create_workqueue");
return func(name, singlethread);
}
#endif
/**********************************************************************/
/* Work queue data structure, used by dtrace.c for dtrace_taskq, */
/* and for timeout() implementation. */
/**********************************************************************/
typedef struct my_work_t {
struct delayed_work t_work;
task_func_t *t_func;
void *t_arg;
timeout_id_t t_id;
struct my_work_t *t_next;
} my_work_t;
/**********************************************************************/
/* List of timers - should only be 1 or 2, not lots. */
/**********************************************************************/
static my_work_t *callo;
static int taskq_enabled;
/**********************************************************************/
/* Hold on to the dtrace_taskq, from dtrace.c, so we can reuse for */
/* the timeout() functions. */
/**********************************************************************/
static taskq_t *global_tq;
/**********************************************************************/
/* Prototypes. */
/**********************************************************************/
taskqid_t taskq_dispatch2(taskq_t *tq, task_func_t func, void *arg, uint_t flags, unsigned long delay);
/**********************************************************************/
/* GPL prototypes. */
/**********************************************************************/
static int (*queue_delayed_work_on_ptr)(int cpu, struct workqueue_struct *wq, struct delayed_work *work, unsigned long delay);
static int (*queue_delayed_work_ptr)(struct workqueue_struct *wq, struct delayed_work *work, unsigned long delay);
static void (*destroy_workqueue_ptr)(struct workqueue_struct *wq);
static void (*flush_workqueue_ptr)(struct workqueue_struct *wq);
static void (*delayed_work_timer_fn_ptr)(unsigned long __data);
static int (*cancel_work_sync_ptr)(struct workqueue_struct *);
/**********************************************************************/
/* Very ugly .. different number of args for 3.3 and above */
/* kernels. All we want is to invoke the real function, but we */
/* cannot link if we try (because we arent GPL). */
/**********************************************************************/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)
# define WQ_ARGS , ...
#else
# define WQ_ARGS
#endif
struct workqueue_struct *(*__alloc_workqueue_key_ptr)(const char *fmt,
unsigned int flags,
int max_active,
struct lock_class_key *key,
const char *lock_name WQ_ARGS);
void (*lockdep_init_map_ptr)(struct lockdep_map *lock, const char *name,
struct lock_class_key *key, int subclass);
/**********************************************************************/
/* Wrapper for functions we cannot directly link against. */
/**********************************************************************/
void dummy_trampoline(void)
{
/***********************************************/
/* We embed the real function here, since */
/* we dont want to touch the stack */
/* arguments or bother with 32/64 compiler */
/* calling conventions. I would like to say */
/* how clever I am, but this aint */
/* clever...its just heavy, man ! :-) */
/***********************************************/
# if defined(__i386) || defined(__amd64)
asm(FUNCTION(local__alloc_workqueue_key));
asm("jmp *__alloc_workqueue_key_ptr\n");
asm(FUNCTION(local_lockdep_init_map));
asm("jmp *lockdep_init_map_ptr\n");
# elif defined(__arm__)
asm(FUNCTION(local__alloc_workqueue_key));
asm("ldr ip, .L3x\n");
asm("ldr ip, [ip, #0]\n");
asm("bx ip\n");
asm(".L3x: .word __alloc_workqueue_key_ptr\n");
asm(FUNCTION(local_lockdep_init_map));
asm("ldr ip, .L4x\n");
asm("ldr ip, [ip, #0]\n");
asm("bx ip\n");
asm(".L4x: .word local_lockdep_init_map\n");
# else
# error "missing trampoline definition for this CPU"
# endif
}
static void
taskq_callback(struct work_struct *work)
{ my_work_t *mw = (my_work_t *) work;
(*mw->t_func)(mw->t_arg);
}
taskq_t *
taskq_create(const char *name, int nthreads, pri_t pri, int minalloc,
int maxalloc, uint_t flags)
{ taskq_t *tq;
if (__alloc_workqueue_key_ptr == NULL) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)
__alloc_workqueue_key_ptr = get_proc_addr("__alloc_workqueue_key");
printk("__alloc_workqueue_key_ptr=%p\n", __alloc_workqueue_key_ptr);
/***********************************************/
/* If, for some reason, we cannot get the */
/* pointers, dont let us do anything. This */
/* will stop fasttrap/pid provider from */
/* working properly (garbage collection), */
/* but at least dtrace will load. */
/***********************************************/
if (__alloc_workqueue_key_ptr == NULL) {
printk("taskq_create failed because of lack of pointers\n");
return 0;
}
#endif
queue_delayed_work_ptr = get_proc_addr("queue_delayed_work");
queue_delayed_work_on_ptr = get_proc_addr("queue_delayed_work_on");
destroy_workqueue_ptr = get_proc_addr("destroy_workqueue");
flush_workqueue_ptr = get_proc_addr("flush_workqueue");
delayed_work_timer_fn_ptr = get_proc_addr("delayed_work_timer_fn");
lockdep_init_map_ptr = get_proc_addr("lockdep_init_map");
cancel_work_sync_ptr = get_proc_addr("cancel_work_sync");
printk("queue_delayed_work_on_ptr=%p\n", queue_delayed_work_on_ptr);
printk("queue_delayed_work_ptr=%p\n", queue_delayed_work_ptr);
printk("destroy_workqueue_ptr=%p\n", destroy_workqueue_ptr);
printk("flush_workqueue_ptr=%p\n", flush_workqueue_ptr);
printk("delayed_work_timer_fn_ptr=%p\n", delayed_work_timer_fn_ptr);
#if !defined(lockdep_init_map)
/***********************************************/
/* This is a macro if CONFIG_LOCKDEP is not */
/* set. */
/***********************************************/
printk("lockdep_init_map_ptr=%p\n", lockdep_init_map_ptr);
#endif
}
tq = (taskq_t *) create_workqueue(name);
global_tq = tq;
taskq_enabled = TRUE;
return tq;
}
taskqid_t
taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
{
return taskq_dispatch2(tq, func, arg, flags, 1);
}
taskqid_t
taskq_dispatch2(taskq_t *tq, task_func_t func, void *arg, uint_t flags, unsigned long delay)
{ my_work_t *work;
if (!taskq_enabled)
return 0;
work = (my_work_t *)kmalloc(sizeof(my_work_t), GFP_KERNEL & ~__GFP_WAIT);
if (work == NULL) {
printk("taskq_dispatch: couldnt alloc work buffer\n");
return -1;
}
INIT_DELAYED_WORK( &work->t_work, taskq_callback);
work->t_func = func;
work->t_arg = arg;
# if HAVE_WORK_CPU_UNBOUND
if (queue_delayed_work_on_ptr) {
/***********************************************/
/* 3.16 and above kernels have this. */
/***********************************************/
queue_delayed_work_on_ptr(WORK_CPU_UNBOUND,
(struct workqueue_struct *) tq,
&work->t_work, delay);
} else
# endif
if (queue_delayed_work_ptr) {
queue_delayed_work_ptr((struct workqueue_struct *) tq,
&work->t_work, delay);
} else {
dtrace_linux_panic("Cannot locate queue_delayed_work or queue_delayed_work_on - giving up\n");
}
return (taskqid_t) work;
}
void
taskq_destroy(taskq_t *tq)
{ struct workqueue_struct *wq = (struct workqueue_struct *) tq;
if (!taskq_enabled || wq == NULL)
return;
if (flush_workqueue_ptr)
flush_workqueue_ptr(wq);
if (destroy_workqueue_ptr)
destroy_workqueue_ptr(wq);
}
/**********************************************************************/
/* Old-fashioned style timer callbacks. Called by fasttrap.c and */
/* dtrace_unregister. We need to ensure timer callbacks are */
/* executed in the context of a process which doesnt care or */
/* couldnt be locking dtrace - avoiding deadlock. This is only */
/* used for fasttrap garbage collection but we mustnt fire whilst */
/* the tear down occurs, else dmutex_enter will deadlock or call */
/* schedule() from an invalid context. */
/**********************************************************************/
timeout_id_t
timeout(void (*func)(void *), void *arg, unsigned long ticks)
{static unsigned long id;
my_work_t *wp;
//printk("timeout(%p, %lu)\n", func, ticks);
id++;
wp = (my_work_t *) taskq_dispatch2(global_tq, func, arg, 0, ticks);
if (wp == NULL) {
printk("timeout: couldnt allocate my_work_t (taskq_enabled=%d)\n", taskq_enabled);
return 0;
}
wp->t_id = (timeout_id_t) id;
wp->t_next = callo;
callo = wp;
return (timeout_id_t) id;
}
void
untimeout(timeout_id_t id)
{ my_work_t *wp;
//printk("untimeout: %p\n", id);
if (callo && callo->t_id == id) {
wp = callo;
callo = wp->t_next;
} else {
my_work_t *wp0 = NULL;
for (wp = callo; wp; wp0 = wp, wp = wp->t_next) {
if (wp->t_id == id) {
wp0->t_next = wp->t_next;
break;
}
}
if (wp == NULL) {
printk("untimeout(%p) - not found\n", id);
return;
}
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0) /* random guess */
cancel_work_sync_ptr(&wp->t_work);
#else
cancel_delayed_work_sync(&wp->t_work);
#endif
}