From nobody Sun May 10 17:53:29 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6229FC433FE for ; Wed, 27 Apr 2022 17:08:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243859AbiD0RLd (ORCPT ); Wed, 27 Apr 2022 13:11:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237739AbiD0RL3 (ORCPT ); Wed, 27 Apr 2022 13:11:29 -0400 Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 04B742F025 for ; Wed, 27 Apr 2022 10:08:17 -0700 (PDT) Received: by mail-ed1-x52f.google.com with SMTP id e23so2684566eda.11 for ; Wed, 27 Apr 2022 10:08:16 -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=LastlIlp3ExuBmWFIzRxFtrilFT5BPcN0d/npk5blEA=; b=XGeqeNiNAy40QSZ1b7uWemHlUQTomh9n3vBdeswdMk7F8xK4atJkgASs1nBCeHPNUw fj8iTanx9ugZj3bYgk9vhDkdFmimSA+Ox7ft2lXxcRs+ZGc6c0mtGiVK+h5ScWc4FIY5 dcamIDhMHE9pUJuSp7BpDQb5m60O/BlhLQmfTbkelRK5zVEBRPCjm+Wq5dTt5OvXVPqz rU377ZbRobZ4ExSpPpmcGzgcgUopsAyKmjb008X3fCmsVe6wcZxGAWpGezC2timZ9pYk HV4vab1JohblVN5dOIrPkK27Ke2oaCN1EGfDPuqjX4vXYZ+obHM/vIWrub8sDA6UzfVr 3EmA== 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=LastlIlp3ExuBmWFIzRxFtrilFT5BPcN0d/npk5blEA=; b=A8wPJbMUWuzQl07Wqs+1bYFnP2GJ7syrJO/v1nwpWEUuDzU3cdYNPwGz0HmZ/H0WB3 wlisU83pCUt4NaHYMziJdNzefwAl4dBZgGgRkatKNkm8/kolRw4Daq3Fu/REkWSpSGBt gAt/oh7PdO3Gy1SRiOIkwNFC2/4mo2ybSGOlokJEgfhSCynx6te+U7+O9Z/xNIF5Uwo0 K/+WO96VFqhzm5QsAPyGOgFnAGW+/H/cErepgh5+eCXCsCEqKxRZLEp3tZydtrQn2mGm E05VgiTvN6oPHJzo+uQKn0VvkFF+9PS3IzqoMPAtnaj6xRX+3Cps9MH8czavJfszsyEc viFg== X-Gm-Message-State: AOAM530mw6wX6KHUNeWZkMx/aznyxwipIm4LisN3fRrQVHZpSqteIUGh KWtC7JpicDWmjlMwLtYjWrM= X-Google-Smtp-Source: ABdhPJzbDvXSVt1I6isfFiI2lRhSnT2RNiPt1AIt3VBoM7ZshYzYxkvkz9hQYZKyHEWVwRqOHI6sbQ== X-Received: by 2002:a05:6402:1912:b0:423:7fe8:6b2c with SMTP id e18-20020a056402191200b004237fe86b2cmr30777467edz.253.1651079295541; Wed, 27 Apr 2022 10:08:15 -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.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 10:08:15 -0700 (PDT) From: Jakob Koschel To: Steven Rostedt Cc: Ingo Molnar , linux-kernel@vger.kernel.org, Jakob Koschel Subject: [PATCH v2 1/4] tracing: Remove usage of list iterator after the loop body Date: Wed, 27 Apr 2022 19:07:31 +0200 Message-Id: <20220427170734.819891-2-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" In preparation to limit the scope of the list iterator variable to the traversal loop, use a dedicated pointer to point to the found element [1]. Before, the code implicitly used the head when no element was found when using &pos->list. Since the new variable is only set if an element was found, the head needs to be used explicitly if the variable is NULL. Link: https://lore.kernel.org/all/CAHk-=3DwgRr_D8CB-D9Kg-c=3DEHreAsk5SqXPwr= 9Y7k9sA6cWXJ6w@mail.gmail.com/ [1] Signed-off-by: Jakob Koschel --- kernel/trace/trace_output.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/kernel/trace/trace_output.c b/kernel/trace/trace_output.c index 8aa493d25c73..733a4d6c20e2 100644 --- a/kernel/trace/trace_output.c +++ b/kernel/trace/trace_output.c @@ -692,7 +692,7 @@ static LIST_HEAD(ftrace_event_list); =20 static int trace_search_list(struct list_head **list) { - struct trace_event *e; + struct trace_event *e =3D NULL, *iter; int next =3D __TRACE_LAST_TYPE; =20 if (list_empty(&ftrace_event_list)) { @@ -704,9 +704,11 @@ static int trace_search_list(struct list_head **list) * We used up all possible max events, * lets see if somebody freed one. */ - list_for_each_entry(e, &ftrace_event_list, list) { - if (e->type !=3D next) + list_for_each_entry(iter, &ftrace_event_list, list) { + if (iter->type !=3D next) { + e =3D iter; break; + } next++; } =20 @@ -714,7 +716,10 @@ static int trace_search_list(struct list_head **list) if (next > TRACE_EVENT_TYPE_MAX) return 0; =20 - *list =3D &e->list; + if (e) + *list =3D &e->list; + else + *list =3D &ftrace_event_list; return next; } =20 --=20 2.25.1 From nobody Sun May 10 17:53:29 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 580FBC433F5 for ; Wed, 27 Apr 2022 17:08:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243883AbiD0RLk (ORCPT ); Wed, 27 Apr 2022 13:11:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243845AbiD0RLa (ORCPT ); Wed, 27 Apr 2022 13:11:30 -0400 Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A66C52E9FC for ; Wed, 27 Apr 2022 10:08:17 -0700 (PDT) Received: by mail-ed1-x52b.google.com with SMTP id k27so2698664edk.4 for ; Wed, 27 Apr 2022 10:08:17 -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=ZkmevGa0S+kHSd+kuDI459P4jPVZOZ4GNeicenq/YJM=; b=jYLpSKtFJ2fPli+qfGHE6JgCsxkugVA1nfeVHdyGex7DKh4H4AH6O/YEc1bN54OjlP Sri028H4WkleHLuiPln/N58jfYJrpt6T4Q3pH5917RVUrZmBu5pLMdi2k6Md8L8tkQKX gHi0ostP7xCfbOCrUjgIghwulOLRzxiXqXZPan0WHLCY/qY3aXIk949Q6dkeKzQ5dxdK 7mR4kFEOsDYhj9Bf7fMMYQWNCnITj52dvvbLa/l6pUhzN6JkNvznqtSpeHv8ONhOJoaK V53eFpsgAcM1uTLSZCVes5G7OMvGEobipLSYhF53y0cuFTYo/bINL/qXkvQRHtdzdUqc v0pg== 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=ZkmevGa0S+kHSd+kuDI459P4jPVZOZ4GNeicenq/YJM=; b=x9L7KOxLkNwPw+j8elyxMZeq46QR1+OH6JzlXGSLDe8n3WR5J3gW7HAqqumPCb01YR m1tN3D0yYHS4gvt/U3zUrvlM3d5yn/lmsY7qwTOTPqVRr+X5HkGKwGsqKJHkEocL3ozx BjBRKkIYasFPyH78XS7ayclcVomR0xbD/RX3qRA69tuMng2uWHS5PELi7+QGAk7sUFf3 dDsYXdYAkOlSheZSYAWIuJDHB42zOUgWdYSj2OC3HCDxaG+mScpmVqt+3qDsjIxuZ3wb zCGLkMcV0r4PNHYMbrUtmEcDsaHwsG9zWwYatgb3ilqOuduNfByArT6EuZEi+T49c+52 4kEQ== X-Gm-Message-State: AOAM532KK8nhlui1rhdwu+eNlj7MemRvmZh5caQENKQirPYluwTYAIoG fo8ZbVTeWTNMmae8EAk/x9g= X-Google-Smtp-Source: ABdhPJxT7SgdBj/cr4MJnQt9yVWBjg9i0492QrjVRkTMxY4GFZ7Edo82tCioSiLPo/K3nL60SJyl/w== X-Received: by 2002:a05:6402:51d0:b0:426:110e:5d05 with SMTP id r16-20020a05640251d000b00426110e5d05mr6113858edd.384.1651079296239; Wed, 27 Apr 2022 10:08:16 -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.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 10:08:15 -0700 (PDT) From: Jakob Koschel To: Steven Rostedt Cc: Ingo Molnar , linux-kernel@vger.kernel.org, Jakob Koschel Subject: [PATCH v2 2/4] tracing: Remove usage of list iterator variable after the loop Date: Wed, 27 Apr 2022 19:07:32 +0200 Message-Id: <20220427170734.819891-3-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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-=3DwgRr_D8CB-D9Kg-c=3DEHreAsk5SqXPwr= 9Y7k9sA6cWXJ6w@mail.gmail.com/ [1] Signed-off-by: Jakob Koschel --- kernel/trace/trace_events.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c index e11e167b7809..e4a442060707 100644 --- a/kernel/trace/trace_events.c +++ b/kernel/trace/trace_events.c @@ -1723,9 +1723,9 @@ static LIST_HEAD(event_subsystems); =20 static int subsystem_open(struct inode *inode, struct file *filp) { + struct trace_subsystem_dir *dir =3D NULL, *iter_dir; + struct trace_array *tr =3D NULL, *iter_tr; struct event_subsystem *system =3D NULL; - struct trace_subsystem_dir *dir =3D NULL; /* Initialize for gcc */ - struct trace_array *tr; int ret; =20 if (tracing_is_disabled()) @@ -1734,10 +1734,12 @@ static int subsystem_open(struct inode *inode, stru= ct file *filp) /* Make sure the system still exists */ mutex_lock(&event_mutex); mutex_lock(&trace_types_lock); - list_for_each_entry(tr, &ftrace_trace_arrays, list) { - list_for_each_entry(dir, &tr->systems, list) { - if (dir =3D=3D inode->i_private) { + list_for_each_entry(iter_tr, &ftrace_trace_arrays, list) { + list_for_each_entry(iter_dir, &iter_tr->systems, list) { + if (iter_dir =3D=3D inode->i_private) { /* Don't open systems with no events */ + tr =3D iter_tr; + dir =3D iter_dir; if (dir->nr_events) { __get_system_dir(dir); system =3D dir->subsystem; @@ -1753,9 +1755,6 @@ static int subsystem_open(struct inode *inode, struct= file *filp) if (!system) return -ENODEV; =20 - /* Some versions of gcc think dir can be uninitialized here */ - WARN_ON(!dir); - /* Still need to increment the ref count of the system */ if (trace_array_get(tr) < 0) { put_system(dir); --=20 2.25.1 From nobody Sun May 10 17:53:29 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3C9AEC433F5 for ; Wed, 27 Apr 2022 17:08:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243868AbiD0RLg (ORCPT ); Wed, 27 Apr 2022 13:11:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50056 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243847AbiD0RLa (ORCPT ); Wed, 27 Apr 2022 13:11:30 -0400 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D1652E9D7 for ; Wed, 27 Apr 2022 10:08:18 -0700 (PDT) Received: by mail-ed1-x532.google.com with SMTP id y21so2713375edo.2 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=BMvhtRn0jl+CJy7uX7FCi4p3mZryFp+0msE3tzv/YXk=; b=DdPhJtlYCHojqdyHfYy30NEwVP1mhNaC0GXK23ikMndM2xBHa/co6CIJZCRQswiWtT H3Bbqufp8Zo4Exbt9y0HwLObDkmv8hz0ZQ38mSsVi3nn2/6oh1YuxpkSatzA0UgTbugM 62EwlbaxtsfrfzSMIGPYTSmPronB3QJaBvrxPzRWJxLSdg+mb4qAmmSX15i4xqMTIXQz I3PW8gdT4FHapom34rjciX/B2nrRigpNwCqWPyJcm3AmCOF6Ibi/XNm7p9OVa7h3m1q1 G8b5DYjLj2dvZNcQQVYfa/JYQEcRY9ajU63tfT68EfKjZaiPXU5vBT8fwiAeUXcQFL8F WbJw== 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=BMvhtRn0jl+CJy7uX7FCi4p3mZryFp+0msE3tzv/YXk=; b=zNE3myo6NnJot1gDZs4yjgXT7F3IkMD7KderqTyHnWkQ9dRbzbKbYqQF/zxOu0FwhV ED/LjpBsRzTiXCZBEUlW1axpvPhxTp9p6flabCit4T+kKhpn0Unv2AEpqGP3S94XbNvC POhY/uudbwwiWIq8Cdqx7GM0xEKUTsLnA6JeCSRsmsnaVfusAaHwmxBLZe7zRgfdGJtV QNnvF6KREj2somM2JYkvZu56zXOof/fC8HBLhyMnMhgmRCpYbecUjF1wkVkZGhUyJbZL PYc7jkpWjCTDyyhWYZrOakMX5pXvXQJDcQDmhLmMwFP69h0/EUzgmyYwJuRESfbz5B8k tLEg== X-Gm-Message-State: AOAM533WDnmh1JYZWIHvKqmV+2PJaBdl5ETa0U8FjNAFTMLlwEYu+1pf MlUWw/DH3yBg412Bfe+EoRI= X-Google-Smtp-Source: ABdhPJxAQO0Ice/dluI4Pq3+rmr34sByp3FRLuEvhuX6HtWwor4XJmOL7ooIRyv/Zpulcdts3l5XzQ== X-Received: by 2002:a05:6402:3587:b0:425:ffd2:8aa0 with SMTP id y7-20020a056402358700b00425ffd28aa0mr8746492edc.403.1651079296838; Wed, 27 Apr 2022 10:08:16 -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:16 -0700 (PDT) From: Jakob Koschel To: Steven Rostedt Cc: Ingo Molnar , linux-kernel@vger.kernel.org, Jakob Koschel Subject: [PATCH v2 3/4] tracing: Replace usage of found with dedicated list iterator variable Date: Wed, 27 Apr 2022 19:07:33 +0200 Message-Id: <20220427170734.819891-4-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" To move the list iterator variable into the list_for_each_entry_*() macro in the future it should be avoided to use the list iterator variable after the loop body. To *never* use the list iterator variable after the loop it was concluded to use a separate iterator variable instead of a found boolean [1]. This removes the need to use a found variable and simply checking if the variable was set, can determine if the break/goto was hit. Link: https://lore.kernel.org/all/CAHk-=3DwgRr_D8CB-D9Kg-c=3DEHreAsk5SqXPwr= 9Y7k9sA6cWXJ6w@mail.gmail.com/ [1] Signed-off-by: Jakob Koschel --- kernel/trace/trace_events_hist.c | 15 +++++++-------- kernel/trace/trace_events_trigger.c | 24 +++++++++++------------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_h= ist.c index 44db5ba9cabb..65f79ac78742 100644 --- a/kernel/trace/trace_events_hist.c +++ b/kernel/trace/trace_events_hist.c @@ -6089,20 +6089,19 @@ static void hist_unregister_trigger(char *glob, struct event_trigger_data *data, struct trace_event_file *file) { + struct event_trigger_data *test =3D NULL, *iter, *named_data =3D NULL; struct hist_trigger_data *hist_data =3D data->private_data; - struct event_trigger_data *test, *named_data =3D NULL; - bool unregistered =3D false; =20 lockdep_assert_held(&event_mutex); =20 if (hist_data->attrs->name) named_data =3D find_named_trigger(hist_data->attrs->name); =20 - list_for_each_entry(test, &file->triggers, list) { - if (test->cmd_ops->trigger_type =3D=3D ETT_EVENT_HIST) { - if (!hist_trigger_match(data, test, named_data, false)) + list_for_each_entry(iter, &file->triggers, list) { + if (iter->cmd_ops->trigger_type =3D=3D ETT_EVENT_HIST) { + if (!hist_trigger_match(data, iter, named_data, false)) continue; - unregistered =3D true; + test =3D iter; list_del_rcu(&test->list); trace_event_trigger_enable_disable(file, 0); update_cond_flag(file); @@ -6110,11 +6109,11 @@ static void hist_unregister_trigger(char *glob, } } =20 - if (unregistered && test->ops->free) + if (test && test->ops->free) test->ops->free(test->ops, test); =20 if (hist_data->enable_timestamps) { - if (!hist_data->remove || unregistered) + if (!hist_data->remove || test) tracing_set_filter_buffering(file->tr, false); } } diff --git a/kernel/trace/trace_events_trigger.c b/kernel/trace/trace_event= s_trigger.c index 7eb9d04f1c2e..349195c93da1 100644 --- a/kernel/trace/trace_events_trigger.c +++ b/kernel/trace/trace_events_trigger.c @@ -614,14 +614,13 @@ static void unregister_trigger(char *glob, struct event_trigger_data *test, struct trace_event_file *file) { - struct event_trigger_data *data; - bool unregistered =3D false; + struct event_trigger_data *data =3D NULL, *iter; =20 lockdep_assert_held(&event_mutex); =20 - list_for_each_entry(data, &file->triggers, list) { - if (data->cmd_ops->trigger_type =3D=3D test->cmd_ops->trigger_type) { - unregistered =3D true; + list_for_each_entry(iter, &file->triggers, list) { + if (iter->cmd_ops->trigger_type =3D=3D test->cmd_ops->trigger_type) { + data =3D iter; list_del_rcu(&data->list); trace_event_trigger_enable_disable(file, 0); update_cond_flag(file); @@ -629,7 +628,7 @@ static void unregister_trigger(char *glob, } } =20 - if (unregistered && data->ops->free) + if (data && data->ops->free) data->ops->free(data->ops, data); } =20 @@ -1976,19 +1975,18 @@ void event_enable_unregister_trigger(char *glob, struct trace_event_file *file) { struct enable_trigger_data *test_enable_data =3D test->private_data; + struct event_trigger_data *data =3D NULL, *iter; struct enable_trigger_data *enable_data; - struct event_trigger_data *data; - bool unregistered =3D false; =20 lockdep_assert_held(&event_mutex); =20 - list_for_each_entry(data, &file->triggers, list) { - enable_data =3D data->private_data; + list_for_each_entry(iter, &file->triggers, list) { + enable_data =3D iter->private_data; if (enable_data && - (data->cmd_ops->trigger_type =3D=3D + (iter->cmd_ops->trigger_type =3D=3D test->cmd_ops->trigger_type) && (enable_data->file =3D=3D test_enable_data->file)) { - unregistered =3D true; + data =3D iter; list_del_rcu(&data->list); trace_event_trigger_enable_disable(file, 0); update_cond_flag(file); @@ -1996,7 +1994,7 @@ void event_enable_unregister_trigger(char *glob, } } =20 - if (unregistered && data->ops->free) + if (data && data->ops->free) data->ops->free(data->ops, data); } =20 --=20 2.25.1 From nobody Sun May 10 17:53:29 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 901B8C433EF for ; Wed, 27 Apr 2022 17:08:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243899AbiD0RLq (ORCPT ); Wed, 27 Apr 2022 13:11:46 -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-Google-Smtp-Source: ABdhPJwLWRpKq9gAjysvaJP4IdGzPUqPzRB1ziV3vsByW/3u3Ag30ZXAh01vGp0jYB6Tec2wua0dxA== 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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 =3D=3D 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-=3DwgRr_D8CB-D9Kg-c=3DEHreAsk5SqXPwr= 9Y7k9sA6cWXJ6w@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 tra= ce_array *tr, struct ftrace_probe_ops *probe_ops, void *data) { + struct ftrace_func_probe *probe =3D 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 t= race_array *tr, =20 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 =3D=3D probe_ops) + list_for_each_entry(iter, &tr->func_probes, list) { + if (iter->probe_ops =3D=3D probe_ops) { + probe =3D iter; break; + } } - if (&probe->list =3D=3D &tr->func_probes) { + if (!probe) { probe =3D 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 =3D 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, s= truct trace_array *tr, =20 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 =3D=3D probe_ops) + list_for_each_entry(iter, &tr->func_probes, list) { + if (iter->probe_ops =3D=3D probe_ops) { + probe =3D iter; break; + } } - if (&probe->list =3D=3D &tr->func_probes) + if (!probe) goto err_unlock_ftrace; =20 ret =3D -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 =3D { static int disable_eprobe(struct trace_eprobe *ep, struct trace_array *tr) { - struct event_trigger_data *trigger; + struct event_trigger_data *trigger =3D NULL, *iter; struct trace_event_file *file; struct eprobe_data *edata; =20 @@ -658,14 +658,16 @@ static int disable_eprobe(struct trace_eprobe *ep, if (!file) return -ENOENT; =20 - 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 =3D trigger->private_data; - if (edata->ep =3D=3D ep) + edata =3D iter->private_data; + if (edata->ep =3D=3D ep) { + trigger =3D iter; break; + } } - if (list_entry_is_head(trigger, &file->triggers, list)) + if (!trigger) return -ENODEV; =20 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; =20 /* First see if we did not already create this dir */ @@ -2294,13 +2294,13 @@ event_subsystem_dir(struct trace_array *tr, const c= har *name, } =20 /* Now see if the system itself exists. */ - list_for_each_entry(system, &event_subsystems, list) { - if (strcmp(system->name, name) =3D=3D 0) + system =3D NULL; + list_for_each_entry(iter, &event_subsystems, list) { + if (strcmp(iter->name, name) =3D=3D 0) { + system =3D iter; break; + } } - /* Reset system variable when not found */ - if (&system->list =3D=3D &event_subsystems) - system =3D NULL; =20 dir =3D kmalloc(sizeof(*dir), GFP_KERNEL); if (!dir) --=20 2.25.1