summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/os/ddi_periodic.c
blob: d6c6006825c16ce9976cf85112a7c25dff7623e6 (plain)
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */
/*
 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
 */

#include <sys/cmn_err.h>
#include <sys/ddi_periodic.h>
#include <sys/id_space.h>
#include <sys/kobj.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/taskq.h>
#include <sys/taskq_impl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/sdt.h>

extern void sir_on(int);

/*
 * The ddi_periodic_add(9F) Implementation
 *
 * This file contains the implementation of the ddi_periodic_add(9F) interface.
 * It is a thin wrapper around the cyclic subsystem (see documentation in
 * uts/common/os/cyclic.c), providing a DDI interface for registering
 * (and unregistering) callbacks for periodic invocation at arbitrary
 * interrupt levels, or in kernel context.
 *
 * Each call to ddi_periodic_add will result in a new opaque handle, as
 * allocated from an id_space, a new "periodic" object (ddi_periodic_impl_t)
 * and a registered cyclic.
 *
 * Operation
 *
 * Whenever the cyclic fires, our cyclic handler checks that the particular
 * periodic is not dispatched already (we do not support overlapping execution
 * of the consumer's handler function), and not yet cancelled.  If both of
 * these conditions hold, we mark the periodic as DPF_DISPATCHED and enqueue it
 * to either the taskq (for DDI_IPL_0) or to one of the soft interrupt queues
 * (DDI_IPL_1 to DDI_IPL_10).
 *
 * While the taskq (or soft interrupt handler) is handling a particular
 * periodic, we mark it as DPF_EXECUTING.  When complete, we reset both
 * DPF_DISPATCHED and DPF_EXECUTING.
 *
 * Cancellation
 *
 * ddi_periodic_delete(9F) historically had spectacularly loose semantics with
 * respect to cancellation concurrent with handler execution.  These semantics
 * are now tighter:
 *
 *   1. At most one invocation of ddi_periodic_delete(9F) will actually
 *      perform the deletion, all others will return immediately.
 *   2. The invocation that performs the deletion will _block_ until
 *      the handler is no longer running, and all resources have been
 *      released.
 *
 * We affect this model by removing the cancelling periodic from the
 * global list and marking it DPF_CANCELLED.  This will prevent further
 * execution of the handler.  We then wait on a CV until the DPF_EXECUTING
 * and DPF_DISPATCHED flags are clear, which means the periodic is removed
 * from all request queues, is no longer executing, and may be freed.  At this
 * point we return the opaque ID to the id_space and free the memory.
 *
 * NOTE:
 * The ddi_periodic_add(9F) interface is presently limited to a minimum period
 * of 10ms between firings.
 */

/*
 * Tuneables:
 */
int ddi_periodic_max_id = 1024;
int ddi_periodic_taskq_threadcount = 4;
hrtime_t ddi_periodic_resolution = 10000000;

/*
 * Globals:
 */
static kmem_cache_t *periodic_cache;
static id_space_t *periodic_id_space;
static taskq_t *periodic_taskq;

/*
 * periodics_lock protects the list of all periodics (periodics), and
 * each of the soft interrupt request queues (periodic_softint_queue).
 *
 * Do not hold an individual periodic's lock while obtaining periodics_lock.
 * While in the periodic_softint_queue list, the periodic will be marked
 * DPF_DISPATCHED, and thus safe from frees.  Only the invocation of
 * i_untimeout() that removes the periodic from the global list is allowed
 * to free it.
 */
static kmutex_t periodics_lock;
static list_t periodics;
static list_t periodic_softint_queue[10]; /* for IPL1 up to IPL10 */

typedef enum periodic_ipl {
	PERI_IPL_0 = 0,
	PERI_IPL_1,
	PERI_IPL_2,
	PERI_IPL_3,
	PERI_IPL_4,
	PERI_IPL_5,
	PERI_IPL_6,
	PERI_IPL_7,
	PERI_IPL_8,
	PERI_IPL_9,
	PERI_IPL_10
} periodic_ipl_t;

static char *
periodic_handler_symbol(ddi_periodic_impl_t *dpr)
{
	ulong_t off;

	return (kobj_getsymname((uintptr_t)dpr->dpr_handler, &off));
}

/*
 * This function may be called either from a soft interrupt handler
 * (ddi_periodic_softintr), or as a taskq worker function.
 */
