marker.c 23.3 KB
Newer Older
M
Mathieu Desnoyers 已提交
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
/*
 * Copyright (C) 2007 Mathieu Desnoyers
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/types.h>
#include <linux/jhash.h>
#include <linux/list.h>
#include <linux/rcupdate.h>
#include <linux/marker.h>
#include <linux/err.h>
26
#include <linux/slab.h>
M
Mathieu Desnoyers 已提交
27 28 29 30

extern struct marker __start___markers[];
extern struct marker __stop___markers[];

31
/* Set to 1 to enable marker debug output */
A
Adrian Bunk 已提交
32
static const int marker_debug;
33

M
Mathieu Desnoyers 已提交
34
/*
35
 * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
36
 * and module markers and the hash table.
M
Mathieu Desnoyers 已提交
37 38 39 40 41 42 43 44 45
 */
static DEFINE_MUTEX(markers_mutex);

/*
 * Marker hash table, containing the active markers.
 * Protected by module_mutex.
 */
#define MARKER_HASH_BITS 6
#define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
46
static struct hlist_head marker_table[MARKER_TABLE_SIZE];
M
Mathieu Desnoyers 已提交
47

48 49 50 51 52 53 54 55
/*
 * Note about RCU :
 * It is used to make sure every handler has finished using its private data
 * between two consecutive operation (add or remove) on a given marker.  It is
 * also used to delay the free of multiple probes array until a quiescent state
 * is reached.
 * marker entries modifications are protected by the markers_mutex.
 */
M
Mathieu Desnoyers 已提交
56 57 58
struct marker_entry {
	struct hlist_node hlist;
	char *format;
59 60
			/* Probe wrapper */
	void (*call)(const struct marker *mdata, void *call_private, ...);
61 62
	struct marker_probe_closure single;
	struct marker_probe_closure *multi;
M
Mathieu Desnoyers 已提交
63
	int refcount;	/* Number of times armed. 0 if disarmed. */
64 65
	struct rcu_head rcu;
	void *oldptr;
66
	int rcu_pending;
67
	unsigned char ptype:1;
68
	unsigned char format_allocated:1;
M
Mathieu Desnoyers 已提交
69 70 71 72 73
	char name[0];	/* Contains name'\0'format'\0' */
};

/**
 * __mark_empty_function - Empty probe callback
74 75
 * @probe_private: probe private data
 * @call_private: call site private data
M
Mathieu Desnoyers 已提交
76 77 78 79 80 81 82 83
 * @fmt: format string
 * @...: variable argument list
 *
 * Empty callback provided as a probe to the markers. By providing this to a
 * disabled marker, we make sure the  execution flow is always valid even
 * though the function pointer change and the marker enabling are two distinct
 * operations that modifies the execution flow of preemptible code.
 */
84
notrace void __mark_empty_function(void *probe_private, void *call_private,
85
	const char *fmt, va_list *args)
M
Mathieu Desnoyers 已提交
86 87 88 89
{
}
EXPORT_SYMBOL_GPL(__mark_empty_function);

90 91 92 93 94 95 96 97 98 99
/*
 * marker_probe_cb Callback that prepares the variable argument list for probes.
 * @mdata: pointer of type struct marker
 * @call_private: caller site private data
 * @...:  Variable argument list.
 *
 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
 * need to put a full smp_rmb() in this branch. This is why we do not use
 * rcu_dereference() for the pointer read.
 */
100 101
notrace void marker_probe_cb(const struct marker *mdata,
		void *call_private, ...)
