Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'tip/perf/core' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-2.6-trace into perf/core

+1122 -151
+26 -7
include/linux/ftrace.h
··· 29 29 30 30 typedef void (*ftrace_func_t)(unsigned long ip, unsigned long parent_ip); 31 31 32 + struct ftrace_hash; 33 + 34 + enum { 35 + FTRACE_OPS_FL_ENABLED = 1 << 0, 36 + FTRACE_OPS_FL_GLOBAL = 1 << 1, 37 + FTRACE_OPS_FL_DYNAMIC = 1 << 2, 38 + }; 39 + 32 40 struct ftrace_ops { 33 - ftrace_func_t func; 34 - struct ftrace_ops *next; 41 + ftrace_func_t func; 42 + struct ftrace_ops *next; 43 + unsigned long flags; 44 + #ifdef CONFIG_DYNAMIC_FTRACE 45 + struct ftrace_hash *notrace_hash; 46 + struct ftrace_hash *filter_hash; 47 + #endif 35 48 }; 36 49 37 50 extern int function_trace_stop; ··· 159 146 extern int ftrace_text_reserved(void *start, void *end); 160 147 161 148 enum { 162 - FTRACE_FL_FREE = (1 << 0), 163 - FTRACE_FL_FILTER = (1 << 1), 164 - FTRACE_FL_ENABLED = (1 << 2), 165 - FTRACE_FL_NOTRACE = (1 << 3), 149 + FTRACE_FL_ENABLED = (1 << 30), 150 + FTRACE_FL_FREE = (1 << 31), 166 151 }; 152 + 153 + #define FTRACE_FL_MASK (0x3UL << 30) 154 + #define FTRACE_REF_MAX ((1 << 30) - 1) 167 155 168 156 struct dyn_ftrace { 169 157 union { ··· 179 165 }; 180 166 181 167 int ftrace_force_update(void); 182 - void ftrace_set_filter(unsigned char *buf, int len, int reset); 168 + void ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf, 169 + int len, int reset); 170 + void ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf, 171 + int len, int reset); 172 + void ftrace_set_global_filter(unsigned char *buf, int len, int reset); 173 + void ftrace_set_global_notrace(unsigned char *buf, int len, int reset); 183 174 184 175 int register_ftrace_command(struct ftrace_func_command *cmd); 185 176 int unregister_ftrace_command(struct ftrace_func_command *cmd);
+1
include/linux/kernel.h
··· 283 283 extern unsigned long long memparse(const char *ptr, char **retptr); 284 284 285 285 extern int core_kernel_text(unsigned long addr); 286 + extern int core_kernel_data(unsigned long addr); 286 287 extern int __kernel_text_address(unsigned long addr); 287 288 extern int kernel_text_address(unsigned long addr); 288 289 extern int func_ptr_is_kernel_text(void *ptr);
+8
kernel/extable.c
··· 72 72 return 0; 73 73 } 74 74 75 + int core_kernel_data(unsigned long addr) 76 + { 77 + if (addr >= (unsigned long)_sdata && 78 + addr < (unsigned long)_edata) 79 + return 1; 80 + return 0; 81 + } 82 + 75 83 int __kernel_text_address(unsigned long addr) 76 84 { 77 85 if (core_kernel_text(addr))
+863 -141
kernel/trace/ftrace.c
··· 57 57 /* hash bits for specific function selection */ 58 58 #define FTRACE_HASH_BITS 7 59 59 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS) 60 + #define FTRACE_HASH_DEFAULT_BITS 10 61 + #define FTRACE_HASH_MAX_BITS 12 60 62 61 63 /* ftrace_enabled is a method to turn ftrace on or off */ 62 64 int ftrace_enabled __read_mostly; ··· 87 85 .func = ftrace_stub, 88 86 }; 89 87 90 - static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end; 88 + static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end; 89 + static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end; 91 90 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub; 92 91 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub; 93 92 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub; 93 + static struct ftrace_ops global_ops; 94 + 95 + static void 96 + ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip); 94 97 95 98 /* 96 - * Traverse the ftrace_list, invoking all entries. The reason that we 99 + * Traverse the ftrace_global_list, invoking all entries. The reason that we 97 100 * can use rcu_dereference_raw() is that elements removed from this list 98 101 * are simply leaked, so there is no need to interact with a grace-period 99 102 * mechanism. The rcu_dereference_raw() calls are needed to handle 100 - * concurrent insertions into the ftrace_list. 103 + * concurrent insertions into the ftrace_global_list. 101 104 * 102 105 * Silly Alpha and silly pointer-speculation compiler optimizations! 103 106 */ 104 - static void ftrace_list_func(unsigned long ip, unsigned long parent_ip) 107 + static void ftrace_global_list_func(unsigned long ip, 108 + unsigned long parent_ip) 105 109 { 106 - struct ftrace_ops *op = rcu_dereference_raw(ftrace_list); /*see above*/ 110 + struct ftrace_ops *op = rcu_dereference_raw(ftrace_global_list); /*see above*/ 107 111 108 112 while (op != &ftrace_list_end) { 109 113 op->func(ip, parent_ip); ··· 159 151 } 160 152 #endif 161 153 162 - static void update_ftrace_function(void) 154 + static void update_global_ops(void) 163 155 { 164 156 ftrace_func_t func; 165 157 ··· 168 160 * function directly. Otherwise, we need to iterate over the 169 161 * registered callers. 170 162 */ 171 - if (ftrace_list == &ftrace_list_end || 172 - ftrace_list->next == &ftrace_list_end) 173 - func = ftrace_list->func; 163 + if (ftrace_global_list == &ftrace_list_end || 164 + ftrace_global_list->next == &ftrace_list_end) 165 + func = ftrace_global_list->func; 174 166 else 175 - func = ftrace_list_func; 167 + func = ftrace_global_list_func; 176 168 177 169 /* If we filter on pids, update to use the pid function */ 178 170 if (!list_empty(&ftrace_pids)) { 179 171 set_ftrace_pid_function(func); 180 172 func = ftrace_pid_func; 181 173 } 174 + 175 + global_ops.func = func; 176 + } 177 + 178 + static void update_ftrace_function(void) 179 + { 180 + ftrace_func_t func; 181 + 182 + update_global_ops(); 183 + 184 + /* 185 + * If we are at the end of the list and this ops is 186 + * not dynamic, then have the mcount trampoline call 187 + * the function directly 188 + */ 189 + if (ftrace_ops_list == &ftrace_list_end || 190 + (ftrace_ops_list->next == &ftrace_list_end && 191 + !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC))) 192 + func = ftrace_ops_list->func; 193 + else 194 + func = ftrace_ops_list_func; 195 + 182 196 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST 183 197 ftrace_trace_function = func; 184 198 #else ··· 209 179 #endif 210 180 } 211 181 212 - static int __register_ftrace_function(struct ftrace_ops *ops) 182 + static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops) 213 183 { 214 - ops->next = ftrace_list; 184 + ops->next = *list; 215 185 /* 216 - * We are entering ops into the ftrace_list but another 186 + * We are entering ops into the list but another 217 187 * CPU might be walking that list. We need to make sure 218 188 * the ops->next pointer is valid before another CPU sees 219 - * the ops pointer included into the ftrace_list. 189 + * the ops pointer included into the list. 220 190 */ 221 - rcu_assign_pointer(ftrace_list, ops); 191 + rcu_assign_pointer(*list, ops); 192 + } 193 + 194 + static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops) 195 + { 196 + struct ftrace_ops **p; 197 + 198 + /* 199 + * If we are removing the last function, then simply point 200 + * to the ftrace_stub. 201 + */ 202 + if (*list == ops && ops->next == &ftrace_list_end) { 203 + *list = &ftrace_list_end; 204 + return 0; 205 + } 206 + 207 + for (p = list; *p != &ftrace_list_end; p = &(*p)->next) 208 + if (*p == ops) 209 + break; 210 + 211 + if (*p != ops) 212 + return -1; 213 + 214 + *p = (*p)->next; 215 + return 0; 216 + } 217 + 218 + static int __register_ftrace_function(struct ftrace_ops *ops) 219 + { 220 + if (ftrace_disabled) 221 + return -ENODEV; 222 + 223 + if (FTRACE_WARN_ON(ops == &global_ops)) 224 + return -EINVAL; 225 + 226 + if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED)) 227 + return -EBUSY; 228 + 229 + if (!core_kernel_data((unsigned long)ops)) 230 + ops->flags |= FTRACE_OPS_FL_DYNAMIC; 231 + 232 + if (ops->flags & FTRACE_OPS_FL_GLOBAL) { 233 + int first = ftrace_global_list == &ftrace_list_end; 234 + add_ftrace_ops(&ftrace_global_list, ops); 235 + ops->flags |= FTRACE_OPS_FL_ENABLED; 236 + if (first) 237 + add_ftrace_ops(&ftrace_ops_list, &global_ops); 238 + } else 239 + add_ftrace_ops(&ftrace_ops_list, ops); 222 240 223 241 if (ftrace_enabled) 224 242 update_ftrace_function(); ··· 276 198 277 199 static int __unregister_ftrace_function(struct ftrace_ops *ops) 278 200 { 279 - struct ftrace_ops **p; 201 + int ret; 280 202 281 - /* 282 - * If we are removing the last function, then simply point 283 - * to the ftrace_stub. 284 - */ 285 - if (ftrace_list == ops && ops->next == &ftrace_list_end) { 286 - ftrace_trace_function = ftrace_stub; 287 - ftrace_list = &ftrace_list_end; 288 - return 0; 289 - } 203 + if (ftrace_disabled) 204 + return -ENODEV; 290 205 291 - for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next) 292 - if (*p == ops) 293 - break; 206 + if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED))) 207 + return -EBUSY; 294 208 295 - if (*p != ops) 296 - return -1; 209 + if (FTRACE_WARN_ON(ops == &global_ops)) 210 + return -EINVAL; 297 211 298 - *p = (*p)->next; 212 + if (ops->flags & FTRACE_OPS_FL_GLOBAL) { 213 + ret = remove_ftrace_ops(&ftrace_global_list, ops); 214 + if (!ret && ftrace_global_list == &ftrace_list_end) 215 + ret = remove_ftrace_ops(&ftrace_ops_list, &global_ops); 216 + if (!ret) 217 + ops->flags &= ~FTRACE_OPS_FL_ENABLED; 218 + } else 219 + ret = remove_ftrace_ops(&ftrace_ops_list, ops); 220 + 221 + if (ret < 0) 222 + return ret; 299 223 300 224 if (ftrace_enabled) 301 225 update_ftrace_function(); 226 + 227 + /* 228 + * Dynamic ops may be freed, we must make sure that all 229 + * callers are done before leaving this function. 230 + */ 231 + if (ops->flags & FTRACE_OPS_FL_DYNAMIC) 232 + synchronize_sched(); 302 233 303 234 return 0; 304 235 } ··· 952 865 FTRACE_START_FUNC_RET = (1 << 3), 953 866 FTRACE_STOP_FUNC_RET = (1 << 4), 954 867 }; 868 + struct ftrace_func_entry { 869 + struct hlist_node hlist; 870 + unsigned long ip; 871 + }; 955 872 956 - static int ftrace_filtered; 873 + struct ftrace_hash { 874 + unsigned long size_bits; 875 + struct hlist_head *buckets; 876 + unsigned long count; 877 + struct rcu_head rcu; 878 + }; 879 + 880 + /* 881 + * We make these constant because no one should touch them, 882 + * but they are used as the default "empty hash", to avoid allocating 883 + * it all the time. These are in a read only section such that if 884 + * anyone does try to modify it, it will cause an exception. 885 + */ 886 + static const struct hlist_head empty_buckets[1]; 887 + static const struct ftrace_hash empty_hash = { 888 + .buckets = (struct hlist_head *)empty_buckets, 889 + }; 890 + #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash) 891 + 892 + static struct ftrace_ops global_ops = { 893 + .func = ftrace_stub, 894 + .notrace_hash = EMPTY_HASH, 895 + .filter_hash = EMPTY_HASH, 896 + }; 957 897 958 898 static struct dyn_ftrace *ftrace_new_addrs; 959 899 ··· 1003 889 1004 890 static struct dyn_ftrace *ftrace_free_records; 1005 891 892 + static struct ftrace_func_entry * 893 + ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip) 894 + { 895 + unsigned long key; 896 + struct ftrace_func_entry *entry; 897 + struct hlist_head *hhd; 898 + struct hlist_node *n; 899 + 900 + if (!hash->count) 901 + return NULL; 902 + 903 + if (hash->size_bits > 0) 904 + key = hash_long(ip, hash->size_bits); 905 + else 906 + key = 0; 907 + 908 + hhd = &hash->buckets[key]; 909 + 910 + hlist_for_each_entry_rcu(entry, n, hhd, hlist) { 911 + if (entry->ip == ip) 912 + return entry; 913 + } 914 + return NULL; 915 + } 916 + 917 + static void __add_hash_entry(struct ftrace_hash *hash, 918 + struct ftrace_func_entry *entry) 919 + { 920 + struct hlist_head *hhd; 921 + unsigned long key; 922 + 923 + if (hash->size_bits) 924 + key = hash_long(entry->ip, hash->size_bits); 925 + else 926 + key = 0; 927 + 928 + hhd = &hash->buckets[key]; 929 + hlist_add_head(&entry->hlist, hhd); 930 + hash->count++; 931 + } 932 + 933 + static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip) 934 + { 935 + struct ftrace_func_entry *entry; 936 + 937 + entry = kmalloc(sizeof(*entry), GFP_KERNEL); 938 + if (!entry) 939 + return -ENOMEM; 940 + 941 + entry->ip = ip; 942 + __add_hash_entry(hash, entry); 943 + 944 + return 0; 945 + } 946 + 947 + static void 948 + free_hash_entry(struct ftrace_hash *hash, 949 + struct ftrace_func_entry *entry) 950 + { 951 + hlist_del(&entry->hlist); 952 + kfree(entry); 953 + hash->count--; 954 + } 955 + 956 + static void 957 + remove_hash_entry(struct ftrace_hash *hash, 958 + struct ftrace_func_entry *entry) 959 + { 960 + hlist_del(&entry->hlist); 961 + hash->count--; 962 + } 963 + 964 + static void ftrace_hash_clear(struct ftrace_hash *hash) 965 + { 966 + struct hlist_head *hhd; 967 + struct hlist_node *tp, *tn; 968 + struct ftrace_func_entry *entry; 969 + int size = 1 << hash->size_bits; 970 + int i; 971 + 972 + if (!hash->count) 973 + return; 974 + 975 + for (i = 0; i < size; i++) { 976 + hhd = &hash->buckets[i]; 977 + hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) 978 + free_hash_entry(hash, entry); 979 + } 980 + FTRACE_WARN_ON(hash->count); 981 + } 982 + 983 + static void free_ftrace_hash(struct ftrace_hash *hash) 984 + { 985 + if (!hash || hash == EMPTY_HASH) 986 + return; 987 + ftrace_hash_clear(hash); 988 + kfree(hash->buckets); 989 + kfree(hash); 990 + } 991 + 992 + static void __free_ftrace_hash_rcu(struct rcu_head *rcu) 993 + { 994 + struct ftrace_hash *hash; 995 + 996 + hash = container_of(rcu, struct ftrace_hash, rcu); 997 + free_ftrace_hash(hash); 998 + } 999 + 1000 + static void free_ftrace_hash_rcu(struct ftrace_hash *hash) 1001 + { 1002 + if (!hash || hash == EMPTY_HASH) 1003 + return; 1004 + call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu); 1005 + } 1006 + 1007 + static struct ftrace_hash *alloc_ftrace_hash(int size_bits) 1008 + { 1009 + struct ftrace_hash *hash; 1010 + int size; 1011 + 1012 + hash = kzalloc(sizeof(*hash), GFP_KERNEL); 1013 + if (!hash) 1014 + return NULL; 1015 + 1016 + size = 1 << size_bits; 1017 + hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL); 1018 + 1019 + if (!hash->buckets) { 1020 + kfree(hash); 1021 + return NULL; 1022 + } 1023 + 1024 + hash->size_bits = size_bits; 1025 + 1026 + return hash; 1027 + } 1028 + 1029 + static struct ftrace_hash * 1030 + alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash) 1031 + { 1032 + struct ftrace_func_entry *entry; 1033 + struct ftrace_hash *new_hash; 1034 + struct hlist_node *tp; 1035 + int size; 1036 + int ret; 1037 + int i; 1038 + 1039 + new_hash = alloc_ftrace_hash(size_bits); 1040 + if (!new_hash) 1041 + return NULL; 1042 + 1043 + /* Empty hash? */ 1044 + if (!hash || !hash->count) 1045 + return new_hash; 1046 + 1047 + size = 1 << hash->size_bits; 1048 + for (i = 0; i < size; i++) { 1049 + hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) { 1050 + ret = add_hash_entry(new_hash, entry->ip); 1051 + if (ret < 0) 1052 + goto free_hash; 1053 + } 1054 + } 1055 + 1056 + FTRACE_WARN_ON(new_hash->count != hash->count); 1057 + 1058 + return new_hash; 1059 + 1060 + free_hash: 1061 + free_ftrace_hash(new_hash); 1062 + return NULL; 1063 + } 1064 + 1065 + static int 1066 + ftrace_hash_move(struct ftrace_hash **dst, struct ftrace_hash *src) 1067 + { 1068 + struct ftrace_func_entry *entry; 1069 + struct hlist_node *tp, *tn; 1070 + struct hlist_head *hhd; 1071 + struct ftrace_hash *old_hash; 1072 + struct ftrace_hash *new_hash; 1073 + unsigned long key; 1074 + int size = src->count; 1075 + int bits = 0; 1076 + int i; 1077 + 1078 + /* 1079 + * If the new source is empty, just free dst and assign it 1080 + * the empty_hash. 1081 + */ 1082 + if (!src->count) { 1083 + free_ftrace_hash_rcu(*dst); 1084 + rcu_assign_pointer(*dst, EMPTY_HASH); 1085 + return 0; 1086 + } 1087 + 1088 + /* 1089 + * Make the hash size about 1/2 the # found 1090 + */ 1091 + for (size /= 2; size; size >>= 1) 1092 + bits++; 1093 + 1094 + /* Don't allocate too much */ 1095 + if (bits > FTRACE_HASH_MAX_BITS) 1096 + bits = FTRACE_HASH_MAX_BITS; 1097 + 1098 + new_hash = alloc_ftrace_hash(bits); 1099 + if (!new_hash) 1100 + return -ENOMEM; 1101 + 1102 + size = 1 << src->size_bits; 1103 + for (i = 0; i < size; i++) { 1104 + hhd = &src->buckets[i]; 1105 + hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) { 1106 + if (bits > 0) 1107 + key = hash_long(entry->ip, bits); 1108 + else 1109 + key = 0; 1110 + remove_hash_entry(src, entry); 1111 + __add_hash_entry(new_hash, entry); 1112 + } 1113 + } 1114 + 1115 + old_hash = *dst; 1116 + rcu_assign_pointer(*dst, new_hash); 1117 + free_ftrace_hash_rcu(old_hash); 1118 + 1119 + return 0; 1120 + } 1121 + 1122 + /* 1123 + * Test the hashes for this ops to see if we want to call 1124 + * the ops->func or not. 1125 + * 1126 + * It's a match if the ip is in the ops->filter_hash or 1127 + * the filter_hash does not exist or is empty, 1128 + * AND 1129 + * the ip is not in the ops->notrace_hash. 1130 + * 1131 + * This needs to be called with preemption disabled as 1132 + * the hashes are freed with call_rcu_sched(). 1133 + */ 1134 + static int 1135 + ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip) 1136 + { 1137 + struct ftrace_hash *filter_hash; 1138 + struct ftrace_hash *notrace_hash; 1139 + int ret; 1140 + 1141 + filter_hash = rcu_dereference_raw(ops->filter_hash); 1142 + notrace_hash = rcu_dereference_raw(ops->notrace_hash); 1143 + 1144 + if ((!filter_hash || !filter_hash->count || 1145 + ftrace_lookup_ip(filter_hash, ip)) && 1146 + (!notrace_hash || !notrace_hash->count || 1147 + !ftrace_lookup_ip(notrace_hash, ip))) 1148 + ret = 1; 1149 + else 1150 + ret = 0; 1151 + 1152 + return ret; 1153 + } 1154 + 1006 1155 /* 1007 1156 * This is a double for. Do not use 'break' to break out of the loop, 1008 1157 * you must use a goto. ··· 1279 902 #define while_for_each_ftrace_rec() \ 1280 903 } \ 1281 904 } 905 + 906 + static void __ftrace_hash_rec_update(struct ftrace_ops *ops, 907 + int filter_hash, 908 + bool inc) 909 + { 910 + struct ftrace_hash *hash; 911 + struct ftrace_hash *other_hash; 912 + struct ftrace_page *pg; 913 + struct dyn_ftrace *rec; 914 + int count = 0; 915 + int all = 0; 916 + 917 + /* Only update if the ops has been registered */ 918 + if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) 919 + return; 920 + 921 + /* 922 + * In the filter_hash case: 923 + * If the count is zero, we update all records. 924 + * Otherwise we just update the items in the hash. 925 + * 926 + * In the notrace_hash case: 927 + * We enable the update in the hash. 928 + * As disabling notrace means enabling the tracing, 929 + * and enabling notrace means disabling, the inc variable 930 + * gets inversed. 931 + */ 932 + if (filter_hash) { 933 + hash = ops->filter_hash; 934 + other_hash = ops->notrace_hash; 935 + if (!hash || !hash->count) 936 + all = 1; 937 + } else { 938 + inc = !inc; 939 + hash = ops->notrace_hash; 940 + other_hash = ops->filter_hash; 941 + /* 942 + * If the notrace hash has no items, 943 + * then there's nothing to do. 944 + */ 945 + if (hash && !hash->count) 946 + return; 947 + } 948 + 949 + do_for_each_ftrace_rec(pg, rec) { 950 + int in_other_hash = 0; 951 + int in_hash = 0; 952 + int match = 0; 953 + 954 + if (all) { 955 + /* 956 + * Only the filter_hash affects all records. 957 + * Update if the record is not in the notrace hash. 958 + */ 959 + if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip)) 960 + match = 1; 961 + } else { 962 + in_hash = hash && !!ftrace_lookup_ip(hash, rec->ip); 963 + in_other_hash = other_hash && !!ftrace_lookup_ip(other_hash, rec->ip); 964 + 965 + /* 966 + * 967 + */ 968 + if (filter_hash && in_hash && !in_other_hash) 969 + match = 1; 970 + else if (!filter_hash && in_hash && 971 + (in_other_hash || !other_hash->count)) 972 + match = 1; 973 + } 974 + if (!match) 975 + continue; 976 + 977 + if (inc) { 978 + rec->flags++; 979 + if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX)) 980 + return; 981 + } else { 982 + if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0)) 983 + return; 984 + rec->flags--; 985 + } 986 + count++; 987 + /* Shortcut, if we handled all records, we are done. */ 988 + if (!all && count == hash->count) 989 + return; 990 + } while_for_each_ftrace_rec(); 991 + } 992 + 993 + static void ftrace_hash_rec_disable(struct ftrace_ops *ops, 994 + int filter_hash) 995 + { 996 + __ftrace_hash_rec_update(ops, filter_hash, 0); 997 + } 998 + 999 + static void ftrace_hash_rec_enable(struct ftrace_ops *ops, 1000 + int filter_hash) 1001 + { 1002 + __ftrace_hash_rec_update(ops, filter_hash, 1); 1003 + } 1282 1004 1283 1005 static void ftrace_free_rec(struct dyn_ftrace *rec) 1284 1006 { ··· 1500 1024 ftrace_addr = (unsigned long)FTRACE_ADDR; 1501 1025 1502 1026 /* 1503 - * If this record is not to be traced or we want to disable it, 1504 - * then disable it. 1027 + * If we are enabling tracing: 1505 1028 * 1506 - * If we want to enable it and filtering is off, then enable it. 1029 + * If the record has a ref count, then we need to enable it 1030 + * because someone is using it. 1507 1031 * 1508 - * If we want to enable it and filtering is on, enable it only if 1509 - * it's filtered 1032 + * Otherwise we make sure its disabled. 1033 + * 1034 + * If we are disabling tracing, then disable all records that 1035 + * are enabled. 1510 1036 */ 1511 - if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) { 1512 - if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER)) 1513 - flag = FTRACE_FL_ENABLED; 1514 - } 1037 + if (enable && (rec->flags & ~FTRACE_FL_MASK)) 1038 + flag = FTRACE_FL_ENABLED; 1515 1039 1516 1040 /* If the state of this record hasn't changed, then do nothing */ 1517 1041 if ((rec->flags & FTRACE_FL_ENABLED) == flag) ··· 1623 1147 1624 1148 static ftrace_func_t saved_ftrace_func; 1625 1149 static int ftrace_start_up; 1150 + static int global_start_up; 1626 1151 1627 1152 static void ftrace_startup_enable(int command) 1628 1153 { ··· 1638 1161 ftrace_run_update_code(command); 1639 1162 } 1640 1163 1641 - static void ftrace_startup(int command) 1164 + static void ftrace_startup(struct ftrace_ops *ops, int command) 1642 1165 { 1166 + bool hash_enable = true; 1167 + 1643 1168 if (unlikely(ftrace_disabled)) 1644 1169 return; 1645 1170 1646 1171 ftrace_start_up++; 1647 1172 command |= FTRACE_ENABLE_CALLS; 1648 1173 1174 + /* ops marked global share the filter hashes */ 1175 + if (ops->flags & FTRACE_OPS_FL_GLOBAL) { 1176 + ops = &global_ops; 1177 + /* Don't update hash if global is already set */ 1178 + if (global_start_up) 1179 + hash_enable = false; 1180 + global_start_up++; 1181 + } 1182 + 1183 + ops->flags |= FTRACE_OPS_FL_ENABLED; 1184 + if (hash_enable) 1185 + ftrace_hash_rec_enable(ops, 1); 1186 + 1649 1187 ftrace_startup_enable(command); 1650 1188 } 1651 1189 1652 - static void ftrace_shutdown(int command) 1190 + static void ftrace_shutdown(struct ftrace_ops *ops, int command) 1653 1191 { 1192 + bool hash_disable = true; 1193 + 1654 1194 if (unlikely(ftrace_disabled)) 1655 1195 return; 1656 1196 ··· 1678 1184 * further ftrace uses. 1679 1185 */ 1680 1186 WARN_ON_ONCE(ftrace_start_up < 0); 1187 + 1188 + if (ops->flags & FTRACE_OPS_FL_GLOBAL) { 1189 + ops = &global_ops; 1190 + global_start_up--; 1191 + WARN_ON_ONCE(global_start_up < 0); 1192 + /* Don't update hash if global still has users */ 1193 + if (global_start_up) { 1194 + WARN_ON_ONCE(!ftrace_start_up); 1195 + hash_disable = false; 1196 + } 1197 + } 1198 + 1199 + if (hash_disable) 1200 + ftrace_hash_rec_disable(ops, 1); 1201 + 1202 + if (ops != &global_ops || !global_start_up) 1203 + ops->flags &= ~FTRACE_OPS_FL_ENABLED; 1681 1204 1682 1205 if (!ftrace_start_up) 1683 1206 command |= FTRACE_DISABLE_CALLS; ··· 1840 1329 FTRACE_ITER_NOTRACE = (1 << 1), 1841 1330 FTRACE_ITER_PRINTALL = (1 << 2), 1842 1331 FTRACE_ITER_HASH = (1 << 3), 1332 + FTRACE_ITER_ENABLED = (1 << 4), 1843 1333 }; 1844 1334 1845 1335 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */ ··· 1852 1340 struct dyn_ftrace *func; 1853 1341 struct ftrace_func_probe *probe; 1854 1342 struct trace_parser parser; 1343 + struct ftrace_hash *hash; 1344 + struct ftrace_ops *ops; 1855 1345 int hidx; 1856 1346 int idx; 1857 1347 unsigned flags; ··· 1950 1436 t_next(struct seq_file *m, void *v, loff_t *pos) 1951 1437 { 1952 1438 struct ftrace_iterator *iter = m->private; 1439 + struct ftrace_ops *ops = &global_ops; 1953 1440 struct dyn_ftrace *rec = NULL; 1954 1441 1955 1442 if (unlikely(ftrace_disabled)) ··· 1977 1462 if ((rec->flags & FTRACE_FL_FREE) || 1978 1463 1979 1464 ((iter->flags & FTRACE_ITER_FILTER) && 1980 - !(rec->flags & FTRACE_FL_FILTER)) || 1465 + !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) || 1981 1466 1982 1467 ((iter->flags & FTRACE_ITER_NOTRACE) && 1983 - !(rec->flags & FTRACE_FL_NOTRACE))) { 1468 + !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) || 1469 + 1470 + ((iter->flags & FTRACE_ITER_ENABLED) && 1471 + !(rec->flags & ~FTRACE_FL_MASK))) { 1472 + 1984 1473 rec = NULL; 1985 1474 goto retry; 1986 1475 } ··· 2008 1489 static void *t_start(struct seq_file *m, loff_t *pos) 2009 1490 { 2010 1491 struct ftrace_iterator *iter = m->private; 1492 + struct ftrace_ops *ops = &global_ops; 2011 1493 void *p = NULL; 2012 1494 loff_t l; 2013 1495 ··· 2028 1508 * off, we can short cut and just print out that all 2029 1509 * functions are enabled. 2030 1510 */ 2031 - if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) { 1511 + if (iter->flags & FTRACE_ITER_FILTER && !ops->filter_hash->count) { 2032 1512 if (*pos > 0) 2033 1513 return t_hash_start(m, pos); 2034 1514 iter->flags |= FTRACE_ITER_PRINTALL; ··· 2086 1566 if (!rec) 2087 1567 return 0; 2088 1568 2089 - seq_printf(m, "%ps\n", (void *)rec->ip); 1569 + seq_printf(m, "%ps", (void *)rec->ip); 1570 + if (iter->flags & FTRACE_ITER_ENABLED) 1571 + seq_printf(m, " (%ld)", 1572 + rec->flags & ~FTRACE_FL_MASK); 1573 + seq_printf(m, "\n"); 2090 1574 2091 1575 return 0; 2092 1576 } ··· 2129 1605 return ret; 2130 1606 } 2131 1607 2132 - static void ftrace_filter_reset(int enable) 1608 + static int 1609 + ftrace_enabled_open(struct inode *inode, struct file *file) 2133 1610 { 2134 - struct ftrace_page *pg; 2135 - struct dyn_ftrace *rec; 2136 - unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE; 1611 + struct ftrace_iterator *iter; 1612 + int ret; 2137 1613 1614 + if (unlikely(ftrace_disabled)) 1615 + return -ENODEV; 1616 + 1617 + iter = kzalloc(sizeof(*iter), GFP_KERNEL); 1618 + if (!iter) 1619 + return -ENOMEM; 1620 + 1621 + iter->pg = ftrace_pages_start; 1622 + iter->flags = FTRACE_ITER_ENABLED; 1623 + 1624 + ret = seq_open(file, &show_ftrace_seq_ops); 1625 + if (!ret) { 1626 + struct seq_file *m = file->private_data; 1627 + 1628 + m->private = iter; 1629 + } else { 1630 + kfree(iter); 1631 + } 1632 + 1633 + return ret; 1634 + } 1635 + 1636 + static void ftrace_filter_reset(struct ftrace_hash *hash) 1637 + { 2138 1638 mutex_lock(&ftrace_lock); 2139 - if (enable) 2140 - ftrace_filtered = 0; 2141 - do_for_each_ftrace_rec(pg, rec) { 2142 - rec->flags &= ~type; 2143 - } while_for_each_ftrace_rec(); 1639 + ftrace_hash_clear(hash); 2144 1640 mutex_unlock(&ftrace_lock); 2145 1641 } 2146 1642 2147 1643 static int 2148 - ftrace_regex_open(struct inode *inode, struct file *file, int enable) 1644 + ftrace_regex_open(struct ftrace_ops *ops, int flag, 1645 + struct inode *inode, struct file *file) 2149 1646 { 2150 1647 struct ftrace_iterator *iter; 1648 + struct ftrace_hash *hash; 2151 1649 int ret = 0; 2152 1650 2153 1651 if (unlikely(ftrace_disabled)) ··· 2184 1638 return -ENOMEM; 2185 1639 } 2186 1640 1641 + if (flag & FTRACE_ITER_NOTRACE) 1642 + hash = ops->notrace_hash; 1643 + else 1644 + hash = ops->filter_hash; 1645 + 1646 + iter->ops = ops; 1647 + iter->flags = flag; 1648 + 1649 + if (file->f_mode & FMODE_WRITE) { 1650 + mutex_lock(&ftrace_lock); 1651 + iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash); 1652 + mutex_unlock(&ftrace_lock); 1653 + 1654 + if (!iter->hash) { 1655 + trace_parser_put(&iter->parser); 1656 + kfree(iter); 1657 + return -ENOMEM; 1658 + } 1659 + } 1660 + 2187 1661 mutex_lock(&ftrace_regex_lock); 1662 + 2188 1663 if ((file->f_mode & FMODE_WRITE) && 2189 1664 (file->f_flags & O_TRUNC)) 2190 - ftrace_filter_reset(enable); 1665 + ftrace_filter_reset(iter->hash); 2191 1666 2192 1667 if (file->f_mode & FMODE_READ) { 2193 1668 iter->pg = ftrace_pages_start; 2194 - iter->flags = enable ? FTRACE_ITER_FILTER : 2195 - FTRACE_ITER_NOTRACE; 2196 1669 2197 1670 ret = seq_open(file, &show_ftrace_seq_ops); 2198 1671 if (!ret) { 2199 1672 struct seq_file *m = file->private_data; 2200 1673 m->private = iter; 2201 1674 } else { 1675 + /* Failed */ 1676 + free_ftrace_hash(iter->hash); 2202 1677 trace_parser_put(&iter->parser); 2203 1678 kfree(iter); 2204 1679 } ··· 2233 1666 static int 2234 1667 ftrace_filter_open(struct inode *inode, struct file *file) 2235 1668 { 2236 - return ftrace_regex_open(inode, file, 1); 1669 + return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER, 1670 + inode, file); 2237 1671 } 2238 1672 2239 1673 static int 2240 1674 ftrace_notrace_open(struct inode *inode, struct file *file) 2241 1675 { 2242 - return ftrace_regex_open(inode, file, 0); 1676 + return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE, 1677 + inode, file); 2243 1678 } 2244 1679 2245 1680 static loff_t ··· 2285 1716 return matched; 2286 1717 } 2287 1718 2288 - static void 2289 - update_record(struct dyn_ftrace *rec, unsigned long flag, int not) 1719 + static int 1720 + enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not) 2290 1721 { 2291 - if (not) 2292 - rec->flags &= ~flag; 2293 - else 2294 - rec->flags |= flag; 1722 + struct ftrace_func_entry *entry; 1723 + int ret = 0; 1724 + 1725 + entry = ftrace_lookup_ip(hash, rec->ip); 1726 + if (not) { 1727 + /* Do nothing if it doesn't exist */ 1728 + if (!entry) 1729 + return 0; 1730 + 1731 + free_hash_entry(hash, entry); 1732 + } else { 1733 + /* Do nothing if it exists */ 1734 + if (entry) 1735 + return 0; 1736 + 1737 + ret = add_hash_entry(hash, rec->ip); 1738 + } 1739 + return ret; 2295 1740 } 2296 1741 2297 1742 static int ··· 2330 1747 return ftrace_match(str, regex, len, type); 2331 1748 } 2332 1749 2333 - static int match_records(char *buff, int len, char *mod, int enable, int not) 1750 + static int 1751 + match_records(struct ftrace_hash *hash, char *buff, 1752 + int len, char *mod, int not) 2334 1753 { 2335 1754 unsigned search_len = 0; 2336 1755 struct ftrace_page *pg; 2337 1756 struct dyn_ftrace *rec; 2338 1757 int type = MATCH_FULL; 2339 1758 char *search = buff; 2340 - unsigned long flag; 2341 1759 int found = 0; 1760 + int ret; 2342 1761 2343 1762 if (len) { 2344 1763 type = filter_parse_regex(buff, len, &search, &not); 2345 1764 search_len = strlen(search); 2346 1765 } 2347 - 2348 - flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE; 2349 1766 2350 1767 mutex_lock(&ftrace_lock); 2351 1768 ··· 2355 1772 do_for_each_ftrace_rec(pg, rec) { 2356 1773 2357 1774 if (ftrace_match_record(rec, mod, search, search_len, type)) { 2358 - update_record(rec, flag, not); 1775 + ret = enter_record(hash, rec, not); 1776 + if (ret < 0) { 1777 + found = ret; 1778 + goto out_unlock; 1779 + } 2359 1780 found = 1; 2360 1781 } 2361 - /* 2362 - * Only enable filtering if we have a function that 2363 - * is filtered on. 2364 - */ 2365 - if (enable && (rec->flags & FTRACE_FL_FILTER)) 2366 - ftrace_filtered = 1; 2367 - 2368 1782 } while_for_each_ftrace_rec(); 2369 1783 out_unlock: 2370 1784 mutex_unlock(&ftrace_lock); ··· 2370 1790 } 2371 1791 2372 1792 static int 2373 - ftrace_match_records(char *buff, int len, int enable) 1793 + ftrace_match_records(struct ftrace_hash *hash, char *buff, int len) 2374 1794 { 2375 - return match_records(buff, len, NULL, enable, 0); 1795 + return match_records(hash, buff, len, NULL, 0); 2376 1796 } 2377 1797 2378 - static int ftrace_match_module_records(char *buff, char *mod, int enable) 1798 + static int 1799 + ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod) 2379 1800 { 2380 1801 int not = 0; 2381 1802 ··· 2390 1809 not = 1; 2391 1810 } 2392 1811 2393 - return match_records(buff, strlen(buff), mod, enable, not); 1812 + return match_records(hash, buff, strlen(buff), mod, not); 2394 1813 } 2395 1814 2396 1815 /* ··· 2401 1820 static int 2402 1821 ftrace_mod_callback(char *func, char *cmd, char *param, int enable) 2403 1822 { 1823 + struct ftrace_ops *ops = &global_ops; 1824 + struct ftrace_hash *hash; 2404 1825 char *mod; 1826 + int ret = -EINVAL; 2405 1827 2406 1828 /* 2407 1829 * cmd == 'mod' because we only registered this func ··· 2416 1832 2417 1833 /* we must have a module name */ 2418 1834 if (!param) 2419 - return -EINVAL; 1835 + return ret; 2420 1836 2421 1837 mod = strsep(&param, ":"); 2422 1838 if (!strlen(mod)) 2423 - return -EINVAL; 1839 + return ret; 2424 1840 2425 - if (ftrace_match_module_records(func, mod, enable)) 2426 - return 0; 2427 - return -EINVAL; 1841 + if (enable) 1842 + hash = ops->filter_hash; 1843 + else 1844 + hash = ops->notrace_hash; 1845 + 1846 + ret = ftrace_match_module_records(hash, func, mod); 1847 + if (!ret) 1848 + ret = -EINVAL; 1849 + if (ret < 0) 1850 + return ret; 1851 + 1852 + return 0; 2428 1853 } 2429 1854 2430 1855 static struct ftrace_func_command ftrace_mod_cmd = { ··· 2484 1891 2485 1892 static void __enable_ftrace_function_probe(void) 2486 1893 { 1894 + int ret; 2487 1895 int i; 2488 1896 2489 1897 if (ftrace_probe_registered) ··· 2499 1905 if (i == FTRACE_FUNC_HASHSIZE) 2500 1906 return; 2501 1907 2502 - __register_ftrace_function(&trace_probe_ops); 2503 - ftrace_startup(0); 1908 + ret = __register_ftrace_function(&trace_probe_ops); 1909 + if (!ret) 1910 + ftrace_startup(&trace_probe_ops, 0); 1911 + 2504 1912 ftrace_probe_registered = 1; 2505 1913 } 2506 1914 2507 1915 static void __disable_ftrace_function_probe(void) 2508 1916 { 1917 + int ret; 2509 1918 int i; 2510 1919 2511 1920 if (!ftrace_probe_registered) ··· 2521 1924 } 2522 1925 2523 1926 /* no more funcs left */ 2524 - __unregister_ftrace_function(&trace_probe_ops); 2525 - ftrace_shutdown(0); 1927 + ret = __unregister_ftrace_function(&trace_probe_ops); 1928 + if (!ret) 1929 + ftrace_shutdown(&trace_probe_ops, 0); 1930 + 2526 1931 ftrace_probe_registered = 0; 2527 1932 } 2528 1933 ··· 2727 2128 return ret; 2728 2129 } 2729 2130 2730 - static int ftrace_process_regex(char *buff, int len, int enable) 2131 + static int ftrace_process_regex(struct ftrace_hash *hash, 2132 + char *buff, int len, int enable) 2731 2133 { 2732 2134 char *func, *command, *next = buff; 2733 2135 struct ftrace_func_command *p; 2734 - int ret = -EINVAL; 2136 + int ret; 2735 2137 2736 2138 func = strsep(&next, ":"); 2737 2139 2738 2140 if (!next) { 2739 - if (ftrace_match_records(func, len, enable)) 2740 - return 0; 2741 - return ret; 2141 + ret = ftrace_match_records(hash, func, len); 2142 + if (!ret) 2143 + ret = -EINVAL; 2144 + if (ret < 0) 2145 + return ret; 2146 + return 0; 2742 2147 } 2743 2148 2744 2149 /* command found */ ··· 2790 2187 2791 2188 if (read >= 0 && trace_parser_loaded(parser) && 2792 2189 !trace_parser_cont(parser)) { 2793 - ret = ftrace_process_regex(parser->buffer, 2190 + ret = ftrace_process_regex(iter->hash, parser->buffer, 2794 2191 parser->idx, enable); 2795 2192 trace_parser_clear(parser); 2796 2193 if (ret) ··· 2818 2215 return ftrace_regex_write(file, ubuf, cnt, ppos, 0); 2819 2216 } 2820 2217 2821 - static void 2822 - ftrace_set_regex(unsigned char *buf, int len, int reset, int enable) 2218 + static int 2219 + ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len, 2220 + int reset, int enable) 2823 2221 { 2222 + struct ftrace_hash **orig_hash; 2223 + struct ftrace_hash *hash; 2224 + int ret; 2225 + 2226 + /* All global ops uses the global ops filters */ 2227 + if (ops->flags & FTRACE_OPS_FL_GLOBAL) 2228 + ops = &global_ops; 2229 + 2824 2230 if (unlikely(ftrace_disabled)) 2825 - return; 2231 + return -ENODEV; 2232 + 2233 + if (enable) 2234 + orig_hash = &ops->filter_hash; 2235 + else 2236 + orig_hash = &ops->notrace_hash; 2237 + 2238 + hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); 2239 + if (!hash) 2240 + return -ENOMEM; 2826 2241 2827 2242 mutex_lock(&ftrace_regex_lock); 2828 2243 if (reset) 2829 - ftrace_filter_reset(enable); 2244 + ftrace_filter_reset(hash); 2830 2245 if (buf) 2831 - ftrace_match_records(buf, len, enable); 2246 + ftrace_match_records(hash, buf, len); 2247 + 2248 + mutex_lock(&ftrace_lock); 2249 + ret = ftrace_hash_move(orig_hash, hash); 2250 + mutex_unlock(&ftrace_lock); 2251 + 2832 2252 mutex_unlock(&ftrace_regex_lock); 2253 + 2254 + free_ftrace_hash(hash); 2255 + return ret; 2833 2256 } 2834 2257 2835 2258 /** 2836 2259 * ftrace_set_filter - set a function to filter on in ftrace 2260 + * @ops - the ops to set the filter with 2837 2261 * @buf - the string that holds the function filter text. 2838 2262 * @len - the length of the string. 2839 2263 * @reset - non zero to reset all filters before applying this filter. ··· 2868 2238 * Filters denote which functions should be enabled when tracing is enabled. 2869 2239 * If @buf is NULL and reset is set, all functions will be enabled for tracing. 2870 2240 */ 2871 - void ftrace_set_filter(unsigned char *buf, int len, int reset) 2241 + void ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf, 2242 + int len, int reset) 2872 2243 { 2873 - ftrace_set_regex(buf, len, reset, 1); 2244 + ftrace_set_regex(ops, buf, len, reset, 1); 2874 2245 } 2246 + EXPORT_SYMBOL_GPL(ftrace_set_filter); 2875 2247 2876 2248 /** 2877 2249 * ftrace_set_notrace - set a function to not trace in ftrace 2250 + * @ops - the ops to set the notrace filter with 2878 2251 * @buf - the string that holds the function notrace text. 2879 2252 * @len - the length of the string. 2880 2253 * @reset - non zero to reset all filters before applying this filter. ··· 2886 2253 * is enabled. If @buf is NULL and reset is set, all functions will be enabled 2887 2254 * for tracing. 2888 2255 */ 2889 - void ftrace_set_notrace(unsigned char *buf, int len, int reset) 2256 + void ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf, 2257 + int len, int reset) 2890 2258 { 2891 - ftrace_set_regex(buf, len, reset, 0); 2259 + ftrace_set_regex(ops, buf, len, reset, 0); 2892 2260 } 2261 + EXPORT_SYMBOL_GPL(ftrace_set_notrace); 2262 + /** 2263 + * ftrace_set_filter - set a function to filter on in ftrace 2264 + * @ops - the ops to set the filter with 2265 + * @buf - the string that holds the function filter text. 2266 + * @len - the length of the string. 2267 + * @reset - non zero to reset all filters before applying this filter. 2268 + * 2269 + * Filters denote which functions should be enabled when tracing is enabled. 2270 + * If @buf is NULL and reset is set, all functions will be enabled for tracing. 2271 + */ 2272 + void ftrace_set_global_filter(unsigned char *buf, int len, int reset) 2273 + { 2274 + ftrace_set_regex(&global_ops, buf, len, reset, 1); 2275 + } 2276 + EXPORT_SYMBOL_GPL(ftrace_set_global_filter); 2277 + 2278 + /** 2279 + * ftrace_set_notrace - set a function to not trace in ftrace 2280 + * @ops - the ops to set the notrace filter with 2281 + * @buf - the string that holds the function notrace text. 2282 + * @len - the length of the string. 2283 + * @reset - non zero to reset all filters before applying this filter. 2284 + * 2285 + * Notrace Filters denote which functions should not be enabled when tracing 2286 + * is enabled. If @buf is NULL and reset is set, all functions will be enabled 2287 + * for tracing. 2288 + */ 2289 + void ftrace_set_global_notrace(unsigned char *buf, int len, int reset) 2290 + { 2291 + ftrace_set_regex(&global_ops, buf, len, reset, 0); 2292 + } 2293 + EXPORT_SYMBOL_GPL(ftrace_set_global_notrace); 2893 2294 2894 2295 /* 2895 2296 * command line interface to allow users to set filters on boot up. ··· 2974 2307 } 2975 2308 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 2976 2309 2977 - static void __init set_ftrace_early_filter(char *buf, int enable) 2310 + static void __init 2311 + set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable) 2978 2312 { 2979 2313 char *func; 2980 2314 2981 2315 while (buf) { 2982 2316 func = strsep(&buf, ","); 2983 - ftrace_set_regex(func, strlen(func), 0, enable); 2317 + ftrace_set_regex(ops, func, strlen(func), 0, enable); 2984 2318 } 2985 2319 } 2986 2320 2987 2321 static void __init set_ftrace_early_filters(void) 2988 2322 { 2989 2323 if (ftrace_filter_buf[0]) 2990 - set_ftrace_early_filter(ftrace_filter_buf, 1); 2324 + set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1); 2991 2325 if (ftrace_notrace_buf[0]) 2992 - set_ftrace_early_filter(ftrace_notrace_buf, 0); 2326 + set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0); 2993 2327 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 2994 2328 if (ftrace_graph_buf[0]) 2995 2329 set_ftrace_early_graph(ftrace_graph_buf); ··· 2998 2330 } 2999 2331 3000 2332 static int 3001 - ftrace_regex_release(struct inode *inode, struct file *file, int enable) 2333 + ftrace_regex_release(struct inode *inode, struct file *file) 3002 2334 { 3003 2335 struct seq_file *m = (struct seq_file *)file->private_data; 3004 2336 struct ftrace_iterator *iter; 2337 + struct ftrace_hash **orig_hash; 3005 2338 struct trace_parser *parser; 2339 + int filter_hash; 2340 + int ret; 3006 2341 3007 2342 mutex_lock(&ftrace_regex_lock); 3008 2343 if (file->f_mode & FMODE_READ) { ··· 3018 2347 parser = &iter->parser; 3019 2348 if (trace_parser_loaded(parser)) { 3020 2349 parser->buffer[parser->idx] = 0; 3021 - ftrace_match_records(parser->buffer, parser->idx, enable); 2350 + ftrace_match_records(iter->hash, parser->buffer, parser->idx); 3022 2351 } 3023 2352 3024 2353 trace_parser_put(parser); 3025 - kfree(iter); 3026 2354 3027 2355 if (file->f_mode & FMODE_WRITE) { 2356 + filter_hash = !!(iter->flags & FTRACE_ITER_FILTER); 2357 + 2358 + if (filter_hash) 2359 + orig_hash = &iter->ops->filter_hash; 2360 + else 2361 + orig_hash = &iter->ops->notrace_hash; 2362 + 3028 2363 mutex_lock(&ftrace_lock); 3029 - if (ftrace_start_up && ftrace_enabled) 3030 - ftrace_run_update_code(FTRACE_ENABLE_CALLS); 2364 + /* 2365 + * Remove the current set, update the hash and add 2366 + * them back. 2367 + */ 2368 + ftrace_hash_rec_disable(iter->ops, filter_hash); 2369 + ret = ftrace_hash_move(orig_hash, iter->hash); 2370 + if (!ret) { 2371 + ftrace_hash_rec_enable(iter->ops, filter_hash); 2372 + if (iter->ops->flags & FTRACE_OPS_FL_ENABLED 2373 + && ftrace_enabled) 2374 + ftrace_run_update_code(FTRACE_ENABLE_CALLS); 2375 + } 3031 2376 mutex_unlock(&ftrace_lock); 3032 2377 } 2378 + free_ftrace_hash(iter->hash); 2379 + kfree(iter); 3033 2380 3034 2381 mutex_unlock(&ftrace_regex_lock); 3035 2382 return 0; 3036 2383 } 3037 2384 3038 - static int 3039 - ftrace_filter_release(struct inode *inode, struct file *file) 3040 - { 3041 - return ftrace_regex_release(inode, file, 1); 3042 - } 3043 - 3044 - static int 3045 - ftrace_notrace_release(struct inode *inode, struct file *file) 3046 - { 3047 - return ftrace_regex_release(inode, file, 0); 3048 - } 3049 - 3050 2385 static const struct file_operations ftrace_avail_fops = { 3051 2386 .open = ftrace_avail_open, 2387 + .read = seq_read, 2388 + .llseek = seq_lseek, 2389 + .release = seq_release_private, 2390 + }; 2391 + 2392 + static const struct file_operations ftrace_enabled_fops = { 2393 + .open = ftrace_enabled_open, 3052 2394 .read = seq_read, 3053 2395 .llseek = seq_lseek, 3054 2396 .release = seq_release_private, ··· 3072 2388 .read = seq_read, 3073 2389 .write = ftrace_filter_write, 3074 2390 .llseek = ftrace_regex_lseek, 3075 - .release = ftrace_filter_release, 2391 + .release = ftrace_regex_release, 3076 2392 }; 3077 2393 3078 2394 static const struct file_operations ftrace_notrace_fops = { ··· 3080 2396 .read = seq_read, 3081 2397 .write = ftrace_notrace_write, 3082 2398 .llseek = ftrace_regex_lseek, 3083 - .release = ftrace_notrace_release, 2399 + .release = ftrace_regex_release, 3084 2400 }; 3085 2401 3086 2402 #ifdef CONFIG_FUNCTION_GRAPH_TRACER ··· 3298 2614 trace_create_file("available_filter_functions", 0444, 3299 2615 d_tracer, NULL, &ftrace_avail_fops); 3300 2616 2617 + trace_create_file("enabled_functions", 0444, 2618 + d_tracer, NULL, &ftrace_enabled_fops); 2619 + 3301 2620 trace_create_file("set_ftrace_filter", 0644, d_tracer, 3302 2621 NULL, &ftrace_filter_fops); 3303 2622 ··· 3452 2765 3453 2766 #else 3454 2767 2768 + static struct ftrace_ops global_ops = { 2769 + .func = ftrace_stub, 2770 + }; 2771 + 3455 2772 static int __init ftrace_nodyn_init(void) 3456 2773 { 3457 2774 ftrace_enabled = 1; ··· 3466 2775 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; } 3467 2776 static inline void ftrace_startup_enable(int command) { } 3468 2777 /* Keep as macros so we do not need to define the commands */ 3469 - # define ftrace_startup(command) do { } while (0) 3470 - # define ftrace_shutdown(command) do { } while (0) 2778 + # define ftrace_startup(ops, command) do { } while (0) 2779 + # define ftrace_shutdown(ops, command) do { } while (0) 3471 2780 # define ftrace_startup_sysctl() do { } while (0) 3472 2781 # define ftrace_shutdown_sysctl() do { } while (0) 2782 + 2783 + static inline int 2784 + ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip) 2785 + { 2786 + return 1; 2787 + } 2788 + 3473 2789 #endif /* CONFIG_DYNAMIC_FTRACE */ 2790 + 2791 + static void 2792 + ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip) 2793 + { 2794 + struct ftrace_ops *op; 2795 + 2796 + /* 2797 + * Some of the ops may be dynamically allocated, 2798 + * they must be freed after a synchronize_sched(). 2799 + */ 2800 + preempt_disable_notrace(); 2801 + op = rcu_dereference_raw(ftrace_ops_list); 2802 + while (op != &ftrace_list_end) { 2803 + if (ftrace_ops_test(op, ip)) 2804 + op->func(ip, parent_ip); 2805 + op = rcu_dereference_raw(op->next); 2806 + }; 2807 + preempt_enable_notrace(); 2808 + } 3474 2809 3475 2810 static void clear_ftrace_swapper(void) 3476 2811 { ··· 3798 3081 goto out_unlock; 3799 3082 3800 3083 ret = __register_ftrace_function(ops); 3801 - ftrace_startup(0); 3084 + if (!ret) 3085 + ftrace_startup(ops, 0); 3086 + 3802 3087 3803 3088 out_unlock: 3804 3089 mutex_unlock(&ftrace_lock); 3805 3090 return ret; 3806 3091 } 3092 + EXPORT_SYMBOL_GPL(register_ftrace_function); 3807 3093 3808 3094 /** 3809 3095 * unregister_ftrace_function - unregister a function for profiling. ··· 3820 3100 3821 3101 mutex_lock(&ftrace_lock); 3822 3102 ret = __unregister_ftrace_function(ops); 3823 - ftrace_shutdown(0); 3103 + if (!ret) 3104 + ftrace_shutdown(ops, 0); 3824 3105 mutex_unlock(&ftrace_lock); 3825 3106 3826 3107 return ret; 3827 3108 } 3109 + EXPORT_SYMBOL_GPL(unregister_ftrace_function); 3828 3110 3829 3111 int 3830 3112 ftrace_enable_sysctl(struct ctl_table *table, int write, ··· 3852 3130 ftrace_startup_sysctl(); 3853 3131 3854 3132 /* we are starting ftrace again */ 3855 - if (ftrace_list != &ftrace_list_end) { 3856 - if (ftrace_list->next == &ftrace_list_end) 3857 - ftrace_trace_function = ftrace_list->func; 3133 + if (ftrace_ops_list != &ftrace_list_end) { 3134 + if (ftrace_ops_list->next == &ftrace_list_end) 3135 + ftrace_trace_function = ftrace_ops_list->func; 3858 3136 else 3859 - ftrace_trace_function = ftrace_list_func; 3137 + ftrace_trace_function = ftrace_ops_list_func; 3860 3138 } 3861 3139 3862 3140 } else { ··· 4045 3323 ftrace_graph_return = retfunc; 4046 3324 ftrace_graph_entry = entryfunc; 4047 3325 4048 - ftrace_startup(FTRACE_START_FUNC_RET); 3326 + ftrace_startup(&global_ops, FTRACE_START_FUNC_RET); 4049 3327 4050 3328 out: 4051 3329 mutex_unlock(&ftrace_lock); ··· 4062 3340 ftrace_graph_active--; 4063 3341 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub; 4064 3342 ftrace_graph_entry = ftrace_graph_entry_stub; 4065 - ftrace_shutdown(FTRACE_STOP_FUNC_RET); 3343 + ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET); 4066 3344 unregister_pm_notifier(&ftrace_suspend_notifier); 4067 3345 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); 4068 3346
+2
kernel/trace/trace.h
··· 419 419 extern unsigned long ftrace_update_tot_cnt; 420 420 #define DYN_FTRACE_TEST_NAME trace_selftest_dynamic_test_func 421 421 extern int DYN_FTRACE_TEST_NAME(void); 422 + #define DYN_FTRACE_TEST_NAME2 trace_selftest_dynamic_test_func2 423 + extern int DYN_FTRACE_TEST_NAME2(void); 422 424 #endif 423 425 424 426 extern int ring_buffer_expanded;
+2
kernel/trace/trace_functions.c
··· 149 149 static struct ftrace_ops trace_ops __read_mostly = 150 150 { 151 151 .func = function_trace_call, 152 + .flags = FTRACE_OPS_FL_GLOBAL, 152 153 }; 153 154 154 155 static struct ftrace_ops trace_stack_ops __read_mostly = 155 156 { 156 157 .func = function_stack_trace_call, 158 + .flags = FTRACE_OPS_FL_GLOBAL, 157 159 }; 158 160 159 161 /* Our two options */
+1
kernel/trace/trace_irqsoff.c
··· 153 153 static struct ftrace_ops trace_ops __read_mostly = 154 154 { 155 155 .func = irqsoff_tracer_call, 156 + .flags = FTRACE_OPS_FL_GLOBAL, 156 157 }; 157 158 #endif /* CONFIG_FUNCTION_TRACER */ 158 159
+1
kernel/trace/trace_sched_wakeup.c
··· 129 129 static struct ftrace_ops trace_ops __read_mostly = 130 130 { 131 131 .func = wakeup_tracer_call, 132 + .flags = FTRACE_OPS_FL_GLOBAL, 132 133 }; 133 134 #endif /* CONFIG_FUNCTION_TRACER */ 134 135
+211 -3
kernel/trace/trace_selftest.c
··· 101 101 102 102 #ifdef CONFIG_DYNAMIC_FTRACE 103 103 104 + static int trace_selftest_test_probe1_cnt; 105 + static void trace_selftest_test_probe1_func(unsigned long ip, 106 + unsigned long pip) 107 + { 108 + trace_selftest_test_probe1_cnt++; 109 + } 110 + 111 + static int trace_selftest_test_probe2_cnt; 112 + static void trace_selftest_test_probe2_func(unsigned long ip, 113 + unsigned long pip) 114 + { 115 + trace_selftest_test_probe2_cnt++; 116 + } 117 + 118 + static int trace_selftest_test_probe3_cnt; 119 + static void trace_selftest_test_probe3_func(unsigned long ip, 120 + unsigned long pip) 121 + { 122 + trace_selftest_test_probe3_cnt++; 123 + } 124 + 125 + static int trace_selftest_test_global_cnt; 126 + static void trace_selftest_test_global_func(unsigned long ip, 127 + unsigned long pip) 128 + { 129 + trace_selftest_test_global_cnt++; 130 + } 131 + 132 + static int trace_selftest_test_dyn_cnt; 133 + static void trace_selftest_test_dyn_func(unsigned long ip, 134 + unsigned long pip) 135 + { 136 + trace_selftest_test_dyn_cnt++; 137 + } 138 + 139 + static struct ftrace_ops test_probe1 = { 140 + .func = trace_selftest_test_probe1_func, 141 + }; 142 + 143 + static struct ftrace_ops test_probe2 = { 144 + .func = trace_selftest_test_probe2_func, 145 + }; 146 + 147 + static struct ftrace_ops test_probe3 = { 148 + .func = trace_selftest_test_probe3_func, 149 + }; 150 + 151 + static struct ftrace_ops test_global = { 152 + .func = trace_selftest_test_global_func, 153 + .flags = FTRACE_OPS_FL_GLOBAL, 154 + }; 155 + 156 + static void print_counts(void) 157 + { 158 + printk("(%d %d %d %d %d) ", 159 + trace_selftest_test_probe1_cnt, 160 + trace_selftest_test_probe2_cnt, 161 + trace_selftest_test_probe3_cnt, 162 + trace_selftest_test_global_cnt, 163 + trace_selftest_test_dyn_cnt); 164 + } 165 + 166 + static void reset_counts(void) 167 + { 168 + trace_selftest_test_probe1_cnt = 0; 169 + trace_selftest_test_probe2_cnt = 0; 170 + trace_selftest_test_probe3_cnt = 0; 171 + trace_selftest_test_global_cnt = 0; 172 + trace_selftest_test_dyn_cnt = 0; 173 + } 174 + 175 + static int trace_selftest_ops(int cnt) 176 + { 177 + int save_ftrace_enabled = ftrace_enabled; 178 + struct ftrace_ops *dyn_ops; 179 + char *func1_name; 180 + char *func2_name; 181 + int len1; 182 + int len2; 183 + int ret = -1; 184 + 185 + printk(KERN_CONT "PASSED\n"); 186 + pr_info("Testing dynamic ftrace ops #%d: ", cnt); 187 + 188 + ftrace_enabled = 1; 189 + reset_counts(); 190 + 191 + /* Handle PPC64 '.' name */ 192 + func1_name = "*" __stringify(DYN_FTRACE_TEST_NAME); 193 + func2_name = "*" __stringify(DYN_FTRACE_TEST_NAME2); 194 + len1 = strlen(func1_name); 195 + len2 = strlen(func2_name); 196 + 197 + /* 198 + * Probe 1 will trace function 1. 199 + * Probe 2 will trace function 2. 200 + * Probe 3 will trace functions 1 and 2. 201 + */ 202 + ftrace_set_filter(&test_probe1, func1_name, len1, 1); 203 + ftrace_set_filter(&test_probe2, func2_name, len2, 1); 204 + ftrace_set_filter(&test_probe3, func1_name, len1, 1); 205 + ftrace_set_filter(&test_probe3, func2_name, len2, 0); 206 + 207 + register_ftrace_function(&test_probe1); 208 + register_ftrace_function(&test_probe2); 209 + register_ftrace_function(&test_probe3); 210 + register_ftrace_function(&test_global); 211 + 212 + DYN_FTRACE_TEST_NAME(); 213 + 214 + print_counts(); 215 + 216 + if (trace_selftest_test_probe1_cnt != 1) 217 + goto out; 218 + if (trace_selftest_test_probe2_cnt != 0) 219 + goto out; 220 + if (trace_selftest_test_probe3_cnt != 1) 221 + goto out; 222 + if (trace_selftest_test_global_cnt == 0) 223 + goto out; 224 + 225 + DYN_FTRACE_TEST_NAME2(); 226 + 227 + print_counts(); 228 + 229 + if (trace_selftest_test_probe1_cnt != 1) 230 + goto out; 231 + if (trace_selftest_test_probe2_cnt != 1) 232 + goto out; 233 + if (trace_selftest_test_probe3_cnt != 2) 234 + goto out; 235 + 236 + /* Add a dynamic probe */ 237 + dyn_ops = kzalloc(sizeof(*dyn_ops), GFP_KERNEL); 238 + if (!dyn_ops) { 239 + printk("MEMORY ERROR "); 240 + goto out; 241 + } 242 + 243 + dyn_ops->func = trace_selftest_test_dyn_func; 244 + 245 + register_ftrace_function(dyn_ops); 246 + 247 + trace_selftest_test_global_cnt = 0; 248 + 249 + DYN_FTRACE_TEST_NAME(); 250 + 251 + print_counts(); 252 + 253 + if (trace_selftest_test_probe1_cnt != 2) 254 + goto out_free; 255 + if (trace_selftest_test_probe2_cnt != 1) 256 + goto out_free; 257 + if (trace_selftest_test_probe3_cnt != 3) 258 + goto out_free; 259 + if (trace_selftest_test_global_cnt == 0) 260 + goto out; 261 + if (trace_selftest_test_dyn_cnt == 0) 262 + goto out_free; 263 + 264 + DYN_FTRACE_TEST_NAME2(); 265 + 266 + print_counts(); 267 + 268 + if (trace_selftest_test_probe1_cnt != 2) 269 + goto out_free; 270 + if (trace_selftest_test_probe2_cnt != 2) 271 + goto out_free; 272 + if (trace_selftest_test_probe3_cnt != 4) 273 + goto out_free; 274 + 275 + ret = 0; 276 + out_free: 277 + unregister_ftrace_function(dyn_ops); 278 + kfree(dyn_ops); 279 + 280 + out: 281 + /* Purposely unregister in the same order */ 282 + unregister_ftrace_function(&test_probe1); 283 + unregister_ftrace_function(&test_probe2); 284 + unregister_ftrace_function(&test_probe3); 285 + unregister_ftrace_function(&test_global); 286 + 287 + /* Make sure everything is off */ 288 + reset_counts(); 289 + DYN_FTRACE_TEST_NAME(); 290 + DYN_FTRACE_TEST_NAME(); 291 + 292 + if (trace_selftest_test_probe1_cnt || 293 + trace_selftest_test_probe2_cnt || 294 + trace_selftest_test_probe3_cnt || 295 + trace_selftest_test_global_cnt || 296 + trace_selftest_test_dyn_cnt) 297 + ret = -1; 298 + 299 + ftrace_enabled = save_ftrace_enabled; 300 + 301 + return ret; 302 + } 303 + 104 304 /* Test dynamic code modification and ftrace filters */ 105 305 int trace_selftest_startup_dynamic_tracing(struct tracer *trace, 106 306 struct trace_array *tr, ··· 331 131 func_name = "*" __stringify(DYN_FTRACE_TEST_NAME); 332 132 333 133 /* filter only on our function */ 334 - ftrace_set_filter(func_name, strlen(func_name), 1); 134 + ftrace_set_global_filter(func_name, strlen(func_name), 1); 335 135 336 136 /* enable tracing */ 337 137 ret = tracer_init(trace, tr); ··· 366 166 367 167 /* check the trace buffer */ 368 168 ret = trace_test_buffer(tr, &count); 369 - trace->reset(tr); 370 169 tracing_start(); 371 170 372 171 /* we should only have one item */ 373 172 if (!ret && count != 1) { 173 + trace->reset(tr); 374 174 printk(KERN_CONT ".. filter failed count=%ld ..", count); 375 175 ret = -1; 376 176 goto out; 377 177 } 178 + 179 + /* Test the ops with global tracing running */ 180 + ret = trace_selftest_ops(1); 181 + trace->reset(tr); 378 182 379 183 out: 380 184 ftrace_enabled = save_ftrace_enabled; 381 185 tracer_enabled = save_tracer_enabled; 382 186 383 187 /* Enable tracing on all functions again */ 384 - ftrace_set_filter(NULL, 0, 1); 188 + ftrace_set_global_filter(NULL, 0, 1); 189 + 190 + /* Test the ops with global tracing off */ 191 + if (!ret) 192 + ret = trace_selftest_ops(2); 385 193 386 194 return ret; 387 195 }
+6
kernel/trace/trace_selftest_dynamic.c
··· 5 5 /* used to call mcount */ 6 6 return 0; 7 7 } 8 + 9 + int DYN_FTRACE_TEST_NAME2(void) 10 + { 11 + /* used to call mcount */ 12 + return 0; 13 + }
+1
kernel/trace/trace_stack.c
··· 133 133 static struct ftrace_ops trace_ops __read_mostly = 134 134 { 135 135 .func = stack_trace_call, 136 + .flags = FTRACE_OPS_FL_GLOBAL, 136 137 }; 137 138 138 139 static ssize_t