aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/trace
diff options
context:
space:
mode:
authorJiri Kosina <jkosina@suse.cz>2014-11-20 08:42:02 -0500
committerJiri Kosina <jkosina@suse.cz>2014-11-20 08:42:02 -0500
commita02001086bbfb4da35d1228bebc2f1b442db455f (patch)
tree62ab47936cef06fd08657ca5b6cd1df98c19be57 /kernel/trace
parenteff264efeeb0898408e8c9df72d8a32621035bed (diff)
parentfc14f9c1272f62c3e8d01300f52467c0d9af50f9 (diff)
Merge Linus' tree to be be to apply submitted patches to newer code than
current trivial.git base
Diffstat (limited to 'kernel/trace')
-rw-r--r--kernel/trace/ftrace.c640
-rw-r--r--kernel/trace/ring_buffer.c128
-rw-r--r--kernel/trace/ring_buffer_benchmark.c3
-rw-r--r--kernel/trace/trace.c33
-rw-r--r--kernel/trace/trace_events.c5
-rw-r--r--kernel/trace/trace_selftest.c51
-rw-r--r--kernel/trace/trace_stack.c4
-rw-r--r--kernel/trace/trace_syscalls.c12
8 files changed, 540 insertions, 336 deletions
diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
index 1654b12c891a..31c90fec4158 100644
--- a/kernel/trace/ftrace.c
+++ b/kernel/trace/ftrace.c
@@ -65,15 +65,21 @@
65#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_CONTROL) 65#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_CONTROL)
66 66
67#ifdef CONFIG_DYNAMIC_FTRACE 67#ifdef CONFIG_DYNAMIC_FTRACE
68#define INIT_REGEX_LOCK(opsname) \ 68#define INIT_OPS_HASH(opsname) \
69 .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock), 69 .func_hash = &opsname.local_hash, \
70 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
71#define ASSIGN_OPS_HASH(opsname, val) \
72 .func_hash = val, \
73 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
70#else 74#else
71#define INIT_REGEX_LOCK(opsname) 75#define INIT_OPS_HASH(opsname)
76#define ASSIGN_OPS_HASH(opsname, val)
72#endif 77#endif
73 78
74static struct ftrace_ops ftrace_list_end __read_mostly = { 79static struct ftrace_ops ftrace_list_end __read_mostly = {
75 .func = ftrace_stub, 80 .func = ftrace_stub,
76 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB, 81 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
82 INIT_OPS_HASH(ftrace_list_end)
77}; 83};
78 84
79/* ftrace_enabled is a method to turn ftrace on or off */ 85/* ftrace_enabled is a method to turn ftrace on or off */
@@ -107,6 +113,9 @@ ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
107static struct ftrace_ops global_ops; 113static struct ftrace_ops global_ops;
108static struct ftrace_ops control_ops; 114static struct ftrace_ops control_ops;
109 115
116static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
117 struct ftrace_ops *op, struct pt_regs *regs);
118
110#if ARCH_SUPPORTS_FTRACE_OPS 119#if ARCH_SUPPORTS_FTRACE_OPS
111static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, 120static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
112 struct ftrace_ops *op, struct pt_regs *regs); 121 struct ftrace_ops *op, struct pt_regs *regs);
@@ -140,7 +149,8 @@ static inline void ftrace_ops_init(struct ftrace_ops *ops)
140{ 149{
141#ifdef CONFIG_DYNAMIC_FTRACE 150#ifdef CONFIG_DYNAMIC_FTRACE
142 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) { 151 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
143 mutex_init(&ops->regex_lock); 152 mutex_init(&ops->local_hash.regex_lock);
153 ops->func_hash = &ops->local_hash;
144 ops->flags |= FTRACE_OPS_FL_INITIALIZED; 154 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
145 } 155 }
146#endif 156#endif
@@ -244,18 +254,24 @@ static void update_ftrace_function(void)
244 ftrace_func_t func; 254 ftrace_func_t func;
245 255
246 /* 256 /*
257 * Prepare the ftrace_ops that the arch callback will use.
258 * If there's only one ftrace_ops registered, the ftrace_ops_list
259 * will point to the ops we want.
260 */
261 set_function_trace_op = ftrace_ops_list;
262
263 /* If there's no ftrace_ops registered, just call the stub function */
264 if (ftrace_ops_list == &ftrace_list_end) {
265 func = ftrace_stub;
266
267 /*
247 * If we are at the end of the list and this ops is 268 * If we are at the end of the list and this ops is
248 * recursion safe and not dynamic and the arch supports passing ops, 269 * recursion safe and not dynamic and the arch supports passing ops,
249 * then have the mcount trampoline call the function directly. 270 * then have the mcount trampoline call the function directly.
250 */ 271 */
251 if (ftrace_ops_list == &ftrace_list_end || 272 } else if (ftrace_ops_list->next == &ftrace_list_end) {
252 (ftrace_ops_list->next == &ftrace_list_end && 273 func = ftrace_ops_get_func(ftrace_ops_list);
253 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) && 274
254 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
255 !FTRACE_FORCE_LIST_FUNC)) {
256 /* Set the ftrace_ops that the arch callback uses */
257 set_function_trace_op = ftrace_ops_list;
258 func = ftrace_ops_list->func;
259 } else { 275 } else {
260 /* Just use the default ftrace_ops */ 276 /* Just use the default ftrace_ops */
261 set_function_trace_op = &ftrace_list_end; 277 set_function_trace_op = &ftrace_list_end;
@@ -899,7 +915,7 @@ static void unregister_ftrace_profiler(void)
899static struct ftrace_ops ftrace_profile_ops __read_mostly = { 915static struct ftrace_ops ftrace_profile_ops __read_mostly = {
900 .func = function_profile_call, 916 .func = function_profile_call,
901 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED, 917 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
902 INIT_REGEX_LOCK(ftrace_profile_ops) 918 INIT_OPS_HASH(ftrace_profile_ops)
903}; 919};
904 920
905static int register_ftrace_profiler(void) 921static int register_ftrace_profiler(void)
@@ -1041,6 +1057,12 @@ static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1041 1057
1042static struct ftrace_ops *removed_ops; 1058static struct ftrace_ops *removed_ops;
1043 1059
1060/*
1061 * Set when doing a global update, like enabling all recs or disabling them.
1062 * It is not set when just updating a single ftrace_ops.
1063 */
1064static bool update_all_ops;
1065
1044#ifndef CONFIG_FTRACE_MCOUNT_RECORD 1066#ifndef CONFIG_FTRACE_MCOUNT_RECORD
1045# error Dynamic ftrace depends on MCOUNT_RECORD 1067# error Dynamic ftrace depends on MCOUNT_RECORD
1046#endif 1068#endif
@@ -1081,11 +1103,12 @@ static const struct ftrace_hash empty_hash = {
1081#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash) 1103#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1082 1104
1083static struct ftrace_ops global_ops = { 1105static struct ftrace_ops global_ops = {
1084 .func = ftrace_stub, 1106 .func = ftrace_stub,
1085 .notrace_hash = EMPTY_HASH, 1107 .local_hash.notrace_hash = EMPTY_HASH,
1086 .filter_hash = EMPTY_HASH, 1108 .local_hash.filter_hash = EMPTY_HASH,
1087 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED, 1109 INIT_OPS_HASH(global_ops)
1088 INIT_REGEX_LOCK(global_ops) 1110 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1111 FTRACE_OPS_FL_INITIALIZED,
1089}; 1112};
1090 1113
1091struct ftrace_page { 1114struct ftrace_page {
@@ -1226,8 +1249,8 @@ static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1226void ftrace_free_filter(struct ftrace_ops *ops) 1249void ftrace_free_filter(struct ftrace_ops *ops)
1227{ 1250{
1228 ftrace_ops_init(ops); 1251 ftrace_ops_init(ops);
1229 free_ftrace_hash(ops->filter_hash); 1252 free_ftrace_hash(ops->func_hash->filter_hash);
1230 free_ftrace_hash(ops->notrace_hash); 1253 free_ftrace_hash(ops->func_hash->notrace_hash);
1231} 1254}
1232 1255
1233static struct ftrace_hash *alloc_ftrace_hash(int size_bits) 1256static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
@@ -1288,9 +1311,9 @@ alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1288} 1311}
1289 1312
1290static void 1313static void
1291ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash); 1314ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1292static void 1315static void
1293ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash); 1316ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1294 1317
1295static int 1318static int
1296ftrace_hash_move(struct ftrace_ops *ops, int enable, 1319ftrace_hash_move(struct ftrace_ops *ops, int enable,
@@ -1299,7 +1322,6 @@ ftrace_hash_move(struct ftrace_ops *ops, int enable,
1299 struct ftrace_func_entry *entry; 1322 struct ftrace_func_entry *entry;
1300 struct hlist_node *tn; 1323 struct hlist_node *tn;
1301 struct hlist_head *hhd; 1324 struct hlist_head *hhd;
1302 struct ftrace_hash *old_hash;
1303 struct ftrace_hash *new_hash; 1325 struct ftrace_hash *new_hash;
1304 int size = src->count; 1326 int size = src->count;
1305 int bits = 0; 1327 int bits = 0;
@@ -1342,17 +1364,30 @@ update:
1342 * Remove the current set, update the hash and add 1364 * Remove the current set, update the hash and add
1343 * them back. 1365 * them back.
1344 */ 1366 */
1345 ftrace_hash_rec_disable(ops, enable); 1367 ftrace_hash_rec_disable_modify(ops, enable);
1346 1368
1347 old_hash = *dst;
1348 rcu_assign_pointer(*dst, new_hash); 1369 rcu_assign_pointer(*dst, new_hash);
1349 free_ftrace_hash_rcu(old_hash);
1350 1370
1351 ftrace_hash_rec_enable(ops, enable); 1371 ftrace_hash_rec_enable_modify(ops, enable);
1352 1372
1353 return 0; 1373 return 0;
1354} 1374}
1355 1375
1376static bool hash_contains_ip(unsigned long ip,
1377 struct ftrace_ops_hash *hash)
1378{
1379 /*
1380 * The function record is a match if it exists in the filter
1381 * hash and not in the notrace hash. Note, an emty hash is
1382 * considered a match for the filter hash, but an empty
1383 * notrace hash is considered not in the notrace hash.
1384 */
1385 return (ftrace_hash_empty(hash->filter_hash) ||
1386 ftrace_lookup_ip(hash->filter_hash, ip)) &&
1387 (ftrace_hash_empty(hash->notrace_hash) ||
1388 !ftrace_lookup_ip(hash->notrace_hash, ip));
1389}
1390
1356/* 1391/*
1357 * Test the hashes for this ops to see if we want to call 1392 * Test the hashes for this ops to see if we want to call
1358 * the ops->func or not. 1393 * the ops->func or not.
@@ -1368,8 +1403,7 @@ update:
1368static int 1403static int
1369ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs) 1404ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1370{ 1405{
1371 struct ftrace_hash *filter_hash; 1406 struct ftrace_ops_hash hash;
1372 struct ftrace_hash *notrace_hash;
1373 int ret; 1407 int ret;
1374 1408
1375#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS 1409#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
@@ -1382,13 +1416,10 @@ ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1382 return 0; 1416 return 0;
1383#endif 1417#endif
1384 1418
1385 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash); 1419 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1386 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash); 1420 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
1387 1421
1388 if ((ftrace_hash_empty(filter_hash) || 1422 if (hash_contains_ip(ip, &hash))
1389 ftrace_lookup_ip(filter_hash, ip)) &&
1390 (ftrace_hash_empty(notrace_hash) ||
1391 !ftrace_lookup_ip(notrace_hash, ip)))
1392 ret = 1; 1423 ret = 1;
1393 else 1424 else
1394 ret = 0; 1425 ret = 0;
@@ -1500,33 +1531,6 @@ static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1500 return keep_regs; 1531 return keep_regs;
1501} 1532}
1502 1533
1503static void ftrace_remove_tramp(struct ftrace_ops *ops,
1504 struct dyn_ftrace *rec)
1505{
1506 struct ftrace_func_entry *entry;
1507
1508 entry = ftrace_lookup_ip(ops->tramp_hash, rec->ip);
1509 if (!entry)
1510 return;
1511
1512 /*
1513 * The tramp_hash entry will be removed at time
1514 * of update.
1515 */
1516 ops->nr_trampolines--;
1517 rec->flags &= ~FTRACE_FL_TRAMP;
1518}
1519
1520static void ftrace_clear_tramps(struct dyn_ftrace *rec)
1521{
1522 struct ftrace_ops *op;
1523
1524 do_for_each_ftrace_op(op, ftrace_ops_list) {
1525 if (op->nr_trampolines)
1526 ftrace_remove_tramp(op, rec);
1527 } while_for_each_ftrace_op(op);
1528}
1529
1530static void __ftrace_hash_rec_update(struct ftrace_ops *ops, 1534static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1531 int filter_hash, 1535 int filter_hash,
1532 bool inc) 1536 bool inc)
@@ -1554,14 +1558,14 @@ static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1554 * gets inversed. 1558 * gets inversed.
1555 */ 1559 */
1556 if (filter_hash) { 1560 if (filter_hash) {
1557 hash = ops->filter_hash; 1561 hash = ops->func_hash->filter_hash;
1558 other_hash = ops->notrace_hash; 1562 other_hash = ops->func_hash->notrace_hash;
1559 if (ftrace_hash_empty(hash)) 1563 if (ftrace_hash_empty(hash))
1560 all = 1; 1564 all = 1;
1561 } else { 1565 } else {
1562 inc = !inc; 1566 inc = !inc;
1563 hash = ops->notrace_hash; 1567 hash = ops->func_hash->notrace_hash;
1564 other_hash = ops->filter_hash; 1568 other_hash = ops->func_hash->filter_hash;
1565 /* 1569 /*
1566 * If the notrace hash has no items, 1570 * If the notrace hash has no items,
1567 * then there's nothing to do. 1571 * then there's nothing to do.
@@ -1615,22 +1619,17 @@ static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1615 * function, and the ops has a trampoline registered 1619 * function, and the ops has a trampoline registered
1616 * for it, then we can call it directly. 1620 * for it, then we can call it directly.
1617 */ 1621 */
1618 if (ftrace_rec_count(rec) == 1 && ops->trampoline) { 1622 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1619 rec->flags |= FTRACE_FL_TRAMP; 1623 rec->flags |= FTRACE_FL_TRAMP;
1620 ops->nr_trampolines++; 1624 else
1621 } else {
1622 /* 1625 /*
1623 * If we are adding another function callback 1626 * If we are adding another function callback
1624 * to this function, and the previous had a 1627 * to this function, and the previous had a
1625 * trampoline used, then we need to go back to 1628 * custom trampoline in use, then we need to go
1626 * the default trampoline. 1629 * back to the default trampoline.
1627 */ 1630 */
1628 rec->flags &= ~FTRACE_FL_TRAMP; 1631 rec->flags &= ~FTRACE_FL_TRAMP;
1629 1632
1630 /* remove trampolines from any ops for this rec */
1631 ftrace_clear_tramps(rec);
1632 }
1633
1634 /* 1633 /*
1635 * If any ops wants regs saved for this function 1634 * If any ops wants regs saved for this function
1636 * then all ops will get saved regs. 1635 * then all ops will get saved regs.
@@ -1642,9 +1641,6 @@ static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1642 return; 1641 return;
1643 rec->flags--; 1642 rec->flags--;
1644 1643
1645 if (ops->trampoline && !ftrace_rec_count(rec))
1646 ftrace_remove_tramp(ops, rec);
1647
1648 /* 1644 /*
1649 * If the rec had REGS enabled and the ops that is 1645 * If the rec had REGS enabled and the ops that is
1650 * being removed had REGS set, then see if there is 1646 * being removed had REGS set, then see if there is
@@ -1659,6 +1655,17 @@ static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1659 } 1655 }
1660 1656
1661 /* 1657 /*
1658 * If the rec had TRAMP enabled, then it needs to
1659 * be cleared. As TRAMP can only be enabled iff
1660 * there is only a single ops attached to it.
1661 * In otherwords, always disable it on decrementing.
1662 * In the future, we may set it if rec count is
1663 * decremented to one, and the ops that is left
1664 * has a trampoline.
1665 */
1666 rec->flags &= ~FTRACE_FL_TRAMP;
1667
1668 /*
1662 * flags will be cleared in ftrace_check_record() 1669 * flags will be cleared in ftrace_check_record()
1663 * if rec count is zero. 1670 * if rec count is zero.
1664 */ 1671 */
@@ -1682,6 +1689,41 @@ static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1682 __ftrace_hash_rec_update(ops, filter_hash, 1); 1689 __ftrace_hash_rec_update(ops, filter_hash, 1);
1683} 1690}
1684 1691
1692static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1693 int filter_hash, int inc)
1694{
1695 struct ftrace_ops *op;
1696
1697 __ftrace_hash_rec_update(ops, filter_hash, inc);
1698
1699 if (ops->func_hash != &global_ops.local_hash)
1700 return;
1701
1702 /*
1703 * If the ops shares the global_ops hash, then we need to update
1704 * all ops that are enabled and use this hash.
1705 */
1706 do_for_each_ftrace_op(op, ftrace_ops_list) {
1707 /* Already done */
1708 if (op == ops)
1709 continue;
1710 if (op->func_hash == &global_ops.local_hash)
1711 __ftrace_hash_rec_update(op, filter_hash, inc);
1712 } while_for_each_ftrace_op(op);
1713}
1714
1715static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1716 int filter_hash)
1717{
1718 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1719}
1720
1721static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1722 int filter_hash)
1723{
1724 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1725}
1726
1685static void print_ip_ins(const char *fmt, unsigned char *p) 1727static void print_ip_ins(const char *fmt, unsigned char *p)
1686{ 1728{
1687 int i; 1729 int i;
@@ -1842,21 +1884,86 @@ int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1842} 1884}
1843 1885
1844static struct ftrace_ops * 1886static struct ftrace_ops *
1887ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
1888{
1889 struct ftrace_ops *op;
1890 unsigned long ip = rec->ip;
1891
1892 do_for_each_ftrace_op(op, ftrace_ops_list) {
1893
1894 if (!op->trampoline)
1895 continue;
1896
1897 if (hash_contains_ip(ip, op->func_hash))
1898 return op;
1899 } while_for_each_ftrace_op(op);
1900
1901 return NULL;
1902}
1903
1904static struct ftrace_ops *
1845ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec) 1905ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
1846{ 1906{
1847 struct ftrace_ops *op; 1907 struct ftrace_ops *op;
1908 unsigned long ip = rec->ip;
1848 1909
1849 /* Removed ops need to be tested first */ 1910 /*
1850 if (removed_ops && removed_ops->tramp_hash) { 1911 * Need to check removed ops first.
1851 if (ftrace_lookup_ip(removed_ops->tramp_hash, rec->ip)) 1912 * If they are being removed, and this rec has a tramp,
1913 * and this rec is in the ops list, then it would be the
1914 * one with the tramp.
1915 */
1916 if (removed_ops) {
1917 if (hash_contains_ip(ip, &removed_ops->old_hash))
1852 return removed_ops; 1918 return removed_ops;
1853 } 1919 }
1854 1920
1921 /*
1922 * Need to find the current trampoline for a rec.
1923 * Now, a trampoline is only attached to a rec if there
1924 * was a single 'ops' attached to it. But this can be called
1925 * when we are adding another op to the rec or removing the
1926 * current one. Thus, if the op is being added, we can
1927 * ignore it because it hasn't attached itself to the rec
1928 * yet.
1929 *
1930 * If an ops is being modified (hooking to different functions)
1931 * then we don't care about the new functions that are being
1932 * added, just the old ones (that are probably being removed).
1933 *
1934 * If we are adding an ops to a function that already is using
1935 * a trampoline, it needs to be removed (trampolines are only
1936 * for single ops connected), then an ops that is not being
1937 * modified also needs to be checked.
1938 */
1855 do_for_each_ftrace_op(op, ftrace_ops_list) { 1939 do_for_each_ftrace_op(op, ftrace_ops_list) {
1856 if (!op->tramp_hash) 1940
1941 if (!op->trampoline)
1857 continue; 1942 continue;
1858 1943
1859 if (ftrace_lookup_ip(op->tramp_hash, rec->ip)) 1944 /*
1945 * If the ops is being added, it hasn't gotten to
1946 * the point to be removed from this tree yet.
1947 */
1948 if (op->flags & FTRACE_OPS_FL_ADDING)
1949 continue;
1950
1951
1952 /*
1953 * If the ops is being modified and is in the old
1954 * hash, then it is probably being removed from this
1955 * function.
1956 */
1957 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
1958 hash_contains_ip(ip, &op->old_hash))
1959 return op;
1960 /*
1961 * If the ops is not being added or modified, and it's
1962 * in its normal filter hash, then this must be the one
1963 * we want!
1964 */
1965 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
1966 hash_contains_ip(ip, op->func_hash))
1860 return op; 1967 return op;
1861 1968
1862 } while_for_each_ftrace_op(op); 1969 } while_for_each_ftrace_op(op);
@@ -1868,10 +1975,11 @@ static struct ftrace_ops *
1868ftrace_find_tramp_ops_new(struct dyn_ftrace *rec) 1975ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
1869{ 1976{
1870 struct ftrace_ops *op; 1977 struct ftrace_ops *op;
1978 unsigned long ip = rec->ip;
1871 1979
1872 do_for_each_ftrace_op(op, ftrace_ops_list) { 1980 do_for_each_ftrace_op(op, ftrace_ops_list) {
1873 /* pass rec in as regs to have non-NULL val */ 1981 /* pass rec in as regs to have non-NULL val */
1874 if (ftrace_ops_test(op, rec->ip, rec)) 1982 if (hash_contains_ip(ip, op->func_hash))
1875 return op; 1983 return op;
1876 } while_for_each_ftrace_op(op); 1984 } while_for_each_ftrace_op(op);
1877 1985
@@ -1896,8 +2004,8 @@ unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
1896 if (rec->flags & FTRACE_FL_TRAMP) { 2004 if (rec->flags & FTRACE_FL_TRAMP) {
1897 ops = ftrace_find_tramp_ops_new(rec); 2005 ops = ftrace_find_tramp_ops_new(rec);
1898 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) { 2006 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
1899 pr_warning("Bad trampoline accounting at: %p (%pS)\n", 2007 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
1900 (void *)rec->ip, (void *)rec->ip); 2008 (void *)rec->ip, (void *)rec->ip, rec->flags);
1901 /* Ftrace is shutting down, return anything */ 2009 /* Ftrace is shutting down, return anything */
1902 return (unsigned long)FTRACE_ADDR; 2010 return (unsigned long)FTRACE_ADDR;
1903 } 2011 }
@@ -1964,7 +2072,7 @@ __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1964 return ftrace_make_call(rec, ftrace_addr); 2072 return ftrace_make_call(rec, ftrace_addr);
1965 2073
1966 case FTRACE_UPDATE_MAKE_NOP: 2074 case FTRACE_UPDATE_MAKE_NOP:
1967 return ftrace_make_nop(NULL, rec, ftrace_addr); 2075 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
1968 2076
1969 case FTRACE_UPDATE_MODIFY_CALL: 2077 case FTRACE_UPDATE_MODIFY_CALL:
1970 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr); 2078 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
@@ -2178,89 +2286,6 @@ void __weak arch_ftrace_update_code(int command)
2178 ftrace_run_stop_machine(command); 2286 ftrace_run_stop_machine(command);
2179} 2287}
2180 2288
2181static int ftrace_save_ops_tramp_hash(struct ftrace_ops *ops)
2182{
2183 struct ftrace_page *pg;
2184 struct dyn_ftrace *rec;
2185 int size, bits;
2186 int ret;
2187
2188 size = ops->nr_trampolines;
2189 bits = 0;
2190 /*
2191 * Make the hash size about 1/2 the # found
2192 */
2193 for (size /= 2; size; size >>= 1)
2194 bits++;
2195
2196 ops->tramp_hash = alloc_ftrace_hash(bits);
2197 /*
2198 * TODO: a failed allocation is going to screw up
2199 * the accounting of what needs to be modified
2200 * and not. For now, we kill ftrace if we fail
2201 * to allocate here. But there are ways around this,
2202 * but that will take a little more work.
2203 */
2204 if (!ops->tramp_hash)
2205 return -ENOMEM;
2206
2207 do_for_each_ftrace_rec(pg, rec) {
2208 if (ftrace_rec_count(rec) == 1 &&
2209 ftrace_ops_test(ops, rec->ip, rec)) {
2210
2211 /*
2212 * If another ops adds to a rec, the rec will
2213 * lose its trampoline and never get it back
2214 * until all ops are off of it.
2215 */
2216 if (!(rec->flags & FTRACE_FL_TRAMP))
2217 continue;
2218
2219 /* This record had better have a trampoline */
2220 if (FTRACE_WARN_ON(!(rec->flags & FTRACE_FL_TRAMP_EN)))
2221 return -1;
2222
2223 ret = add_hash_entry(ops->tramp_hash, rec->ip);
2224 if (ret < 0)
2225 return ret;
2226 }
2227 } while_for_each_ftrace_rec();
2228
2229 /* The number of recs in the hash must match nr_trampolines */
2230 FTRACE_WARN_ON(ops->tramp_hash->count != ops->nr_trampolines);
2231
2232 return 0;
2233}
2234
2235static int ftrace_save_tramp_hashes(void)
2236{
2237 struct ftrace_ops *op;
2238 int ret;
2239
2240 /*
2241 * Now that any trampoline is being used, we need to save the
2242 * hashes for the ops that have them. This allows the mapping
2243 * back from the record to the ops that has the trampoline to
2244 * know what code is being replaced. Modifying code must always
2245 * verify what it is changing.
2246 */
2247 do_for_each_ftrace_op(op, ftrace_ops_list) {
2248
2249 /* The tramp_hash is recreated each time. */
2250 free_ftrace_hash(op->tramp_hash);
2251 op->tramp_hash = NULL;
2252
2253 if (op->nr_trampolines) {
2254 ret = ftrace_save_ops_tramp_hash(op);
2255 if (ret)
2256 return ret;
2257 }
2258
2259 } while_for_each_ftrace_op(op);
2260
2261 return 0;
2262}
2263
2264static void ftrace_run_update_code(int command) 2289static void ftrace_run_update_code(int command)
2265{ 2290{
2266 int ret; 2291 int ret;
@@ -2280,9 +2305,16 @@ static void ftrace_run_update_code(int command)
2280 2305
2281 ret = ftrace_arch_code_modify_post_process(); 2306 ret = ftrace_arch_code_modify_post_process();
2282 FTRACE_WARN_ON(ret); 2307 FTRACE_WARN_ON(ret);
2308}
2283 2309
2284 ret = ftrace_save_tramp_hashes(); 2310static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2285 FTRACE_WARN_ON(ret); 2311 struct ftrace_hash *old_hash)
2312{
2313 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2314 ops->old_hash.filter_hash = old_hash;
2315 ftrace_run_update_code(command);
2316 ops->old_hash.filter_hash = NULL;
2317 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2286} 2318}
2287 2319
2288static ftrace_func_t saved_ftrace_func; 2320static ftrace_func_t saved_ftrace_func;
@@ -2306,6 +2338,13 @@ static void ftrace_startup_enable(int command)
2306 ftrace_run_update_code(command); 2338 ftrace_run_update_code(command);
2307} 2339}
2308 2340
2341static void ftrace_startup_all(int command)
2342{
2343 update_all_ops = true;
2344 ftrace_startup_enable(command);
2345 update_all_ops = false;
2346}
2347
2309static int ftrace_startup(struct ftrace_ops *ops, int command) 2348static int ftrace_startup(struct ftrace_ops *ops, int command)
2310{ 2349{
2311 int ret; 2350 int ret;
@@ -2320,12 +2359,22 @@ static int ftrace_startup(struct ftrace_ops *ops, int command)
2320 ftrace_start_up++; 2359 ftrace_start_up++;
2321 command |= FTRACE_UPDATE_CALLS; 2360 command |= FTRACE_UPDATE_CALLS;
2322 2361
2323 ops->flags |= FTRACE_OPS_FL_ENABLED; 2362 /*
2363 * Note that ftrace probes uses this to start up
2364 * and modify functions it will probe. But we still
2365 * set the ADDING flag for modification, as probes
2366 * do not have trampolines. If they add them in the
2367 * future, then the probes will need to distinguish
2368 * between adding and updating probes.
2369 */
2370 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2324 2371
2325 ftrace_hash_rec_enable(ops, 1); 2372 ftrace_hash_rec_enable(ops, 1);
2326 2373
2327 ftrace_startup_enable(command); 2374 ftrace_startup_enable(command);
2328 2375
2376 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2377
2329 return 0; 2378 return 0;
2330} 2379}
2331 2380
@@ -2375,11 +2424,35 @@ static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2375 * If the ops uses a trampoline, then it needs to be 2424 * If the ops uses a trampoline, then it needs to be
2376 * tested first on update. 2425 * tested first on update.
2377 */ 2426 */
2427 ops->flags |= FTRACE_OPS_FL_REMOVING;
2378 removed_ops = ops; 2428 removed_ops = ops;
2379 2429
2430 /* The trampoline logic checks the old hashes */
2431 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2432 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2433
2380 ftrace_run_update_code(command); 2434 ftrace_run_update_code(command);
2381 2435
2436 /*
2437 * If there's no more ops registered with ftrace, run a
2438 * sanity check to make sure all rec flags are cleared.
2439 */
2440 if (ftrace_ops_list == &ftrace_list_end) {
2441 struct ftrace_page *pg;
2442 struct dyn_ftrace *rec;
2443
2444 do_for_each_ftrace_rec(pg, rec) {
2445 if (FTRACE_WARN_ON_ONCE(rec->flags))
2446 pr_warn(" %pS flags:%lx\n",
2447 (void *)rec->ip, rec->flags);
2448 } while_for_each_ftrace_rec();
2449 }
2450
2451 ops->old_hash.filter_hash = NULL;
2452 ops->old_hash.notrace_hash = NULL;
2453
2382 removed_ops = NULL; 2454 removed_ops = NULL;
2455 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2383 2456
2384 /* 2457 /*
2385 * Dynamic ops may be freed, we must make sure that all 2458 * Dynamic ops may be freed, we must make sure that all
@@ -2436,8 +2509,8 @@ static inline int ops_traces_mod(struct ftrace_ops *ops)
2436 * Filter_hash being empty will default to trace module. 2509 * Filter_hash being empty will default to trace module.
2437 * But notrace hash requires a test of individual module functions. 2510 * But notrace hash requires a test of individual module functions.
2438 */ 2511 */
2439 return ftrace_hash_empty(ops->filter_hash) && 2512 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2440 ftrace_hash_empty(ops->notrace_hash); 2513 ftrace_hash_empty(ops->func_hash->notrace_hash);
2441} 2514}
2442 2515
2443/* 2516/*
@@ -2459,12 +2532,12 @@ ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2459 return 0; 2532 return 0;
2460 2533
2461 /* The function must be in the filter */ 2534 /* The function must be in the filter */
2462 if (!ftrace_hash_empty(ops->filter_hash) && 2535 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2463 !ftrace_lookup_ip(ops->filter_hash, rec->ip)) 2536 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
2464 return 0; 2537 return 0;
2465 2538
2466 /* If in notrace hash, we ignore it too */ 2539 /* If in notrace hash, we ignore it too */
2467 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip)) 2540 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
2468 return 0; 2541 return 0;
2469 2542
2470 return 1; 2543 return 1;
@@ -2785,10 +2858,10 @@ t_next(struct seq_file *m, void *v, loff_t *pos)
2785 } else { 2858 } else {
2786 rec = &iter->pg->records[iter->idx++]; 2859 rec = &iter->pg->records[iter->idx++];
2787 if (((iter->flags & FTRACE_ITER_FILTER) && 2860 if (((iter->flags & FTRACE_ITER_FILTER) &&
2788 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) || 2861 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
2789 2862
2790 ((iter->flags & FTRACE_ITER_NOTRACE) && 2863 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2791 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) || 2864 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
2792 2865
2793 ((iter->flags & FTRACE_ITER_ENABLED) && 2866 ((iter->flags & FTRACE_ITER_ENABLED) &&
2794 !(rec->flags & FTRACE_FL_ENABLED))) { 2867 !(rec->flags & FTRACE_FL_ENABLED))) {
@@ -2837,9 +2910,9 @@ static void *t_start(struct seq_file *m, loff_t *pos)
2837 * functions are enabled. 2910 * functions are enabled.
2838 */ 2911 */
2839 if ((iter->flags & FTRACE_ITER_FILTER && 2912 if ((iter->flags & FTRACE_ITER_FILTER &&
2840 ftrace_hash_empty(ops->filter_hash)) || 2913 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
2841 (iter->flags & FTRACE_ITER_NOTRACE && 2914 (iter->flags & FTRACE_ITER_NOTRACE &&
2842 ftrace_hash_empty(ops->notrace_hash))) { 2915 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
2843 if (*pos > 0) 2916 if (*pos > 0)
2844 return t_hash_start(m, pos); 2917 return t_hash_start(m, pos);
2845 iter->flags |= FTRACE_ITER_PRINTALL; 2918 iter->flags |= FTRACE_ITER_PRINTALL;
@@ -2904,8 +2977,8 @@ static int t_show(struct seq_file *m, void *v)
2904 if (rec->flags & FTRACE_FL_TRAMP_EN) { 2977 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2905 struct ftrace_ops *ops; 2978 struct ftrace_ops *ops;
2906 2979
2907 ops = ftrace_find_tramp_ops_curr(rec); 2980 ops = ftrace_find_tramp_ops_any(rec);
2908 if (ops && ops->trampoline) 2981 if (ops)
2909 seq_printf(m, "\ttramp: %pS", 2982 seq_printf(m, "\ttramp: %pS",
2910 (void *)ops->trampoline); 2983 (void *)ops->trampoline);
2911 else 2984 else
@@ -3001,12 +3074,12 @@ ftrace_regex_open(struct ftrace_ops *ops, int flag,
3001 iter->ops = ops; 3074 iter->ops = ops;
3002 iter->flags = flag; 3075 iter->flags = flag;
3003 3076
3004 mutex_lock(&ops->regex_lock); 3077 mutex_lock(&ops->func_hash->regex_lock);
3005 3078
3006 if (flag & FTRACE_ITER_NOTRACE) 3079 if (flag & FTRACE_ITER_NOTRACE)
3007 hash = ops->notrace_hash; 3080 hash = ops->func_hash->notrace_hash;
3008 else 3081 else
3009 hash = ops->filter_hash; 3082 hash = ops->func_hash->filter_hash;
3010 3083
3011 if (file->f_mode & FMODE_WRITE) { 3084 if (file->f_mode & FMODE_WRITE) {
3012 const int size_bits = FTRACE_HASH_DEFAULT_BITS; 3085 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
@@ -3041,7 +3114,7 @@ ftrace_regex_open(struct ftrace_ops *ops, int flag,
3041 file->private_data = iter; 3114 file->private_data = iter;
3042 3115
3043 out_unlock: 3116 out_unlock:
3044 mutex_unlock(&ops->regex_lock); 3117 mutex_unlock(&ops->func_hash->regex_lock);
3045 3118
3046 return ret; 3119 return ret;
3047} 3120}
@@ -3279,12 +3352,12 @@ static struct ftrace_ops trace_probe_ops __read_mostly =
3279{ 3352{
3280 .func = function_trace_probe_call, 3353 .func = function_trace_probe_call,
3281 .flags = FTRACE_OPS_FL_INITIALIZED, 3354 .flags = FTRACE_OPS_FL_INITIALIZED,
3282 INIT_REGEX_LOCK(trace_probe_ops) 3355 INIT_OPS_HASH(trace_probe_ops)
3283}; 3356};
3284 3357
3285static int ftrace_probe_registered; 3358static int ftrace_probe_registered;
3286 3359
3287static void __enable_ftrace_function_probe(void) 3360static void __enable_ftrace_function_probe(struct ftrace_hash *old_hash)
3288{ 3361{
3289 int ret; 3362 int ret;
3290 int i; 3363 int i;
@@ -3292,7 +3365,8 @@ static void __enable_ftrace_function_probe(void)
3292 if (ftrace_probe_registered) { 3365 if (ftrace_probe_registered) {
3293 /* still need to update the function call sites */ 3366 /* still need to update the function call sites */
3294 if (ftrace_enabled) 3367 if (ftrace_enabled)
3295 ftrace_run_update_code(FTRACE_UPDATE_CALLS); 3368 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3369 old_hash);
3296 return; 3370 return;
3297 } 3371 }
3298 3372
@@ -3342,7 +3416,8 @@ register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3342 void *data) 3416 void *data)
3343{ 3417{
3344 struct ftrace_func_probe *entry; 3418 struct ftrace_func_probe *entry;
3345 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash; 3419 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3420 struct ftrace_hash *old_hash = *orig_hash;
3346 struct ftrace_hash *hash; 3421 struct ftrace_hash *hash;
3347 struct ftrace_page *pg; 3422 struct ftrace_page *pg;
3348 struct dyn_ftrace *rec; 3423 struct dyn_ftrace *rec;
@@ -3359,9 +3434,9 @@ register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3359 if (WARN_ON(not)) 3434 if (WARN_ON(not))
3360 return -EINVAL; 3435 return -EINVAL;
3361 3436
3362 mutex_lock(&trace_probe_ops.regex_lock); 3437 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3363 3438
3364 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); 3439 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
3365 if (!hash) { 3440 if (!hash) {
3366 count = -ENOMEM; 3441 count = -ENOMEM;
3367 goto out; 3442 goto out;
@@ -3420,15 +3495,18 @@ register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3420 } while_for_each_ftrace_rec(); 3495 } while_for_each_ftrace_rec();
3421 3496
3422 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash); 3497 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3423 if (ret < 0)
3424 count = ret;
3425 3498
3426 __enable_ftrace_function_probe(); 3499 __enable_ftrace_function_probe(old_hash);
3500
3501 if (!ret)
3502 free_ftrace_hash_rcu(old_hash);
3503 else
3504 count = ret;
3427 3505
3428 out_unlock: 3506 out_unlock:
3429 mutex_unlock(&ftrace_lock); 3507 mutex_unlock(&ftrace_lock);
3430 out: 3508 out:
3431 mutex_unlock(&trace_probe_ops.regex_lock); 3509 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3432 free_ftrace_hash(hash); 3510 free_ftrace_hash(hash);
3433 3511
3434 return count; 3512 return count;
@@ -3446,7 +3524,8 @@ __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3446 struct ftrace_func_entry *rec_entry; 3524 struct ftrace_func_entry *rec_entry;
3447 struct ftrace_func_probe *entry; 3525 struct ftrace_func_probe *entry;
3448 struct ftrace_func_probe *p; 3526 struct ftrace_func_probe *p;
3449 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash; 3527 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3528 struct ftrace_hash *old_hash = *orig_hash;
3450 struct list_head free_list; 3529 struct list_head free_list;
3451 struct ftrace_hash *hash; 3530 struct ftrace_hash *hash;
3452 struct hlist_node *tmp; 3531 struct hlist_node *tmp;
@@ -3454,6 +3533,7 @@ __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3454 int type = MATCH_FULL; 3533 int type = MATCH_FULL;
3455 int i, len = 0; 3534 int i, len = 0;
3456 char *search; 3535 char *search;
3536 int ret;
3457 3537
3458 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob))) 3538 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3459 glob = NULL; 3539 glob = NULL;
@@ -3468,7 +3548,7 @@ __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3468 return; 3548 return;
3469 } 3549 }
3470 3550
3471 mutex_lock(&trace_probe_ops.regex_lock); 3551 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3472 3552
3473 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); 3553 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3474 if (!hash) 3554 if (!hash)
@@ -3512,8 +3592,11 @@ __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3512 * Remove after the disable is called. Otherwise, if the last 3592 * Remove after the disable is called. Otherwise, if the last
3513 * probe is removed, a null hash means *all enabled*. 3593 * probe is removed, a null hash means *all enabled*.
3514 */ 3594 */
3515 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash); 3595 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3516 synchronize_sched(); 3596 synchronize_sched();
3597 if (!ret)
3598 free_ftrace_hash_rcu(old_hash);
3599
3517 list_for_each_entry_safe(entry, p, &free_list, free_list) { 3600 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3518 list_del(&entry->free_list); 3601 list_del(&entry->free_list);
3519 ftrace_free_entry(entry); 3602 ftrace_free_entry(entry);
@@ -3521,7 +3604,7 @@ __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3521 mutex_unlock(&ftrace_lock); 3604 mutex_unlock(&ftrace_lock);
3522 3605
3523 out_unlock: 3606 out_unlock:
3524 mutex_unlock(&trace_probe_ops.regex_lock); 3607 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3525 free_ftrace_hash(hash); 3608 free_ftrace_hash(hash);
3526} 3609}
3527 3610
@@ -3700,10 +3783,11 @@ ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3700 return add_hash_entry(hash, ip); 3783 return add_hash_entry(hash, ip);
3701} 3784}
3702 3785
3703static void ftrace_ops_update_code(struct ftrace_ops *ops) 3786static void ftrace_ops_update_code(struct ftrace_ops *ops,
3787 struct ftrace_hash *old_hash)
3704{ 3788{
3705 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled) 3789 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3706 ftrace_run_update_code(FTRACE_UPDATE_CALLS); 3790 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3707} 3791}
3708 3792
3709static int 3793static int
@@ -3711,18 +3795,19 @@ ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3711 unsigned long ip, int remove, int reset, int enable) 3795 unsigned long ip, int remove, int reset, int enable)
3712{ 3796{
3713 struct ftrace_hash **orig_hash; 3797 struct ftrace_hash **orig_hash;
3798 struct ftrace_hash *old_hash;
3714 struct ftrace_hash *hash; 3799 struct ftrace_hash *hash;
3715 int ret; 3800 int ret;
3716 3801
3717 if (unlikely(ftrace_disabled)) 3802 if (unlikely(ftrace_disabled))
3718 return -ENODEV; 3803 return -ENODEV;
3719 3804
3720 mutex_lock(&ops->regex_lock); 3805 mutex_lock(&ops->func_hash->regex_lock);
3721 3806
3722 if (enable) 3807 if (enable)
3723 orig_hash = &ops->filter_hash; 3808 orig_hash = &ops->func_hash->filter_hash;
3724 else 3809 else
3725 orig_hash = &ops->notrace_hash; 3810 orig_hash = &ops->func_hash->notrace_hash;
3726 3811
3727 if (reset) 3812 if (reset)
3728 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); 3813 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
@@ -3745,14 +3830,16 @@ ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3745 } 3830 }
3746 3831
3747 mutex_lock(&ftrace_lock); 3832 mutex_lock(&ftrace_lock);
3833 old_hash = *orig_hash;
3748 ret = ftrace_hash_move(ops, enable, orig_hash, hash); 3834 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3749 if (!ret) 3835 if (!ret) {
3750 ftrace_ops_update_code(ops); 3836 ftrace_ops_update_code(ops, old_hash);
3751 3837 free_ftrace_hash_rcu(old_hash);
3838 }
3752 mutex_unlock(&ftrace_lock); 3839 mutex_unlock(&ftrace_lock);
3753 3840
3754 out_regex_unlock: 3841 out_regex_unlock:
3755 mutex_unlock(&ops->regex_lock); 3842 mutex_unlock(&ops->func_hash->regex_lock);
3756 3843
3757 free_ftrace_hash(hash); 3844 free_ftrace_hash(hash);
3758 return ret; 3845 return ret;
@@ -3957,6 +4044,7 @@ int ftrace_regex_release(struct inode *inode, struct file *file)
3957 struct seq_file *m = (struct seq_file *)file->private_data; 4044 struct seq_file *m = (struct seq_file *)file->private_data;
3958 struct ftrace_iterator *iter; 4045 struct ftrace_iterator *iter;
3959 struct ftrace_hash **orig_hash; 4046 struct ftrace_hash **orig_hash;
4047 struct ftrace_hash *old_hash;
3960 struct trace_parser *parser; 4048 struct trace_parser *parser;
3961 int filter_hash; 4049 int filter_hash;
3962 int ret; 4050 int ret;
@@ -3975,26 +4063,28 @@ int ftrace_regex_release(struct inode *inode, struct file *file)
3975 4063
3976 trace_parser_put(parser); 4064 trace_parser_put(parser);
3977 4065
3978 mutex_lock(&iter->ops->regex_lock); 4066 mutex_lock(&iter->ops->func_hash->regex_lock);
3979 4067
3980 if (file->f_mode & FMODE_WRITE) { 4068 if (file->f_mode & FMODE_WRITE) {
3981 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER); 4069 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3982 4070
3983 if (filter_hash) 4071 if (filter_hash)
3984 orig_hash = &iter->ops->filter_hash; 4072 orig_hash = &iter->ops->func_hash->filter_hash;
3985 else 4073 else
3986 orig_hash = &iter->ops->notrace_hash; 4074 orig_hash = &iter->ops->func_hash->notrace_hash;
3987 4075
3988 mutex_lock(&ftrace_lock); 4076 mutex_lock(&ftrace_lock);
4077 old_hash = *orig_hash;
3989 ret = ftrace_hash_move(iter->ops, filter_hash, 4078 ret = ftrace_hash_move(iter->ops, filter_hash,
3990 orig_hash, iter->hash); 4079 orig_hash, iter->hash);
3991 if (!ret) 4080 if (!ret) {
3992 ftrace_ops_update_code(iter->ops); 4081 ftrace_ops_update_code(iter->ops, old_hash);
3993 4082 free_ftrace_hash_rcu(old_hash);
4083 }
3994 mutex_unlock(&ftrace_lock); 4084 mutex_unlock(&ftrace_lock);
3995 } 4085 }
3996 4086
3997 mutex_unlock(&iter->ops->regex_lock); 4087 mutex_unlock(&iter->ops->func_hash->regex_lock);
3998 free_ftrace_hash(iter->hash); 4088 free_ftrace_hash(iter->hash);
3999 kfree(iter); 4089 kfree(iter);
4000 4090
@@ -4611,7 +4701,6 @@ void __init ftrace_init(void)
4611static struct ftrace_ops global_ops = { 4701static struct ftrace_ops global_ops = {
4612 .func = ftrace_stub, 4702 .func = ftrace_stub,
4613 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED, 4703 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4614 INIT_REGEX_LOCK(global_ops)
4615}; 4704};
4616 4705
4617static int __init ftrace_nodyn_init(void) 4706static int __init ftrace_nodyn_init(void)
@@ -4623,6 +4712,7 @@ core_initcall(ftrace_nodyn_init);
4623 4712
4624static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; } 4713static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4625static inline void ftrace_startup_enable(int command) { } 4714static inline void ftrace_startup_enable(int command) { }
4715static inline void ftrace_startup_all(int command) { }
4626/* Keep as macros so we do not need to define the commands */ 4716/* Keep as macros so we do not need to define the commands */
4627# define ftrace_startup(ops, command) \ 4717# define ftrace_startup(ops, command) \
4628 ({ \ 4718 ({ \
@@ -4713,7 +4803,7 @@ ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4713static struct ftrace_ops control_ops = { 4803static struct ftrace_ops control_ops = {
4714 .func = ftrace_ops_control_func, 4804 .func = ftrace_ops_control_func,
4715 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED, 4805 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4716 INIT_REGEX_LOCK(control_ops) 4806 INIT_OPS_HASH(control_ops)
4717}; 4807};
4718 4808
4719static inline void 4809static inline void
@@ -4772,6 +4862,56 @@ static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4772} 4862}
4773#endif 4863#endif
4774 4864
4865/*
4866 * If there's only one function registered but it does not support
4867 * recursion, this function will be called by the mcount trampoline.
4868 * This function will handle recursion protection.
4869 */
4870static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
4871 struct ftrace_ops *op, struct pt_regs *regs)
4872{
4873 int bit;
4874
4875 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4876 if (bit < 0)
4877 return;
4878
4879 op->func(ip, parent_ip, op, regs);
4880
4881 trace_clear_recursion(bit);
4882}
4883
4884/**
4885 * ftrace_ops_get_func - get the function a trampoline should call
4886 * @ops: the ops to get the function for
4887 *
4888 * Normally the mcount trampoline will call the ops->func, but there
4889 * are times that it should not. For example, if the ops does not
4890 * have its own recursion protection, then it should call the
4891 * ftrace_ops_recurs_func() instead.
4892 *
4893 * Returns the function that the trampoline should call for @ops.
4894 */
4895ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
4896{
4897 /*
4898 * If this is a dynamic ops or we force list func,
4899 * then it needs to call the list anyway.
4900 */
4901 if (ops->flags & FTRACE_OPS_FL_DYNAMIC || FTRACE_FORCE_LIST_FUNC)
4902 return ftrace_ops_list_func;
4903
4904 /*
4905 * If the func handles its own recursion, call it directly.
4906 * Otherwise call the recursion protected function that
4907 * will call the ftrace ops function.
4908 */
4909 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE))
4910 return ftrace_ops_recurs_func;
4911
4912 return ops->func;
4913}
4914
4775static void clear_ftrace_swapper(void) 4915static void clear_ftrace_swapper(void)
4776{ 4916{
4777 struct task_struct *p; 4917 struct task_struct *p;
@@ -4872,7 +5012,8 @@ static int ftrace_pid_add(int p)
4872 set_ftrace_pid_task(pid); 5012 set_ftrace_pid_task(pid);
4873 5013
4874 ftrace_update_pid_func(); 5014 ftrace_update_pid_func();
4875 ftrace_startup_enable(0); 5015
5016 ftrace_startup_all(0);
4876 5017
4877 mutex_unlock(&ftrace_lock); 5018 mutex_unlock(&ftrace_lock);
4878 return 0; 5019 return 0;
@@ -4901,7 +5042,7 @@ static void ftrace_pid_reset(void)
4901 } 5042 }
4902 5043
4903 ftrace_update_pid_func(); 5044 ftrace_update_pid_func();
4904 ftrace_startup_enable(0); 5045 ftrace_startup_all(0);
4905 5046
4906 mutex_unlock(&ftrace_lock); 5047 mutex_unlock(&ftrace_lock);
4907} 5048}
@@ -5145,6 +5286,17 @@ ftrace_enable_sysctl(struct ctl_table *table, int write,
5145 5286
5146#ifdef CONFIG_FUNCTION_GRAPH_TRACER 5287#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5147 5288
5289static struct ftrace_ops graph_ops = {
5290 .func = ftrace_stub,
5291 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5292 FTRACE_OPS_FL_INITIALIZED |
5293 FTRACE_OPS_FL_STUB,
5294#ifdef FTRACE_GRAPH_TRAMP_ADDR
5295 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
5296#endif
5297 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5298};
5299
5148static int ftrace_graph_active; 5300static int ftrace_graph_active;
5149 5301
5150int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace) 5302int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
@@ -5307,12 +5459,28 @@ static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5307 */ 5459 */
5308static void update_function_graph_func(void) 5460static void update_function_graph_func(void)
5309{ 5461{
5310 if (ftrace_ops_list == &ftrace_list_end || 5462 struct ftrace_ops *op;
5311 (ftrace_ops_list == &global_ops && 5463 bool do_test = false;
5312 global_ops.next == &ftrace_list_end)) 5464
5313 ftrace_graph_entry = __ftrace_graph_entry; 5465 /*
5314 else 5466 * The graph and global ops share the same set of functions
5467 * to test. If any other ops is on the list, then
5468 * the graph tracing needs to test if its the function
5469 * it should call.
5470 */
5471 do_for_each_ftrace_op(op, ftrace_ops_list) {
5472 if (op != &global_ops && op != &graph_ops &&
5473 op != &ftrace_list_end) {
5474 do_test = true;
5475 /* in double loop, break out with goto */
5476 goto out;
5477 }
5478 } while_for_each_ftrace_op(op);
5479 out:
5480 if (do_test)
5315 ftrace_graph_entry = ftrace_graph_entry_test; 5481 ftrace_graph_entry = ftrace_graph_entry_test;
5482 else
5483 ftrace_graph_entry = __ftrace_graph_entry;
5316} 5484}
5317 5485
5318static struct notifier_block ftrace_suspend_notifier = { 5486static struct notifier_block ftrace_suspend_notifier = {
@@ -5353,16 +5521,7 @@ int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5353 ftrace_graph_entry = ftrace_graph_entry_test; 5521 ftrace_graph_entry = ftrace_graph_entry_test;
5354 update_function_graph_func(); 5522 update_function_graph_func();
5355 5523
5356 /* Function graph doesn't use the .func field of global_ops */ 5524 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
5357 global_ops.flags |= FTRACE_OPS_FL_STUB;
5358
5359#ifdef CONFIG_DYNAMIC_FTRACE
5360 /* Optimize function graph calling (if implemented by arch) */
5361 if (FTRACE_GRAPH_TRAMP_ADDR != 0)
5362 global_ops.trampoline = FTRACE_GRAPH_TRAMP_ADDR;
5363#endif
5364
5365 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
5366 5525
5367out: 5526out:
5368 mutex_unlock(&ftrace_lock); 5527 mutex_unlock(&ftrace_lock);
@@ -5380,12 +5539,7 @@ void unregister_ftrace_graph(void)
5380 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub; 5539 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
5381 ftrace_graph_entry = ftrace_graph_entry_stub; 5540 ftrace_graph_entry = ftrace_graph_entry_stub;
5382 __ftrace_graph_entry = ftrace_graph_entry_stub; 5541 __ftrace_graph_entry = ftrace_graph_entry_stub;
5383 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET); 5542 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
5384 global_ops.flags &= ~FTRACE_OPS_FL_STUB;
5385#ifdef CONFIG_DYNAMIC_FTRACE
5386 if (FTRACE_GRAPH_TRAMP_ADDR != 0)
5387 global_ops.trampoline = 0;
5388#endif
5389 unregister_pm_notifier(&ftrace_suspend_notifier); 5543 unregister_pm_notifier(&ftrace_suspend_notifier);
5390 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); 5544 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5391 5545
diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index 925f629658d6..a56e07c8d15b 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -538,16 +538,18 @@ static void rb_wake_up_waiters(struct irq_work *work)
538 * ring_buffer_wait - wait for input to the ring buffer 538 * ring_buffer_wait - wait for input to the ring buffer
539 * @buffer: buffer to wait on 539 * @buffer: buffer to wait on
540 * @cpu: the cpu buffer to wait on 540 * @cpu: the cpu buffer to wait on
541 * @full: wait until a full page is available, if @cpu != RING_BUFFER_ALL_CPUS
541 * 542 *
542 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon 543 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
543 * as data is added to any of the @buffer's cpu buffers. Otherwise 544 * as data is added to any of the @buffer's cpu buffers. Otherwise
544 * it will wait for data to be added to a specific cpu buffer. 545 * it will wait for data to be added to a specific cpu buffer.
545 */ 546 */
546int ring_buffer_wait(struct ring_buffer *buffer, int cpu) 547int ring_buffer_wait(struct ring_buffer *buffer, int cpu, bool full)
547{ 548{
548 struct ring_buffer_per_cpu *cpu_buffer; 549 struct ring_buffer_per_cpu *uninitialized_var(cpu_buffer);
549 DEFINE_WAIT(wait); 550 DEFINE_WAIT(wait);
550 struct rb_irq_work *work; 551 struct rb_irq_work *work;
552 int ret = 0;
551 553
552 /* 554 /*
553 * Depending on what the caller is waiting for, either any 555 * Depending on what the caller is waiting for, either any
@@ -564,36 +566,61 @@ int ring_buffer_wait(struct ring_buffer *buffer, int cpu)
564 } 566 }
565 567
566 568
567 prepare_to_wait(&work->waiters, &wait, TASK_INTERRUPTIBLE); 569 while (true) {
570 prepare_to_wait(&work->waiters, &wait, TASK_INTERRUPTIBLE);
568 571
569 /* 572 /*
570 * The events can happen in critical sections where 573 * The events can happen in critical sections where
571 * checking a work queue can cause deadlocks. 574 * checking a work queue can cause deadlocks.
572 * After adding a task to the queue, this flag is set 575 * After adding a task to the queue, this flag is set
573 * only to notify events to try to wake up the queue 576 * only to notify events to try to wake up the queue
574 * using irq_work. 577 * using irq_work.
575 * 578 *
576 * We don't clear it even if the buffer is no longer 579 * We don't clear it even if the buffer is no longer
577 * empty. The flag only causes the next event to run 580 * empty. The flag only causes the next event to run
578 * irq_work to do the work queue wake up. The worse 581 * irq_work to do the work queue wake up. The worse
579 * that can happen if we race with !trace_empty() is that 582 * that can happen if we race with !trace_empty() is that
580 * an event will cause an irq_work to try to wake up 583 * an event will cause an irq_work to try to wake up
581 * an empty queue. 584 * an empty queue.
582 * 585 *
583 * There's no reason to protect this flag either, as 586 * There's no reason to protect this flag either, as
584 * the work queue and irq_work logic will do the necessary 587 * the work queue and irq_work logic will do the necessary
585 * synchronization for the wake ups. The only thing 588 * synchronization for the wake ups. The only thing
586 * that is necessary is that the wake up happens after 589 * that is necessary is that the wake up happens after
587 * a task has been queued. It's OK for spurious wake ups. 590 * a task has been queued. It's OK for spurious wake ups.
588 */ 591 */
589 work->waiters_pending = true; 592 work->waiters_pending = true;
593
594 if (signal_pending(current)) {
595 ret = -EINTR;
596 break;
597 }
598
599 if (cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer))
600 break;
601
602 if (cpu != RING_BUFFER_ALL_CPUS &&
603 !ring_buffer_empty_cpu(buffer, cpu)) {
604 unsigned long flags;
605 bool pagebusy;
606
607 if (!full)
608 break;
609
610 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
611 pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page;
612 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
613
614 if (!pagebusy)
615 break;
616 }
590 617
591 if ((cpu == RING_BUFFER_ALL_CPUS && ring_buffer_empty(buffer)) ||
592 (cpu != RING_BUFFER_ALL_CPUS && ring_buffer_empty_cpu(buffer, cpu)))
593 schedule(); 618 schedule();
619 }
594 620
595 finish_wait(&work->waiters, &wait); 621 finish_wait(&work->waiters, &wait);
596 return 0; 622
623 return ret;
597} 624}
598 625
599/** 626/**
@@ -626,8 +653,22 @@ int ring_buffer_poll_wait(struct ring_buffer *buffer, int cpu,
626 work = &cpu_buffer->irq_work; 653 work = &cpu_buffer->irq_work;
627 } 654 }
628 655
629 work->waiters_pending = true;
630 poll_wait(filp, &work->waiters, poll_table); 656 poll_wait(filp, &work->waiters, poll_table);
657 work->waiters_pending = true;
658 /*
659 * There's a tight race between setting the waiters_pending and
660 * checking if the ring buffer is empty. Once the waiters_pending bit
661 * is set, the next event will wake the task up, but we can get stuck
662 * if there's only a single event in.
663 *
664 * FIXME: Ideally, we need a memory barrier on the writer side as well,
665 * but adding a memory barrier to all events will cause too much of a
666 * performance hit in the fast path. We only need a memory barrier when
667 * the buffer goes from empty to having content. But as this race is
668 * extremely small, and it's not a problem if another event comes in, we
669 * will fix it later.
670 */
671 smp_mb();
631 672
632 if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) || 673 if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) ||
633 (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu))) 674 (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu)))
@@ -1968,7 +2009,7 @@ rb_add_time_stamp(struct ring_buffer_event *event, u64 delta)
1968 2009
1969/** 2010/**
1970 * rb_update_event - update event type and data 2011 * rb_update_event - update event type and data
1971 * @event: the even to update 2012 * @event: the event to update
1972 * @type: the type of event 2013 * @type: the type of event
1973 * @length: the size of the event field in the ring buffer 2014 * @length: the size of the event field in the ring buffer
1974 * 2015 *
@@ -3341,21 +3382,16 @@ static void rb_iter_reset(struct ring_buffer_iter *iter)
3341 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; 3382 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3342 3383
3343 /* Iterator usage is expected to have record disabled */ 3384 /* Iterator usage is expected to have record disabled */
3344 if (list_empty(&cpu_buffer->reader_page->list)) { 3385 iter->head_page = cpu_buffer->reader_page;
3345 iter->head_page = rb_set_head_page(cpu_buffer); 3386 iter->head = cpu_buffer->reader_page->read;
3346 if (unlikely(!iter->head_page)) 3387
3347 return; 3388 iter->cache_reader_page = iter->head_page;
3348 iter->head = iter->head_page->read; 3389 iter->cache_read = cpu_buffer->read;
3349 } else { 3390
3350 iter->head_page = cpu_buffer->reader_page;
3351 iter->head = cpu_buffer->reader_page->read;
3352 }
3353 if (iter->head) 3391 if (iter->head)
3354 iter->read_stamp = cpu_buffer->read_stamp; 3392 iter->read_stamp = cpu_buffer->read_stamp;
3355 else 3393 else
3356 iter->read_stamp = iter->head_page->page->time_stamp; 3394 iter->read_stamp = iter->head_page->page->time_stamp;
3357 iter->cache_reader_page = cpu_buffer->reader_page;
3358 iter->cache_read = cpu_buffer->read;
3359} 3395}
3360 3396
3361/** 3397/**
@@ -3748,12 +3784,14 @@ rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3748 return NULL; 3784 return NULL;
3749 3785
3750 /* 3786 /*
3751 * We repeat when a time extend is encountered. 3787 * We repeat when a time extend is encountered or we hit
3752 * Since the time extend is always attached to a data event, 3788 * the end of the page. Since the time extend is always attached
3753 * we should never loop more than once. 3789 * to a data event, we should never loop more than three times.
3754 * (We never hit the following condition more than twice). 3790 * Once for going to next page, once on time extend, and
3791 * finally once to get the event.
3792 * (We never hit the following condition more than thrice).
3755 */ 3793 */
3756 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2)) 3794 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3))
3757 return NULL; 3795 return NULL;
3758 3796
3759 if (rb_per_cpu_empty(cpu_buffer)) 3797 if (rb_per_cpu_empty(cpu_buffer))
diff --git a/kernel/trace/ring_buffer_benchmark.c b/kernel/trace/ring_buffer_benchmark.c
index 0434ff1b808e..3f9e328c30b5 100644
--- a/kernel/trace/ring_buffer_benchmark.c
+++ b/kernel/trace/ring_buffer_benchmark.c
@@ -205,7 +205,6 @@ static void ring_buffer_consumer(void)
205 break; 205 break;
206 206
207 schedule(); 207 schedule();
208 __set_current_state(TASK_RUNNING);
209 } 208 }
210 reader_finish = 0; 209 reader_finish = 0;
211 complete(&read_done); 210 complete(&read_done);
@@ -379,7 +378,6 @@ static int ring_buffer_consumer_thread(void *arg)
379 break; 378 break;
380 379
381 schedule(); 380 schedule();
382 __set_current_state(TASK_RUNNING);
383 } 381 }
384 __set_current_state(TASK_RUNNING); 382 __set_current_state(TASK_RUNNING);
385 383
@@ -407,7 +405,6 @@ static int ring_buffer_producer_thread(void *arg)
407 trace_printk("Sleeping for 10 secs\n"); 405 trace_printk("Sleeping for 10 secs\n");
408 set_current_state(TASK_INTERRUPTIBLE); 406 set_current_state(TASK_INTERRUPTIBLE);
409 schedule_timeout(HZ * SLEEP_TIME); 407 schedule_timeout(HZ * SLEEP_TIME);
410 __set_current_state(TASK_RUNNING);
411 } 408 }
412 409
413 if (kill_test) 410 if (kill_test)
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index f3ef80c8914c..0fa2d2070bd4 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -1076,13 +1076,14 @@ update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
1076} 1076}
1077#endif /* CONFIG_TRACER_MAX_TRACE */ 1077#endif /* CONFIG_TRACER_MAX_TRACE */
1078 1078
1079static int wait_on_pipe(struct trace_iterator *iter) 1079static int wait_on_pipe(struct trace_iterator *iter, bool full)
1080{ 1080{
1081 /* Iterators are static, they should be filled or empty */ 1081 /* Iterators are static, they should be filled or empty */
1082 if (trace_buffer_iter(iter, iter->cpu_file)) 1082 if (trace_buffer_iter(iter, iter->cpu_file))
1083 return 0; 1083 return 0;
1084 1084
1085 return ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file); 1085 return ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file,
1086 full);
1086} 1087}
1087 1088
1088#ifdef CONFIG_FTRACE_STARTUP_TEST 1089#ifdef CONFIG_FTRACE_STARTUP_TEST
@@ -4434,15 +4435,12 @@ static int tracing_wait_pipe(struct file *filp)
4434 4435
4435 mutex_unlock(&iter->mutex); 4436 mutex_unlock(&iter->mutex);
4436 4437
4437 ret = wait_on_pipe(iter); 4438 ret = wait_on_pipe(iter, false);
4438 4439
4439 mutex_lock(&iter->mutex); 4440 mutex_lock(&iter->mutex);
4440 4441
4441 if (ret) 4442 if (ret)
4442 return ret; 4443 return ret;
4443
4444 if (signal_pending(current))
4445 return -EINTR;
4446 } 4444 }
4447 4445
4448 return 1; 4446 return 1;
@@ -5372,16 +5370,12 @@ tracing_buffers_read(struct file *filp, char __user *ubuf,
5372 goto out_unlock; 5370 goto out_unlock;
5373 } 5371 }
5374 mutex_unlock(&trace_types_lock); 5372 mutex_unlock(&trace_types_lock);
5375 ret = wait_on_pipe(iter); 5373 ret = wait_on_pipe(iter, false);
5376 mutex_lock(&trace_types_lock); 5374 mutex_lock(&trace_types_lock);
5377 if (ret) { 5375 if (ret) {
5378 size = ret; 5376 size = ret;
5379 goto out_unlock; 5377 goto out_unlock;
5380 } 5378 }
5381 if (signal_pending(current)) {
5382 size = -EINTR;
5383 goto out_unlock;
5384 }
5385 goto again; 5379 goto again;
5386 } 5380 }
5387 size = 0; 5381 size = 0;
@@ -5500,7 +5494,7 @@ tracing_buffers_splice_read(struct file *file, loff_t *ppos,
5500 }; 5494 };
5501 struct buffer_ref *ref; 5495 struct buffer_ref *ref;
5502 int entries, size, i; 5496 int entries, size, i;
5503 ssize_t ret; 5497 ssize_t ret = 0;
5504 5498
5505 mutex_lock(&trace_types_lock); 5499 mutex_lock(&trace_types_lock);
5506 5500
@@ -5538,13 +5532,16 @@ tracing_buffers_splice_read(struct file *file, loff_t *ppos,
5538 int r; 5532 int r;
5539 5533
5540 ref = kzalloc(sizeof(*ref), GFP_KERNEL); 5534 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
5541 if (!ref) 5535 if (!ref) {
5536 ret = -ENOMEM;
5542 break; 5537 break;
5538 }
5543 5539
5544 ref->ref = 1; 5540 ref->ref = 1;
5545 ref->buffer = iter->trace_buffer->buffer; 5541 ref->buffer = iter->trace_buffer->buffer;
5546 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file); 5542 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
5547 if (!ref->page) { 5543 if (!ref->page) {
5544 ret = -ENOMEM;
5548 kfree(ref); 5545 kfree(ref);
5549 break; 5546 break;
5550 } 5547 }
@@ -5582,19 +5579,19 @@ tracing_buffers_splice_read(struct file *file, loff_t *ppos,
5582 5579
5583 /* did we read anything? */ 5580 /* did we read anything? */
5584 if (!spd.nr_pages) { 5581 if (!spd.nr_pages) {
5582 if (ret)
5583 goto out;
5584
5585 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) { 5585 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
5586 ret = -EAGAIN; 5586 ret = -EAGAIN;
5587 goto out; 5587 goto out;
5588 } 5588 }
5589 mutex_unlock(&trace_types_lock); 5589 mutex_unlock(&trace_types_lock);
5590 ret = wait_on_pipe(iter); 5590 ret = wait_on_pipe(iter, true);
5591 mutex_lock(&trace_types_lock); 5591 mutex_lock(&trace_types_lock);
5592 if (ret) 5592 if (ret)
5593 goto out; 5593 goto out;
5594 if (signal_pending(current)) { 5594
5595 ret = -EINTR;
5596 goto out;
5597 }
5598 goto again; 5595 goto again;
5599 } 5596 }
5600 5597
diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c
index ef06ce7e9cf8..0cc51edde3a8 100644
--- a/kernel/trace/trace_events.c
+++ b/kernel/trace/trace_events.c
@@ -2513,8 +2513,11 @@ static __init int event_test_thread(void *unused)
2513 kfree(test_malloc); 2513 kfree(test_malloc);
2514 2514
2515 set_current_state(TASK_INTERRUPTIBLE); 2515 set_current_state(TASK_INTERRUPTIBLE);
2516 while (!kthread_should_stop()) 2516 while (!kthread_should_stop()) {
2517 schedule(); 2517 schedule();
2518 set_current_state(TASK_INTERRUPTIBLE);
2519 }
2520 __set_current_state(TASK_RUNNING);
2518 2521
2519 return 0; 2522 return 0;
2520} 2523}
diff --git a/kernel/trace/trace_selftest.c b/kernel/trace/trace_selftest.c
index 5ef60499dc8e..b0f86ea77881 100644
--- a/kernel/trace/trace_selftest.c
+++ b/kernel/trace/trace_selftest.c
@@ -382,6 +382,8 @@ static int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
382 382
383 /* check the trace buffer */ 383 /* check the trace buffer */
384 ret = trace_test_buffer(&tr->trace_buffer, &count); 384 ret = trace_test_buffer(&tr->trace_buffer, &count);
385
386 ftrace_enabled = 1;
385 tracing_start(); 387 tracing_start();
386 388
387 /* we should only have one item */ 389 /* we should only have one item */
@@ -679,6 +681,8 @@ trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
679 681
680 /* check the trace buffer */ 682 /* check the trace buffer */
681 ret = trace_test_buffer(&tr->trace_buffer, &count); 683 ret = trace_test_buffer(&tr->trace_buffer, &count);
684
685 ftrace_enabled = 1;
682 trace->reset(tr); 686 trace->reset(tr);
683 tracing_start(); 687 tracing_start();
684 688
@@ -1025,6 +1029,12 @@ trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
1025#endif 1029#endif
1026 1030
1027#ifdef CONFIG_SCHED_TRACER 1031#ifdef CONFIG_SCHED_TRACER
1032
1033struct wakeup_test_data {
1034 struct completion is_ready;
1035 int go;
1036};
1037
1028static int trace_wakeup_test_thread(void *data) 1038static int trace_wakeup_test_thread(void *data)
1029{ 1039{
1030 /* Make this a -deadline thread */ 1040 /* Make this a -deadline thread */
@@ -1034,51 +1044,56 @@ static int trace_wakeup_test_thread(void *data)
1034 .sched_deadline = 10000000ULL, 1044 .sched_deadline = 10000000ULL,
1035 .sched_period = 10000000ULL 1045 .sched_period = 10000000ULL
1036 }; 1046 };
1037 struct completion *x = data; 1047 struct wakeup_test_data *x = data;
1038 1048
1039 sched_setattr(current, &attr); 1049 sched_setattr(current, &attr);
1040 1050
1041 /* Make it know we have a new prio */ 1051 /* Make it know we have a new prio */
1042 complete(x); 1052 complete(&x->is_ready);
1043 1053
1044 /* now go to sleep and let the test wake us up */ 1054 /* now go to sleep and let the test wake us up */
1045 set_current_state(TASK_INTERRUPTIBLE); 1055 set_current_state(TASK_INTERRUPTIBLE);
1046 schedule(); 1056 while (!x->go) {
1057 schedule();
1058 set_current_state(TASK_INTERRUPTIBLE);
1059 }
1047 1060
1048 complete(x); 1061 complete(&x->is_ready);
1062
1063 set_current_state(TASK_INTERRUPTIBLE);
1049 1064
1050 /* we are awake, now wait to disappear */ 1065 /* we are awake, now wait to disappear */
1051 while (!kthread_should_stop()) { 1066 while (!kthread_should_stop()) {
1052 /* 1067 schedule();
1053 * This will likely be the system top priority 1068 set_current_state(TASK_INTERRUPTIBLE);
1054 * task, do short sleeps to let others run.
1055 */
1056 msleep(100);
1057 } 1069 }
1058 1070
1071 __set_current_state(TASK_RUNNING);
1072
1059 return 0; 1073 return 0;
1060} 1074}
1061
1062int 1075int
1063trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr) 1076trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
1064{ 1077{
1065 unsigned long save_max = tr->max_latency; 1078 unsigned long save_max = tr->max_latency;
1066 struct task_struct *p; 1079 struct task_struct *p;
1067 struct completion is_ready; 1080 struct wakeup_test_data data;
1068 unsigned long count; 1081 unsigned long count;
1069 int ret; 1082 int ret;
1070 1083
1071 init_completion(&is_ready); 1084 memset(&data, 0, sizeof(data));
1085
1086 init_completion(&data.is_ready);
1072 1087
1073 /* create a -deadline thread */ 1088 /* create a -deadline thread */
1074 p = kthread_run(trace_wakeup_test_thread, &is_ready, "ftrace-test"); 1089 p = kthread_run(trace_wakeup_test_thread, &data, "ftrace-test");
1075 if (IS_ERR(p)) { 1090 if (IS_ERR(p)) {
1076 printk(KERN_CONT "Failed to create ftrace wakeup test thread "); 1091 printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
1077 return -1; 1092 return -1;
1078 } 1093 }
1079 1094
1080 /* make sure the thread is running at -deadline policy */ 1095 /* make sure the thread is running at -deadline policy */
1081 wait_for_completion(&is_ready); 1096 wait_for_completion(&data.is_ready);
1082 1097
1083 /* start the tracing */ 1098 /* start the tracing */
1084 ret = tracer_init(trace, tr); 1099 ret = tracer_init(trace, tr);
@@ -1099,18 +1114,20 @@ trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
1099 msleep(100); 1114 msleep(100);
1100 } 1115 }
1101 1116
1102 init_completion(&is_ready); 1117 init_completion(&data.is_ready);
1118
1119 data.go = 1;
1120 /* memory barrier is in the wake_up_process() */
1103 1121
1104 wake_up_process(p); 1122 wake_up_process(p);
1105 1123
1106 /* Wait for the task to wake up */ 1124 /* Wait for the task to wake up */
1107 wait_for_completion(&is_ready); 1125 wait_for_completion(&data.is_ready);
1108 1126
1109 /* stop the tracing. */ 1127 /* stop the tracing. */
1110 tracing_stop(); 1128 tracing_stop();
1111 /* check both trace buffers */ 1129 /* check both trace buffers */
1112 ret = trace_test_buffer(&tr->trace_buffer, NULL); 1130 ret = trace_test_buffer(&tr->trace_buffer, NULL);
1113 printk("ret = %d\n", ret);
1114 if (!ret) 1131 if (!ret)
1115 ret = trace_test_buffer(&tr->max_buffer, &count); 1132 ret = trace_test_buffer(&tr->max_buffer, &count);
1116 1133
diff --git a/kernel/trace/trace_stack.c b/kernel/trace/trace_stack.c
index 8a4e5cb66a4c..16eddb308c33 100644
--- a/kernel/trace/trace_stack.c
+++ b/kernel/trace/trace_stack.c
@@ -13,7 +13,6 @@
13#include <linux/sysctl.h> 13#include <linux/sysctl.h>
14#include <linux/init.h> 14#include <linux/init.h>
15#include <linux/fs.h> 15#include <linux/fs.h>
16#include <linux/magic.h>
17 16
18#include <asm/setup.h> 17#include <asm/setup.h>
19 18
@@ -171,8 +170,7 @@ check_stack(unsigned long ip, unsigned long *stack)
171 i++; 170 i++;
172 } 171 }
173 172
174 if ((current != &init_task && 173 if (task_stack_end_corrupted(current)) {
175 *(end_of_stack(current)) != STACK_END_MAGIC)) {
176 print_max_stack(); 174 print_max_stack();
177 BUG(); 175 BUG();
178 } 176 }
diff --git a/kernel/trace/trace_syscalls.c b/kernel/trace/trace_syscalls.c
index 759d5e004517..29228c4d5696 100644
--- a/kernel/trace/trace_syscalls.c
+++ b/kernel/trace/trace_syscalls.c
@@ -313,7 +313,7 @@ static void ftrace_syscall_enter(void *data, struct pt_regs *regs, long id)
313 int size; 313 int size;
314 314
315 syscall_nr = trace_get_syscall_nr(current, regs); 315 syscall_nr = trace_get_syscall_nr(current, regs);
316 if (syscall_nr < 0) 316 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
317 return; 317 return;
318 318
319 /* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE) */ 319 /* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE) */
@@ -360,7 +360,7 @@ static void ftrace_syscall_exit(void *data, struct pt_regs *regs, long ret)
360 int syscall_nr; 360 int syscall_nr;
361 361
362 syscall_nr = trace_get_syscall_nr(current, regs); 362 syscall_nr = trace_get_syscall_nr(current, regs);
363 if (syscall_nr < 0) 363 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
364 return; 364 return;
365 365
366 /* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE()) */ 366 /* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE()) */
@@ -425,7 +425,7 @@ static void unreg_event_syscall_enter(struct ftrace_event_file *file,
425 return; 425 return;
426 mutex_lock(&syscall_trace_lock); 426 mutex_lock(&syscall_trace_lock);
427 tr->sys_refcount_enter--; 427 tr->sys_refcount_enter--;
428 rcu_assign_pointer(tr->enter_syscall_files[num], NULL); 428 RCU_INIT_POINTER(tr->enter_syscall_files[num], NULL);
429 if (!tr->sys_refcount_enter) 429 if (!tr->sys_refcount_enter)
430 unregister_trace_sys_enter(ftrace_syscall_enter, tr); 430 unregister_trace_sys_enter(ftrace_syscall_enter, tr);
431 mutex_unlock(&syscall_trace_lock); 431 mutex_unlock(&syscall_trace_lock);
@@ -463,7 +463,7 @@ static void unreg_event_syscall_exit(struct ftrace_event_file *file,
463 return; 463 return;
464 mutex_lock(&syscall_trace_lock); 464 mutex_lock(&syscall_trace_lock);
465 tr->sys_refcount_exit--; 465 tr->sys_refcount_exit--;
466 rcu_assign_pointer(tr->exit_syscall_files[num], NULL); 466 RCU_INIT_POINTER(tr->exit_syscall_files[num], NULL);
467 if (!tr->sys_refcount_exit) 467 if (!tr->sys_refcount_exit)
468 unregister_trace_sys_exit(ftrace_syscall_exit, tr); 468 unregister_trace_sys_exit(ftrace_syscall_exit, tr);
469 mutex_unlock(&syscall_trace_lock); 469 mutex_unlock(&syscall_trace_lock);
@@ -567,7 +567,7 @@ static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)
567 int size; 567 int size;
568 568
569 syscall_nr = trace_get_syscall_nr(current, regs); 569 syscall_nr = trace_get_syscall_nr(current, regs);
570 if (syscall_nr < 0) 570 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
571 return; 571 return;
572 if (!test_bit(syscall_nr, enabled_perf_enter_syscalls)) 572 if (!test_bit(syscall_nr, enabled_perf_enter_syscalls))
573 return; 573 return;
@@ -641,7 +641,7 @@ static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
641 int size; 641 int size;
642 642
643 syscall_nr = trace_get_syscall_nr(current, regs); 643 syscall_nr = trace_get_syscall_nr(current, regs);
644 if (syscall_nr < 0) 644 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
645 return; 645 return;
646 if (!test_bit(syscall_nr, enabled_perf_exit_syscalls)) 646 if (!test_bit(syscall_nr, enabled_perf_exit_syscalls))
647 return; 647 return;