102 103 104 105 106
{
	va_list args;
	char ptype;

	/*
107 108 109
	 * rcu_read_lock_sched does two things : disabling preemption to make
	 * sure the teardown of the callbacks can be done correctly when they
	 * are in modules and they insure RCU read coherency.
110
	 */
111
	rcu_read_lock_sched_notrace();
M
Mathieu Desnoyers 已提交
112
	ptype = mdata->ptype;
113 114 115 116 117
	if (likely(!ptype)) {
		marker_probe_func *func;
		/* Must read the ptype before ptr. They are not data dependant,
		 * so we put an explicit smp_rmb() here. */
		smp_rmb();
M
Mathieu Desnoyers 已提交
118
		func = mdata->single.func;
119 120 121
		/* Must read the ptr before private data. They are not data
		 * dependant, so we put an explicit smp_rmb() here. */
		smp_rmb();
122 123 124
		va_start(args, call_private);
		func(mdata->single.probe_private, call_private, mdata->format,
			&args);
125 126 127 128
		va_end(args);
	} else {
		struct marker_probe_closure *multi;
		int i;
129 130 131 132 133
		/*
		 * Read mdata->ptype before mdata->multi.
		 */
		smp_rmb();
		multi = mdata->multi;
134 135 136 137 138 139 140 141 142
		/*
		 * multi points to an array, therefore accessing the array
		 * depends on reading multi. However, even in this case,
		 * we must insure that the pointer is read _before_ the array
		 * data. Same as rcu_dereference, but we need a full smp_rmb()
		 * in the fast path, so put the explicit barrier here.
		 */
		smp_read_barrier_depends();
		for (i = 0; multi[i].func; i++) {
143 144 145
			va_start(args, call_private);
			multi[i].func(multi[i].probe_private, call_private,
				mdata->format, &args);
146 147 148
			va_end(args);
		}
	}
149
	rcu_read_unlock_sched_notrace();
150 151 152 153 154 155 156 157 158 159 160
}
EXPORT_SYMBOL_GPL(marker_probe_cb);

/*
 * marker_probe_cb Callback that does not prepare the variable argument list.
 * @mdata: pointer of type struct marker
 * @call_private: caller site private data
 * @...:  Variable argument list.
 *
 * Should be connected to markers "MARK_NOARGS".
 */
161 162
static notrace void marker_probe_cb_noarg(const struct marker *mdata,
		void *call_private, ...)
163 164 165 166
{
	va_list args;	/* not initialized */
	char ptype;

167
	rcu_read_lock_sched_notrace();
M
Mathieu Desnoyers 已提交
168
	ptype = mdata->ptype;
169 170 171 172 173
	if (likely(!ptype)) {
		marker_probe_func *func;
		/* Must read the ptype before ptr. They are not data dependant,
		 * so we put an explicit smp_rmb() here. */
		smp_rmb();
M
Mathieu Desnoyers 已提交
174
		func = mdata->single.func;
175 176 177
		/* Must read the ptr before private data. They are not data
		 * dependant, so we put an explicit smp_rmb() here. */
		smp_rmb();
178 179
		func(mdata->single.probe_private, call_private, mdata->format,
			&args);
180 181 182
	} else {
		struct marker_probe_closure *multi;
		int i;
183 184 185 186 187
		/*
		 * Read mdata->ptype before mdata->multi.
		 */
		smp_rmb();
		multi = mdata->multi;
188 189 190 191 192 193 194 195 196
		/*
		 * multi points to an array, therefore accessing the array
		 * depends on reading multi. However, even in this case,
		 * we must insure that the pointer is read _before_ the array
		 * data. Same as rcu_dereference, but we need a full smp_rmb()
		 * in the fast path, so put the explicit barrier here.
		 */
		smp_read_barrier_depends();
		for (i = 0; multi[i].func; i++)
197 198
			multi[i].func(multi[i].probe_private, call_private,
				mdata->format, &args);
199
	}
200
	rcu_read_unlock_sched_notrace();
201 202
}

203 204 205 206 207 208 209 210 211 212
static void free_old_closure(struct rcu_head *head)
{
	struct marker_entry *entry = container_of(head,
		struct marker_entry, rcu);
	kfree(entry->oldptr);
	/* Make sure we free the data before setting the pending flag to 0 */
	smp_wmb();
	entry->rcu_pending = 0;
}

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
static void debug_print_probes(struct marker_entry *entry)
{
	int i;

	if (!marker_debug)
		return;

	if (!entry->ptype) {
		printk(KERN_DEBUG "Single probe : %p %p\n",
			entry->single.func,
			entry->single.probe_private);
	} else {
		for (i = 0; entry->multi[i].func; i++)
			printk(KERN_DEBUG "Multi probe %d : %p %p\n", i,
				entry->multi[i].func,
				entry->multi[i].probe_private);
	}
}