static void
periodic_execute(void *arg)
{
	ddi_periodic_impl_t *dpr = arg;
	mutex_enter(&dpr->dpr_lock);

	/*
	 * We must be DISPATCHED, but not yet EXECUTING:
	 */
	VERIFY((dpr->dpr_flags & (DPF_DISPATCHED | DPF_EXECUTING)) ==
	    DPF_DISPATCHED);
	VERIFY(dpr->dpr_thread == NULL);

	if (!(dpr->dpr_flags & DPF_CANCELLED)) {
		int level = dpr->dpr_level;
		uint64_t count = dpr->dpr_fire_count;
		/*
		 * If we have not yet been cancelled, then
		 * mark us executing:
		 */
		dpr->dpr_flags |= DPF_EXECUTING;
		dpr->dpr_thread = curthread;
		mutex_exit(&dpr->dpr_lock);

		/*
		 * Execute the handler, without holding locks:
		 */
		DTRACE_PROBE4(ddi__periodic__execute, void *, dpr->dpr_handler,
		    void *, dpr->dpr_arg, int, level, uint64_t, count);
		(*dpr->dpr_handler)(dpr->dpr_arg);
		DTRACE_PROBE4(ddi__periodic__done, void *, dpr->dpr_handler,
		    void *, dpr->dpr_arg, int, level, uint64_t, count);

		mutex_enter(&dpr->dpr_lock);
		dpr->dpr_thread = NULL;
		dpr->dpr_fire_count++;
	}

	/*
	 * We're done with this periodic for now, so release it and
	 * wake anybody that was waiting for us to be finished:
	 */
	dpr->dpr_flags &= ~(DPF_DISPATCHED | DPF_EXECUTING);
	cv_broadcast(&dpr->dpr_cv);
	mutex_exit(&dpr->dpr_lock);
}

void
ddi_periodic_softintr(int level)
{
	ddi_periodic_impl_t *dpr;
	VERIFY(level >= PERI_IPL_1 && level <= PERI_IPL_10);

	mutex_enter(&periodics_lock);
	/*
	 * Pull the first scheduled periodic off the queue for this priority
	 * level:
	 */
	while ((dpr = list_remove_head(&periodic_softint_queue[level - 1])) !=
	    NULL) {
		mutex_exit(&periodics_lock);
		/*
		 * And execute it:
		 */
		periodic_execute(dpr);
		mutex_enter(&periodics_lock);
	}
	mutex_exit(&periodics_lock);
}

