hc
2024-05-10 61598093bbdd283a7edc367d900f223070ead8d2
kernel/kernel/trace/ftrace.c
....@@ -18,8 +18,8 @@
1818 #include <linux/clocksource.h>
1919 #include <linux/sched/task.h>
2020 #include <linux/kallsyms.h>
21
+#include <linux/security.h>
2122 #include <linux/seq_file.h>
22
-#include <linux/suspend.h>
2323 #include <linux/tracefs.h>
2424 #include <linux/hardirq.h>
2525 #include <linux/kthread.h>
....@@ -41,6 +41,7 @@
4141 #include <asm/sections.h>
4242 #include <asm/setup.h>
4343
44
+#include "ftrace_internal.h"
4445 #include "trace_output.h"
4546 #include "trace_stat.h"
4647
....@@ -61,8 +62,6 @@
6162 })
6263
6364 /* hash bits for specific function selection */
64
-#define FTRACE_HASH_BITS 7
65
-#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
6665 #define FTRACE_HASH_DEFAULT_BITS 10
6766 #define FTRACE_HASH_MAX_BITS 12
6867
....@@ -70,15 +69,16 @@
7069 #define INIT_OPS_HASH(opsname) \
7170 .func_hash = &opsname.local_hash, \
7271 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
73
-#define ASSIGN_OPS_HASH(opsname, val) \
74
- .func_hash = val, \
75
- .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
7672 #else
7773 #define INIT_OPS_HASH(opsname)
78
-#define ASSIGN_OPS_HASH(opsname, val)
7974 #endif
8075
81
-static struct ftrace_ops ftrace_list_end __read_mostly = {
76
+enum {
77
+ FTRACE_MODIFY_ENABLE_FL = (1 << 0),
78
+ FTRACE_MODIFY_MAY_SLEEP_FL = (1 << 1),
79
+};
80
+
81
+struct ftrace_ops ftrace_list_end __read_mostly = {
8282 .func = ftrace_stub,
8383 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
8484 INIT_OPS_HASH(ftrace_list_end)
....@@ -102,7 +102,7 @@
102102
103103 tr = ops->private;
104104
105
- return tr->function_pids != NULL;
105
+ return tr->function_pids != NULL || tr->function_no_pids != NULL;
106106 }
107107
108108 static void ftrace_update_trampoline(struct ftrace_ops *ops);
....@@ -113,41 +113,20 @@
113113 */
114114 static int ftrace_disabled __read_mostly;
115115
116
-static DEFINE_MUTEX(ftrace_lock);
116
+DEFINE_MUTEX(ftrace_lock);
117117
118
-static struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
118
+struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
119119 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
120
-static struct ftrace_ops global_ops;
120
+struct ftrace_ops global_ops;
121121
122122 #if ARCH_SUPPORTS_FTRACE_OPS
123123 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
124124 struct ftrace_ops *op, struct pt_regs *regs);
125125 #else
126126 /* See comment below, where ftrace_ops_list_func is defined */
127
-static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip,
128
- struct ftrace_ops *op, struct pt_regs *regs);
129
-#define ftrace_ops_list_func ftrace_ops_no_ops
127
+static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
128
+#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
130129 #endif
131
-
132
-/*
133
- * Traverse the ftrace_global_list, invoking all entries. The reason that we
134
- * can use rcu_dereference_raw_notrace() is that elements removed from this list
135
- * are simply leaked, so there is no need to interact with a grace-period
136
- * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
137
- * concurrent insertions into the ftrace_global_list.
138
- *
139
- * Silly Alpha and silly pointer-speculation compiler optimizations!
140
- */
141
-#define do_for_each_ftrace_op(op, list) \
142
- op = rcu_dereference_raw_notrace(list); \
143
- do
144
-
145
-/*
146
- * Optimized for just a single item in the list (as that is the normal case).
147
- */
148
-#define while_for_each_ftrace_op(op) \
149
- while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
150
- unlikely((op) != &ftrace_list_end))
151130
152131 static inline void ftrace_ops_init(struct ftrace_ops *ops)
153132 {
....@@ -164,22 +143,18 @@
164143 struct ftrace_ops *op, struct pt_regs *regs)
165144 {
166145 struct trace_array *tr = op->private;
146
+ int pid;
167147
168
- if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
169
- return;
148
+ if (tr) {
149
+ pid = this_cpu_read(tr->array_buffer.data->ftrace_ignore_pid);
150
+ if (pid == FTRACE_PID_IGNORE)
151
+ return;
152
+ if (pid != FTRACE_PID_TRACE &&
153
+ pid != current->pid)
154
+ return;
155
+ }
170156
171157 op->saved_func(ip, parent_ip, op, regs);
172
-}
173
-
174
-static void ftrace_sync(struct work_struct *work)
175
-{
176
- /*
177
- * This function is just a stub to implement a hard force
178
- * of synchronize_sched(). This requires synchronizing
179
- * tasks even in userspace and idle.
180
- *
181
- * Yes, function tracing is rude.
182
- */
183158 }
184159
185160 static void ftrace_sync_ipi(void *data)
....@@ -187,18 +162,6 @@
187162 /* Probably not needed, but do it anyway */
188163 smp_rmb();
189164 }
190
-
191
-#ifdef CONFIG_FUNCTION_GRAPH_TRACER
192
-static void update_function_graph_func(void);
193
-
194
-/* Both enabled by default (can be cleared by function_graph tracer flags */
195
-static bool fgraph_sleep_time = true;
196
-static bool fgraph_graph_time = true;
197
-
198
-#else
199
-static inline void update_function_graph_func(void) { }
200
-#endif
201
-
202165
203166 static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
204167 {
....@@ -267,7 +230,7 @@
267230 /*
268231 * For static tracing, we need to be a bit more careful.
269232 * The function change takes affect immediately. Thus,
270
- * we need to coorditate the setting of the function_trace_ops
233
+ * we need to coordinate the setting of the function_trace_ops
271234 * with the setting of the ftrace_trace_function.
272235 *
273236 * Set the function to the list ops, which will call the
....@@ -279,7 +242,7 @@
279242 * Make sure all CPUs see this. Yes this is slow, but static
280243 * tracing is slow and nasty to have enabled.
281244 */
282
- schedule_on_each_cpu(ftrace_sync);
245
+ synchronize_rcu_tasks_rude();
283246 /* Now all cpus are using the list ops. */
284247 function_trace_op = set_function_trace_op;
285248 /* Make sure the function_trace_op is visible on all CPUs */
....@@ -336,7 +299,7 @@
336299
337300 static void ftrace_update_trampoline(struct ftrace_ops *ops);
338301
339
-static int __register_ftrace_function(struct ftrace_ops *ops)
302
+int __register_ftrace_function(struct ftrace_ops *ops)
340303 {
341304 if (ops->flags & FTRACE_OPS_FL_DELETED)
342305 return -EINVAL;
....@@ -357,6 +320,8 @@
357320 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
358321 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
359322 #endif
323
+ if (!ftrace_enabled && (ops->flags & FTRACE_OPS_FL_PERMANENT))
324
+ return -EBUSY;
360325
361326 if (!core_kernel_data((unsigned long)ops))
362327 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
....@@ -377,7 +342,7 @@
377342 return 0;
378343 }
379344
380
-static int __unregister_ftrace_function(struct ftrace_ops *ops)
345
+int __unregister_ftrace_function(struct ftrace_ops *ops)
381346 {
382347 int ret;
383348
....@@ -494,10 +459,10 @@
494459
495460 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
496461 /* function graph compares on total time */
497
-static int function_stat_cmp(void *p1, void *p2)
462
+static int function_stat_cmp(const void *p1, const void *p2)
498463 {
499
- struct ftrace_profile *a = p1;
500
- struct ftrace_profile *b = p2;
464
+ const struct ftrace_profile *a = p1;
465
+ const struct ftrace_profile *b = p2;
501466
502467 if (a->time < b->time)
503468 return -1;
....@@ -508,10 +473,10 @@
508473 }
509474 #else
510475 /* not function graph compares against hits */
511
-static int function_stat_cmp(void *p1, void *p2)
476
+static int function_stat_cmp(const void *p1, const void *p2)
512477 {
513
- struct ftrace_profile *a = p1;
514
- struct ftrace_profile *b = p2;
478
+ const struct ftrace_profile *a = p1;
479
+ const struct ftrace_profile *b = p2;
515480
516481 if (a->counter < b->counter)
517482 return -1;
....@@ -817,9 +782,16 @@
817782 }
818783
819784 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
785
+static bool fgraph_graph_time = true;
786
+
787
+void ftrace_graph_graph_time_control(bool enable)
788
+{
789
+ fgraph_graph_time = enable;
790
+}
791
+
820792 static int profile_graph_entry(struct ftrace_graph_ent *trace)
821793 {
822
- int index = current->curr_ret_stack;
794
+ struct ftrace_ret_stack *ret_stack;
823795
824796 function_profile_call(trace->func, 0, NULL, NULL);
825797
....@@ -827,14 +799,16 @@
827799 if (!current->ret_stack)
828800 return 0;
829801
830
- if (index >= 0 && index < FTRACE_RETFUNC_DEPTH)
831
- current->ret_stack[index].subtime = 0;
802
+ ret_stack = ftrace_graph_get_ret_stack(current, 0);
803
+ if (ret_stack)
804
+ ret_stack->subtime = 0;
832805
833806 return 1;
834807 }
835808
836809 static void profile_graph_return(struct ftrace_graph_ret *trace)
837810 {
811
+ struct ftrace_ret_stack *ret_stack;
838812 struct ftrace_profile_stat *stat;
839813 unsigned long long calltime;
840814 struct ftrace_profile *rec;
....@@ -852,16 +826,15 @@
852826 calltime = trace->rettime - trace->calltime;
853827
854828 if (!fgraph_graph_time) {
855
- int index;
856
-
857
- index = current->curr_ret_stack;
858829
859830 /* Append this call time to the parent time to subtract */
860
- if (index)
861
- current->ret_stack[index - 1].subtime += calltime;
831
+ ret_stack = ftrace_graph_get_ret_stack(current, 1);
832
+ if (ret_stack)
833
+ ret_stack->subtime += calltime;
862834
863
- if (current->ret_stack[index].subtime < calltime)
864
- calltime -= current->ret_stack[index].subtime;
835
+ ret_stack = ftrace_graph_get_ret_stack(current, 0);
836
+ if (ret_stack && ret_stack->subtime < calltime)
837
+ calltime -= ret_stack->subtime;
865838 else
866839 calltime = 0;
867840 }
....@@ -876,15 +849,19 @@
876849 local_irq_restore(flags);
877850 }
878851
852
+static struct fgraph_ops fprofiler_ops = {
853
+ .entryfunc = &profile_graph_entry,
854
+ .retfunc = &profile_graph_return,
855
+};
856
+
879857 static int register_ftrace_profiler(void)
880858 {
881
- return register_ftrace_graph(&profile_graph_return,
882
- &profile_graph_entry);
859
+ return register_ftrace_graph(&fprofiler_ops);
883860 }
884861
885862 static void unregister_ftrace_profiler(void)
886863 {
887
- unregister_ftrace_graph();
864
+ unregister_ftrace_graph(&fprofiler_ops);
888865 }
889866 #else
890867 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
....@@ -936,7 +913,7 @@
936913 ftrace_profile_enabled = 0;
937914 /*
938915 * unregister_ftrace_profiler calls stop_machine
939
- * so this acts like an synchronize_sched.
916
+ * so this acts like an synchronize_rcu.
940917 */
941918 unregister_ftrace_profiler();
942919 }
....@@ -1023,12 +1000,6 @@
10231000 }
10241001 #endif /* CONFIG_FUNCTION_PROFILER */
10251002
1026
-#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1027
-static int ftrace_graph_active;
1028
-#else
1029
-# define ftrace_graph_active 0
1030
-#endif
1031
-
10321003 #ifdef CONFIG_DYNAMIC_FTRACE
10331004
10341005 static struct ftrace_ops *removed_ops;
....@@ -1042,11 +1013,6 @@
10421013 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
10431014 # error Dynamic ftrace depends on MCOUNT_RECORD
10441015 #endif
1045
-
1046
-struct ftrace_func_entry {
1047
- struct hlist_node hlist;
1048
- unsigned long ip;
1049
-};
10501016
10511017 struct ftrace_func_probe {
10521018 struct ftrace_probe_ops *probe_ops;
....@@ -1069,7 +1035,7 @@
10691035 };
10701036 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
10711037
1072
-static struct ftrace_ops global_ops = {
1038
+struct ftrace_ops global_ops = {
10731039 .func = ftrace_stub,
10741040 .local_hash.notrace_hash = EMPTY_HASH,
10751041 .local_hash.filter_hash = EMPTY_HASH,
....@@ -1088,7 +1054,7 @@
10881054
10891055 /*
10901056 * Some of the ops may be dynamically allocated,
1091
- * they are freed after a synchronize_sched().
1057
+ * they are freed after a synchronize_rcu().
10921058 */
10931059 preempt_disable_notrace();
10941060
....@@ -1125,14 +1091,11 @@
11251091 struct ftrace_page *next;
11261092 struct dyn_ftrace *records;
11271093 int index;
1128
- int size;
1094
+ int order;
11291095 };
11301096
11311097 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
11321098 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1133
-
1134
-/* estimate from running different kernels */
1135
-#define NR_TO_INIT 10000
11361099
11371100 static struct ftrace_page *ftrace_pages_start;
11381101 static struct ftrace_page *ftrace_pages;
....@@ -1288,7 +1251,7 @@
12881251 {
12891252 if (!hash || hash == EMPTY_HASH)
12901253 return;
1291
- call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1254
+ call_rcu(&hash->rcu, __free_ftrace_hash_rcu);
12921255 }
12931256
12941257 void ftrace_free_filter(struct ftrace_ops *ops)
....@@ -1332,6 +1295,7 @@
13321295 if (!ftrace_mod)
13331296 return -ENOMEM;
13341297
1298
+ INIT_LIST_HEAD(&ftrace_mod->list);
13351299 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
13361300 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
13371301 ftrace_mod->enable = enable;
....@@ -1395,28 +1359,20 @@
13951359 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
13961360 struct ftrace_hash *new_hash);
13971361
1398
-static struct ftrace_hash *
1399
-__ftrace_hash_move(struct ftrace_hash *src)
1362
+static struct ftrace_hash *dup_hash(struct ftrace_hash *src, int size)
14001363 {
14011364 struct ftrace_func_entry *entry;
1402
- struct hlist_node *tn;
1403
- struct hlist_head *hhd;
14041365 struct ftrace_hash *new_hash;
1405
- int size = src->count;
1366
+ struct hlist_head *hhd;
1367
+ struct hlist_node *tn;
14061368 int bits = 0;
14071369 int i;
14081370
14091371 /*
1410
- * If the new source is empty, just return the empty_hash.
1372
+ * Use around half the size (max bit of it), but
1373
+ * a minimum of 2 is fine (as size of 0 or 1 both give 1 for bits).
14111374 */
1412
- if (ftrace_hash_empty(src))
1413
- return EMPTY_HASH;
1414
-
1415
- /*
1416
- * Make the hash size about 1/2 the # found
1417
- */
1418
- for (size /= 2; size; size >>= 1)
1419
- bits++;
1375
+ bits = fls(size / 2);
14201376
14211377 /* Don't allocate too much */
14221378 if (bits > FTRACE_HASH_MAX_BITS)
....@@ -1436,8 +1392,21 @@
14361392 __add_hash_entry(new_hash, entry);
14371393 }
14381394 }
1439
-
14401395 return new_hash;
1396
+}
1397
+
1398
+static struct ftrace_hash *
1399
+__ftrace_hash_move(struct ftrace_hash *src)
1400
+{
1401
+ int size = src->count;
1402
+
1403
+ /*
1404
+ * If the new source is empty, just return the empty_hash.
1405
+ */
1406
+ if (ftrace_hash_empty(src))
1407
+ return EMPTY_HASH;
1408
+
1409
+ return dup_hash(src, size);
14411410 }
14421411
14431412 static int
....@@ -1483,7 +1452,7 @@
14831452 {
14841453 /*
14851454 * The function record is a match if it exists in the filter
1486
- * hash and not in the notrace hash. Note, an emty hash is
1455
+ * hash and not in the notrace hash. Note, an empty hash is
14871456 * considered a match for the filter hash, but an empty
14881457 * notrace hash is considered not in the notrace hash.
14891458 */
....@@ -1503,9 +1472,9 @@
15031472 * the ip is not in the ops->notrace_hash.
15041473 *
15051474 * This needs to be called with preemption disabled as
1506
- * the hashes are freed with call_rcu_sched().
1475
+ * the hashes are freed with call_rcu().
15071476 */
1508
-static int
1477
+int
15091478 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
15101479 {
15111480 struct ftrace_ops_hash hash;
....@@ -1559,6 +1528,29 @@
15591528 return 0;
15601529 }
15611530
1531
+static struct dyn_ftrace *lookup_rec(unsigned long start, unsigned long end)
1532
+{
1533
+ struct ftrace_page *pg;
1534
+ struct dyn_ftrace *rec = NULL;
1535
+ struct dyn_ftrace key;
1536
+
1537
+ key.ip = start;
1538
+ key.flags = end; /* overload flags, as it is unsigned long */
1539
+
1540
+ for (pg = ftrace_pages_start; pg; pg = pg->next) {
1541
+ if (pg->index == 0 ||
1542
+ end < pg->records[0].ip ||
1543
+ start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1544
+ continue;
1545
+ rec = bsearch(&key, pg->records, pg->index,
1546
+ sizeof(struct dyn_ftrace),
1547
+ ftrace_cmp_recs);
1548
+ if (rec)
1549
+ break;
1550
+ }
1551
+ return rec;
1552
+}
1553
+
15621554 /**
15631555 * ftrace_location_range - return the first address of a traced location
15641556 * if it touches the given ip range
....@@ -1573,23 +1565,11 @@
15731565 */
15741566 unsigned long ftrace_location_range(unsigned long start, unsigned long end)
15751567 {
1576
- struct ftrace_page *pg;
15771568 struct dyn_ftrace *rec;
1578
- struct dyn_ftrace key;
15791569
1580
- key.ip = start;
1581
- key.flags = end; /* overload flags, as it is unsigned long */
1582
-
1583
- for (pg = ftrace_pages_start; pg; pg = pg->next) {
1584
- if (end < pg->records[0].ip ||
1585
- start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1586
- continue;
1587
- rec = bsearch(&key, pg->records, pg->index,
1588
- sizeof(struct dyn_ftrace),
1589
- ftrace_cmp_recs);
1590
- if (rec)
1591
- return rec->ip;
1592
- }
1570
+ rec = lookup_rec(start, end);
1571
+ if (rec)
1572
+ return rec->ip;
15931573
15941574 return 0;
15951575 }
....@@ -1742,6 +1722,9 @@
17421722 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
17431723 return false;
17441724
1725
+ if (ops->flags & FTRACE_OPS_FL_DIRECT)
1726
+ rec->flags |= FTRACE_FL_DIRECT;
1727
+
17451728 /*
17461729 * If there's only a single callback registered to a
17471730 * function, and the ops has a trampoline registered
....@@ -1768,6 +1751,15 @@
17681751 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
17691752 return false;
17701753 rec->flags--;
1754
+
1755
+ /*
1756
+ * Only the internal direct_ops should have the
1757
+ * DIRECT flag set. Thus, if it is removing a
1758
+ * function, then that function should no longer
1759
+ * be direct.
1760
+ */
1761
+ if (ops->flags & FTRACE_OPS_FL_DIRECT)
1762
+ rec->flags &= ~FTRACE_FL_DIRECT;
17711763
17721764 /*
17731765 * If the rec had REGS enabled and the ops that is
....@@ -1803,7 +1795,7 @@
18031795 count++;
18041796
18051797 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1806
- update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1798
+ update |= ftrace_test_record(rec, true) != FTRACE_UPDATE_IGNORE;
18071799
18081800 /* Shortcut, if we handled all records, we are done. */
18091801 if (!all && count == hash->count)
....@@ -1981,7 +1973,7 @@
19811973 char ins[MCOUNT_INSN_SIZE];
19821974 int i;
19831975
1984
- if (probe_kernel_read(ins, p, MCOUNT_INSN_SIZE)) {
1976
+ if (copy_from_kernel_nofault(ins, p, MCOUNT_INSN_SIZE)) {
19851977 printk(KERN_CONT "%s[FAULT] %px\n", fmt, p);
19861978 return;
19871979 }
....@@ -2025,22 +2017,22 @@
20252017 * modifying the code. @failed should be one of either:
20262018 * EFAULT - if the problem happens on reading the @ip address
20272019 * EINVAL - if what is read at @ip is not what was expected
2028
- * EPERM - if the problem happens on writting to the @ip address
2020
+ * EPERM - if the problem happens on writing to the @ip address
20292021 */
20302022 void ftrace_bug(int failed, struct dyn_ftrace *rec)
20312023 {
20322024 unsigned long ip = rec ? rec->ip : 0;
20332025
2026
+ pr_info("------------[ ftrace bug ]------------\n");
2027
+
20342028 switch (failed) {
20352029 case -EFAULT:
2036
- FTRACE_WARN_ON_ONCE(1);
20372030 pr_info("ftrace faulted on modifying ");
2038
- print_ip_sym(ip);
2031
+ print_ip_sym(KERN_INFO, ip);
20392032 break;
20402033 case -EINVAL:
2041
- FTRACE_WARN_ON_ONCE(1);
20422034 pr_info("ftrace failed to modify ");
2043
- print_ip_sym(ip);
2035
+ print_ip_sym(KERN_INFO, ip);
20442036 print_ip_ins(" actual: ", (unsigned char *)ip);
20452037 pr_cont("\n");
20462038 if (ftrace_expected) {
....@@ -2049,14 +2041,12 @@
20492041 }
20502042 break;
20512043 case -EPERM:
2052
- FTRACE_WARN_ON_ONCE(1);
20532044 pr_info("ftrace faulted on writing ");
2054
- print_ip_sym(ip);
2045
+ print_ip_sym(KERN_INFO, ip);
20552046 break;
20562047 default:
2057
- FTRACE_WARN_ON_ONCE(1);
20582048 pr_info("ftrace faulted on unknown error ");
2059
- print_ip_sym(ip);
2049
+ print_ip_sym(KERN_INFO, ip);
20602050 }
20612051 print_bug_type();
20622052 if (rec) {
....@@ -2081,9 +2071,11 @@
20812071 ip = ftrace_get_addr_curr(rec);
20822072 pr_cont("\n expected tramp: %lx\n", ip);
20832073 }
2074
+
2075
+ FTRACE_WARN_ON_ONCE(1);
20842076 }
20852077
2086
-static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
2078
+static int ftrace_check_record(struct dyn_ftrace *rec, bool enable, bool update)
20872079 {
20882080 unsigned long flag = 0UL;
20892081
....@@ -2110,15 +2102,34 @@
21102102 * If enabling and the REGS flag does not match the REGS_EN, or
21112103 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
21122104 * this record. Set flags to fail the compare against ENABLED.
2105
+ * Same for direct calls.
21132106 */
21142107 if (flag) {
2115
- if (!(rec->flags & FTRACE_FL_REGS) !=
2108
+ if (!(rec->flags & FTRACE_FL_REGS) !=
21162109 !(rec->flags & FTRACE_FL_REGS_EN))
21172110 flag |= FTRACE_FL_REGS;
21182111
2119
- if (!(rec->flags & FTRACE_FL_TRAMP) !=
2112
+ if (!(rec->flags & FTRACE_FL_TRAMP) !=
21202113 !(rec->flags & FTRACE_FL_TRAMP_EN))
21212114 flag |= FTRACE_FL_TRAMP;
2115
+
2116
+ /*
2117
+ * Direct calls are special, as count matters.
2118
+ * We must test the record for direct, if the
2119
+ * DIRECT and DIRECT_EN do not match, but only
2120
+ * if the count is 1. That's because, if the
2121
+ * count is something other than one, we do not
2122
+ * want the direct enabled (it will be done via the
2123
+ * direct helper). But if DIRECT_EN is set, and
2124
+ * the count is not one, we need to clear it.
2125
+ */
2126
+ if (ftrace_rec_count(rec) == 1) {
2127
+ if (!(rec->flags & FTRACE_FL_DIRECT) !=
2128
+ !(rec->flags & FTRACE_FL_DIRECT_EN))
2129
+ flag |= FTRACE_FL_DIRECT;
2130
+ } else if (rec->flags & FTRACE_FL_DIRECT_EN) {
2131
+ flag |= FTRACE_FL_DIRECT;
2132
+ }
21222133 }
21232134
21242135 /* If the state of this record hasn't changed, then do nothing */
....@@ -2142,6 +2153,25 @@
21422153 rec->flags |= FTRACE_FL_TRAMP_EN;
21432154 else
21442155 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2156
+ }
2157
+ if (flag & FTRACE_FL_DIRECT) {
2158
+ /*
2159
+ * If there's only one user (direct_ops helper)
2160
+ * then we can call the direct function
2161
+ * directly (no ftrace trampoline).
2162
+ */
2163
+ if (ftrace_rec_count(rec) == 1) {
2164
+ if (rec->flags & FTRACE_FL_DIRECT)
2165
+ rec->flags |= FTRACE_FL_DIRECT_EN;
2166
+ else
2167
+ rec->flags &= ~FTRACE_FL_DIRECT_EN;
2168
+ } else {
2169
+ /*
2170
+ * Can only call directly if there's
2171
+ * only one callback to the function.
2172
+ */
2173
+ rec->flags &= ~FTRACE_FL_DIRECT_EN;
2174
+ }
21452175 }
21462176 }
21472177
....@@ -2172,7 +2202,7 @@
21722202 * and REGS states. The _EN flags must be disabled though.
21732203 */
21742204 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2175
- FTRACE_FL_REGS_EN);
2205
+ FTRACE_FL_REGS_EN | FTRACE_FL_DIRECT_EN);
21762206 }
21772207
21782208 ftrace_bug_type = FTRACE_BUG_NOP;
....@@ -2182,28 +2212,28 @@
21822212 /**
21832213 * ftrace_update_record, set a record that now is tracing or not
21842214 * @rec: the record to update
2185
- * @enable: set to 1 if the record is tracing, zero to force disable
2215
+ * @enable: set to true if the record is tracing, false to force disable
21862216 *
21872217 * The records that represent all functions that can be traced need
21882218 * to be updated when tracing has been enabled.
21892219 */
2190
-int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2220
+int ftrace_update_record(struct dyn_ftrace *rec, bool enable)
21912221 {
2192
- return ftrace_check_record(rec, enable, 1);
2222
+ return ftrace_check_record(rec, enable, true);
21932223 }
21942224
21952225 /**
21962226 * ftrace_test_record, check if the record has been enabled or not
21972227 * @rec: the record to test
2198
- * @enable: set to 1 to check if enabled, 0 if it is disabled
2228
+ * @enable: set to true to check if enabled, false if it is disabled
21992229 *
22002230 * The arch code may need to test if a record is already set to
22012231 * tracing to determine how to modify the function code that it
22022232 * represents.
22032233 */
2204
-int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2234
+int ftrace_test_record(struct dyn_ftrace *rec, bool enable)
22052235 {
2206
- return ftrace_check_record(rec, enable, 0);
2236
+ return ftrace_check_record(rec, enable, false);
22072237 }
22082238
22092239 static struct ftrace_ops *
....@@ -2255,7 +2285,7 @@
22552285
22562286 if (hash_contains_ip(ip, op->func_hash))
22572287 return op;
2258
- }
2288
+ }
22592289
22602290 return NULL;
22612291 }
....@@ -2345,19 +2375,77 @@
23452375 return NULL;
23462376 }
23472377
2378
+#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
2379
+/* Protected by rcu_tasks for reading, and direct_mutex for writing */
2380
+static struct ftrace_hash *direct_functions = EMPTY_HASH;
2381
+static DEFINE_MUTEX(direct_mutex);
2382
+int ftrace_direct_func_count;
2383
+
2384
+/*
2385
+ * Search the direct_functions hash to see if the given instruction pointer
2386
+ * has a direct caller attached to it.
2387
+ */
2388
+unsigned long ftrace_find_rec_direct(unsigned long ip)
2389
+{
2390
+ struct ftrace_func_entry *entry;
2391
+
2392
+ entry = __ftrace_lookup_ip(direct_functions, ip);
2393
+ if (!entry)
2394
+ return 0;
2395
+
2396
+ return entry->direct;
2397
+}
2398
+
2399
+static void call_direct_funcs(unsigned long ip, unsigned long pip,
2400
+ struct ftrace_ops *ops, struct pt_regs *regs)
2401
+{
2402
+ unsigned long addr;
2403
+
2404
+ addr = ftrace_find_rec_direct(ip);
2405
+ if (!addr)
2406
+ return;
2407
+
2408
+ arch_ftrace_set_direct_caller(regs, addr);
2409
+}
2410
+
2411
+struct ftrace_ops direct_ops = {
2412
+ .func = call_direct_funcs,
2413
+ .flags = FTRACE_OPS_FL_IPMODIFY | FTRACE_OPS_FL_RECURSION_SAFE
2414
+ | FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS
2415
+ | FTRACE_OPS_FL_PERMANENT,
2416
+ /*
2417
+ * By declaring the main trampoline as this trampoline
2418
+ * it will never have one allocated for it. Allocated
2419
+ * trampolines should not call direct functions.
2420
+ * The direct_ops should only be called by the builtin
2421
+ * ftrace_regs_caller trampoline.
2422
+ */
2423
+ .trampoline = FTRACE_REGS_ADDR,
2424
+};
2425
+#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
2426
+
23482427 /**
23492428 * ftrace_get_addr_new - Get the call address to set to
23502429 * @rec: The ftrace record descriptor
23512430 *
23522431 * If the record has the FTRACE_FL_REGS set, that means that it
23532432 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2354
- * is not not set, then it wants to convert to the normal callback.
2433
+ * is not set, then it wants to convert to the normal callback.
23552434 *
23562435 * Returns the address of the trampoline to set to
23572436 */
23582437 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
23592438 {
23602439 struct ftrace_ops *ops;
2440
+ unsigned long addr;
2441
+
2442
+ if ((rec->flags & FTRACE_FL_DIRECT) &&
2443
+ (ftrace_rec_count(rec) == 1)) {
2444
+ addr = ftrace_find_rec_direct(rec->ip);
2445
+ if (addr)
2446
+ return addr;
2447
+ WARN_ON_ONCE(1);
2448
+ }
23612449
23622450 /* Trampolines take precedence over regs */
23632451 if (rec->flags & FTRACE_FL_TRAMP) {
....@@ -2390,6 +2478,15 @@
23902478 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
23912479 {
23922480 struct ftrace_ops *ops;
2481
+ unsigned long addr;
2482
+
2483
+ /* Direct calls take precedence over trampolines */
2484
+ if (rec->flags & FTRACE_FL_DIRECT_EN) {
2485
+ addr = ftrace_find_rec_direct(rec->ip);
2486
+ if (addr)
2487
+ return addr;
2488
+ WARN_ON_ONCE(1);
2489
+ }
23932490
23942491 /* Trampolines take precedence over regs */
23952492 if (rec->flags & FTRACE_FL_TRAMP_EN) {
....@@ -2410,7 +2507,7 @@
24102507 }
24112508
24122509 static int
2413
-__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2510
+__ftrace_replace_code(struct dyn_ftrace *rec, bool enable)
24142511 {
24152512 unsigned long ftrace_old_addr;
24162513 unsigned long ftrace_addr;
....@@ -2442,13 +2539,15 @@
24422539 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
24432540 }
24442541
2445
- return -1; /* unknow ftrace bug */
2542
+ return -1; /* unknown ftrace bug */
24462543 }
24472544
2448
-void __weak ftrace_replace_code(int enable)
2545
+void __weak ftrace_replace_code(int mod_flags)
24492546 {
24502547 struct dyn_ftrace *rec;
24512548 struct ftrace_page *pg;
2549
+ bool enable = mod_flags & FTRACE_MODIFY_ENABLE_FL;
2550
+ int schedulable = mod_flags & FTRACE_MODIFY_MAY_SLEEP_FL;
24522551 int failed;
24532552
24542553 if (unlikely(ftrace_disabled))
....@@ -2465,6 +2564,8 @@
24652564 /* Stop processing */
24662565 return;
24672566 }
2567
+ if (schedulable)
2568
+ cond_resched();
24682569 } while_for_each_ftrace_rec();
24692570 }
24702571
....@@ -2541,14 +2642,14 @@
25412642 }
25422643
25432644 static int
2544
-ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2645
+ftrace_nop_initialize(struct module *mod, struct dyn_ftrace *rec)
25452646 {
25462647 int ret;
25472648
25482649 if (unlikely(ftrace_disabled))
25492650 return 0;
25502651
2551
- ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2652
+ ret = ftrace_init_nop(mod, rec);
25522653 if (ret) {
25532654 ftrace_bug_type = FTRACE_BUG_INIT;
25542655 ftrace_bug(ret, rec);
....@@ -2578,7 +2679,11 @@
25782679 void ftrace_modify_all_code(int command)
25792680 {
25802681 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2682
+ int mod_flags = 0;
25812683 int err = 0;
2684
+
2685
+ if (command & FTRACE_MAY_SLEEP)
2686
+ mod_flags = FTRACE_MODIFY_MAY_SLEEP_FL;
25822687
25832688 /*
25842689 * If the ftrace_caller calls a ftrace_ops func directly,
....@@ -2597,9 +2702,9 @@
25972702 }
25982703
25992704 if (command & FTRACE_UPDATE_CALLS)
2600
- ftrace_replace_code(1);
2705
+ ftrace_replace_code(mod_flags | FTRACE_MODIFY_ENABLE_FL);
26012706 else if (command & FTRACE_DISABLE_CALLS)
2602
- ftrace_replace_code(0);
2707
+ ftrace_replace_code(mod_flags);
26032708
26042709 if (update && ftrace_trace_function != ftrace_ops_list_func) {
26052710 function_trace_op = set_function_trace_op;
....@@ -2692,6 +2797,51 @@
26922797 {
26932798 }
26942799
2800
+/* List of trace_ops that have allocated trampolines */
2801
+static LIST_HEAD(ftrace_ops_trampoline_list);
2802
+
2803
+static void ftrace_add_trampoline_to_kallsyms(struct ftrace_ops *ops)
2804
+{
2805
+ lockdep_assert_held(&ftrace_lock);
2806
+ list_add_rcu(&ops->list, &ftrace_ops_trampoline_list);
2807
+}
2808
+
2809
+static void ftrace_remove_trampoline_from_kallsyms(struct ftrace_ops *ops)
2810
+{
2811
+ lockdep_assert_held(&ftrace_lock);
2812
+ list_del_rcu(&ops->list);
2813
+ synchronize_rcu();
2814
+}
2815
+
2816
+/*
2817
+ * "__builtin__ftrace" is used as a module name in /proc/kallsyms for symbols
2818
+ * for pages allocated for ftrace purposes, even though "__builtin__ftrace" is
2819
+ * not a module.
2820
+ */
2821
+#define FTRACE_TRAMPOLINE_MOD "__builtin__ftrace"
2822
+#define FTRACE_TRAMPOLINE_SYM "ftrace_trampoline"
2823
+
2824
+static void ftrace_trampoline_free(struct ftrace_ops *ops)
2825
+{
2826
+ if (ops && (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP) &&
2827
+ ops->trampoline) {
2828
+ /*
2829
+ * Record the text poke event before the ksymbol unregister
2830
+ * event.
2831
+ */
2832
+ perf_event_text_poke((void *)ops->trampoline,
2833
+ (void *)ops->trampoline,
2834
+ ops->trampoline_size, NULL, 0);
2835
+ perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
2836
+ ops->trampoline, ops->trampoline_size,
2837
+ true, FTRACE_TRAMPOLINE_SYM);
2838
+ /* Remove from kallsyms after the perf events */
2839
+ ftrace_remove_trampoline_from_kallsyms(ops);
2840
+ }
2841
+
2842
+ arch_ftrace_trampoline_free(ops);
2843
+}
2844
+
26952845 static void ftrace_startup_enable(int command)
26962846 {
26972847 if (saved_ftrace_func != ftrace_trace_function) {
....@@ -2712,7 +2862,7 @@
27122862 update_all_ops = false;
27132863 }
27142864
2715
-static int ftrace_startup(struct ftrace_ops *ops, int command)
2865
+int ftrace_startup(struct ftrace_ops *ops, int command)
27162866 {
27172867 int ret;
27182868
....@@ -2741,6 +2891,8 @@
27412891 __unregister_ftrace_function(ops);
27422892 ftrace_start_up--;
27432893 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2894
+ if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
2895
+ ftrace_trampoline_free(ops);
27442896 return ret;
27452897 }
27462898
....@@ -2749,12 +2901,22 @@
27492901
27502902 ftrace_startup_enable(command);
27512903
2904
+ /*
2905
+ * If ftrace is in an undefined state, we just remove ops from list
2906
+ * to prevent the NULL pointer, instead of totally rolling it back and
2907
+ * free trampoline, because those actions could cause further damage.
2908
+ */
2909
+ if (unlikely(ftrace_disabled)) {
2910
+ __unregister_ftrace_function(ops);
2911
+ return -ENODEV;
2912
+ }
2913
+
27522914 ops->flags &= ~FTRACE_OPS_FL_ADDING;
27532915
27542916 return 0;
27552917 }
27562918
2757
-static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2919
+int ftrace_shutdown(struct ftrace_ops *ops, int command)
27582920 {
27592921 int ret;
27602922
....@@ -2786,18 +2948,8 @@
27862948 command |= FTRACE_UPDATE_TRACE_FUNC;
27872949 }
27882950
2789
- if (!command || !ftrace_enabled) {
2790
- /*
2791
- * If these are dynamic or per_cpu ops, they still
2792
- * need their data freed. Since, function tracing is
2793
- * not currently active, we can just free them
2794
- * without synchronizing all CPUs.
2795
- */
2796
- if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
2797
- goto free_ops;
2798
-
2799
- return 0;
2800
- }
2951
+ if (!command || !ftrace_enabled)
2952
+ goto out;
28012953
28022954 /*
28032955 * If the ops uses a trampoline, then it needs to be
....@@ -2834,6 +2986,7 @@
28342986 removed_ops = NULL;
28352987 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
28362988
2989
+out:
28372990 /*
28382991 * Dynamic ops may be freed, we must make sure that all
28392992 * callers are done before leaving this function.
....@@ -2849,20 +3002,19 @@
28493002 * infrastructure to do the synchronization, thus we must do it
28503003 * ourselves.
28513004 */
2852
- schedule_on_each_cpu(ftrace_sync);
3005
+ synchronize_rcu_tasks_rude();
28533006
28543007 /*
2855
- * When the kernel is preeptive, tasks can be preempted
3008
+ * When the kernel is preemptive, tasks can be preempted
28563009 * while on a ftrace trampoline. Just scheduling a task on
28573010 * a CPU is not good enough to flush them. Calling
28583011 * synchornize_rcu_tasks() will wait for those tasks to
28593012 * execute and either schedule voluntarily or enter user space.
28603013 */
2861
- if (IS_ENABLED(CONFIG_PREEMPT))
3014
+ if (IS_ENABLED(CONFIG_PREEMPTION))
28623015 synchronize_rcu_tasks();
28633016
2864
- free_ops:
2865
- arch_ftrace_trampoline_free(ops);
3017
+ ftrace_trampoline_free(ops);
28663018 }
28673019
28683020 return 0;
....@@ -2904,6 +3056,8 @@
29043056
29053057 static u64 ftrace_update_time;
29063058 unsigned long ftrace_update_tot_cnt;
3059
+unsigned long ftrace_number_of_pages;
3060
+unsigned long ftrace_number_of_groups;
29073061
29083062 static inline int ops_traces_mod(struct ftrace_ops *ops)
29093063 {
....@@ -2986,7 +3140,7 @@
29863140 * to the NOP instructions.
29873141 */
29883142 if (!__is_defined(CC_USING_NOP_MCOUNT) &&
2989
- !ftrace_code_disable(mod, p))
3143
+ !ftrace_nop_initialize(mod, p))
29903144 break;
29913145
29923146 update_cnt++;
....@@ -3003,18 +3157,20 @@
30033157 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
30043158 {
30053159 int order;
3160
+ int pages;
30063161 int cnt;
30073162
30083163 if (WARN_ON(!count))
30093164 return -EINVAL;
30103165
3011
- order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
3166
+ pages = DIV_ROUND_UP(count, ENTRIES_PER_PAGE);
3167
+ order = get_count_order(pages);
30123168
30133169 /*
30143170 * We want to fill as much as possible. No more than a page
30153171 * may be empty.
30163172 */
3017
- while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
3173
+ if (!is_power_of_2(pages))
30183174 order--;
30193175
30203176 again:
....@@ -3024,12 +3180,15 @@
30243180 /* if we can't allocate this size, try something smaller */
30253181 if (!order)
30263182 return -ENOMEM;
3027
- order >>= 1;
3183
+ order--;
30283184 goto again;
30293185 }
30303186
3187
+ ftrace_number_of_pages += 1 << order;
3188
+ ftrace_number_of_groups++;
3189
+
30313190 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3032
- pg->size = cnt;
3191
+ pg->order = order;
30333192
30343193 if (cnt > count)
30353194 cnt = count;
....@@ -3037,16 +3196,31 @@
30373196 return cnt;
30383197 }
30393198
3199
+static void ftrace_free_pages(struct ftrace_page *pages)
3200
+{
3201
+ struct ftrace_page *pg = pages;
3202
+
3203
+ while (pg) {
3204
+ if (pg->records) {
3205
+ free_pages((unsigned long)pg->records, pg->order);
3206
+ ftrace_number_of_pages -= 1 << pg->order;
3207
+ }
3208
+ pages = pg->next;
3209
+ kfree(pg);
3210
+ pg = pages;
3211
+ ftrace_number_of_groups--;
3212
+ }
3213
+}
3214
+
30403215 static struct ftrace_page *
30413216 ftrace_allocate_pages(unsigned long num_to_init)
30423217 {
30433218 struct ftrace_page *start_pg;
30443219 struct ftrace_page *pg;
3045
- int order;
30463220 int cnt;
30473221
30483222 if (!num_to_init)
3049
- return 0;
3223
+ return NULL;
30503224
30513225 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
30523226 if (!pg)
....@@ -3076,14 +3250,7 @@
30763250 return start_pg;
30773251
30783252 free_pages:
3079
- pg = start_pg;
3080
- while (pg) {
3081
- order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3082
- free_pages((unsigned long)pg->records, order);
3083
- start_pg = pg->next;
3084
- kfree(pg);
3085
- pg = start_pg;
3086
- }
3253
+ ftrace_free_pages(start_pg);
30873254 pr_info("ftrace: FAILED to allocate memory for functions\n");
30883255 return NULL;
30893256 }
....@@ -3493,10 +3660,11 @@
34933660 if (iter->flags & FTRACE_ITER_ENABLED) {
34943661 struct ftrace_ops *ops;
34953662
3496
- seq_printf(m, " (%ld)%s%s",
3663
+ seq_printf(m, " (%ld)%s%s%s",
34973664 ftrace_rec_count(rec),
34983665 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3499
- rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
3666
+ rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ",
3667
+ rec->flags & FTRACE_FL_DIRECT ? " D" : " ");
35003668 if (rec->flags & FTRACE_FL_TRAMP_EN) {
35013669 ops = ftrace_find_tramp_ops_any(rec);
35023670 if (ops) {
....@@ -3512,7 +3680,14 @@
35123680 } else {
35133681 add_trampoline_func(m, NULL, rec);
35143682 }
3515
- }
3683
+ if (rec->flags & FTRACE_FL_DIRECT) {
3684
+ unsigned long direct;
3685
+
3686
+ direct = ftrace_find_rec_direct(rec->ip);
3687
+ if (direct)
3688
+ seq_printf(m, "\n\tdirect-->%pS", (void *)direct);
3689
+ }
3690
+ }
35163691
35173692 seq_putc(m, '\n');
35183693
....@@ -3530,6 +3705,11 @@
35303705 ftrace_avail_open(struct inode *inode, struct file *file)
35313706 {
35323707 struct ftrace_iterator *iter;
3708
+ int ret;
3709
+
3710
+ ret = security_locked_down(LOCKDOWN_TRACEFS);
3711
+ if (ret)
3712
+ return ret;
35333713
35343714 if (unlikely(ftrace_disabled))
35353715 return -ENODEV;
....@@ -3548,6 +3728,15 @@
35483728 ftrace_enabled_open(struct inode *inode, struct file *file)
35493729 {
35503730 struct ftrace_iterator *iter;
3731
+
3732
+ /*
3733
+ * This shows us what functions are currently being
3734
+ * traced and by what. Not sure if we want lockdown
3735
+ * to hide such critical information for an admin.
3736
+ * Although, perhaps it can show information we don't
3737
+ * want people to see, but if something is tracing
3738
+ * something, we probably want to know about it.
3739
+ */
35513740
35523741 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
35533742 if (!iter)
....@@ -3591,7 +3780,7 @@
35913780 if (unlikely(ftrace_disabled))
35923781 return -ENODEV;
35933782
3594
- if (tr && trace_array_get(tr) < 0)
3783
+ if (tracing_check_open_get_tr(tr))
35953784 return -ENODEV;
35963785
35973786 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
....@@ -3669,6 +3858,7 @@
36693858 {
36703859 struct ftrace_ops *ops = inode->i_private;
36713860
3861
+ /* Checks for tracefs lockdown */
36723862 return ftrace_regex_open(ops,
36733863 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
36743864 inode, file);
....@@ -3679,6 +3869,7 @@
36793869 {
36803870 struct ftrace_ops *ops = inode->i_private;
36813871
3872
+ /* Checks for tracefs lockdown */
36823873 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
36833874 inode, file);
36843875 }
....@@ -3759,6 +3950,31 @@
37593950 }
37603951
37613952 static int
3953
+add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g,
3954
+ int clear_filter)
3955
+{
3956
+ long index = simple_strtoul(func_g->search, NULL, 0);
3957
+ struct ftrace_page *pg;
3958
+ struct dyn_ftrace *rec;
3959
+
3960
+ /* The index starts at 1 */
3961
+ if (--index < 0)
3962
+ return 0;
3963
+
3964
+ do_for_each_ftrace_rec(pg, rec) {
3965
+ if (pg->index <= index) {
3966
+ index -= pg->index;
3967
+ /* this is a double loop, break goes to the next page */
3968
+ break;
3969
+ }
3970
+ rec = &pg->records[index];
3971
+ enter_record(hash, rec, clear_filter);
3972
+ return 1;
3973
+ } while_for_each_ftrace_rec();
3974
+ return 0;
3975
+}
3976
+
3977
+static int
37623978 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
37633979 struct ftrace_glob *mod_g, int exclude_mod)
37643980 {
....@@ -3825,6 +4041,11 @@
38254041
38264042 if (unlikely(ftrace_disabled))
38274043 goto out_unlock;
4044
+
4045
+ if (func_g.type == MATCH_INDEX) {
4046
+ found = add_rec_by_index(hash, &func_g, clear_filter);
4047
+ goto out_unlock;
4048
+ }
38284049
38294050 do_for_each_ftrace_rec(pg, rec) {
38304051
....@@ -3906,7 +4127,7 @@
39064127 static bool module_exists(const char *module)
39074128 {
39084129 /* All modules have the symbol __this_module */
3909
- const char this_mod[] = "__this_module";
4130
+ static const char this_mod[] = "__this_module";
39104131 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
39114132 unsigned long val;
39124133 int n;
....@@ -4183,7 +4404,7 @@
41834404 * @ip: The instruction pointer address to map @data to
41844405 * @data: The data to map to @ip
41854406 *
4186
- * Returns 0 on succes otherwise an error.
4407
+ * Returns 0 on success otherwise an error.
41874408 */
41884409 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
41894410 unsigned long ip, void *data)
....@@ -4213,7 +4434,7 @@
42134434 * @ip: The instruction pointer address to remove the data from
42144435 *
42154436 * Returns the data if it is found, otherwise NULL.
4216
- * Note, if the data pointer is used as the data itself, (see
4437
+ * Note, if the data pointer is used as the data itself, (see
42174438 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
42184439 * if the data pointer was set to zero.
42194440 */
....@@ -4351,7 +4572,7 @@
43514572
43524573 /*
43534574 * Note, there's a small window here that the func_hash->filter_hash
4354
- * may be NULL or empty. Need to be carefule when reading the loop.
4575
+ * may be NULL or empty. Need to be careful when reading the loop.
43554576 */
43564577 mutex_lock(&probe->ops.func_hash->regex_lock);
43574578
....@@ -4552,7 +4773,7 @@
45524773 if (ftrace_enabled && !ftrace_hash_empty(hash))
45534774 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
45544775 &old_hash_ops);
4555
- synchronize_sched();
4776
+ synchronize_rcu();
45564777
45574778 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
45584779 hlist_del(&entry->hlist);
....@@ -4794,8 +5015,404 @@
47945015 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
47955016 int reset, int enable)
47965017 {
4797
- return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
5018
+ return ftrace_set_hash(ops, NULL, 0, ip, remove, reset, enable);
47985019 }
5020
+
5021
+#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
5022
+
5023
+struct ftrace_direct_func {
5024
+ struct list_head next;
5025
+ unsigned long addr;
5026
+ int count;
5027
+};
5028
+
5029
+static LIST_HEAD(ftrace_direct_funcs);
5030
+
5031
+/**
5032
+ * ftrace_find_direct_func - test an address if it is a registered direct caller
5033
+ * @addr: The address of a registered direct caller
5034
+ *
5035
+ * This searches to see if a ftrace direct caller has been registered
5036
+ * at a specific address, and if so, it returns a descriptor for it.
5037
+ *
5038
+ * This can be used by architecture code to see if an address is
5039
+ * a direct caller (trampoline) attached to a fentry/mcount location.
5040
+ * This is useful for the function_graph tracer, as it may need to
5041
+ * do adjustments if it traced a location that also has a direct
5042
+ * trampoline attached to it.
5043
+ */
5044
+struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr)
5045
+{
5046
+ struct ftrace_direct_func *entry;
5047
+ bool found = false;
5048
+
5049
+ /* May be called by fgraph trampoline (protected by rcu tasks) */
5050
+ list_for_each_entry_rcu(entry, &ftrace_direct_funcs, next) {
5051
+ if (entry->addr == addr) {
5052
+ found = true;
5053
+ break;
5054
+ }
5055
+ }
5056
+ if (found)
5057
+ return entry;
5058
+
5059
+ return NULL;
5060
+}
5061
+
5062
+static struct ftrace_direct_func *ftrace_alloc_direct_func(unsigned long addr)
5063
+{
5064
+ struct ftrace_direct_func *direct;
5065
+
5066
+ direct = kmalloc(sizeof(*direct), GFP_KERNEL);
5067
+ if (!direct)
5068
+ return NULL;
5069
+ direct->addr = addr;
5070
+ direct->count = 0;
5071
+ list_add_rcu(&direct->next, &ftrace_direct_funcs);
5072
+ ftrace_direct_func_count++;
5073
+ return direct;
5074
+}
5075
+
5076
+/**
5077
+ * register_ftrace_direct - Call a custom trampoline directly
5078
+ * @ip: The address of the nop at the beginning of a function
5079
+ * @addr: The address of the trampoline to call at @ip
5080
+ *
5081
+ * This is used to connect a direct call from the nop location (@ip)
5082
+ * at the start of ftrace traced functions. The location that it calls
5083
+ * (@addr) must be able to handle a direct call, and save the parameters
5084
+ * of the function being traced, and restore them (or inject new ones
5085
+ * if needed), before returning.
5086
+ *
5087
+ * Returns:
5088
+ * 0 on success
5089
+ * -EBUSY - Another direct function is already attached (there can be only one)
5090
+ * -ENODEV - @ip does not point to a ftrace nop location (or not supported)
5091
+ * -ENOMEM - There was an allocation failure.
5092
+ */
5093
+int register_ftrace_direct(unsigned long ip, unsigned long addr)
5094
+{
5095
+ struct ftrace_direct_func *direct;
5096
+ struct ftrace_func_entry *entry;
5097
+ struct ftrace_hash *free_hash = NULL;
5098
+ struct dyn_ftrace *rec;
5099
+ int ret = -EBUSY;
5100
+
5101
+ mutex_lock(&direct_mutex);
5102
+
5103
+ /* See if there's a direct function at @ip already */
5104
+ if (ftrace_find_rec_direct(ip))
5105
+ goto out_unlock;
5106
+
5107
+ ret = -ENODEV;
5108
+ rec = lookup_rec(ip, ip);
5109
+ if (!rec)
5110
+ goto out_unlock;
5111
+
5112
+ /*
5113
+ * Check if the rec says it has a direct call but we didn't
5114
+ * find one earlier?
5115
+ */
5116
+ if (WARN_ON(rec->flags & FTRACE_FL_DIRECT))
5117
+ goto out_unlock;
5118
+
5119
+ /* Make sure the ip points to the exact record */
5120
+ if (ip != rec->ip) {
5121
+ ip = rec->ip;
5122
+ /* Need to check this ip for a direct. */
5123
+ if (ftrace_find_rec_direct(ip))
5124
+ goto out_unlock;
5125
+ }
5126
+
5127
+ ret = -ENOMEM;
5128
+ if (ftrace_hash_empty(direct_functions) ||
5129
+ direct_functions->count > 2 * (1 << direct_functions->size_bits)) {
5130
+ struct ftrace_hash *new_hash;
5131
+ int size = ftrace_hash_empty(direct_functions) ? 0 :
5132
+ direct_functions->count + 1;
5133
+
5134
+ if (size < 32)
5135
+ size = 32;
5136
+
5137
+ new_hash = dup_hash(direct_functions, size);
5138
+ if (!new_hash)
5139
+ goto out_unlock;
5140
+
5141
+ free_hash = direct_functions;
5142
+ direct_functions = new_hash;
5143
+ }
5144
+
5145
+ entry = kmalloc(sizeof(*entry), GFP_KERNEL);
5146
+ if (!entry)
5147
+ goto out_unlock;
5148
+
5149
+ direct = ftrace_find_direct_func(addr);
5150
+ if (!direct) {
5151
+ direct = ftrace_alloc_direct_func(addr);
5152
+ if (!direct) {
5153
+ kfree(entry);
5154
+ goto out_unlock;
5155
+ }
5156
+ }
5157
+
5158
+ entry->ip = ip;
5159
+ entry->direct = addr;
5160
+ __add_hash_entry(direct_functions, entry);
5161
+
5162
+ ret = ftrace_set_filter_ip(&direct_ops, ip, 0, 0);
5163
+
5164
+ if (!ret && !(direct_ops.flags & FTRACE_OPS_FL_ENABLED)) {
5165
+ ret = register_ftrace_function(&direct_ops);
5166
+ if (ret)
5167
+ ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5168
+ }
5169
+
5170
+ if (ret) {
5171
+ remove_hash_entry(direct_functions, entry);
5172
+ kfree(entry);
5173
+ if (!direct->count) {
5174
+ list_del_rcu(&direct->next);
5175
+ synchronize_rcu_tasks();
5176
+ kfree(direct);
5177
+ if (free_hash)
5178
+ free_ftrace_hash(free_hash);
5179
+ free_hash = NULL;
5180
+ ftrace_direct_func_count--;
5181
+ }
5182
+ } else {
5183
+ direct->count++;
5184
+ }
5185
+ out_unlock:
5186
+ mutex_unlock(&direct_mutex);
5187
+
5188
+ if (free_hash) {
5189
+ synchronize_rcu_tasks();
5190
+ free_ftrace_hash(free_hash);
5191
+ }
5192
+
5193
+ return ret;
5194
+}
5195
+EXPORT_SYMBOL_GPL(register_ftrace_direct);
5196
+
5197
+static struct ftrace_func_entry *find_direct_entry(unsigned long *ip,
5198
+ struct dyn_ftrace **recp)
5199
+{
5200
+ struct ftrace_func_entry *entry;
5201
+ struct dyn_ftrace *rec;
5202
+
5203
+ rec = lookup_rec(*ip, *ip);
5204
+ if (!rec)
5205
+ return NULL;
5206
+
5207
+ entry = __ftrace_lookup_ip(direct_functions, rec->ip);
5208
+ if (!entry) {
5209
+ WARN_ON(rec->flags & FTRACE_FL_DIRECT);
5210
+ return NULL;
5211
+ }
5212
+
5213
+ WARN_ON(!(rec->flags & FTRACE_FL_DIRECT));
5214
+
5215
+ /* Passed in ip just needs to be on the call site */
5216
+ *ip = rec->ip;
5217
+
5218
+ if (recp)
5219
+ *recp = rec;
5220
+
5221
+ return entry;
5222
+}
5223
+
5224
+int unregister_ftrace_direct(unsigned long ip, unsigned long addr)
5225
+{
5226
+ struct ftrace_direct_func *direct;
5227
+ struct ftrace_func_entry *entry;
5228
+ int ret = -ENODEV;
5229
+
5230
+ mutex_lock(&direct_mutex);
5231
+
5232
+ entry = find_direct_entry(&ip, NULL);
5233
+ if (!entry)
5234
+ goto out_unlock;
5235
+
5236
+ if (direct_functions->count == 1)
5237
+ unregister_ftrace_function(&direct_ops);
5238
+
5239
+ ret = ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5240
+
5241
+ WARN_ON(ret);
5242
+
5243
+ remove_hash_entry(direct_functions, entry);
5244
+
5245
+ direct = ftrace_find_direct_func(addr);
5246
+ if (!WARN_ON(!direct)) {
5247
+ /* This is the good path (see the ! before WARN) */
5248
+ direct->count--;
5249
+ WARN_ON(direct->count < 0);
5250
+ if (!direct->count) {
5251
+ list_del_rcu(&direct->next);
5252
+ synchronize_rcu_tasks();
5253
+ kfree(direct);
5254
+ kfree(entry);
5255
+ ftrace_direct_func_count--;
5256
+ }
5257
+ }
5258
+ out_unlock:
5259
+ mutex_unlock(&direct_mutex);
5260
+
5261
+ return ret;
5262
+}
5263
+EXPORT_SYMBOL_GPL(unregister_ftrace_direct);
5264
+
5265
+static struct ftrace_ops stub_ops = {
5266
+ .func = ftrace_stub,
5267
+};
5268
+
5269
+/**
5270
+ * ftrace_modify_direct_caller - modify ftrace nop directly
5271
+ * @entry: The ftrace hash entry of the direct helper for @rec
5272
+ * @rec: The record representing the function site to patch
5273
+ * @old_addr: The location that the site at @rec->ip currently calls
5274
+ * @new_addr: The location that the site at @rec->ip should call
5275
+ *
5276
+ * An architecture may overwrite this function to optimize the
5277
+ * changing of the direct callback on an ftrace nop location.
5278
+ * This is called with the ftrace_lock mutex held, and no other
5279
+ * ftrace callbacks are on the associated record (@rec). Thus,
5280
+ * it is safe to modify the ftrace record, where it should be
5281
+ * currently calling @old_addr directly, to call @new_addr.
5282
+ *
5283
+ * Safety checks should be made to make sure that the code at
5284
+ * @rec->ip is currently calling @old_addr. And this must
5285
+ * also update entry->direct to @new_addr.
5286
+ */
5287
+int __weak ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
5288
+ struct dyn_ftrace *rec,
5289
+ unsigned long old_addr,
5290
+ unsigned long new_addr)
5291
+{
5292
+ unsigned long ip = rec->ip;
5293
+ int ret;
5294
+
5295
+ /*
5296
+ * The ftrace_lock was used to determine if the record
5297
+ * had more than one registered user to it. If it did,
5298
+ * we needed to prevent that from changing to do the quick
5299
+ * switch. But if it did not (only a direct caller was attached)
5300
+ * then this function is called. But this function can deal
5301
+ * with attached callers to the rec that we care about, and
5302
+ * since this function uses standard ftrace calls that take
5303
+ * the ftrace_lock mutex, we need to release it.
5304
+ */
5305
+ mutex_unlock(&ftrace_lock);
5306
+
5307
+ /*
5308
+ * By setting a stub function at the same address, we force
5309
+ * the code to call the iterator and the direct_ops helper.
5310
+ * This means that @ip does not call the direct call, and
5311
+ * we can simply modify it.
5312
+ */
5313
+ ret = ftrace_set_filter_ip(&stub_ops, ip, 0, 0);
5314
+ if (ret)
5315
+ goto out_lock;
5316
+
5317
+ ret = register_ftrace_function(&stub_ops);
5318
+ if (ret) {
5319
+ ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5320
+ goto out_lock;
5321
+ }
5322
+
5323
+ entry->direct = new_addr;
5324
+
5325
+ /*
5326
+ * By removing the stub, we put back the direct call, calling
5327
+ * the @new_addr.
5328
+ */
5329
+ unregister_ftrace_function(&stub_ops);
5330
+ ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5331
+
5332
+ out_lock:
5333
+ mutex_lock(&ftrace_lock);
5334
+
5335
+ return ret;
5336
+}
5337
+
5338
+/**
5339
+ * modify_ftrace_direct - Modify an existing direct call to call something else
5340
+ * @ip: The instruction pointer to modify
5341
+ * @old_addr: The address that the current @ip calls directly
5342
+ * @new_addr: The address that the @ip should call
5343
+ *
5344
+ * This modifies a ftrace direct caller at an instruction pointer without
5345
+ * having to disable it first. The direct call will switch over to the
5346
+ * @new_addr without missing anything.
5347
+ *
5348
+ * Returns: zero on success. Non zero on error, which includes:
5349
+ * -ENODEV : the @ip given has no direct caller attached
5350
+ * -EINVAL : the @old_addr does not match the current direct caller
5351
+ */
5352
+int modify_ftrace_direct(unsigned long ip,
5353
+ unsigned long old_addr, unsigned long new_addr)
5354
+{
5355
+ struct ftrace_direct_func *direct, *new_direct = NULL;
5356
+ struct ftrace_func_entry *entry;
5357
+ struct dyn_ftrace *rec;
5358
+ int ret = -ENODEV;
5359
+
5360
+ mutex_lock(&direct_mutex);
5361
+
5362
+ mutex_lock(&ftrace_lock);
5363
+ entry = find_direct_entry(&ip, &rec);
5364
+ if (!entry)
5365
+ goto out_unlock;
5366
+
5367
+ ret = -EINVAL;
5368
+ if (entry->direct != old_addr)
5369
+ goto out_unlock;
5370
+
5371
+ direct = ftrace_find_direct_func(old_addr);
5372
+ if (WARN_ON(!direct))
5373
+ goto out_unlock;
5374
+ if (direct->count > 1) {
5375
+ ret = -ENOMEM;
5376
+ new_direct = ftrace_alloc_direct_func(new_addr);
5377
+ if (!new_direct)
5378
+ goto out_unlock;
5379
+ direct->count--;
5380
+ new_direct->count++;
5381
+ } else {
5382
+ direct->addr = new_addr;
5383
+ }
5384
+
5385
+ /*
5386
+ * If there's no other ftrace callback on the rec->ip location,
5387
+ * then it can be changed directly by the architecture.
5388
+ * If there is another caller, then we just need to change the
5389
+ * direct caller helper to point to @new_addr.
5390
+ */
5391
+ if (ftrace_rec_count(rec) == 1) {
5392
+ ret = ftrace_modify_direct_caller(entry, rec, old_addr, new_addr);
5393
+ } else {
5394
+ entry->direct = new_addr;
5395
+ ret = 0;
5396
+ }
5397
+
5398
+ if (ret) {
5399
+ direct->addr = old_addr;
5400
+ if (unlikely(new_direct)) {
5401
+ direct->count++;
5402
+ list_del_rcu(&new_direct->next);
5403
+ synchronize_rcu_tasks();
5404
+ kfree(new_direct);
5405
+ ftrace_direct_func_count--;
5406
+ }
5407
+ }
5408
+
5409
+ out_unlock:
5410
+ mutex_unlock(&ftrace_lock);
5411
+ mutex_unlock(&direct_mutex);
5412
+ return ret;
5413
+}
5414
+EXPORT_SYMBOL_GPL(modify_ftrace_direct);
5415
+#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
47995416
48005417 /**
48015418 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
....@@ -4967,7 +5584,7 @@
49675584 struct ftrace_hash *hash;
49685585
49695586 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4970
- if (WARN_ON(!hash))
5587
+ if (MEM_FAIL(!hash, "Failed to allocate hash\n"))
49715588 return;
49725589
49735590 while (buf) {
....@@ -5045,8 +5662,12 @@
50455662
50465663 if (filter_hash) {
50475664 orig_hash = &iter->ops->func_hash->filter_hash;
5048
- if (iter->tr && !list_empty(&iter->tr->mod_trace))
5049
- iter->hash->flags |= FTRACE_HASH_FL_MOD;
5665
+ if (iter->tr) {
5666
+ if (list_empty(&iter->tr->mod_trace))
5667
+ iter->hash->flags &= ~FTRACE_HASH_FL_MOD;
5668
+ else
5669
+ iter->hash->flags |= FTRACE_HASH_FL_MOD;
5670
+ }
50505671 } else
50515672 orig_hash = &iter->ops->func_hash->notrace_hash;
50525673
....@@ -5220,8 +5841,12 @@
52205841 __ftrace_graph_open(struct inode *inode, struct file *file,
52215842 struct ftrace_graph_data *fgd)
52225843 {
5223
- int ret = 0;
5844
+ int ret;
52245845 struct ftrace_hash *new_hash = NULL;
5846
+
5847
+ ret = security_locked_down(LOCKDOWN_TRACEFS);
5848
+ if (ret)
5849
+ return ret;
52255850
52265851 if (file->f_mode & FMODE_WRITE) {
52275852 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
....@@ -5382,7 +6007,7 @@
53826007 * infrastructure to do the synchronization, thus we must do it
53836008 * ourselves.
53846009 */
5385
- schedule_on_each_cpu(ftrace_sync);
6010
+ synchronize_rcu_tasks_rude();
53866011
53876012 free_ftrace_hash(old_hash);
53886013 }
....@@ -5514,7 +6139,7 @@
55146139
55156140 /*
55166141 * The name "destroy_filter_files" is really a misnomer. Although
5517
- * in the future, it may actualy delete the files, but this is
6142
+ * in the future, it may actually delete the files, but this is
55186143 * really intended to make sure the ops passed in are disabled
55196144 * and that when this function returns, the caller is free to
55206145 * free the ops.
....@@ -5567,13 +6192,15 @@
55676192 return 0;
55686193 }
55696194
5570
-static int __norecordmcount ftrace_process_locs(struct module *mod,
5571
- unsigned long *start,
5572
- unsigned long *end)
6195
+static int ftrace_process_locs(struct module *mod,
6196
+ unsigned long *start,
6197
+ unsigned long *end)
55736198 {
6199
+ struct ftrace_page *pg_unuse = NULL;
55746200 struct ftrace_page *start_pg;
55756201 struct ftrace_page *pg;
55766202 struct dyn_ftrace *rec;
6203
+ unsigned long skipped = 0;
55776204 unsigned long count;
55786205 unsigned long *p;
55796206 unsigned long addr;
....@@ -5619,6 +6246,7 @@
56196246 p = start;
56206247 pg = start_pg;
56216248 while (p < end) {
6249
+ unsigned long end_offset;
56226250 addr = ftrace_call_adjust(*p++);
56236251 /*
56246252 * Some architecture linkers will pad between
....@@ -5626,10 +6254,13 @@
56266254 * object files to satisfy alignments.
56276255 * Skip any NULL pointers.
56286256 */
5629
- if (!addr)
6257
+ if (!addr) {
6258
+ skipped++;
56306259 continue;
6260
+ }
56316261
5632
- if (pg->index == pg->size) {
6262
+ end_offset = (pg->index+1) * sizeof(pg->records[0]);
6263
+ if (end_offset > PAGE_SIZE << pg->order) {
56336264 /* We should have allocated enough */
56346265 if (WARN_ON(!pg->next))
56356266 break;
....@@ -5640,8 +6271,10 @@
56406271 rec->ip = addr;
56416272 }
56426273
5643
- /* We should have used all pages */
5644
- WARN_ON(pg->next);
6274
+ if (pg->next) {
6275
+ pg_unuse = pg->next;
6276
+ pg->next = NULL;
6277
+ }
56456278
56466279 /* Assign the last page to ftrace_pages */
56476280 ftrace_pages = pg;
....@@ -5663,6 +6296,11 @@
56636296 out:
56646297 mutex_unlock(&ftrace_lock);
56656298
6299
+ /* We should have used all pages unless we skipped some */
6300
+ if (pg_unuse) {
6301
+ WARN_ON(!skipped);
6302
+ ftrace_free_pages(pg_unuse);
6303
+ }
56666304 return ret;
56676305 }
56686306
....@@ -5683,6 +6321,27 @@
56836321 unsigned int num_funcs;
56846322 };
56856323
6324
+static int ftrace_get_trampoline_kallsym(unsigned int symnum,
6325
+ unsigned long *value, char *type,
6326
+ char *name, char *module_name,
6327
+ int *exported)
6328
+{
6329
+ struct ftrace_ops *op;
6330
+
6331
+ list_for_each_entry_rcu(op, &ftrace_ops_trampoline_list, list) {
6332
+ if (!op->trampoline || symnum--)
6333
+ continue;
6334
+ *value = op->trampoline;
6335
+ *type = 't';
6336
+ strlcpy(name, FTRACE_TRAMPOLINE_SYM, KSYM_NAME_LEN);
6337
+ strlcpy(module_name, FTRACE_TRAMPOLINE_MOD, MODULE_NAME_LEN);
6338
+ *exported = 0;
6339
+ return 0;
6340
+ }
6341
+
6342
+ return -ERANGE;
6343
+}
6344
+
56866345 #ifdef CONFIG_MODULES
56876346
56886347 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
....@@ -5696,9 +6355,17 @@
56966355
56976356 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
56986357 if (ops_references_rec(ops, rec)) {
6358
+ if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_DIRECT))
6359
+ continue;
6360
+ if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_IPMODIFY))
6361
+ continue;
56996362 cnt++;
57006363 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
57016364 rec->flags |= FTRACE_FL_REGS;
6365
+ if (cnt == 1 && ops->trampoline)
6366
+ rec->flags |= FTRACE_FL_TRAMP;
6367
+ else
6368
+ rec->flags &= ~FTRACE_FL_TRAMP;
57026369 }
57036370 }
57046371
....@@ -5769,7 +6436,6 @@
57696436 struct ftrace_page **last_pg;
57706437 struct ftrace_page *tmp_page = NULL;
57716438 struct ftrace_page *pg;
5772
- int order;
57736439
57746440 mutex_lock(&ftrace_lock);
57756441
....@@ -5779,7 +6445,7 @@
57796445 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
57806446 if (mod_map->mod == mod) {
57816447 list_del_rcu(&mod_map->list);
5782
- call_rcu_sched(&mod_map->rcu, ftrace_free_mod_map);
6448
+ call_rcu(&mod_map->rcu, ftrace_free_mod_map);
57836449 break;
57846450 }
57856451 }
....@@ -5820,10 +6486,13 @@
58206486 /* Needs to be called outside of ftrace_lock */
58216487 clear_mod_from_hashes(pg);
58226488
5823
- order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5824
- free_pages((unsigned long)pg->records, order);
6489
+ if (pg->records) {
6490
+ free_pages((unsigned long)pg->records, pg->order);
6491
+ ftrace_number_of_pages -= 1 << pg->order;
6492
+ }
58256493 tmp_page = pg->next;
58266494 kfree(pg);
6495
+ ftrace_number_of_groups--;
58276496 }
58286497 }
58296498
....@@ -5840,7 +6509,7 @@
58406509 /*
58416510 * If the tracing is enabled, go ahead and enable the record.
58426511 *
5843
- * The reason not to enable the record immediatelly is the
6512
+ * The reason not to enable the record immediately is the
58446513 * inherent check of ftrace_make_nop/ftrace_make_call for
58456514 * correct previous instructions. Making first the NOP
58466515 * conversion puts the module to the correct state, thus
....@@ -5999,7 +6668,7 @@
59996668 struct ftrace_mod_map *mod_map;
60006669 const char *ret = NULL;
60016670
6002
- /* mod_map is freed via call_rcu_sched() */
6671
+ /* mod_map is freed via call_rcu() */
60036672 preempt_disable();
60046673 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
60056674 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
....@@ -6020,6 +6689,7 @@
60206689 {
60216690 struct ftrace_mod_map *mod_map;
60226691 struct ftrace_mod_func *mod_func;
6692
+ int ret;
60236693
60246694 preempt_disable();
60256695 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
....@@ -6046,8 +6716,10 @@
60466716 WARN_ON(1);
60476717 break;
60486718 }
6719
+ ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
6720
+ module_name, exported);
60496721 preempt_enable();
6050
- return -ERANGE;
6722
+ return ret;
60516723 }
60526724
60536725 #else
....@@ -6058,6 +6730,18 @@
60586730 unsigned long start, unsigned long end)
60596731 {
60606732 return NULL;
6733
+}
6734
+int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6735
+ char *type, char *name, char *module_name,
6736
+ int *exported)
6737
+{
6738
+ int ret;
6739
+
6740
+ preempt_disable();
6741
+ ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
6742
+ module_name, exported);
6743
+ preempt_enable();
6744
+ return ret;
60616745 }
60626746 #endif /* CONFIG_MODULES */
60636747
....@@ -6072,11 +6756,7 @@
60726756 {
60736757 struct ftrace_func_entry *entry;
60746758
6075
- if (ftrace_hash_empty(hash))
6076
- return;
6077
-
6078
- entry = __ftrace_lookup_ip(hash, func->ip);
6079
-
6759
+ entry = ftrace_lookup_ip(hash, func->ip);
60806760 /*
60816761 * Do not allow this rec to match again.
60826762 * Yeah, it may waste some memory, but will be removed
....@@ -6110,7 +6790,7 @@
61106790
61116791 func = kmalloc(sizeof(*func), GFP_KERNEL);
61126792 if (!func) {
6113
- WARN_ONCE(1, "alloc failure, ftrace filter could be stale\n");
6793
+ MEM_FAIL(1, "alloc failure, ftrace filter could be stale\n");
61146794 return;
61156795 }
61166796
....@@ -6129,7 +6809,6 @@
61296809 struct ftrace_mod_map *mod_map = NULL;
61306810 struct ftrace_init_func *func, *func_next;
61316811 struct list_head clear_hash;
6132
- int order;
61336812
61346813 INIT_LIST_HEAD(&clear_hash);
61356814
....@@ -6167,8 +6846,11 @@
61676846 ftrace_update_tot_cnt--;
61686847 if (!pg->index) {
61696848 *last_pg = pg->next;
6170
- order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6171
- free_pages((unsigned long)pg->records, order);
6849
+ if (pg->records) {
6850
+ free_pages((unsigned long)pg->records, pg->order);
6851
+ ftrace_number_of_pages -= 1 << pg->order;
6852
+ }
6853
+ ftrace_number_of_groups--;
61726854 kfree(pg);
61736855 pg = container_of(last_pg, struct ftrace_page, next);
61746856 if (!(*last_pg))
....@@ -6216,13 +6898,16 @@
62166898 }
62176899
62186900 pr_info("ftrace: allocating %ld entries in %ld pages\n",
6219
- count, count / ENTRIES_PER_PAGE + 1);
6901
+ count, DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
62206902
62216903 last_ftrace_enabled = ftrace_enabled = 1;
62226904
62236905 ret = ftrace_process_locs(NULL,
62246906 __start_mcount_loc,
62256907 __stop_mcount_loc);
6908
+
6909
+ pr_info("ftrace: allocated %ld pages with %ld groups\n",
6910
+ ftrace_number_of_pages, ftrace_number_of_groups);
62266911
62276912 set_ftrace_early_filters();
62286913
....@@ -6238,7 +6923,24 @@
62386923
62396924 static void ftrace_update_trampoline(struct ftrace_ops *ops)
62406925 {
6926
+ unsigned long trampoline = ops->trampoline;
6927
+
62416928 arch_ftrace_update_trampoline(ops);
6929
+ if (ops->trampoline && ops->trampoline != trampoline &&
6930
+ (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP)) {
6931
+ /* Add to kallsyms before the perf events */
6932
+ ftrace_add_trampoline_to_kallsyms(ops);
6933
+ perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
6934
+ ops->trampoline, ops->trampoline_size, false,
6935
+ FTRACE_TRAMPOLINE_SYM);
6936
+ /*
6937
+ * Record the perf text poke event after the ksymbol register
6938
+ * event.
6939
+ */
6940
+ perf_event_text_poke((void *)ops->trampoline, NULL, 0,
6941
+ (void *)ops->trampoline,
6942
+ ops->trampoline_size);
6943
+ }
62426944 }
62436945
62446946 void ftrace_init_trace_array(struct trace_array *tr)
....@@ -6249,7 +6951,7 @@
62496951 }
62506952 #else
62516953
6252
-static struct ftrace_ops global_ops = {
6954
+struct ftrace_ops global_ops = {
62536955 .func = ftrace_stub,
62546956 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
62556957 FTRACE_OPS_FL_INITIALIZED |
....@@ -6266,30 +6968,9 @@
62666968 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
62676969 static inline void ftrace_startup_enable(int command) { }
62686970 static inline void ftrace_startup_all(int command) { }
6269
-/* Keep as macros so we do not need to define the commands */
6270
-# define ftrace_startup(ops, command) \
6271
- ({ \
6272
- int ___ret = __register_ftrace_function(ops); \
6273
- if (!___ret) \
6274
- (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
6275
- ___ret; \
6276
- })
6277
-# define ftrace_shutdown(ops, command) \
6278
- ({ \
6279
- int ___ret = __unregister_ftrace_function(ops); \
6280
- if (!___ret) \
6281
- (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
6282
- ___ret; \
6283
- })
62846971
62856972 # define ftrace_startup_sysctl() do { } while (0)
62866973 # define ftrace_shutdown_sysctl() do { } while (0)
6287
-
6288
-static inline int
6289
-ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
6290
-{
6291
- return 1;
6292
-}
62936974
62946975 static void ftrace_update_trampoline(struct ftrace_ops *ops)
62956976 {
....@@ -6334,11 +7015,14 @@
63347015
63357016 /*
63367017 * Some of the ops may be dynamically allocated,
6337
- * they must be freed after a synchronize_sched().
7018
+ * they must be freed after a synchronize_rcu().
63387019 */
63397020 preempt_disable_notrace();
63407021
63417022 do_for_each_ftrace_op(op, ftrace_ops_list) {
7023
+ /* Stub functions don't need to be called nor tested */
7024
+ if (op->flags & FTRACE_OPS_FL_STUB)
7025
+ continue;
63427026 /*
63437027 * Check the following for each ops before calling their func:
63447028 * if RCU flag is set, then rcu_is_watching() must be true
....@@ -6383,8 +7067,7 @@
63837067 }
63847068 NOKPROBE_SYMBOL(ftrace_ops_list_func);
63857069 #else
6386
-static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip,
6387
- struct ftrace_ops *op, struct pt_regs *regs)
7070
+static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
63887071 {
63897072 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
63907073 }
....@@ -6445,11 +7128,17 @@
64457128 {
64467129 struct trace_array *tr = data;
64477130 struct trace_pid_list *pid_list;
7131
+ struct trace_pid_list *no_pid_list;
64487132
64497133 pid_list = rcu_dereference_sched(tr->function_pids);
7134
+ no_pid_list = rcu_dereference_sched(tr->function_no_pids);
64507135
6451
- this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6452
- trace_ignore_this_task(pid_list, next));
7136
+ if (trace_ignore_this_task(pid_list, no_pid_list, next))
7137
+ this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7138
+ FTRACE_PID_IGNORE);
7139
+ else
7140
+ this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7141
+ next->pid);
64537142 }
64547143
64557144 static void
....@@ -6462,6 +7151,9 @@
64627151
64637152 pid_list = rcu_dereference_sched(tr->function_pids);
64647153 trace_filter_add_remove_task(pid_list, self, task);
7154
+
7155
+ pid_list = rcu_dereference_sched(tr->function_no_pids);
7156
+ trace_filter_add_remove_task(pid_list, self, task);
64657157 }
64667158
64677159 static void
....@@ -6471,6 +7163,9 @@
64717163 struct trace_array *tr = data;
64727164
64737165 pid_list = rcu_dereference_sched(tr->function_pids);
7166
+ trace_filter_add_remove_task(pid_list, NULL, task);
7167
+
7168
+ pid_list = rcu_dereference_sched(tr->function_no_pids);
64747169 trace_filter_add_remove_task(pid_list, NULL, task);
64757170 }
64767171
....@@ -6489,42 +7184,57 @@
64897184 }
64907185 }
64917186
6492
-static void clear_ftrace_pids(struct trace_array *tr)
7187
+static void clear_ftrace_pids(struct trace_array *tr, int type)
64937188 {
64947189 struct trace_pid_list *pid_list;
7190
+ struct trace_pid_list *no_pid_list;
64957191 int cpu;
64967192
64977193 pid_list = rcu_dereference_protected(tr->function_pids,
64987194 lockdep_is_held(&ftrace_lock));
6499
- if (!pid_list)
7195
+ no_pid_list = rcu_dereference_protected(tr->function_no_pids,
7196
+ lockdep_is_held(&ftrace_lock));
7197
+
7198
+ /* Make sure there's something to do */
7199
+ if (!pid_type_enabled(type, pid_list, no_pid_list))
65007200 return;
65017201
6502
- unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
7202
+ /* See if the pids still need to be checked after this */
7203
+ if (!still_need_pid_events(type, pid_list, no_pid_list)) {
7204
+ unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
7205
+ for_each_possible_cpu(cpu)
7206
+ per_cpu_ptr(tr->array_buffer.data, cpu)->ftrace_ignore_pid = FTRACE_PID_TRACE;
7207
+ }
65037208
6504
- for_each_possible_cpu(cpu)
6505
- per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
7209
+ if (type & TRACE_PIDS)
7210
+ rcu_assign_pointer(tr->function_pids, NULL);
65067211
6507
- rcu_assign_pointer(tr->function_pids, NULL);
7212
+ if (type & TRACE_NO_PIDS)
7213
+ rcu_assign_pointer(tr->function_no_pids, NULL);
65087214
65097215 /* Wait till all users are no longer using pid filtering */
6510
- synchronize_sched();
7216
+ synchronize_rcu();
65117217
6512
- trace_free_pid_list(pid_list);
7218
+ if ((type & TRACE_PIDS) && pid_list)
7219
+ trace_free_pid_list(pid_list);
7220
+
7221
+ if ((type & TRACE_NO_PIDS) && no_pid_list)
7222
+ trace_free_pid_list(no_pid_list);
65137223 }
65147224
65157225 void ftrace_clear_pids(struct trace_array *tr)
65167226 {
65177227 mutex_lock(&ftrace_lock);
65187228
6519
- clear_ftrace_pids(tr);
7229
+ clear_ftrace_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
65207230
65217231 mutex_unlock(&ftrace_lock);
65227232 }
65237233
6524
-static void ftrace_pid_reset(struct trace_array *tr)
7234
+static void ftrace_pid_reset(struct trace_array *tr, int type)
65257235 {
65267236 mutex_lock(&ftrace_lock);
6527
- clear_ftrace_pids(tr);
7237
+ clear_ftrace_pids(tr, type);
65287238
65297239 ftrace_update_pid_func();
65307240 ftrace_startup_all(0);
....@@ -6588,21 +7298,71 @@
65887298 .show = fpid_show,
65897299 };
65907300
6591
-static int
6592
-ftrace_pid_open(struct inode *inode, struct file *file)
7301
+static void *fnpid_start(struct seq_file *m, loff_t *pos)
7302
+ __acquires(RCU)
65937303 {
7304
+ struct trace_pid_list *pid_list;
7305
+ struct trace_array *tr = m->private;
7306
+
7307
+ mutex_lock(&ftrace_lock);
7308
+ rcu_read_lock_sched();
7309
+
7310
+ pid_list = rcu_dereference_sched(tr->function_no_pids);
7311
+
7312
+ if (!pid_list)
7313
+ return !(*pos) ? FTRACE_NO_PIDS : NULL;
7314
+
7315
+ return trace_pid_start(pid_list, pos);
7316
+}
7317
+
7318
+static void *fnpid_next(struct seq_file *m, void *v, loff_t *pos)
7319
+{
7320
+ struct trace_array *tr = m->private;
7321
+ struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_no_pids);
7322
+
7323
+ if (v == FTRACE_NO_PIDS) {
7324
+ (*pos)++;
7325
+ return NULL;
7326
+ }
7327
+ return trace_pid_next(pid_list, v, pos);
7328
+}
7329
+
7330
+static const struct seq_operations ftrace_no_pid_sops = {
7331
+ .start = fnpid_start,
7332
+ .next = fnpid_next,
7333
+ .stop = fpid_stop,
7334
+ .show = fpid_show,
7335
+};
7336
+
7337
+static int pid_open(struct inode *inode, struct file *file, int type)
7338
+{
7339
+ const struct seq_operations *seq_ops;
65947340 struct trace_array *tr = inode->i_private;
65957341 struct seq_file *m;
65967342 int ret = 0;
65977343
6598
- if (trace_array_get(tr) < 0)
6599
- return -ENODEV;
7344
+ ret = tracing_check_open_get_tr(tr);
7345
+ if (ret)
7346
+ return ret;
66007347
66017348 if ((file->f_mode & FMODE_WRITE) &&
66027349 (file->f_flags & O_TRUNC))
6603
- ftrace_pid_reset(tr);
7350
+ ftrace_pid_reset(tr, type);
66047351
6605
- ret = seq_open(file, &ftrace_pid_sops);
7352
+ switch (type) {
7353
+ case TRACE_PIDS:
7354
+ seq_ops = &ftrace_pid_sops;
7355
+ break;
7356
+ case TRACE_NO_PIDS:
7357
+ seq_ops = &ftrace_no_pid_sops;
7358
+ break;
7359
+ default:
7360
+ trace_array_put(tr);
7361
+ WARN_ON_ONCE(1);
7362
+ return -EINVAL;
7363
+ }
7364
+
7365
+ ret = seq_open(file, seq_ops);
66067366 if (ret < 0) {
66077367 trace_array_put(tr);
66087368 } else {
....@@ -6614,10 +7374,23 @@
66147374 return ret;
66157375 }
66167376
7377
+static int
7378
+ftrace_pid_open(struct inode *inode, struct file *file)
7379
+{
7380
+ return pid_open(inode, file, TRACE_PIDS);
7381
+}
7382
+
7383
+static int
7384
+ftrace_no_pid_open(struct inode *inode, struct file *file)
7385
+{
7386
+ return pid_open(inode, file, TRACE_NO_PIDS);
7387
+}
7388
+
66177389 static void ignore_task_cpu(void *data)
66187390 {
66197391 struct trace_array *tr = data;
66207392 struct trace_pid_list *pid_list;
7393
+ struct trace_pid_list *no_pid_list;
66217394
66227395 /*
66237396 * This function is called by on_each_cpu() while the
....@@ -6625,18 +7398,25 @@
66257398 */
66267399 pid_list = rcu_dereference_protected(tr->function_pids,
66277400 mutex_is_locked(&ftrace_lock));
7401
+ no_pid_list = rcu_dereference_protected(tr->function_no_pids,
7402
+ mutex_is_locked(&ftrace_lock));
66287403
6629
- this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6630
- trace_ignore_this_task(pid_list, current));
7404
+ if (trace_ignore_this_task(pid_list, no_pid_list, current))
7405
+ this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7406
+ FTRACE_PID_IGNORE);
7407
+ else
7408
+ this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7409
+ current->pid);
66317410 }
66327411
66337412 static ssize_t
6634
-ftrace_pid_write(struct file *filp, const char __user *ubuf,
6635
- size_t cnt, loff_t *ppos)
7413
+pid_write(struct file *filp, const char __user *ubuf,
7414
+ size_t cnt, loff_t *ppos, int type)
66367415 {
66377416 struct seq_file *m = filp->private_data;
66387417 struct trace_array *tr = m->private;
6639
- struct trace_pid_list *filtered_pids = NULL;
7418
+ struct trace_pid_list *filtered_pids;
7419
+ struct trace_pid_list *other_pids;
66407420 struct trace_pid_list *pid_list;
66417421 ssize_t ret;
66427422
....@@ -6645,19 +7425,43 @@
66457425
66467426 mutex_lock(&ftrace_lock);
66477427
6648
- filtered_pids = rcu_dereference_protected(tr->function_pids,
7428
+ switch (type) {
7429
+ case TRACE_PIDS:
7430
+ filtered_pids = rcu_dereference_protected(tr->function_pids,
66497431 lockdep_is_held(&ftrace_lock));
7432
+ other_pids = rcu_dereference_protected(tr->function_no_pids,
7433
+ lockdep_is_held(&ftrace_lock));
7434
+ break;
7435
+ case TRACE_NO_PIDS:
7436
+ filtered_pids = rcu_dereference_protected(tr->function_no_pids,
7437
+ lockdep_is_held(&ftrace_lock));
7438
+ other_pids = rcu_dereference_protected(tr->function_pids,
7439
+ lockdep_is_held(&ftrace_lock));
7440
+ break;
7441
+ default:
7442
+ ret = -EINVAL;
7443
+ WARN_ON_ONCE(1);
7444
+ goto out;
7445
+ }
66507446
66517447 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
66527448 if (ret < 0)
66537449 goto out;
66547450
6655
- rcu_assign_pointer(tr->function_pids, pid_list);
7451
+ switch (type) {
7452
+ case TRACE_PIDS:
7453
+ rcu_assign_pointer(tr->function_pids, pid_list);
7454
+ break;
7455
+ case TRACE_NO_PIDS:
7456
+ rcu_assign_pointer(tr->function_no_pids, pid_list);
7457
+ break;
7458
+ }
7459
+
66567460
66577461 if (filtered_pids) {
6658
- synchronize_sched();
7462
+ synchronize_rcu();
66597463 trace_free_pid_list(filtered_pids);
6660
- } else if (pid_list) {
7464
+ } else if (pid_list && !other_pids) {
66617465 /* Register a probe to set whether to ignore the tracing of a task */
66627466 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
66637467 }
....@@ -6680,6 +7484,20 @@
66807484 return ret;
66817485 }
66827486
7487
+static ssize_t
7488
+ftrace_pid_write(struct file *filp, const char __user *ubuf,
7489
+ size_t cnt, loff_t *ppos)
7490
+{
7491
+ return pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
7492
+}
7493
+
7494
+static ssize_t
7495
+ftrace_no_pid_write(struct file *filp, const char __user *ubuf,
7496
+ size_t cnt, loff_t *ppos)
7497
+{
7498
+ return pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
7499
+}
7500
+
66837501 static int
66847502 ftrace_pid_release(struct inode *inode, struct file *file)
66857503 {
....@@ -6698,10 +7516,20 @@
66987516 .release = ftrace_pid_release,
66997517 };
67007518
7519
+static const struct file_operations ftrace_no_pid_fops = {
7520
+ .open = ftrace_no_pid_open,
7521
+ .write = ftrace_no_pid_write,
7522
+ .read = seq_read,
7523
+ .llseek = tracing_lseek,
7524
+ .release = ftrace_pid_release,
7525
+};
7526
+
67017527 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
67027528 {
67037529 trace_create_file("set_ftrace_pid", 0644, d_tracer,
67047530 tr, &ftrace_pid_fops);
7531
+ trace_create_file("set_ftrace_notrace_pid", 0644, d_tracer,
7532
+ tr, &ftrace_no_pid_fops);
67057533 }
67067534
67077535 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
....@@ -6781,10 +7609,21 @@
67817609 }
67827610 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
67837611
7612
+static bool is_permanent_ops_registered(void)
7613
+{
7614
+ struct ftrace_ops *op;
7615
+
7616
+ do_for_each_ftrace_op(op, ftrace_ops_list) {
7617
+ if (op->flags & FTRACE_OPS_FL_PERMANENT)
7618
+ return true;
7619
+ } while_for_each_ftrace_op(op);
7620
+
7621
+ return false;
7622
+}
7623
+
67847624 int
67857625 ftrace_enable_sysctl(struct ctl_table *table, int write,
6786
- void __user *buffer, size_t *lenp,
6787
- loff_t *ppos)
7626
+ void *buffer, size_t *lenp, loff_t *ppos)
67887627 {
67897628 int ret = -ENODEV;
67907629
....@@ -6798,8 +7637,6 @@
67987637 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
67997638 goto out;
68007639
6801
- last_ftrace_enabled = !!ftrace_enabled;
6802
-
68037640 if (ftrace_enabled) {
68047641
68057642 /* we are starting ftrace again */
....@@ -6810,364 +7647,20 @@
68107647 ftrace_startup_sysctl();
68117648
68127649 } else {
7650
+ if (is_permanent_ops_registered()) {
7651
+ ftrace_enabled = true;
7652
+ ret = -EBUSY;
7653
+ goto out;
7654
+ }
7655
+
68137656 /* stopping ftrace calls (just send to ftrace_stub) */
68147657 ftrace_trace_function = ftrace_stub;
68157658
68167659 ftrace_shutdown_sysctl();
68177660 }
68187661
7662
+ last_ftrace_enabled = !!ftrace_enabled;
68197663 out:
68207664 mutex_unlock(&ftrace_lock);
68217665 return ret;
68227666 }
6823
-
6824
-#ifdef CONFIG_FUNCTION_GRAPH_TRACER
6825
-
6826
-static struct ftrace_ops graph_ops = {
6827
- .func = ftrace_stub,
6828
- .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6829
- FTRACE_OPS_FL_INITIALIZED |
6830
- FTRACE_OPS_FL_PID |
6831
- FTRACE_OPS_FL_STUB,
6832
-#ifdef FTRACE_GRAPH_TRAMP_ADDR
6833
- .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
6834
- /* trampoline_size is only needed for dynamically allocated tramps */
6835
-#endif
6836
- ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
6837
-};
6838
-
6839
-void ftrace_graph_sleep_time_control(bool enable)
6840
-{
6841
- fgraph_sleep_time = enable;
6842
-}
6843
-
6844
-void ftrace_graph_graph_time_control(bool enable)
6845
-{
6846
- fgraph_graph_time = enable;
6847
-}
6848
-
6849
-void ftrace_graph_return_stub(struct ftrace_graph_ret *trace)
6850
-{
6851
-}
6852
-
6853
-int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
6854
-{
6855
- return 0;
6856
-}
6857
-
6858
-/* The callbacks that hook a function */
6859
-trace_func_graph_ret_t ftrace_graph_return = ftrace_graph_return_stub;
6860
-trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
6861
-static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
6862
-
6863
-/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
6864
-static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
6865
-{
6866
- int i;
6867
- int ret = 0;
6868
- int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
6869
- struct task_struct *g, *t;
6870
-
6871
- for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
6872
- ret_stack_list[i] =
6873
- kmalloc_array(FTRACE_RETFUNC_DEPTH,
6874
- sizeof(struct ftrace_ret_stack),
6875
- GFP_KERNEL);
6876
- if (!ret_stack_list[i]) {
6877
- start = 0;
6878
- end = i;
6879
- ret = -ENOMEM;
6880
- goto free;
6881
- }
6882
- }
6883
-
6884
- read_lock(&tasklist_lock);
6885
- do_each_thread(g, t) {
6886
- if (start == end) {
6887
- ret = -EAGAIN;
6888
- goto unlock;
6889
- }
6890
-
6891
- if (t->ret_stack == NULL) {
6892
- atomic_set(&t->trace_overrun, 0);
6893
- t->curr_ret_stack = -1;
6894
- t->curr_ret_depth = -1;
6895
- /* Make sure the tasks see the -1 first: */
6896
- smp_wmb();
6897
- t->ret_stack = ret_stack_list[start++];
6898
- }
6899
- } while_each_thread(g, t);
6900
-
6901
-unlock:
6902
- read_unlock(&tasklist_lock);
6903
-free:
6904
- for (i = start; i < end; i++)
6905
- kfree(ret_stack_list[i]);
6906
- return ret;
6907
-}
6908
-
6909
-static void
6910
-ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
6911
- struct task_struct *prev, struct task_struct *next)
6912
-{
6913
- unsigned long long timestamp;
6914
- int index;
6915
-
6916
- /*
6917
- * Does the user want to count the time a function was asleep.
6918
- * If so, do not update the time stamps.
6919
- */
6920
- if (fgraph_sleep_time)
6921
- return;
6922
-
6923
- timestamp = trace_clock_local();
6924
-
6925
- prev->ftrace_timestamp = timestamp;
6926
-
6927
- /* only process tasks that we timestamped */
6928
- if (!next->ftrace_timestamp)
6929
- return;
6930
-
6931
- /*
6932
- * Update all the counters in next to make up for the
6933
- * time next was sleeping.
6934
- */
6935
- timestamp -= next->ftrace_timestamp;
6936
-
6937
- for (index = next->curr_ret_stack; index >= 0; index--)
6938
- next->ret_stack[index].calltime += timestamp;
6939
-}
6940
-
6941
-/* Allocate a return stack for each task */
6942
-static int start_graph_tracing(void)
6943
-{
6944
- struct ftrace_ret_stack **ret_stack_list;
6945
- int ret, cpu;
6946
-
6947
- ret_stack_list = kmalloc_array(FTRACE_RETSTACK_ALLOC_SIZE,
6948
- sizeof(struct ftrace_ret_stack *),
6949
- GFP_KERNEL);
6950
-
6951
- if (!ret_stack_list)
6952
- return -ENOMEM;
6953
-
6954
- /* The cpu_boot init_task->ret_stack will never be freed */
6955
- for_each_online_cpu(cpu) {
6956
- if (!idle_task(cpu)->ret_stack)
6957
- ftrace_graph_init_idle_task(idle_task(cpu), cpu);
6958
- }
6959
-
6960
- do {
6961
- ret = alloc_retstack_tasklist(ret_stack_list);
6962
- } while (ret == -EAGAIN);
6963
-
6964
- if (!ret) {
6965
- ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
6966
- if (ret)
6967
- pr_info("ftrace_graph: Couldn't activate tracepoint"
6968
- " probe to kernel_sched_switch\n");
6969
- }
6970
-
6971
- kfree(ret_stack_list);
6972
- return ret;
6973
-}
6974
-
6975
-/*
6976
- * Hibernation protection.
6977
- * The state of the current task is too much unstable during
6978
- * suspend/restore to disk. We want to protect against that.
6979
- */
6980
-static int
6981
-ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
6982
- void *unused)
6983
-{
6984
- switch (state) {
6985
- case PM_HIBERNATION_PREPARE:
6986
- pause_graph_tracing();
6987
- break;
6988
-
6989
- case PM_POST_HIBERNATION:
6990
- unpause_graph_tracing();
6991
- break;
6992
- }
6993
- return NOTIFY_DONE;
6994
-}
6995
-
6996
-static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
6997
-{
6998
- if (!ftrace_ops_test(&global_ops, trace->func, NULL))
6999
- return 0;
7000
- return __ftrace_graph_entry(trace);
7001
-}
7002
-
7003
-/*
7004
- * The function graph tracer should only trace the functions defined
7005
- * by set_ftrace_filter and set_ftrace_notrace. If another function
7006
- * tracer ops is registered, the graph tracer requires testing the
7007
- * function against the global ops, and not just trace any function
7008
- * that any ftrace_ops registered.
7009
- */
7010
-static void update_function_graph_func(void)
7011
-{
7012
- struct ftrace_ops *op;
7013
- bool do_test = false;
7014
-
7015
- /*
7016
- * The graph and global ops share the same set of functions
7017
- * to test. If any other ops is on the list, then
7018
- * the graph tracing needs to test if its the function
7019
- * it should call.
7020
- */
7021
- do_for_each_ftrace_op(op, ftrace_ops_list) {
7022
- if (op != &global_ops && op != &graph_ops &&
7023
- op != &ftrace_list_end) {
7024
- do_test = true;
7025
- /* in double loop, break out with goto */
7026
- goto out;
7027
- }
7028
- } while_for_each_ftrace_op(op);
7029
- out:
7030
- if (do_test)
7031
- ftrace_graph_entry = ftrace_graph_entry_test;
7032
- else
7033
- ftrace_graph_entry = __ftrace_graph_entry;
7034
-}
7035
-
7036
-static struct notifier_block ftrace_suspend_notifier = {
7037
- .notifier_call = ftrace_suspend_notifier_call,
7038
-};
7039
-
7040
-int register_ftrace_graph(trace_func_graph_ret_t retfunc,
7041
- trace_func_graph_ent_t entryfunc)
7042
-{
7043
- int ret = 0;
7044
-
7045
- mutex_lock(&ftrace_lock);
7046
-
7047
- /* we currently allow only one tracer registered at a time */
7048
- if (ftrace_graph_active) {
7049
- ret = -EBUSY;
7050
- goto out;
7051
- }
7052
-
7053
- register_pm_notifier(&ftrace_suspend_notifier);
7054
-
7055
- ftrace_graph_active++;
7056
- ret = start_graph_tracing();
7057
- if (ret) {
7058
- ftrace_graph_active--;
7059
- goto out;
7060
- }
7061
-
7062
- ftrace_graph_return = retfunc;
7063
-
7064
- /*
7065
- * Update the indirect function to the entryfunc, and the
7066
- * function that gets called to the entry_test first. Then
7067
- * call the update fgraph entry function to determine if
7068
- * the entryfunc should be called directly or not.
7069
- */
7070
- __ftrace_graph_entry = entryfunc;
7071
- ftrace_graph_entry = ftrace_graph_entry_test;
7072
- update_function_graph_func();
7073
-
7074
- ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
7075
-out:
7076
- mutex_unlock(&ftrace_lock);
7077
- return ret;
7078
-}
7079
-
7080
-void unregister_ftrace_graph(void)
7081
-{
7082
- mutex_lock(&ftrace_lock);
7083
-
7084
- if (unlikely(!ftrace_graph_active))
7085
- goto out;
7086
-
7087
- ftrace_graph_active--;
7088
- ftrace_graph_return = ftrace_graph_return_stub;
7089
- ftrace_graph_entry = ftrace_graph_entry_stub;
7090
- __ftrace_graph_entry = ftrace_graph_entry_stub;
7091
- ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
7092
- unregister_pm_notifier(&ftrace_suspend_notifier);
7093
- unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
7094
-
7095
- out:
7096
- mutex_unlock(&ftrace_lock);
7097
-}
7098
-
7099
-static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
7100
-
7101
-static void
7102
-graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
7103
-{
7104
- atomic_set(&t->trace_overrun, 0);
7105
- t->ftrace_timestamp = 0;
7106
- /* make curr_ret_stack visible before we add the ret_stack */
7107
- smp_wmb();
7108
- t->ret_stack = ret_stack;
7109
-}
7110
-
7111
-/*
7112
- * Allocate a return stack for the idle task. May be the first
7113
- * time through, or it may be done by CPU hotplug online.
7114
- */
7115
-void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
7116
-{
7117
- t->curr_ret_stack = -1;
7118
- t->curr_ret_depth = -1;
7119
- /*
7120
- * The idle task has no parent, it either has its own
7121
- * stack or no stack at all.
7122
- */
7123
- if (t->ret_stack)
7124
- WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
7125
-
7126
- if (ftrace_graph_active) {
7127
- struct ftrace_ret_stack *ret_stack;
7128
-
7129
- ret_stack = per_cpu(idle_ret_stack, cpu);
7130
- if (!ret_stack) {
7131
- ret_stack =
7132
- kmalloc_array(FTRACE_RETFUNC_DEPTH,
7133
- sizeof(struct ftrace_ret_stack),
7134
- GFP_KERNEL);
7135
- if (!ret_stack)
7136
- return;
7137
- per_cpu(idle_ret_stack, cpu) = ret_stack;
7138
- }
7139
- graph_init_task(t, ret_stack);
7140
- }
7141
-}
7142
-
7143
-/* Allocate a return stack for newly created task */
7144
-void ftrace_graph_init_task(struct task_struct *t)
7145
-{
7146
- /* Make sure we do not use the parent ret_stack */
7147
- t->ret_stack = NULL;
7148
- t->curr_ret_stack = -1;
7149
- t->curr_ret_depth = -1;
7150
-
7151
- if (ftrace_graph_active) {
7152
- struct ftrace_ret_stack *ret_stack;
7153
-
7154
- ret_stack = kmalloc_array(FTRACE_RETFUNC_DEPTH,
7155
- sizeof(struct ftrace_ret_stack),
7156
- GFP_KERNEL);
7157
- if (!ret_stack)
7158
- return;
7159
- graph_init_task(t, ret_stack);
7160
- }
7161
-}
7162
-
7163
-void ftrace_graph_exit_task(struct task_struct *t)
7164
-{
7165
- struct ftrace_ret_stack *ret_stack = t->ret_stack;
7166
-
7167
- t->ret_stack = NULL;
7168
- /* NULL must become visible to IRQs before we free it: */
7169
- barrier();
7170
-
7171
- kfree(ret_stack);
7172
-}
7173
-#endif