static struct marker_probe_closure *
marker_entry_add_probe(struct marker_entry *entry,
		marker_probe_func *probe, void *probe_private)
{
	int nr_probes = 0;
	struct marker_probe_closure *old, *new;

	WARN_ON(!probe);

	debug_print_probes(entry);
	old = entry->multi;
	if (!entry->ptype) {
		if (entry->single.func == probe &&
				entry->single.probe_private == probe_private)
			return ERR_PTR(-EBUSY);
		if (entry->single.func == __mark_empty_function) {
			/* 0 -> 1 probes */
			entry->single.func = probe;
			entry->single.probe_private = probe_private;
			entry->refcount = 1;
			entry->ptype = 0;
			debug_print_probes(entry);
			return NULL;
		} else {
			/* 1 -> 2 probes */
			nr_probes = 1;
			old = NULL;
		}
	} else {
		/* (N -> N+1), (N != 0, 1) probes */
		for (nr_probes = 0; old[nr_probes].func; nr_probes++)
			if (old[nr_probes].func == probe
					&& old[nr_probes].probe_private
						== probe_private)
				return ERR_PTR(-EBUSY);
	}
	/* + 2 : one for new probe, one for NULL func */
	new = kzalloc((nr_probes + 2) * sizeof(struct marker_probe_closure),
			GFP_KERNEL);
	if (new == NULL)
		return ERR_PTR(-ENOMEM);
	if (!old)
		new[0] = entry->single;
	else
		memcpy(new, old,
			nr_probes * sizeof(struct marker_probe_closure));
	new[nr_probes].func = probe;
	new[nr_probes].probe_private = probe_private;
	entry->refcount = nr_probes + 1;
	entry->multi = new;
	entry->ptype = 1;
	debug_print_probes(entry);
	return old;
}

static struct marker_probe_closure *
marker_entry_remove_probe(struct marker_entry *entry,
		marker_probe_func *probe, void *probe_private)
{
	int nr_probes = 0, nr_del = 0, i;
	struct marker_probe_closure *old, *new;

	old = entry->multi;

	debug_print_probes(entry);
	if (!entry->ptype) {
		/* 0 -> N is an error */
		WARN_ON(entry->single.func == __mark_empty_function);
		/* 1 -> 0 probes */
		WARN_ON(probe && entry->single.func != probe);
		WARN_ON(entry->single.probe_private != probe_private);
		entry->single.func = __mark_empty_function;
		entry->refcount = 0;
		entry->ptype = 0;
		debug_print_probes(entry);
		return NULL;
	} else {
		/* (N -> M), (N > 1, M >= 0) probes */
		for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
			if ((!probe || old[nr_probes].func == probe)
					&& old[nr_probes].probe_private
						== probe_private)
				nr_del++;
		}
	}

	if (nr_probes - nr_del == 0) {
		/* N -> 0, (N > 1) */
		entry->single.func = __mark_empty_function;
		entry->refcount = 0;
		entry->ptype = 0;
	} else if (nr_probes - nr_del == 1) {
		/* N -> 1, (N > 1) */
		for (i = 0; old[i].func; i++)
			if ((probe && old[i].func != probe) ||
					old[i].probe_private != probe_private)
				entry->single = old[i];
		entry->refcount = 1;
		entry->ptype = 0;
	} else {
		int j = 0;
		/* N -> M, (N > 1, M > 1) */
		/* + 1 for NULL */
		new = kzalloc((nr_probes - nr_del + 1)
			* sizeof(struct marker_probe_closure), GFP_KERNEL);
		if (new == NULL)
			return ERR_PTR(-ENOMEM);
		for (i = 0; old[i].func; i++)
			if ((probe && old[i].func != probe) ||
					old[i].probe_private != probe_private)
				new[j++] = old[i];
		entry->refcount = nr_probes - nr_del;
		entry->ptype = 1;
		entry->multi = new;
	}
	debug_print_probes(entry);
	return old;
}

