Commit ba27d855 authored by Jakob Koschel's avatar Jakob Koschel Committed by Steven Rostedt (Google)
Browse files

tracing: Remove check of list iterator against head past the loop body

When list_for_each_entry() completes the iteration over the whole list
without breaking the loop, the iterator value will be a bogus pointer
computed based on the head element.

While it is safe to use the pointer to determine if it was computed
based on the head element, either with list_entry_is_head() or
&pos->member == head, using the iterator variable after the loop should
be avoided.

In preparation to limit the scope of a list iterator to the list
traversal loop, use a dedicated pointer to point to the found element [1].

Link: https://lkml.kernel.org/r/20220427170734.819891-5-jakobkoschel@gmail.com

Cc: Ingo Molnar <mingo@redhat.com>
Link: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/


Signed-off-by: default avatarJakob Koschel <jakobkoschel@gmail.com>
Signed-off-by: default avatarSteven Rostedt (Google) <rostedt@goodmis.org>
parent 45e333ce
Loading
Loading
Loading
Loading
+12 −8
Original line number Original line Diff line number Diff line
@@ -4560,8 +4560,8 @@ register_ftrace_function_probe(char *glob, struct trace_array *tr,
			       struct ftrace_probe_ops *probe_ops,
			       struct ftrace_probe_ops *probe_ops,
			       void *data)
			       void *data)
{
{
	struct ftrace_func_probe *probe = NULL, *iter;
	struct ftrace_func_entry *entry;
	struct ftrace_func_entry *entry;
	struct ftrace_func_probe *probe;
	struct ftrace_hash **orig_hash;
	struct ftrace_hash **orig_hash;
	struct ftrace_hash *old_hash;
	struct ftrace_hash *old_hash;
	struct ftrace_hash *hash;
	struct ftrace_hash *hash;
@@ -4580,11 +4580,13 @@ register_ftrace_function_probe(char *glob, struct trace_array *tr,


	mutex_lock(&ftrace_lock);
	mutex_lock(&ftrace_lock);
	/* Check if the probe_ops is already registered */
	/* Check if the probe_ops is already registered */
	list_for_each_entry(probe, &tr->func_probes, list) {
	list_for_each_entry(iter, &tr->func_probes, list) {
		if (probe->probe_ops == probe_ops)
		if (iter->probe_ops == probe_ops) {
			probe = iter;
			break;
			break;
		}
		}
	if (&probe->list == &tr->func_probes) {
	}
	if (!probe) {
		probe = kzalloc(sizeof(*probe), GFP_KERNEL);
		probe = kzalloc(sizeof(*probe), GFP_KERNEL);
		if (!probe) {
		if (!probe) {
			mutex_unlock(&ftrace_lock);
			mutex_unlock(&ftrace_lock);
@@ -4702,9 +4704,9 @@ int
unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
				      struct ftrace_probe_ops *probe_ops)
				      struct ftrace_probe_ops *probe_ops)
{
{
	struct ftrace_func_probe *probe = NULL, *iter;
	struct ftrace_ops_hash old_hash_ops;
	struct ftrace_ops_hash old_hash_ops;
	struct ftrace_func_entry *entry;
	struct ftrace_func_entry *entry;
	struct ftrace_func_probe *probe;
	struct ftrace_glob func_g;
	struct ftrace_glob func_g;
	struct ftrace_hash **orig_hash;
	struct ftrace_hash **orig_hash;
	struct ftrace_hash *old_hash;
	struct ftrace_hash *old_hash;
@@ -4732,11 +4734,13 @@ unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,


	mutex_lock(&ftrace_lock);
	mutex_lock(&ftrace_lock);
	/* Check if the probe_ops is already registered */
	/* Check if the probe_ops is already registered */
	list_for_each_entry(probe, &tr->func_probes, list) {
	list_for_each_entry(iter, &tr->func_probes, list) {
		if (probe->probe_ops == probe_ops)
		if (iter->probe_ops == probe_ops) {
			probe = iter;
			break;
			break;
		}
		}
	if (&probe->list == &tr->func_probes)
	}
	if (!probe)
		goto err_unlock_ftrace;
		goto err_unlock_ftrace;


	ret = -EINVAL;
	ret = -EINVAL;
+8 −6
Original line number Original line Diff line number Diff line
@@ -648,7 +648,7 @@ static struct trace_event_functions eprobe_funcs = {
static int disable_eprobe(struct trace_eprobe *ep,
static int disable_eprobe(struct trace_eprobe *ep,
			  struct trace_array *tr)
			  struct trace_array *tr)
{
{
	struct event_trigger_data *trigger;
	struct event_trigger_data *trigger = NULL, *iter;
	struct trace_event_file *file;
	struct trace_event_file *file;
	struct eprobe_data *edata;
	struct eprobe_data *edata;


@@ -656,14 +656,16 @@ static int disable_eprobe(struct trace_eprobe *ep,
	if (!file)
	if (!file)
		return -ENOENT;
		return -ENOENT;


	list_for_each_entry(trigger, &file->triggers, list) {
	list_for_each_entry(iter, &file->triggers, list) {
		if (!(trigger->flags & EVENT_TRIGGER_FL_PROBE))
		if (!(iter->flags & EVENT_TRIGGER_FL_PROBE))
			continue;
			continue;
		edata = trigger->private_data;
		edata = iter->private_data;
		if (edata->ep == ep)
		if (edata->ep == ep) {
			trigger = iter;
			break;
			break;
		}
		}
	if (list_entry_is_head(trigger, &file->triggers, list))
	}
	if (!trigger)
		return -ENODEV;
		return -ENODEV;


	list_del_rcu(&trigger->list);
	list_del_rcu(&trigger->list);
+6 −6
Original line number Original line Diff line number Diff line
@@ -2279,8 +2279,8 @@ static struct dentry *
event_subsystem_dir(struct trace_array *tr, const char *name,
event_subsystem_dir(struct trace_array *tr, const char *name,
		    struct trace_event_file *file, struct dentry *parent)
		    struct trace_event_file *file, struct dentry *parent)
{
{
	struct event_subsystem *system, *iter;
	struct trace_subsystem_dir *dir;
	struct trace_subsystem_dir *dir;
	struct event_subsystem *system;
	struct dentry *entry;
	struct dentry *entry;


	/* First see if we did not already create this dir */
	/* First see if we did not already create this dir */
@@ -2294,13 +2294,13 @@ event_subsystem_dir(struct trace_array *tr, const char *name,
	}
	}


	/* Now see if the system itself exists. */
	/* Now see if the system itself exists. */
	list_for_each_entry(system, &event_subsystems, list) {
	system = NULL;
		if (strcmp(system->name, name) == 0)
	list_for_each_entry(iter, &event_subsystems, list) {
		if (strcmp(iter->name, name) == 0) {
			system = iter;
			break;
			break;
		}
		}
	/* Reset system variable when not found */
	}
	if (&system->list == &event_subsystems)
		system = NULL;


	dir = kmalloc(sizeof(*dir), GFP_KERNEL);
	dir = kmalloc(sizeof(*dir), GFP_KERNEL);
	if (!dir)
	if (!dir)