History

ftrace: Protect direct_functions in update_ftrace_direct_mod

Fix accessing the __rcu pointer direct_functions with RCU protection.

Leon Hwang committed 3 months ago

Commit 092f8ec1 parent f26e5fa1 file · +8 −3Browse files

Unified · Split

kernel/trace/ftrace.c+8−3
@@ -6617,6 +6617,7 @@ int update_ftrace_direct_mod(struct ftrace_ops *ops, struct ftrace_hash *hash, b · 6616 unmodified lines
66176617 .func = ftrace_stub,
66186618 .flags = FTRACE_OPS_FL_STUB,
66196619 };
6620+ struct ftrace_hash *direct_hash;
66206621 struct ftrace_hash *orig_hash;
66216622 unsigned long size, i;
66226623 int err = -EINVAL;
@@ -6627,8 +6628,6 @@ int update_ftrace_direct_mod(struct ftrace_ops *ops, struct ftrace_hash *hash, b · 4 unmodified lines
66276628 return -EINVAL;
66286629 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
66296630 return -EINVAL;
6630− if (direct_functions == EMPTY_HASH)
6631− return -EINVAL;
66326631
66336632 /*
66346633 * We can be called from within ops_func callback with direct_mutex
@@ -6636,6 +6635,12 @@ int update_ftrace_direct_mod(struct ftrace_ops *ops, struct ftrace_hash *hash, b · 1 unmodified line
66366635 */
66376636 if (do_direct_lock)
66386637 mutex_lock(&direct_mutex);
6638+ else
6639+ lockdep_assert_held_once(&direct_mutex);
6640+
6641+ direct_hash = rcu_dereference_protected(direct_functions, lockdep_is_held(&direct_mutex));
6642+ if (direct_hash == EMPTY_HASH)
6643+ goto unlock;
66396644
66406645 orig_hash = ops->func_hash ? ops->func_hash->filter_hash : NULL;
66416646 if (!orig_hash)
@@ -6667,7 +6672,7 @@ int update_ftrace_direct_mod(struct ftrace_ops *ops, struct ftrace_hash *hash, b · 25 unmodified lines
66676672 size = 1 << hash->size_bits;
66686673 for (i = 0; i < size; i++) {
66696674 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
6670− tmp = __ftrace_lookup_ip(direct_functions, entry->ip);
6675+ tmp = __ftrace_lookup_ip(direct_hash, entry->ip);
66716676 if (!tmp)
66726677 continue;
66736678 tmp->direct = entry->direct;