M
Mathieu Desnoyers 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
/*
 * Get marker if the marker is present in the marker hash table.
 * Must be called with markers_mutex held.
 * Returns NULL if not present.
 */
static struct marker_entry *get_marker(const char *name)
{
	struct hlist_head *head;
	struct hlist_node *node;
	struct marker_entry *e;
	u32 hash = jhash(name, strlen(name), 0);

	head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
	hlist_for_each_entry(e, node, head, hlist) {
		if (!strcmp(name, e->name))
			return e;
	}
	return NULL;
}

/*
 * Add the marker to the marker hash table. Must be called with markers_mutex
 * held.
 */
375
static struct marker_entry *add_marker(const char *name, const char *format)
M
Mathieu Desnoyers 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389
{
	struct hlist_head *head;
	struct hlist_node *node;
	struct marker_entry *e;
	size_t name_len = strlen(name) + 1;
	size_t format_len = 0;
	u32 hash = jhash(name, name_len-1, 0);

	if (format)
		format_len = strlen(format) + 1;
	head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
	hlist_for_each_entry(e, node, head, hlist) {
		if (!strcmp(name, e->name)) {
			printk(KERN_NOTICE
390 391
				"Marker %s busy\n", name);
			return ERR_PTR(-EBUSY);	/* Already there */
M
Mathieu Desnoyers 已提交
392 393 394 395 396 397 398 399 400
		}
	}
	/*
	 * Using kmalloc here to allocate a variable length element. Could
	 * cause some memory fragmentation if overused.
	 */
	e = kmalloc(sizeof(struct marker_entry) + name_len + format_len,
			GFP_KERNEL);
	if (!e)
401
		return ERR_PTR(-ENOMEM);
M
Mathieu Desnoyers 已提交
402 403 404 405
	memcpy(&e->name[0], name, name_len);
	if (format) {
		e->format = &e->name[name_len];
		memcpy(e->format, format, format_len);
406 407 408 409
		if (strcmp(e->format, MARK_NOARGS) == 0)
			e->call = marker_probe_cb_noarg;
		else
			e->call = marker_probe_cb;
M
Mathieu Desnoyers 已提交
410 411
		trace_mark(core_marker_format, "name %s format %s",
				e->name, e->format);
412
	} else {
M
Mathieu Desnoyers 已提交
413
		e->format = NULL;
414 415 416 417 418 419
		e->call = marker_probe_cb;
	}
	e->single.func = __mark_empty_function;
	e->single.probe_private = NULL;
	e->multi = NULL;
	e->ptype = 0;
420
	e->format_allocated = 0;
M
Mathieu Desnoyers 已提交
421
	e->refcount = 0;
422
	e->rcu_pending = 0;
M
Mathieu Desnoyers 已提交
423
	hlist_add_head(&e->hlist, head);
424
	return e;
M
Mathieu Desnoyers 已提交
425 426 427 428 429 430
}

/*
 * Remove the marker from the marker hash table. Must be called with mutex_lock
 * held.
 */
431
static int remove_marker(const char *name)
M
Mathieu Desnoyers 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
{
	struct hlist_head *head;
	struct hlist_node *node;
	struct marker_entry *e;
	int found = 0;
	size_t len = strlen(name) + 1;
	u32 hash = jhash(name, len-1, 0);

	head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
	hlist_for_each_entry(e, node, head, hlist) {
		if (!strcmp(name, e->name)) {
			found = 1;
			break;
		}
	}
447 448 449 450 451
	if (!found)
		return -ENOENT;
	if (e->single.func != __mark_empty_function)
		return -EBUSY;
	hlist_del(&e->hlist);
452 453
	if (e->format_allocated)
		kfree(e->format);
454 455 456
	/* Make sure the call_rcu has been executed */
	if (e->rcu_pending)
		rcu_barrier_sched();
457 458
	kfree(e);
	return 0;
M
Mathieu Desnoyers 已提交
459 460 461 462 463
}

