Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp4765801ioa; Wed, 27 Apr 2022 10:35:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwP9oD3QIg9cQ+SuCctVTV0xZLWo85kaG1aYOPvRTH2UkG0x+DLzqc0zGalh5lNPhDTtg+i X-Received: by 2002:a05:6a00:1307:b0:4b0:b1c:6fd9 with SMTP id j7-20020a056a00130700b004b00b1c6fd9mr30838164pfu.27.1651080936382; Wed, 27 Apr 2022 10:35:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651080936; cv=none; d=google.com; s=arc-20160816; b=opHNHx5LsxRmZVWZZNaXjtoM0zsoynfQe1R/Xl8PCYkJIPrT43zKtxdWj9Ckh78eA3 5Zn+4AeHVTQpP1XWZ31u2sAR5F2gv5oIoQB/HN6VZ1kR4wDDPLMYpLgEYm9rJsQG9o/O +EaBuFyuT9B2m48YhydLqYMMdn9/UjfLogayfhq/8GQYMeU6EHRjJV4QhLBhyYH9XTdU pEf9p9+WN2DbRltRM5Se5jcNkglWHcV2sD0IHJliTTlAtIoBcKPyxol5NjROCXzZNfVW C7thKQphL1mxTSelANnHC3H5E6ATcIwBc00+bvJHSt7a4YjatrebSaXaMnwtcE/PuAgf 0XVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=oLh+ycGxoaHmPKTcUcTA+KBn17ZH9Xl0EK9Vq7S0llg=; b=T0Q7HtxRnpqTCw4f2XNtc4OH/hvH++0sivmKas9x23bmi2lRX/gDwnDw65uKcIOyFA MyZBu0fEb1iOHheOYzfi3eWBeNBG/joICI4LIWpBiaqw73F3N1vh4TBvfUv4t8SXy3qJ SlinoYOvmG2l/UvLDzbBm7C6ferCPtSM8aahQalhVb3XbWS8acGchr/AIWCcOMWBN7k3 21l5mq3T8/sQyrwNxv8sjSL9HvuU6wnrFpk+Hs7f+enBoByGbPlWvJvBmO+ksT+QPiWW XERq0snPNj2fTZe+vnuBM7pj03pOpY1ZeVy2L/gop0rXrj3/3v36TtkyTtrhZZh9wjOZ hzmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=XvLiZvVD; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id j192-20020a638bc9000000b003aae4f1097asi2020198pge.559.2022.04.27.10.35.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 10:35:36 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=XvLiZvVD; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 2037831220; Wed, 27 Apr 2022 10:08:42 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243890AbiD0RLp (ORCPT + 99 others); Wed, 27 Apr 2022 13:11:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243855AbiD0RLb (ORCPT ); Wed, 27 Apr 2022 13:11:31 -0400 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7F4B2F01F for ; Wed, 27 Apr 2022 10:08:18 -0700 (PDT) Received: by mail-ed1-x52e.google.com with SMTP id p4so2743614edx.0 for ; Wed, 27 Apr 2022 10:08:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=oLh+ycGxoaHmPKTcUcTA+KBn17ZH9Xl0EK9Vq7S0llg=; b=XvLiZvVD/w9uq+CjPd24vGOPjfQuHim7dqyaVX2XUnFTxrHi+2X8SL+3rOHyue7vbD DudOOYQ7I0VPZaW3bMJrymheR/rpM6CEx+EIzMhVPQWvGOaGujTs90aftEmV0/T/obC5 cysaGWk2Is1kkC441R4GaM28/aXpBcQnPgez0Okm2QilYMVKUvlD1zPgysgPrAuJspN7 vaoj64J8POP2QQZdjIHEsAARiUArUcSp1J0AROuw9m7Ut3gNz0S45amgamOBuHbblFhO iocvbG4IkTTzOi005i5XjJ+7u07oGWeG6UFed6l1RAn6Iim3q2qdScRKiaYR/79uADrW AAAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=oLh+ycGxoaHmPKTcUcTA+KBn17ZH9Xl0EK9Vq7S0llg=; b=vExb4e9kDu/eLTMgsiYx01WnwbBtSifAzU86Bopib/I4sA0LTSuektxS4CEW0CmrfI HXppgUIU2HKAdxD5SrlUUwPJHm1DP2v1lt2lJcjcBgifTSCK/CsBaaZbtkGV1n+Mjs4t cc9BH3uz4ZPZ8z4QAfFa39V3eYI54Cvo+uF/t68ymGp1FViHaCah6BaQUBXKK5FAcqdk Fu36vu2H/qu6oW4/7Dzr1UxMfFb0Tq9Tkt+pnH8uKx8etN82ixrNctA84AWnIfhkrPEa afIh9GbXgzFVQmimZt1ghRpRbrpuKUW3egwe0EWDQiUmBG3XvVNfHY3mc9+lFadMCi0w dFQA== X-Gm-Message-State: AOAM531tOB/iDKAf0VB6ZFvzL+9OstFPSowKT6kgtXGIJb43XI7agV/O W9ZFz4Ih2gg+Mfl5+i+2MbTcKKyy+THRO14210g= X-Received: by 2002:a05:6402:524d:b0:425:fae3:732d with SMTP id t13-20020a056402524d00b00425fae3732dmr10550228edd.398.1651079297439; Wed, 27 Apr 2022 10:08:17 -0700 (PDT) Received: from localhost.localdomain (i130160.upc-i.chello.nl. [62.195.130.160]) by smtp.googlemail.com with ESMTPSA id z3-20020a50cd03000000b00425d72fd0besm6694359edi.97.2022.04.27.10.08.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 10:08:17 -0700 (PDT) From: Jakob Koschel To: Steven Rostedt Cc: Ingo Molnar , linux-kernel@vger.kernel.org, Jakob Koschel Subject: [PATCH v2 4/4] tracing: Remove check of list iterator against head past the loop body Date: Wed, 27 Apr 2022 19:07:34 +0200 Message-Id: <20220427170734.819891-5-jakobkoschel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220427170734.819891-1-jakobkoschel@gmail.com> References: <20220427170734.819891-1-jakobkoschel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/ Signed-off-by: Jakob Koschel --- kernel/trace/ftrace.c | 20 ++++++++++++-------- kernel/trace/trace_eprobe.c | 14 ++++++++------ kernel/trace/trace_events.c | 12 ++++++------ 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index 4f1d2f5e7263..5c465e70d146 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -4560,8 +4560,8 @@ register_ftrace_function_probe(char *glob, struct trace_array *tr, struct ftrace_probe_ops *probe_ops, void *data) { + struct ftrace_func_probe *probe = NULL, *iter; struct ftrace_func_entry *entry; - struct ftrace_func_probe *probe; struct ftrace_hash **orig_hash; struct ftrace_hash *old_hash; struct ftrace_hash *hash; @@ -4580,11 +4580,13 @@ register_ftrace_function_probe(char *glob, struct trace_array *tr, mutex_lock(&ftrace_lock); /* Check if the probe_ops is already registered */ - list_for_each_entry(probe, &tr->func_probes, list) { - if (probe->probe_ops == probe_ops) + list_for_each_entry(iter, &tr->func_probes, list) { + if (iter->probe_ops == probe_ops) { + probe = iter; break; + } } - if (&probe->list == &tr->func_probes) { + if (!probe) { probe = kzalloc(sizeof(*probe), GFP_KERNEL); if (!probe) { mutex_unlock(&ftrace_lock); @@ -4702,9 +4704,9 @@ int unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr, struct ftrace_probe_ops *probe_ops) { + struct ftrace_func_probe *probe = NULL, *iter; struct ftrace_ops_hash old_hash_ops; struct ftrace_func_entry *entry; - struct ftrace_func_probe *probe; struct ftrace_glob func_g; struct ftrace_hash **orig_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); /* Check if the probe_ops is already registered */ - list_for_each_entry(probe, &tr->func_probes, list) { - if (probe->probe_ops == probe_ops) + list_for_each_entry(iter, &tr->func_probes, list) { + if (iter->probe_ops == probe_ops) { + probe = iter; break; + } } - if (&probe->list == &tr->func_probes) + if (!probe) goto err_unlock_ftrace; ret = -EINVAL; diff --git a/kernel/trace/trace_eprobe.c b/kernel/trace/trace_eprobe.c index 541aa13581b9..63e901a28425 100644 --- a/kernel/trace/trace_eprobe.c +++ b/kernel/trace/trace_eprobe.c @@ -650,7 +650,7 @@ static struct trace_event_functions eprobe_funcs = { static int disable_eprobe(struct trace_eprobe *ep, struct trace_array *tr) { - struct event_trigger_data *trigger; + struct event_trigger_data *trigger = NULL, *iter; struct trace_event_file *file; struct eprobe_data *edata; @@ -658,14 +658,16 @@ static int disable_eprobe(struct trace_eprobe *ep, if (!file) return -ENOENT; - list_for_each_entry(trigger, &file->triggers, list) { - if (!(trigger->flags & EVENT_TRIGGER_FL_PROBE)) + list_for_each_entry(iter, &file->triggers, list) { + if (!(iter->flags & EVENT_TRIGGER_FL_PROBE)) continue; - edata = trigger->private_data; - if (edata->ep == ep) + edata = iter->private_data; + if (edata->ep == ep) { + trigger = iter; break; + } } - if (list_entry_is_head(trigger, &file->triggers, list)) + if (!trigger) return -ENODEV; list_del_rcu(&trigger->list); diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c index e4a442060707..78f313b7b315 100644 --- a/kernel/trace/trace_events.c +++ b/kernel/trace/trace_events.c @@ -2279,8 +2279,8 @@ static struct dentry * event_subsystem_dir(struct trace_array *tr, const char *name, struct trace_event_file *file, struct dentry *parent) { + struct event_subsystem *system, *iter; struct trace_subsystem_dir *dir; - struct event_subsystem *system; struct dentry *entry; /* 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. */ - list_for_each_entry(system, &event_subsystems, list) { - if (strcmp(system->name, name) == 0) + system = NULL; + list_for_each_entry(iter, &event_subsystems, list) { + if (strcmp(iter->name, name) == 0) { + system = iter; break; + } } - /* Reset system variable when not found */ - if (&system->list == &event_subsystems) - system = NULL; dir = kmalloc(sizeof(*dir), GFP_KERNEL); if (!dir) -- 2.25.1