History

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

Jakob Koschel committed 4 years ago

Commit ba27d851 parent 45e333c3 files · +26 −20Browse files

Unified · Split

3 files
  1. kernel/trace/ftrace.c +12 −8
  2. kernel/trace/trace_eprobe.c +8 −6
  3. kernel/trace/trace_events.c +6 −6
kernel/trace/ftrace.c+12−8
@@ -4560,8 +4560,8 @@ register_ftrace_function_probe(char *glob, struct trace_array *tr, · 4559 unmodified lines
45604560 struct ftrace_probe_ops *probe_ops,
45614561 void *data)
45624562 {
4563+ struct ftrace_func_probe *probe = NULL, *iter;
45634564 struct ftrace_func_entry *entry;
4564− struct ftrace_func_probe *probe;
45654565 struct ftrace_hash **orig_hash;
45664566 struct ftrace_hash *old_hash;
45674567 struct ftrace_hash *hash;
@@ -4580,11 +4580,13 @@ register_ftrace_function_probe(char *glob, struct trace_array *tr, · 12 unmodified lines
45804580
45814581 mutex_lock(&ftrace_lock);
45824582 /* Check if the probe_ops is already registered */
4583− list_for_each_entry(probe, &tr->func_probes, list) {
4584− if (probe->probe_ops == probe_ops)
4583+ list_for_each_entry(iter, &tr->func_probes, list) {
4584+ if (iter->probe_ops == probe_ops) {
4585+ probe = iter;
45854586 break;
4587+ }
45864588 }
4587− if (&probe->list == &tr->func_probes) {
4589+ if (!probe) {
45884590 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
45894591 if (!probe) {
45904592 mutex_unlock(&ftrace_lock);
@@ -4702,9 +4704,9 @@ int · 111 unmodified lines
47024704 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
47034705 struct ftrace_probe_ops *probe_ops)
47044706 {
4707+ struct ftrace_func_probe *probe = NULL, *iter;
47054708 struct ftrace_ops_hash old_hash_ops;
47064709 struct ftrace_func_entry *entry;
4707− struct ftrace_func_probe *probe;
47084710 struct ftrace_glob func_g;
47094711 struct ftrace_hash **orig_hash;
47104712 struct ftrace_hash *old_hash;
@@ -4732,11 +4734,13 @@ unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr, · 21 unmodified lines
47324734
47334735 mutex_lock(&ftrace_lock);
47344736 /* Check if the probe_ops is already registered */
4735− list_for_each_entry(probe, &tr->func_probes, list) {
4736− if (probe->probe_ops == probe_ops)
4737+ list_for_each_entry(iter, &tr->func_probes, list) {
4738+ if (iter->probe_ops == probe_ops) {
4739+ probe = iter;
47374740 break;
4741+ }
47384742 }
4739− if (&probe->list == &tr->func_probes)
4743+ if (!probe)
47404744 goto err_unlock_ftrace;
47414745
47424746 ret = -EINVAL;
kernel/trace/trace_eprobe.c+8−6
@@ -648,7 +648,7 @@ static struct trace_event_functions eprobe_funcs = { · 647 unmodified lines
648648 static int disable_eprobe(struct trace_eprobe *ep,
649649 struct trace_array *tr)
650650 {
651− struct event_trigger_data *trigger;
651+ struct event_trigger_data *trigger = NULL, *iter;
652652 struct trace_event_file *file;
653653 struct eprobe_data *edata;
654654
@@ -656,14 +656,16 @@ static int disable_eprobe(struct trace_eprobe *ep, · 1 unmodified line
656656 if (!file)
657657 return -ENOENT;
658658
659− list_for_each_entry(trigger, &file->triggers, list) {
660− if (!(trigger->flags & EVENT_TRIGGER_FL_PROBE))
659+ list_for_each_entry(iter, &file->triggers, list) {
660+ if (!(iter->flags & EVENT_TRIGGER_FL_PROBE))
661661 continue;
662− edata = trigger->private_data;
663− if (edata->ep == ep)
662+ edata = iter->private_data;
663+ if (edata->ep == ep) {
664+ trigger = iter;
664665 break;
666+ }
665667 }
666− if (list_entry_is_head(trigger, &file->triggers, list))
668+ if (!trigger)
667669 return -ENODEV;
668670
669671 list_del_rcu(&trigger->list);
kernel/trace/trace_events.c+6−6
@@ -2279,8 +2279,8 @@ static struct dentry * · 2278 unmodified lines
22792279 event_subsystem_dir(struct trace_array *tr, const char *name,
22802280 struct trace_event_file *file, struct dentry *parent)
22812281 {
2282+ struct event_subsystem *system, *iter;
22822283 struct trace_subsystem_dir *dir;
2283− struct event_subsystem *system;
22842284 struct dentry *entry;
22852285
22862286 /* First see if we did not already create this dir */
@@ -2294,13 +2294,13 @@ event_subsystem_dir(struct trace_array *tr, const char *name, · 7 unmodified lines
22942294 }
22952295
22962296 /* Now see if the system itself exists. */
2297− list_for_each_entry(system, &event_subsystems, list) {
2298− if (strcmp(system->name, name) == 0)
2297+ system = NULL;
2298+ list_for_each_entry(iter, &event_subsystems, list) {
2299+ if (strcmp(iter->name, name) == 0) {
2300+ system = iter;
22992301 break;
2302+ }
23002303 }
2301− /* Reset system variable when not found */
2302− if (&system->list == &event_subsystems)
2303− system = NULL;
23042304
23052305 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
23062306 if (!dir)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t