/*
 * Set the mark_entry format to the format found in the element.
 */
464
static int marker_set_format(struct marker_entry *entry, const char *format)
M
Mathieu Desnoyers 已提交
465
{
466 467
	entry->format = kstrdup(format, GFP_KERNEL);
	if (!entry->format)
M
Mathieu Desnoyers 已提交
468
		return -ENOMEM;
469 470
	entry->format_allocated = 1;

M
Mathieu Desnoyers 已提交
471
	trace_mark(core_marker_format, "name %s format %s",
472
			entry->name, entry->format);
M
Mathieu Desnoyers 已提交
473 474 475 476 477 478
	return 0;
}

/*
 * Sets the probe callback corresponding to one marker.
 */
479
static int set_marker(struct marker_entry *entry, struct marker *elem,
480
		int active)
M
Mathieu Desnoyers 已提交
481 482
{
	int ret;
483
	WARN_ON(strcmp(entry->name, elem->name) != 0);
M
Mathieu Desnoyers 已提交
484

485 486
	if (entry->format) {
		if (strcmp(entry->format, elem->format) != 0) {
M
Mathieu Desnoyers 已提交
487 488 489
			printk(KERN_NOTICE
				"Format mismatch for probe %s "
				"(%s), marker (%s)\n",
490 491
				entry->name,
				entry->format,
M
Mathieu Desnoyers 已提交
492 493 494 495 496 497 498 499
				elem->format);
			return -EPERM;
		}
	} else {
		ret = marker_set_format(entry, elem->format);
		if (ret)
			return ret;
	}
500 501 502 503 504 505 506

	/*
	 * probe_cb setup (statically known) is done here. It is
	 * asynchronous with the rest of execution, therefore we only
	 * pass from a "safe" callback (with argument) to an "unsafe"
	 * callback (does not set arguments).
	 */
507
	elem->call = entry->call;
508 509 510 511 512 513
	/*
	 * Sanity check :
	 * We only update the single probe private data when the ptr is
	 * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
	 */
	WARN_ON(elem->single.func != __mark_empty_function
514 515 516
		&& elem->single.probe_private != entry->single.probe_private
		&& !elem->ptype);
	elem->single.probe_private = entry->single.probe_private;
517 518 519 520 521
	/*
	 * Make sure the private data is valid when we update the
	 * single probe ptr.
	 */
	smp_wmb();
522
	elem->single.func = entry->single.func;
523 524 525 526
	/*
	 * We also make sure that the new probe callbacks array is consistent
	 * before setting a pointer to it.
	 */
527
	rcu_assign_pointer(elem->multi, entry->multi);
528 529 530 531 532
	/*
	 * Update the function or multi probe array pointer before setting the
	 * ptype.
	 */
	smp_wmb();
533
	elem->ptype = entry->ptype;
534 535
	elem->state = active;

M
Mathieu Desnoyers 已提交
536 537 538 539 540
	return 0;
}

/*
 * Disable a marker and its probe callback.
541 542
 * Note: only waiting an RCU period after setting elem->call to the empty
 * function insures that the original callback is not used anymore. This insured
543
 * by rcu_read_lock_sched around the call site.
M
Mathieu Desnoyers 已提交
544 545 546
 */
static void disable_marker(struct marker *elem)
{
547
	/* leave "call" as is. It is known statically. */
M
Mathieu Desnoyers 已提交
548
	elem->state = 0;
549 550 551 552
	elem->single.func = __mark_empty_function;
	/* Update the function before setting the ptype */
	smp_wmb();
	elem->ptype = 0;	/* single probe */
M
Mathieu Desnoyers 已提交
553 554
	/*
	 * Leave the private data and id there, because removal is racy and
555 556
	 * should be done only after an RCU period. These are never used until
	 * the next initialization anyway.
M
Mathieu Desnoyers 已提交
557 558 559 560 561 562 563 564 565 566 567
	 */
}

/**
 * marker_update_probe_range - Update a probe range
 * @begin: beginning of the range
 * @end: end of the range
 *
 * Updates the probe callback corresponding to a range of markers.
 */
