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
6617 .func = ftrace_stub,6617 .func = ftrace_stub,
6618 .flags = FTRACE_OPS_FL_STUB,6618 .flags = FTRACE_OPS_FL_STUB,
6619 };6619 };
6620 struct ftrace_hash *direct_hash;
6620 struct ftrace_hash *orig_hash;6621 struct ftrace_hash *orig_hash;
6621 unsigned long size, i;6622 unsigned long size, i;
6622 int err = -EINVAL;6623 int err = -EINVAL;
@@ -6627,8 +6628,6 @@ int update_ftrace_direct_mod(struct ftrace_ops *ops, struct ftrace_hash *hash, b · 4 unmodified lines
6627 return -EINVAL;6628 return -EINVAL;
6628 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))6629 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
6629 return -EINVAL;6630 return -EINVAL;
6630 if (direct_functions == EMPTY_HASH)
6631 return -EINVAL;
6632 6631
6633 /*6632 /*
6634 * We can be called from within ops_func callback with direct_mutex6633 * 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
6636 */6635 */
6637 if (do_direct_lock)6636 if (do_direct_lock)
6638 mutex_lock(&direct_mutex);6637 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;
6639 6644
6640 orig_hash = ops->func_hash ? ops->func_hash->filter_hash : NULL;6645 orig_hash = ops->func_hash ? ops->func_hash->filter_hash : NULL;
6641 if (!orig_hash)6646 if (!orig_hash)
@@ -6667,7 +6672,7 @@ int update_ftrace_direct_mod(struct ftrace_ops *ops, struct ftrace_hash *hash, b · 25 unmodified lines
6667 size = 1 << hash->size_bits;6672 size = 1 << hash->size_bits;
6668 for (i = 0; i < size; i++) {6673 for (i = 0; i < size; i++) {
6669 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {6674 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);
6671 if (!tmp)6676 if (!tmp)
6672 continue;6677 continue;
6673 tmp->direct = entry->direct;6678 tmp->direct = entry->direct;