197e1c18eSMathieu Desnoyers /* 297e1c18eSMathieu Desnoyers * Copyright (C) 2008 Mathieu Desnoyers 397e1c18eSMathieu Desnoyers * 497e1c18eSMathieu Desnoyers * This program is free software; you can redistribute it and/or modify 597e1c18eSMathieu Desnoyers * it under the terms of the GNU General Public License as published by 697e1c18eSMathieu Desnoyers * the Free Software Foundation; either version 2 of the License, or 797e1c18eSMathieu Desnoyers * (at your option) any later version. 897e1c18eSMathieu Desnoyers * 997e1c18eSMathieu Desnoyers * This program is distributed in the hope that it will be useful, 1097e1c18eSMathieu Desnoyers * but WITHOUT ANY WARRANTY; without even the implied warranty of 1197e1c18eSMathieu Desnoyers * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1297e1c18eSMathieu Desnoyers * GNU General Public License for more details. 1397e1c18eSMathieu Desnoyers * 1497e1c18eSMathieu Desnoyers * You should have received a copy of the GNU General Public License 1597e1c18eSMathieu Desnoyers * along with this program; if not, write to the Free Software 1697e1c18eSMathieu Desnoyers * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 1797e1c18eSMathieu Desnoyers */ 1897e1c18eSMathieu Desnoyers #include <linux/module.h> 1997e1c18eSMathieu Desnoyers #include <linux/mutex.h> 2097e1c18eSMathieu Desnoyers #include <linux/types.h> 2197e1c18eSMathieu Desnoyers #include <linux/jhash.h> 2297e1c18eSMathieu Desnoyers #include <linux/list.h> 2397e1c18eSMathieu Desnoyers #include <linux/rcupdate.h> 2497e1c18eSMathieu Desnoyers #include <linux/tracepoint.h> 2597e1c18eSMathieu Desnoyers #include <linux/err.h> 2697e1c18eSMathieu Desnoyers #include <linux/slab.h> 27a871bd33SJason Baron #include <linux/sched.h> 2897e1c18eSMathieu Desnoyers 2997e1c18eSMathieu Desnoyers extern struct tracepoint __start___tracepoints[]; 3097e1c18eSMathieu Desnoyers extern struct tracepoint __stop___tracepoints[]; 3197e1c18eSMathieu Desnoyers 3297e1c18eSMathieu Desnoyers /* Set to 1 to enable tracepoint debug output */ 3397e1c18eSMathieu Desnoyers static const int tracepoint_debug; 3497e1c18eSMathieu Desnoyers 3597e1c18eSMathieu Desnoyers /* 3697e1c18eSMathieu Desnoyers * tracepoints_mutex nests inside module_mutex. Tracepoints mutex protects the 3797e1c18eSMathieu Desnoyers * builtin and module tracepoints and the hash table. 3897e1c18eSMathieu Desnoyers */ 3997e1c18eSMathieu Desnoyers static DEFINE_MUTEX(tracepoints_mutex); 4097e1c18eSMathieu Desnoyers 4197e1c18eSMathieu Desnoyers /* 4297e1c18eSMathieu Desnoyers * Tracepoint hash table, containing the active tracepoints. 4397e1c18eSMathieu Desnoyers * Protected by tracepoints_mutex. 4497e1c18eSMathieu Desnoyers */ 4597e1c18eSMathieu Desnoyers #define TRACEPOINT_HASH_BITS 6 4697e1c18eSMathieu Desnoyers #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS) 4719dba33cSLai Jiangshan static struct hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE]; 4897e1c18eSMathieu Desnoyers 4997e1c18eSMathieu Desnoyers /* 5097e1c18eSMathieu Desnoyers * Note about RCU : 5197e1c18eSMathieu Desnoyers * It is used to to delay the free of multiple probes array until a quiescent 5297e1c18eSMathieu Desnoyers * state is reached. 5397e1c18eSMathieu Desnoyers * Tracepoint entries modifications are protected by the tracepoints_mutex. 5497e1c18eSMathieu Desnoyers */ 5597e1c18eSMathieu Desnoyers struct tracepoint_entry { 5697e1c18eSMathieu Desnoyers struct hlist_node hlist; 5797e1c18eSMathieu Desnoyers void **funcs; 5897e1c18eSMathieu Desnoyers int refcount; /* Number of times armed. 0 if disarmed. */ 5997e1c18eSMathieu Desnoyers char name[0]; 6097e1c18eSMathieu Desnoyers }; 6197e1c18eSMathieu Desnoyers 6219dba33cSLai Jiangshan struct tp_probes { 63127cafbbSLai Jiangshan union { 6419dba33cSLai Jiangshan struct rcu_head rcu; 65127cafbbSLai Jiangshan struct list_head list; 66127cafbbSLai Jiangshan } u; 6719dba33cSLai Jiangshan void *probes[0]; 6819dba33cSLai Jiangshan }; 6997e1c18eSMathieu Desnoyers 7019dba33cSLai Jiangshan static inline void *allocate_probes(int count) 7197e1c18eSMathieu Desnoyers { 7219dba33cSLai Jiangshan struct tp_probes *p = kmalloc(count * sizeof(void *) 7319dba33cSLai Jiangshan + sizeof(struct tp_probes), GFP_KERNEL); 7419dba33cSLai Jiangshan return p == NULL ? NULL : p->probes; 7597e1c18eSMathieu Desnoyers } 7697e1c18eSMathieu Desnoyers 7719dba33cSLai Jiangshan static void rcu_free_old_probes(struct rcu_head *head) 7897e1c18eSMathieu Desnoyers { 79127cafbbSLai Jiangshan kfree(container_of(head, struct tp_probes, u.rcu)); 8019dba33cSLai Jiangshan } 8119dba33cSLai Jiangshan 8219dba33cSLai Jiangshan static inline void release_probes(void *old) 8319dba33cSLai Jiangshan { 8419dba33cSLai Jiangshan if (old) { 8519dba33cSLai Jiangshan struct tp_probes *tp_probes = container_of(old, 8619dba33cSLai Jiangshan struct tp_probes, probes[0]); 87127cafbbSLai Jiangshan call_rcu_sched(&tp_probes->u.rcu, rcu_free_old_probes); 8819dba33cSLai Jiangshan } 8997e1c18eSMathieu Desnoyers } 9097e1c18eSMathieu Desnoyers 9197e1c18eSMathieu Desnoyers static void debug_print_probes(struct tracepoint_entry *entry) 9297e1c18eSMathieu Desnoyers { 9397e1c18eSMathieu Desnoyers int i; 9497e1c18eSMathieu Desnoyers 9519dba33cSLai Jiangshan if (!tracepoint_debug || !entry->funcs) 9697e1c18eSMathieu Desnoyers return; 9797e1c18eSMathieu Desnoyers 9897e1c18eSMathieu Desnoyers for (i = 0; entry->funcs[i]; i++) 9997e1c18eSMathieu Desnoyers printk(KERN_DEBUG "Probe %d : %p\n", i, entry->funcs[i]); 10097e1c18eSMathieu Desnoyers } 10197e1c18eSMathieu Desnoyers 10297e1c18eSMathieu Desnoyers static void * 10397e1c18eSMathieu Desnoyers tracepoint_entry_add_probe(struct tracepoint_entry *entry, void *probe) 10497e1c18eSMathieu Desnoyers { 10597e1c18eSMathieu Desnoyers int nr_probes = 0; 10697e1c18eSMathieu Desnoyers void **old, **new; 10797e1c18eSMathieu Desnoyers 10897e1c18eSMathieu Desnoyers WARN_ON(!probe); 10997e1c18eSMathieu Desnoyers 11097e1c18eSMathieu Desnoyers debug_print_probes(entry); 11197e1c18eSMathieu Desnoyers old = entry->funcs; 11297e1c18eSMathieu Desnoyers if (old) { 11397e1c18eSMathieu Desnoyers /* (N -> N+1), (N != 0, 1) probes */ 11497e1c18eSMathieu Desnoyers for (nr_probes = 0; old[nr_probes]; nr_probes++) 11597e1c18eSMathieu Desnoyers if (old[nr_probes] == probe) 11697e1c18eSMathieu Desnoyers return ERR_PTR(-EEXIST); 11797e1c18eSMathieu Desnoyers } 11897e1c18eSMathieu Desnoyers /* + 2 : one for new probe, one for NULL func */ 11919dba33cSLai Jiangshan new = allocate_probes(nr_probes + 2); 12097e1c18eSMathieu Desnoyers if (new == NULL) 12197e1c18eSMathieu Desnoyers return ERR_PTR(-ENOMEM); 12297e1c18eSMathieu Desnoyers if (old) 12397e1c18eSMathieu Desnoyers memcpy(new, old, nr_probes * sizeof(void *)); 12497e1c18eSMathieu Desnoyers new[nr_probes] = probe; 12519dba33cSLai Jiangshan new[nr_probes + 1] = NULL; 12697e1c18eSMathieu Desnoyers entry->refcount = nr_probes + 1; 12797e1c18eSMathieu Desnoyers entry->funcs = new; 12897e1c18eSMathieu Desnoyers debug_print_probes(entry); 12997e1c18eSMathieu Desnoyers return old; 13097e1c18eSMathieu Desnoyers } 13197e1c18eSMathieu Desnoyers 13297e1c18eSMathieu Desnoyers static void * 13397e1c18eSMathieu Desnoyers tracepoint_entry_remove_probe(struct tracepoint_entry *entry, void *probe) 13497e1c18eSMathieu Desnoyers { 13597e1c18eSMathieu Desnoyers int nr_probes = 0, nr_del = 0, i; 13697e1c18eSMathieu Desnoyers void **old, **new; 13797e1c18eSMathieu Desnoyers 13897e1c18eSMathieu Desnoyers old = entry->funcs; 13997e1c18eSMathieu Desnoyers 140f66af459SFrederic Weisbecker if (!old) 14119dba33cSLai Jiangshan return ERR_PTR(-ENOENT); 142f66af459SFrederic Weisbecker 14397e1c18eSMathieu Desnoyers debug_print_probes(entry); 14497e1c18eSMathieu Desnoyers /* (N -> M), (N > 1, M >= 0) probes */ 14597e1c18eSMathieu Desnoyers for (nr_probes = 0; old[nr_probes]; nr_probes++) { 14697e1c18eSMathieu Desnoyers if ((!probe || old[nr_probes] == probe)) 14797e1c18eSMathieu Desnoyers nr_del++; 14897e1c18eSMathieu Desnoyers } 14997e1c18eSMathieu Desnoyers 15097e1c18eSMathieu Desnoyers if (nr_probes - nr_del == 0) { 15197e1c18eSMathieu Desnoyers /* N -> 0, (N > 1) */ 15297e1c18eSMathieu Desnoyers entry->funcs = NULL; 15397e1c18eSMathieu Desnoyers entry->refcount = 0; 15497e1c18eSMathieu Desnoyers debug_print_probes(entry); 15597e1c18eSMathieu Desnoyers return old; 15697e1c18eSMathieu Desnoyers } else { 15797e1c18eSMathieu Desnoyers int j = 0; 15897e1c18eSMathieu Desnoyers /* N -> M, (N > 1, M > 0) */ 15997e1c18eSMathieu Desnoyers /* + 1 for NULL */ 16019dba33cSLai Jiangshan new = allocate_probes(nr_probes - nr_del + 1); 16197e1c18eSMathieu Desnoyers if (new == NULL) 16297e1c18eSMathieu Desnoyers return ERR_PTR(-ENOMEM); 16397e1c18eSMathieu Desnoyers for (i = 0; old[i]; i++) 16497e1c18eSMathieu Desnoyers if ((probe && old[i] != probe)) 16597e1c18eSMathieu Desnoyers new[j++] = old[i]; 16619dba33cSLai Jiangshan new[nr_probes - nr_del] = NULL; 16797e1c18eSMathieu Desnoyers entry->refcount = nr_probes - nr_del; 16897e1c18eSMathieu Desnoyers entry->funcs = new; 16997e1c18eSMathieu Desnoyers } 17097e1c18eSMathieu Desnoyers debug_print_probes(entry); 17197e1c18eSMathieu Desnoyers return old; 17297e1c18eSMathieu Desnoyers } 17397e1c18eSMathieu Desnoyers 17497e1c18eSMathieu Desnoyers /* 17597e1c18eSMathieu Desnoyers * Get tracepoint if the tracepoint is present in the tracepoint hash table. 17697e1c18eSMathieu Desnoyers * Must be called with tracepoints_mutex held. 17797e1c18eSMathieu Desnoyers * Returns NULL if not present. 17897e1c18eSMathieu Desnoyers */ 17997e1c18eSMathieu Desnoyers static struct tracepoint_entry *get_tracepoint(const char *name) 18097e1c18eSMathieu Desnoyers { 18197e1c18eSMathieu Desnoyers struct hlist_head *head; 18297e1c18eSMathieu Desnoyers struct hlist_node *node; 18397e1c18eSMathieu Desnoyers struct tracepoint_entry *e; 18497e1c18eSMathieu Desnoyers u32 hash = jhash(name, strlen(name), 0); 18597e1c18eSMathieu Desnoyers 1869795302aSMathieu Desnoyers head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)]; 18797e1c18eSMathieu Desnoyers hlist_for_each_entry(e, node, head, hlist) { 18897e1c18eSMathieu Desnoyers if (!strcmp(name, e->name)) 18997e1c18eSMathieu Desnoyers return e; 19097e1c18eSMathieu Desnoyers } 19197e1c18eSMathieu Desnoyers return NULL; 19297e1c18eSMathieu Desnoyers } 19397e1c18eSMathieu Desnoyers 19497e1c18eSMathieu Desnoyers /* 19597e1c18eSMathieu Desnoyers * Add the tracepoint to the tracepoint hash table. Must be called with 19697e1c18eSMathieu Desnoyers * tracepoints_mutex held. 19797e1c18eSMathieu Desnoyers */ 19897e1c18eSMathieu Desnoyers static struct tracepoint_entry *add_tracepoint(const char *name) 19997e1c18eSMathieu Desnoyers { 20097e1c18eSMathieu Desnoyers struct hlist_head *head; 20197e1c18eSMathieu Desnoyers struct hlist_node *node; 20297e1c18eSMathieu Desnoyers struct tracepoint_entry *e; 20397e1c18eSMathieu Desnoyers size_t name_len = strlen(name) + 1; 20497e1c18eSMathieu Desnoyers u32 hash = jhash(name, name_len-1, 0); 20597e1c18eSMathieu Desnoyers 2069795302aSMathieu Desnoyers head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)]; 20797e1c18eSMathieu Desnoyers hlist_for_each_entry(e, node, head, hlist) { 20897e1c18eSMathieu Desnoyers if (!strcmp(name, e->name)) { 20997e1c18eSMathieu Desnoyers printk(KERN_NOTICE 21097e1c18eSMathieu Desnoyers "tracepoint %s busy\n", name); 21197e1c18eSMathieu Desnoyers return ERR_PTR(-EEXIST); /* Already there */ 21297e1c18eSMathieu Desnoyers } 21397e1c18eSMathieu Desnoyers } 21497e1c18eSMathieu Desnoyers /* 21597e1c18eSMathieu Desnoyers * Using kmalloc here to allocate a variable length element. Could 21697e1c18eSMathieu Desnoyers * cause some memory fragmentation if overused. 21797e1c18eSMathieu Desnoyers */ 21897e1c18eSMathieu Desnoyers e = kmalloc(sizeof(struct tracepoint_entry) + name_len, GFP_KERNEL); 21997e1c18eSMathieu Desnoyers if (!e) 22097e1c18eSMathieu Desnoyers return ERR_PTR(-ENOMEM); 22197e1c18eSMathieu Desnoyers memcpy(&e->name[0], name, name_len); 22297e1c18eSMathieu Desnoyers e->funcs = NULL; 22397e1c18eSMathieu Desnoyers e->refcount = 0; 22497e1c18eSMathieu Desnoyers hlist_add_head(&e->hlist, head); 22597e1c18eSMathieu Desnoyers return e; 22697e1c18eSMathieu Desnoyers } 22797e1c18eSMathieu Desnoyers 22897e1c18eSMathieu Desnoyers /* 22997e1c18eSMathieu Desnoyers * Remove the tracepoint from the tracepoint hash table. Must be called with 23097e1c18eSMathieu Desnoyers * mutex_lock held. 23197e1c18eSMathieu Desnoyers */ 23219dba33cSLai Jiangshan static inline void remove_tracepoint(struct tracepoint_entry *e) 23397e1c18eSMathieu Desnoyers { 23497e1c18eSMathieu Desnoyers hlist_del(&e->hlist); 23597e1c18eSMathieu Desnoyers kfree(e); 23697e1c18eSMathieu Desnoyers } 23797e1c18eSMathieu Desnoyers 23897e1c18eSMathieu Desnoyers /* 23997e1c18eSMathieu Desnoyers * Sets the probe callback corresponding to one tracepoint. 24097e1c18eSMathieu Desnoyers */ 24197e1c18eSMathieu Desnoyers static void set_tracepoint(struct tracepoint_entry **entry, 24297e1c18eSMathieu Desnoyers struct tracepoint *elem, int active) 24397e1c18eSMathieu Desnoyers { 24497e1c18eSMathieu Desnoyers WARN_ON(strcmp((*entry)->name, elem->name) != 0); 24597e1c18eSMathieu Desnoyers 24697e1c18eSMathieu Desnoyers /* 24797e1c18eSMathieu Desnoyers * rcu_assign_pointer has a smp_wmb() which makes sure that the new 24897e1c18eSMathieu Desnoyers * probe callbacks array is consistent before setting a pointer to it. 24997e1c18eSMathieu Desnoyers * This array is referenced by __DO_TRACE from 25097e1c18eSMathieu Desnoyers * include/linux/tracepoints.h. A matching smp_read_barrier_depends() 25197e1c18eSMathieu Desnoyers * is used. 25297e1c18eSMathieu Desnoyers */ 25397e1c18eSMathieu Desnoyers rcu_assign_pointer(elem->funcs, (*entry)->funcs); 25497e1c18eSMathieu Desnoyers elem->state = active; 25597e1c18eSMathieu Desnoyers } 25697e1c18eSMathieu Desnoyers 25797e1c18eSMathieu Desnoyers /* 25897e1c18eSMathieu Desnoyers * Disable a tracepoint and its probe callback. 25997e1c18eSMathieu Desnoyers * Note: only waiting an RCU period after setting elem->call to the empty 26097e1c18eSMathieu Desnoyers * function insures that the original callback is not used anymore. This insured 26197e1c18eSMathieu Desnoyers * by preempt_disable around the call site. 26297e1c18eSMathieu Desnoyers */ 26397e1c18eSMathieu Desnoyers static void disable_tracepoint(struct tracepoint *elem) 26497e1c18eSMathieu Desnoyers { 26597e1c18eSMathieu Desnoyers elem->state = 0; 266de0baf9aSMathieu Desnoyers rcu_assign_pointer(elem->funcs, NULL); 26797e1c18eSMathieu Desnoyers } 26897e1c18eSMathieu Desnoyers 26997e1c18eSMathieu Desnoyers /** 27097e1c18eSMathieu Desnoyers * tracepoint_update_probe_range - Update a probe range 27197e1c18eSMathieu Desnoyers * @begin: beginning of the range 27297e1c18eSMathieu Desnoyers * @end: end of the range 27397e1c18eSMathieu Desnoyers * 27497e1c18eSMathieu Desnoyers * Updates the probe callback corresponding to a range of tracepoints. 27597e1c18eSMathieu Desnoyers */ 27609933a10SJaswinder Singh Rajput void 27709933a10SJaswinder Singh Rajput tracepoint_update_probe_range(struct tracepoint *begin, struct tracepoint *end) 27897e1c18eSMathieu Desnoyers { 27997e1c18eSMathieu Desnoyers struct tracepoint *iter; 28097e1c18eSMathieu Desnoyers struct tracepoint_entry *mark_entry; 28197e1c18eSMathieu Desnoyers 282ec625cb2SIngo Molnar if (!begin) 28309933a10SJaswinder Singh Rajput return; 28409933a10SJaswinder Singh Rajput 28597e1c18eSMathieu Desnoyers mutex_lock(&tracepoints_mutex); 28697e1c18eSMathieu Desnoyers for (iter = begin; iter < end; iter++) { 28797e1c18eSMathieu Desnoyers mark_entry = get_tracepoint(iter->name); 28897e1c18eSMathieu Desnoyers if (mark_entry) { 28997e1c18eSMathieu Desnoyers set_tracepoint(&mark_entry, iter, 29097e1c18eSMathieu Desnoyers !!mark_entry->refcount); 29197e1c18eSMathieu Desnoyers } else { 29297e1c18eSMathieu Desnoyers disable_tracepoint(iter); 29397e1c18eSMathieu Desnoyers } 29497e1c18eSMathieu Desnoyers } 29597e1c18eSMathieu Desnoyers mutex_unlock(&tracepoints_mutex); 29697e1c18eSMathieu Desnoyers } 29797e1c18eSMathieu Desnoyers 29897e1c18eSMathieu Desnoyers /* 29997e1c18eSMathieu Desnoyers * Update probes, removing the faulty probes. 30097e1c18eSMathieu Desnoyers */ 30197e1c18eSMathieu Desnoyers static void tracepoint_update_probes(void) 30297e1c18eSMathieu Desnoyers { 30397e1c18eSMathieu Desnoyers /* Core kernel tracepoints */ 30497e1c18eSMathieu Desnoyers tracepoint_update_probe_range(__start___tracepoints, 30597e1c18eSMathieu Desnoyers __stop___tracepoints); 30697e1c18eSMathieu Desnoyers /* tracepoints in modules. */ 30797e1c18eSMathieu Desnoyers module_update_tracepoints(); 30897e1c18eSMathieu Desnoyers } 30997e1c18eSMathieu Desnoyers 310127cafbbSLai Jiangshan static void *tracepoint_add_probe(const char *name, void *probe) 311127cafbbSLai Jiangshan { 312127cafbbSLai Jiangshan struct tracepoint_entry *entry; 313127cafbbSLai Jiangshan void *old; 314127cafbbSLai Jiangshan 315127cafbbSLai Jiangshan entry = get_tracepoint(name); 316127cafbbSLai Jiangshan if (!entry) { 317127cafbbSLai Jiangshan entry = add_tracepoint(name); 318127cafbbSLai Jiangshan if (IS_ERR(entry)) 319127cafbbSLai Jiangshan return entry; 320127cafbbSLai Jiangshan } 321127cafbbSLai Jiangshan old = tracepoint_entry_add_probe(entry, probe); 322127cafbbSLai Jiangshan if (IS_ERR(old) && !entry->refcount) 323127cafbbSLai Jiangshan remove_tracepoint(entry); 324127cafbbSLai Jiangshan return old; 325127cafbbSLai Jiangshan } 326127cafbbSLai Jiangshan 32797e1c18eSMathieu Desnoyers /** 32897e1c18eSMathieu Desnoyers * tracepoint_probe_register - Connect a probe to a tracepoint 32997e1c18eSMathieu Desnoyers * @name: tracepoint name 33097e1c18eSMathieu Desnoyers * @probe: probe handler 33197e1c18eSMathieu Desnoyers * 33297e1c18eSMathieu Desnoyers * Returns 0 if ok, error value on error. 33397e1c18eSMathieu Desnoyers * The probe address must at least be aligned on the architecture pointer size. 33497e1c18eSMathieu Desnoyers */ 33597e1c18eSMathieu Desnoyers int tracepoint_probe_register(const char *name, void *probe) 33697e1c18eSMathieu Desnoyers { 33797e1c18eSMathieu Desnoyers void *old; 33897e1c18eSMathieu Desnoyers 33997e1c18eSMathieu Desnoyers mutex_lock(&tracepoints_mutex); 340127cafbbSLai Jiangshan old = tracepoint_add_probe(name, probe); 34197e1c18eSMathieu Desnoyers mutex_unlock(&tracepoints_mutex); 342127cafbbSLai Jiangshan if (IS_ERR(old)) 343127cafbbSLai Jiangshan return PTR_ERR(old); 344127cafbbSLai Jiangshan 34597e1c18eSMathieu Desnoyers tracepoint_update_probes(); /* may update entry */ 34619dba33cSLai Jiangshan release_probes(old); 34719dba33cSLai Jiangshan return 0; 34897e1c18eSMathieu Desnoyers } 34997e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_probe_register); 35097e1c18eSMathieu Desnoyers 351127cafbbSLai Jiangshan static void *tracepoint_remove_probe(const char *name, void *probe) 352127cafbbSLai Jiangshan { 353127cafbbSLai Jiangshan struct tracepoint_entry *entry; 354127cafbbSLai Jiangshan void *old; 355127cafbbSLai Jiangshan 356127cafbbSLai Jiangshan entry = get_tracepoint(name); 357127cafbbSLai Jiangshan if (!entry) 358127cafbbSLai Jiangshan return ERR_PTR(-ENOENT); 359127cafbbSLai Jiangshan old = tracepoint_entry_remove_probe(entry, probe); 360127cafbbSLai Jiangshan if (IS_ERR(old)) 361127cafbbSLai Jiangshan return old; 362127cafbbSLai Jiangshan if (!entry->refcount) 363127cafbbSLai Jiangshan remove_tracepoint(entry); 364127cafbbSLai Jiangshan return old; 365127cafbbSLai Jiangshan } 366127cafbbSLai Jiangshan 36797e1c18eSMathieu Desnoyers /** 36897e1c18eSMathieu Desnoyers * tracepoint_probe_unregister - Disconnect a probe from a tracepoint 36997e1c18eSMathieu Desnoyers * @name: tracepoint name 37097e1c18eSMathieu Desnoyers * @probe: probe function pointer 37197e1c18eSMathieu Desnoyers * 37297e1c18eSMathieu Desnoyers * We do not need to call a synchronize_sched to make sure the probes have 37397e1c18eSMathieu Desnoyers * finished running before doing a module unload, because the module unload 37497e1c18eSMathieu Desnoyers * itself uses stop_machine(), which insures that every preempt disabled section 37597e1c18eSMathieu Desnoyers * have finished. 37697e1c18eSMathieu Desnoyers */ 37797e1c18eSMathieu Desnoyers int tracepoint_probe_unregister(const char *name, void *probe) 37897e1c18eSMathieu Desnoyers { 37997e1c18eSMathieu Desnoyers void *old; 38097e1c18eSMathieu Desnoyers 38197e1c18eSMathieu Desnoyers mutex_lock(&tracepoints_mutex); 382127cafbbSLai Jiangshan old = tracepoint_remove_probe(name, probe); 38397e1c18eSMathieu Desnoyers mutex_unlock(&tracepoints_mutex); 384127cafbbSLai Jiangshan if (IS_ERR(old)) 385127cafbbSLai Jiangshan return PTR_ERR(old); 386127cafbbSLai Jiangshan 38797e1c18eSMathieu Desnoyers tracepoint_update_probes(); /* may update entry */ 38819dba33cSLai Jiangshan release_probes(old); 38919dba33cSLai Jiangshan return 0; 39097e1c18eSMathieu Desnoyers } 39197e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_probe_unregister); 39297e1c18eSMathieu Desnoyers 393127cafbbSLai Jiangshan static LIST_HEAD(old_probes); 394127cafbbSLai Jiangshan static int need_update; 395127cafbbSLai Jiangshan 396127cafbbSLai Jiangshan static void tracepoint_add_old_probes(void *old) 397127cafbbSLai Jiangshan { 398127cafbbSLai Jiangshan need_update = 1; 399127cafbbSLai Jiangshan if (old) { 400127cafbbSLai Jiangshan struct tp_probes *tp_probes = container_of(old, 401127cafbbSLai Jiangshan struct tp_probes, probes[0]); 402127cafbbSLai Jiangshan list_add(&tp_probes->u.list, &old_probes); 403127cafbbSLai Jiangshan } 404127cafbbSLai Jiangshan } 405127cafbbSLai Jiangshan 406127cafbbSLai Jiangshan /** 407127cafbbSLai Jiangshan * tracepoint_probe_register_noupdate - register a probe but not connect 408127cafbbSLai Jiangshan * @name: tracepoint name 409127cafbbSLai Jiangshan * @probe: probe handler 410127cafbbSLai Jiangshan * 411127cafbbSLai Jiangshan * caller must call tracepoint_probe_update_all() 412127cafbbSLai Jiangshan */ 413127cafbbSLai Jiangshan int tracepoint_probe_register_noupdate(const char *name, void *probe) 414127cafbbSLai Jiangshan { 415127cafbbSLai Jiangshan void *old; 416127cafbbSLai Jiangshan 417127cafbbSLai Jiangshan mutex_lock(&tracepoints_mutex); 418127cafbbSLai Jiangshan old = tracepoint_add_probe(name, probe); 419127cafbbSLai Jiangshan if (IS_ERR(old)) { 420127cafbbSLai Jiangshan mutex_unlock(&tracepoints_mutex); 421127cafbbSLai Jiangshan return PTR_ERR(old); 422127cafbbSLai Jiangshan } 423127cafbbSLai Jiangshan tracepoint_add_old_probes(old); 424127cafbbSLai Jiangshan mutex_unlock(&tracepoints_mutex); 425127cafbbSLai Jiangshan return 0; 426127cafbbSLai Jiangshan } 427127cafbbSLai Jiangshan EXPORT_SYMBOL_GPL(tracepoint_probe_register_noupdate); 428127cafbbSLai Jiangshan 429127cafbbSLai Jiangshan /** 430127cafbbSLai Jiangshan * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect 431127cafbbSLai Jiangshan * @name: tracepoint name 432127cafbbSLai Jiangshan * @probe: probe function pointer 433127cafbbSLai Jiangshan * 434127cafbbSLai Jiangshan * caller must call tracepoint_probe_update_all() 435127cafbbSLai Jiangshan */ 436127cafbbSLai Jiangshan int tracepoint_probe_unregister_noupdate(const char *name, void *probe) 437127cafbbSLai Jiangshan { 438127cafbbSLai Jiangshan void *old; 439127cafbbSLai Jiangshan 440127cafbbSLai Jiangshan mutex_lock(&tracepoints_mutex); 441127cafbbSLai Jiangshan old = tracepoint_remove_probe(name, probe); 442127cafbbSLai Jiangshan if (IS_ERR(old)) { 443127cafbbSLai Jiangshan mutex_unlock(&tracepoints_mutex); 444127cafbbSLai Jiangshan return PTR_ERR(old); 445127cafbbSLai Jiangshan } 446127cafbbSLai Jiangshan tracepoint_add_old_probes(old); 447127cafbbSLai Jiangshan mutex_unlock(&tracepoints_mutex); 448127cafbbSLai Jiangshan return 0; 449127cafbbSLai Jiangshan } 450127cafbbSLai Jiangshan EXPORT_SYMBOL_GPL(tracepoint_probe_unregister_noupdate); 451127cafbbSLai Jiangshan 452127cafbbSLai Jiangshan /** 453127cafbbSLai Jiangshan * tracepoint_probe_update_all - update tracepoints 454127cafbbSLai Jiangshan */ 455127cafbbSLai Jiangshan void tracepoint_probe_update_all(void) 456127cafbbSLai Jiangshan { 457127cafbbSLai Jiangshan LIST_HEAD(release_probes); 458127cafbbSLai Jiangshan struct tp_probes *pos, *next; 459127cafbbSLai Jiangshan 460127cafbbSLai Jiangshan mutex_lock(&tracepoints_mutex); 461127cafbbSLai Jiangshan if (!need_update) { 462127cafbbSLai Jiangshan mutex_unlock(&tracepoints_mutex); 463127cafbbSLai Jiangshan return; 464127cafbbSLai Jiangshan } 465127cafbbSLai Jiangshan if (!list_empty(&old_probes)) 466127cafbbSLai Jiangshan list_replace_init(&old_probes, &release_probes); 467127cafbbSLai Jiangshan need_update = 0; 468127cafbbSLai Jiangshan mutex_unlock(&tracepoints_mutex); 469127cafbbSLai Jiangshan 470127cafbbSLai Jiangshan tracepoint_update_probes(); 471127cafbbSLai Jiangshan list_for_each_entry_safe(pos, next, &release_probes, u.list) { 472127cafbbSLai Jiangshan list_del(&pos->u.list); 473127cafbbSLai Jiangshan call_rcu_sched(&pos->u.rcu, rcu_free_old_probes); 474127cafbbSLai Jiangshan } 475127cafbbSLai Jiangshan } 476127cafbbSLai Jiangshan EXPORT_SYMBOL_GPL(tracepoint_probe_update_all); 477127cafbbSLai Jiangshan 47897e1c18eSMathieu Desnoyers /** 47997e1c18eSMathieu Desnoyers * tracepoint_get_iter_range - Get a next tracepoint iterator given a range. 48097e1c18eSMathieu Desnoyers * @tracepoint: current tracepoints (in), next tracepoint (out) 48197e1c18eSMathieu Desnoyers * @begin: beginning of the range 48297e1c18eSMathieu Desnoyers * @end: end of the range 48397e1c18eSMathieu Desnoyers * 48497e1c18eSMathieu Desnoyers * Returns whether a next tracepoint has been found (1) or not (0). 48597e1c18eSMathieu Desnoyers * Will return the first tracepoint in the range if the input tracepoint is 48697e1c18eSMathieu Desnoyers * NULL. 48797e1c18eSMathieu Desnoyers */ 48897e1c18eSMathieu Desnoyers int tracepoint_get_iter_range(struct tracepoint **tracepoint, 48997e1c18eSMathieu Desnoyers struct tracepoint *begin, struct tracepoint *end) 49097e1c18eSMathieu Desnoyers { 49197e1c18eSMathieu Desnoyers if (!*tracepoint && begin != end) { 49297e1c18eSMathieu Desnoyers *tracepoint = begin; 49397e1c18eSMathieu Desnoyers return 1; 49497e1c18eSMathieu Desnoyers } 49597e1c18eSMathieu Desnoyers if (*tracepoint >= begin && *tracepoint < end) 49697e1c18eSMathieu Desnoyers return 1; 49797e1c18eSMathieu Desnoyers return 0; 49897e1c18eSMathieu Desnoyers } 49997e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_get_iter_range); 50097e1c18eSMathieu Desnoyers 50197e1c18eSMathieu Desnoyers static void tracepoint_get_iter(struct tracepoint_iter *iter) 50297e1c18eSMathieu Desnoyers { 50397e1c18eSMathieu Desnoyers int found = 0; 50497e1c18eSMathieu Desnoyers 50597e1c18eSMathieu Desnoyers /* Core kernel tracepoints */ 50697e1c18eSMathieu Desnoyers if (!iter->module) { 50797e1c18eSMathieu Desnoyers found = tracepoint_get_iter_range(&iter->tracepoint, 50897e1c18eSMathieu Desnoyers __start___tracepoints, __stop___tracepoints); 50997e1c18eSMathieu Desnoyers if (found) 51097e1c18eSMathieu Desnoyers goto end; 51197e1c18eSMathieu Desnoyers } 51297e1c18eSMathieu Desnoyers /* tracepoints in modules. */ 51397e1c18eSMathieu Desnoyers found = module_get_iter_tracepoints(iter); 51497e1c18eSMathieu Desnoyers end: 51597e1c18eSMathieu Desnoyers if (!found) 51697e1c18eSMathieu Desnoyers tracepoint_iter_reset(iter); 51797e1c18eSMathieu Desnoyers } 51897e1c18eSMathieu Desnoyers 51997e1c18eSMathieu Desnoyers void tracepoint_iter_start(struct tracepoint_iter *iter) 52097e1c18eSMathieu Desnoyers { 52197e1c18eSMathieu Desnoyers tracepoint_get_iter(iter); 52297e1c18eSMathieu Desnoyers } 52397e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_iter_start); 52497e1c18eSMathieu Desnoyers 52597e1c18eSMathieu Desnoyers void tracepoint_iter_next(struct tracepoint_iter *iter) 52697e1c18eSMathieu Desnoyers { 52797e1c18eSMathieu Desnoyers iter->tracepoint++; 52897e1c18eSMathieu Desnoyers /* 52997e1c18eSMathieu Desnoyers * iter->tracepoint may be invalid because we blindly incremented it. 53097e1c18eSMathieu Desnoyers * Make sure it is valid by marshalling on the tracepoints, getting the 53197e1c18eSMathieu Desnoyers * tracepoints from following modules if necessary. 53297e1c18eSMathieu Desnoyers */ 53397e1c18eSMathieu Desnoyers tracepoint_get_iter(iter); 53497e1c18eSMathieu Desnoyers } 53597e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_iter_next); 53697e1c18eSMathieu Desnoyers 53797e1c18eSMathieu Desnoyers void tracepoint_iter_stop(struct tracepoint_iter *iter) 53897e1c18eSMathieu Desnoyers { 53997e1c18eSMathieu Desnoyers } 54097e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_iter_stop); 54197e1c18eSMathieu Desnoyers 54297e1c18eSMathieu Desnoyers void tracepoint_iter_reset(struct tracepoint_iter *iter) 54397e1c18eSMathieu Desnoyers { 54497e1c18eSMathieu Desnoyers iter->module = NULL; 54597e1c18eSMathieu Desnoyers iter->tracepoint = NULL; 54697e1c18eSMathieu Desnoyers } 54797e1c18eSMathieu Desnoyers EXPORT_SYMBOL_GPL(tracepoint_iter_reset); 54832f85742SMathieu Desnoyers 549227a8375SIngo Molnar #ifdef CONFIG_MODULES 550227a8375SIngo Molnar 55132f85742SMathieu Desnoyers int tracepoint_module_notify(struct notifier_block *self, 55232f85742SMathieu Desnoyers unsigned long val, void *data) 55332f85742SMathieu Desnoyers { 55432f85742SMathieu Desnoyers struct module *mod = data; 55532f85742SMathieu Desnoyers 55632f85742SMathieu Desnoyers switch (val) { 55732f85742SMathieu Desnoyers case MODULE_STATE_COMING: 55832f85742SMathieu Desnoyers tracepoint_update_probe_range(mod->tracepoints, 55932f85742SMathieu Desnoyers mod->tracepoints + mod->num_tracepoints); 56032f85742SMathieu Desnoyers break; 56132f85742SMathieu Desnoyers case MODULE_STATE_GOING: 56232f85742SMathieu Desnoyers tracepoint_update_probe_range(mod->tracepoints, 56332f85742SMathieu Desnoyers mod->tracepoints + mod->num_tracepoints); 56432f85742SMathieu Desnoyers break; 56532f85742SMathieu Desnoyers } 56632f85742SMathieu Desnoyers return 0; 56732f85742SMathieu Desnoyers } 56832f85742SMathieu Desnoyers 56932f85742SMathieu Desnoyers struct notifier_block tracepoint_module_nb = { 57032f85742SMathieu Desnoyers .notifier_call = tracepoint_module_notify, 57132f85742SMathieu Desnoyers .priority = 0, 57232f85742SMathieu Desnoyers }; 57332f85742SMathieu Desnoyers 57432f85742SMathieu Desnoyers static int init_tracepoints(void) 57532f85742SMathieu Desnoyers { 57632f85742SMathieu Desnoyers return register_module_notifier(&tracepoint_module_nb); 57732f85742SMathieu Desnoyers } 57832f85742SMathieu Desnoyers __initcall(init_tracepoints); 579227a8375SIngo Molnar 580227a8375SIngo Molnar #endif /* CONFIG_MODULES */ 581a871bd33SJason Baron 582*60d970c2SIngo Molnar #ifdef CONFIG_FTRACE_SYSCALLS 583*60d970c2SIngo Molnar 584a871bd33SJason Baron static DEFINE_MUTEX(regfunc_mutex); 585a871bd33SJason Baron static int sys_tracepoint_refcount; 586a871bd33SJason Baron 587a871bd33SJason Baron void syscall_regfunc(void) 588a871bd33SJason Baron { 589a871bd33SJason Baron unsigned long flags; 590a871bd33SJason Baron struct task_struct *g, *t; 591a871bd33SJason Baron 592a871bd33SJason Baron mutex_lock(®func_mutex); 593a871bd33SJason Baron if (!sys_tracepoint_refcount) { 594a871bd33SJason Baron read_lock_irqsave(&tasklist_lock, flags); 595a871bd33SJason Baron do_each_thread(g, t) { 596a871bd33SJason Baron set_tsk_thread_flag(t, TIF_SYSCALL_FTRACE); 597a871bd33SJason Baron } while_each_thread(g, t); 598a871bd33SJason Baron read_unlock_irqrestore(&tasklist_lock, flags); 599a871bd33SJason Baron } 600a871bd33SJason Baron sys_tracepoint_refcount++; 601a871bd33SJason Baron mutex_unlock(®func_mutex); 602a871bd33SJason Baron } 603a871bd33SJason Baron 604a871bd33SJason Baron void syscall_unregfunc(void) 605a871bd33SJason Baron { 606a871bd33SJason Baron unsigned long flags; 607a871bd33SJason Baron struct task_struct *g, *t; 608a871bd33SJason Baron 609a871bd33SJason Baron mutex_lock(®func_mutex); 610a871bd33SJason Baron sys_tracepoint_refcount--; 611a871bd33SJason Baron if (!sys_tracepoint_refcount) { 612a871bd33SJason Baron read_lock_irqsave(&tasklist_lock, flags); 613a871bd33SJason Baron do_each_thread(g, t) { 614a871bd33SJason Baron clear_tsk_thread_flag(t, TIF_SYSCALL_FTRACE); 615a871bd33SJason Baron } while_each_thread(g, t); 616a871bd33SJason Baron read_unlock_irqrestore(&tasklist_lock, flags); 617a871bd33SJason Baron } 618a871bd33SJason Baron mutex_unlock(®func_mutex); 619a871bd33SJason Baron } 620*60d970c2SIngo Molnar #endif 621