void marker_update_probe_range(struct marker *begin,
568
	struct marker *end)
M
Mathieu Desnoyers 已提交
569 570 571 572
{
	struct marker *iter;
	struct marker_entry *mark_entry;

573
	mutex_lock(&markers_mutex);
M
Mathieu Desnoyers 已提交
574 575
	for (iter = begin; iter < end; iter++) {
		mark_entry = get_marker(iter->name);
576
		if (mark_entry) {
577
			set_marker(mark_entry, iter, !!mark_entry->refcount);
M
Mathieu Desnoyers 已提交
578 579 580 581 582 583 584
			/*
			 * ignore error, continue
			 */
		} else {
			disable_marker(iter);
		}
	}
585
	mutex_unlock(&markers_mutex);
M
Mathieu Desnoyers 已提交
586 587 588 589
}

/*
 * Update probes, removing the faulty probes.
590 591 592 593 594 595 596 597 598 599 600 601 602 603
 *
 * Internal callback only changed before the first probe is connected to it.
 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
 * transitions.  All other transitions will leave the old private data valid.
 * This makes the non-atomicity of the callback/private data updates valid.
 *
 * "special case" updates :
 * 0 -> 1 callback
 * 1 -> 0 callback
 * 1 -> 2 callbacks
 * 2 -> 1 callbacks
 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
 * Site effect : marker_set_format may delete the marker entry (creating a
 * replacement).
M
Mathieu Desnoyers 已提交
604
 */
605
static void marker_update_probes(void)
M
Mathieu Desnoyers 已提交
606 607
{
	/* Core kernel markers */
608
	marker_update_probe_range(__start___markers, __stop___markers);
M
Mathieu Desnoyers 已提交
609
	/* Markers in modules. */
610
	module_update_markers();
M
Mathieu Desnoyers 已提交
611 612 613 614 615 616 617
}

/**
 * marker_probe_register -  Connect a probe to a marker
 * @name: marker name
 * @format: format string
 * @probe: probe handler
618
 * @probe_private: probe private data
M
Mathieu Desnoyers 已提交
619 620 621
 *
 * private data must be a valid allocated memory address, or NULL.
 * Returns 0 if ok, error value on error.
622
 * The probe address must at least be aligned on the architecture pointer size.
M
Mathieu Desnoyers 已提交
623 624
 */
int marker_probe_register(const char *name, const char *format,
625
			marker_probe_func *probe, void *probe_private)
M
Mathieu Desnoyers 已提交
626 627
{
	struct marker_entry *entry;
628
	int ret = 0;
629
	struct marker_probe_closure *old;
M
Mathieu Desnoyers 已提交
630 631 632

	mutex_lock(&markers_mutex);
	entry = get_marker(name);
633 634
	if (!entry) {
		entry = add_marker(name, format);
L
Lai Jiangshan 已提交
635
		if (IS_ERR(entry))
636
			ret = PTR_ERR(entry);
L
Lai Jiangshan 已提交
637 638
	} else if (format) {
		if (!entry->format)
639
			ret = marker_set_format(entry, format);
L
Lai Jiangshan 已提交
640 641
		else if (strcmp(entry->format, format))
			ret = -EPERM;
M
Mathieu Desnoyers 已提交
642
	}
L
Lai Jiangshan 已提交
643 644 645
	if (ret)
		goto end;

646 647 648 649 650 651
	/*
	 * If we detect that a call_rcu is pending for this marker,
	 * make sure it's executed now.
	 */
	if (entry->rcu_pending)
		rcu_barrier_sched();
652 653 654
	old = marker_entry_add_probe(entry, probe, probe_private);
	if (IS_ERR(old)) {
		ret = PTR_ERR(old);
M
Mathieu Desnoyers 已提交
655
		goto end;
656
	}
657
	mutex_unlock(&markers_mutex);
M
Mathieu Desnoyers 已提交
658
	marker_update_probes();
659 660
	mutex_lock(&markers_mutex);
	entry = get_marker(name);
M
Mathieu Desnoyers 已提交
661 662
	if (!entry)
		goto end;
663 664 665 666 667 668 669
	if (entry->rcu_pending)
		rcu_barrier_sched();
	entry->oldptr = old;
	entry->rcu_pending = 1;
	/* write rcu_pending before calling the RCU callback */
	smp_wmb();
	call_rcu_sched(&entry->rcu, free_old_closure);
M
Mathieu Desnoyers 已提交
670 671 672 673 674 675 676 677 678
end:
	mutex_unlock(&markers_mutex);
	return ret;
}
EXPORT_SYMBOL_GPL(marker_probe_register);