void
ddi_periodic_init(void)
{
	int i;

	/*
	 * Create a kmem_cache for request tracking objects, and a list
	 * to store them in so we can later delete based on opaque handles:
	 */
	periodic_cache = kmem_cache_create("ddi_periodic",
	    sizeof (ddi_periodic_impl_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
	list_create(&periodics, sizeof (ddi_periodic_impl_t),
	    offsetof(ddi_periodic_impl_t, dpr_link));

	/*
	 * Initialise the identifier space for ddi_periodic_add(9F):
	 */
	periodic_id_space = id_space_create("ddi_periodic", 1,
	    ddi_periodic_max_id);

	/*
	 * Initialise the request queue for each soft interrupt level:
	 */
	for (i = PERI_IPL_1; i <= PERI_IPL_10; i++) {
		list_create(&periodic_softint_queue[i - 1],
		    sizeof (ddi_periodic_impl_t), offsetof(ddi_periodic_impl_t,
		    dpr_softint_link));
	}

	/*
	 * Create the taskq for running PERI_IPL_0 handlers.  This taskq will
	 * _only_ be used with taskq_dispatch_ent(), and a taskq_ent_t
	 * pre-allocated with the ddi_periodic_impl_t.
	 */
	periodic_taskq = taskq_create_instance("ddi_periodic_taskq", -1,
	    ddi_periodic_taskq_threadcount, maxclsyspri, 0, 0, 0);

	/*
	 * Initialize the mutex lock used for the soft interrupt request
	 * queues.
	 */
	mutex_init(&periodics_lock, NULL, MUTEX_ADAPTIVE, NULL);
}

void
ddi_periodic_fini(void)
{
	int i;
	ddi_periodic_impl_t *dpr;

	/*
	 * Find all periodics that have not yet been unregistered and,
	 * on DEBUG bits, print a warning about this resource leak.
	 */
	mutex_enter(&periodics_lock);
	while ((dpr = list_head(&periodics)) != NULL) {
#ifdef	DEBUG
		printf("DDI periodic handler not deleted (id=%lx, hdlr=%s)\n",
		    (unsigned long)dpr->dpr_id, periodic_handler_symbol(dpr));
#endif

		mutex_exit(&periodics_lock);
		/*
		 * Delete the periodic ourselves:
		 */
		i_untimeout((timeout_t)(uintptr_t)dpr->dpr_id);
		mutex_enter(&periodics_lock);
	}
	mutex_exit(&periodics_lock);

	/*
	 * At this point there are no remaining cyclics, so clean up the
	 * remaining resources:
	 */
	taskq_destroy(periodic_taskq);
	periodic_taskq = NULL;

	id_space_destroy(periodic_id_space);
	periodic_id_space = NULL;

	kmem_cache_destroy(periodic_cache);
	periodic_cache = NULL;

	list_destroy(&periodics);
	for (i = PERI_IPL_1; i <= PERI_IPL_10; i++)
		list_destroy(&periodic_softint_queue[i - 1]);

	mutex_destroy(&periodics_lock);
}

static void
periodic_cyclic_handler(void *arg)
{
	ddi_periodic_impl_t *dpr = arg;

	mutex_enter(&dpr->dpr_lock);
	/*
	 * If we've been cancelled, or we're already dispatched, then exit
	 * immediately:
	 */
	if (dpr->dpr_flags & (DPF_CANCELLED | DPF_DISPATCHED)) {
		mutex_exit(&dpr->dpr_lock);
		return;
	}
	VERIFY(!(dpr->dpr_flags & DPF_EXECUTING));

	/*
	 * This periodic is not presently dispatched, so dispatch it now:
	 */
	dpr->dpr_flags |= DPF_DISPATCHED;
	mutex_exit(&dpr->dpr_lock);

	if (dpr->dpr_level == PERI_IPL_0) {
		/*
		 * DDI_IPL_0 periodics are dispatched onto the taskq:
		 */
		taskq_dispatch_ent(periodic_taskq, periodic_execute,
		    dpr, 0, &dpr->dpr_taskq_ent);
	} else {
		/*
		 * Higher priority periodics are handled by a soft interrupt
		 * handler.  Enqueue us for processing by the handler:
		 */
		mutex_enter(&periodics_lock);
		list_insert_tail(&periodic_softint_queue[dpr->dpr_level - 1],
		    dpr);
		mutex_exit(&periodics_lock);

		/*
		 * Request the execution of the soft interrupt handler for this
		 * periodic's priority level.
		 */
		sir_on(dpr->dpr_level);
	}
}

static void
periodic_destroy(ddi_periodic_impl_t *dpr)
{
	if (dpr == NULL)
		return;

	/*
	 * By now, we should have a periodic that is not busy, and has been
	 * cancelled:
	 */
	VERIFY(dpr->dpr_flags == DPF_CANCELLED);
	VERIFY(dpr->dpr_thread == NULL);

	id_free(periodic_id_space, dpr->dpr_id);
	cv_destroy(&dpr->dpr_cv);
	mutex_destroy(&dpr->dpr_lock);
	kmem_cache_free(periodic_cache, dpr);
}

static ddi_periodic_impl_t *
periodic_create(void)
{
	ddi_periodic_impl_t *dpr;

	dpr = kmem_cache_alloc(periodic_cache, KM_SLEEP);
	bzero(dpr, sizeof (*dpr));
	dpr->dpr_id = id_alloc(periodic_id_space);
	mutex_init(&dpr->dpr_lock, NULL, MUTEX_ADAPTIVE, NULL);
	cv_init(&dpr->dpr_cv, NULL, CV_DEFAULT, NULL);

	return (dpr);
}

/*
 * This function provides the implementation for the ddi_periodic_add(9F)
 * interface.  It registers a periodic handler and returns an opaque identifier
 * that can be unregistered via ddi_periodic_delete(9F)/i_untimeout().
 *
 * It may be called in user or kernel context, provided cpu_lock is not held.
 */
timeout_t
i_timeout(void (*func)(void *), void *arg, hrtime_t interval, int level)
{
	cyc_handler_t cyh;
	cyc_time_t cyt;
	ddi_periodic_impl_t *dpr;

	VERIFY(func != NULL);
	VERIFY(level >= 0 && level <= 10);

	/*
	 * Allocate object to track this periodic:
	 */
	dpr = periodic_create();
	dpr->dpr_level = level;
	dpr->dpr_handler = func;
	dpr->dpr_arg = arg;

	/*
	 * The minimum supported interval between firings of the periodic
	 * handler is 10ms; see ddi_periodic_add(9F) for more details.  If a
	 * shorter interval is requested, round up.
	 */
	if (ddi_periodic_resolution > interval) {
		cmn_err(CE_WARN,
		    "The periodic timeout (handler=%s, interval=%lld) "
		    "requests a finer interval than the supported resolution. "
		    "It rounds up to %lld\n", periodic_handler_symbol(dpr),
		    interval, ddi_periodic_resolution);
		interval = ddi_periodic_resolution;
	}

	/*
	 * Ensure that the interval is an even multiple of the base resolution
	 * that is at least as long as the requested interval.
	 */
	dpr->dpr_interval = roundup(interval, ddi_periodic_resolution);

	/*
	 * Create the underlying cyclic:
	 */
	cyh.cyh_func = periodic_cyclic_handler;
	cyh.cyh_arg = dpr;
	cyh.cyh_level = CY_LOCK_LEVEL;

	cyt.cyt_when = 0;
	cyt.cyt_interval = dpr->dpr_interval;

	mutex_enter(&cpu_lock);
	dpr->dpr_cyclic_id = cyclic_add(&cyh, &cyt);
	mutex_exit(&cpu_lock);

	/*
	 * Make the id visible to ddi_periodic_delete(9F) before we
	 * return it:
	 */
	mutex_enter(&periodics_lock);
	list_insert_tail(&periodics, dpr);
	mutex_exit(&periodics_lock);

	return ((timeout_t)(uintptr_t)dpr->dpr_id);
}

/*
 * This function provides the implementation for the ddi_periodic_delete(9F)
 * interface.  It cancels a periodic handler previously registered through
 * ddi_periodic_add(9F)/i_timeout().
 *
 * It may be called in user or kernel context, provided cpu_lock is not held.
 * It may NOT be called from within a periodic handler.
 */
void
i_untimeout(timeout_t id)
{
	ddi_periodic_impl_t *dpr;

	/*
	 * Find the periodic in the list of all periodics and remove it.
	 * If we find in (and remove it from) the global list, we have
	 * license to free it once it is no longer busy.
	 */
	mutex_enter(&periodics_lock);
	for (dpr = list_head(&periodics); dpr != NULL; dpr =
	    list_next(&periodics, dpr)) {
		if (dpr->dpr_id == (id_t)(uintptr_t)id) {
			list_remove(&periodics, dpr);
			break;
		}
	}
	mutex_exit(&periodics_lock);

	/*
	 * We could not find a periodic for this id, so bail out:
	 */
	if (dpr == NULL)
		return;

	mutex_enter(&dpr->dpr_lock);
	/*
	 * We should be the only one trying to cancel this periodic:
	 */
	VERIFY(!(dpr->dpr_flags & DPF_CANCELLED));
	/*
	 * Removing a periodic from within its own handler function will
	 * cause a deadlock, so panic explicitly.
	 */
	if (dpr->dpr_thread == curthread) {
		panic("ddi_periodic_delete(%lx) called from its own handler\n",
		    (unsigned long)dpr->dpr_id);
	}
	/*
	 * Mark the periodic as cancelled:
	 */
	dpr->dpr_flags |= DPF_CANCELLED;
	mutex_exit(&dpr->dpr_lock);

	/*
	 * Cancel our cyclic.  cyclic_remove() guarantees that the cyclic
	 * handler will not run again after it returns.  Note that the cyclic
	 * handler merely _dispatches_ the periodic, so this does _not_ mean
	 * the periodic handler is also finished running.
	 */
	mutex_enter(&cpu_lock);
	cyclic_remove(dpr->dpr_cyclic_id);
	mutex_exit(&cpu_lock);

	/*
	 * Wait until the periodic handler is no longer running:
	 */
	mutex_enter(&dpr->dpr_lock);
	while (dpr->dpr_flags & (DPF_DISPATCHED | DPF_EXECUTING)) {
		cv_wait(&dpr->dpr_cv, &dpr->dpr_lock);
	}
	mutex_exit(&dpr->dpr_lock);

	periodic_destroy(dpr);
}