/**
 * marker_probe_unregister -  Disconnect a probe from a marker
 * @name: marker name
679 680
 * @probe: probe function pointer
 * @probe_private: probe private data
M
Mathieu Desnoyers 已提交
681 682
 *
 * Returns the private data given to marker_probe_register, or an ERR_PTR().
683 684 685 686
 * We do not need to call a synchronize_sched to make sure the probes have
 * finished running before doing a module unload, because the module unload
 * itself uses stop_machine(), which insures that every preempt disabled section
 * have finished.
M
Mathieu Desnoyers 已提交
687
 */
688 689
int marker_probe_unregister(const char *name,
	marker_probe_func *probe, void *probe_private)
M
Mathieu Desnoyers 已提交
690 691
{
	struct marker_entry *entry;
692
	struct marker_probe_closure *old;
693
	int ret = -ENOENT;
M
Mathieu Desnoyers 已提交
694 695 696

	mutex_lock(&markers_mutex);
	entry = get_marker(name);
697
	if (!entry)
M
Mathieu Desnoyers 已提交
698
		goto end;
699 700
	if (entry->rcu_pending)
		rcu_barrier_sched();
701
	old = marker_entry_remove_probe(entry, probe, probe_private);
702
	mutex_unlock(&markers_mutex);
M
Mathieu Desnoyers 已提交
703
	marker_update_probes();
704 705
	mutex_lock(&markers_mutex);
	entry = get_marker(name);
706 707
	if (!entry)
		goto end;
708 709 710 711 712 713 714
	if (entry->rcu_pending)
		rcu_barrier_sched();
	entry->oldptr = old;
	entry->rcu_pending = 1;
	/* write rcu_pending before calling the RCU callback */
	smp_wmb();
	call_rcu_sched(&entry->rcu, free_old_closure);
715
	remove_marker(name);	/* Ignore busy error message */
716
	ret = 0;
M
Mathieu Desnoyers 已提交
717 718
end:
	mutex_unlock(&markers_mutex);
719
	return ret;
M
Mathieu Desnoyers 已提交
720 721 722
}
EXPORT_SYMBOL_GPL(marker_probe_unregister);

723 724
static struct marker_entry *
get_marker_from_private_data(marker_probe_func *probe, void *probe_private)
M
Mathieu Desnoyers 已提交
725 726 727
{
	struct marker_entry *entry;
	unsigned int i;
728 729
	struct hlist_head *head;
	struct hlist_node *node;
M
Mathieu Desnoyers 已提交
730 731 732 733

	for (i = 0; i < MARKER_TABLE_SIZE; i++) {
		head = &marker_table[i];
		hlist_for_each_entry(entry, node, head, hlist) {
734 735 736 737 738 739 740 741 742 743 744 745 746 747
			if (!entry->ptype) {
				if (entry->single.func == probe
						&& entry->single.probe_private
						== probe_private)
					return entry;
			} else {
				struct marker_probe_closure *closure;
				closure = entry->multi;
				for (i = 0; closure[i].func; i++) {
					if (closure[i].func == probe &&
							closure[i].probe_private
							== probe_private)
						return entry;
				}
M
Mathieu Desnoyers 已提交
748 749 750
			}
		}
	}
751
	return NULL;
M
Mathieu Desnoyers 已提交
752 753 754
}

/**
755 756 757
 * marker_probe_unregister_private_data -  Disconnect a probe from a marker
 * @probe: probe function
 * @probe_private: probe private data
M
Mathieu Desnoyers 已提交
758
 *
759 760 761 762 763 764 765
 * Unregister a probe by providing the registered private data.
 * Only removes the first marker found in hash table.
 * Return 0 on success or error value.
 * We do not need to call a synchronize_sched to make sure the probes have
 * finished running before doing a module unload, because the module unload
 * itself uses stop_machine(), which insures that every preempt disabled section
 * have finished.
M
Mathieu Desnoyers 已提交
766
 */
767 768
int marker_probe_unregister_private_data(marker_probe_func *probe,
		void *probe_private)
M
Mathieu Desnoyers 已提交
769 770
{
	struct marker_entry *entry;
771
	int ret = 0;
772
	struct marker_probe_closure *old;
M
Mathieu Desnoyers 已提交
773 774

	mutex_lock(&markers_mutex);
775
	entry = get_marker_from_private_data(probe, probe_private);
M
Mathieu Desnoyers 已提交
776 777 778 779
	if (!entry) {
		ret = -ENOENT;
		goto end;
	}
780 781
	if (entry->rcu_pending)
		rcu_barrier_sched();
782
	old = marker_entry_remove_probe(entry, NULL, probe_private);
M
Mathieu Desnoyers 已提交
783
	mutex_unlock(&markers_mutex);
M
Mathieu Desnoyers 已提交
784
	marker_update_probes();
M
Mathieu Desnoyers 已提交
785
	mutex_lock(&markers_mutex);
786
	entry = get_marker_from_private_data(probe, probe_private);
M
Mathieu Desnoyers 已提交
787 788
	if (!entry)
		goto end;
789 790 791 792 793 794 795
	if (entry->rcu_pending)
		rcu_barrier_sched();
	entry->oldptr = old;
	entry->rcu_pending = 1;
	/* write rcu_pending before calling the RCU callback */
	smp_wmb();
	call_rcu_sched(&entry->rcu, free_old_closure);
796
	remove_marker(entry->name);	/* Ignore busy error message */
M
Mathieu Desnoyers 已提交
797 798 799 800
end:
	mutex_unlock(&markers_mutex);
	return ret;
}
801
EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
M
Mathieu Desnoyers 已提交
802 803 804 805

/**
 * marker_get_private_data - Get a marker's probe private data
 * @name: marker name
806 807
 * @probe: probe to match
 * @num: get the nth matching probe's private data
M
Mathieu Desnoyers 已提交
808
 *
809 810
 * Returns the nth private data pointer (starting from 0) matching, or an
 * ERR_PTR.
M
Mathieu Desnoyers 已提交
811 812 813 814 815
 * Returns the private data pointer, or an ERR_PTR.
 * The private data pointer should _only_ be dereferenced if the caller is the
 * owner of the data, or its content could vanish. This is mostly used to
 * confirm that a caller is the owner of a registered probe.
 */
816 817
void *marker_get_private_data(const char *name, marker_probe_func *probe,
		int num)
M
Mathieu Desnoyers 已提交
818 819 820 821 822 823
{
	struct hlist_head *head;
	struct hlist_node *node;
	struct marker_entry *e;
	size_t name_len = strlen(name) + 1;
	u32 hash = jhash(name, name_len-1, 0);
824
	int i;
M
Mathieu Desnoyers 已提交
825 826 827 828

	head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
	hlist_for_each_entry(e, node, head, hlist) {
		if (!strcmp(name, e->name)) {
829 830 831 832 833 834 835 836 837 838 839 840 841 842
			if (!e->ptype) {
				if (num == 0 && e->single.func == probe)
					return e->single.probe_private;
			} else {
				struct marker_probe_closure *closure;
				int match = 0;
				closure = e->multi;
				for (i = 0; closure[i].func; i++) {
					if (closure[i].func != probe)
						continue;
					if (match++ == num)
						return closure[i].probe_private;
				}
			}
843
			break;
M
Mathieu Desnoyers 已提交
844 845 846 847 848
		}
	}
	return ERR_PTR(-ENOENT);
}
EXPORT_SYMBOL_GPL(marker_get_private_data);