From patchwork Tue Nov 22 23:09:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 24626 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2487318wrr; Tue, 22 Nov 2022 15:16:28 -0800 (PST) X-Google-Smtp-Source: AA0mqf4spBbzAnR8HUsBQQG5p1jKa3efTgyFv+SpFhJxw+RpXAXtM5dfa252UHaku8tPKLqDOw2b X-Received: by 2002:a17:906:2693:b0:7aa:57c3:3f26 with SMTP id t19-20020a170906269300b007aa57c33f26mr5463976ejc.195.1669158988421; Tue, 22 Nov 2022 15:16:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669158988; cv=none; d=google.com; s=arc-20160816; b=IFzsDsoMND99IUKPdSdAqEq24QY0rrvZtJf1ditKcCEtETMaEik6MZ8zY4bcMHgpRl Vs9T0fswsxVWNb8TJRV0lnZ1QM+QsV+2d20Qsr51A6Q1jLnSas9K9efesSw99zXWoskr 0eE3aAGo1P8xlhJy5JnfgqqD4EKeVEy/alU5B1osMiCEIdx4hCdXKJ2VI6iLZXbbjlpy DdcHEbiil7ICeO2zGI9Ius7NGj7mYaUnlzKIT6nVYbMpo4Nh1xctBXdtDVugqRWP+8pq UTm2VZ8olQIUzXET8iNCGnbjSIeah90QBeIu3S5JlYNjTDLfn1urv9mSqExGO5+GHS3i 7gpA== 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 :message-id:subject:cc:to:from:date; bh=3vvmW1d2FtKR5/I0Tx97I8RkbkcRUMd2VnepEYFDC7Y=; b=zqX69iWfUu1Imzsf8N7IDPtXB9dA7KjZKlBRbZeSIp7lTCGq6QcKmE8kPTPHvJrEoB ZyxG3kRzLNg+Fe32S9zJNPkd81dAdb9Ub3ve+oILUhwve6na4wkEV+e6vjpK5rOzYoRt yILZ8Q5VxKsRDGRiLOUrI0rXhDQcGgkO7O623eLrCkZVtfgqC2oxs8sJUroIRWu/nViz Aut95DPSumMBDd34dozaH2mQfkLqq/xNKh64NCt9pOf0vdtOIq/iUQzLK2X25p94K7Sp 0KOb1oA2ehe/54Np7zPA30aDl7t99JT3EPG/1wo3g6gJ6Ul/7cevZuaHuABbi/ZcJSe7 oGRg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cs17-20020a170906dc9100b00780e022dfb8si13526407ejc.494.2022.11.22.15.16.04; Tue, 22 Nov 2022 15:16:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235263AbiKVXJS (ORCPT + 99 others); Tue, 22 Nov 2022 18:09:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235214AbiKVXJP (ORCPT ); Tue, 22 Nov 2022 18:09:15 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6391C495A; Tue, 22 Nov 2022 15:09:10 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 461D0B81D4B; Tue, 22 Nov 2022 23:09:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 923E5C433D6; Tue, 22 Nov 2022 23:09:07 +0000 (UTC) Date: Tue, 22 Nov 2022 18:09:05 -0500 From: Steven Rostedt To: LKML , Linux Trace Kernel Cc: Masami Hiramatsu , Song Shuai Subject: [PATCH] ftrace: Avoid needless updates of the ftrace function call Message-ID: <20221122180905.737b6f52@gandalf.local.home> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 X-Spam-Status: No, score=-6.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750240055353069272?= X-GMAIL-MSGID: =?utf-8?q?1750240055353069272?= From: "Steven Rostedt (Google)" Song Shuai reported: The list func (ftrace_ops_list_func) will be patched first before the transition between old and new calls are set, which fixed the race described in this commit `59338f75`. While ftrace_trace_function changes from the list func to a ftrace_ops func, like unregistering the klp_ops to leave the only global_ops in ftrace_ops_list, the ftrace_[regs]_call will be replaced with the list func although it already exists. So there should be a condition to avoid this. And suggested using another variable to keep track of what the ftrace function is set to. But this could be simplified by using a helper function that does the same with a static variable. Link: https://lore.kernel.org/lkml/20221026132039.2236233-1-suagrfillet@gmail.com/ Reported-by: Song Shuai Signed-off-by: Steven Rostedt (Google) Reviewed-by: Masami Hiramatsu (Google) --- kernel/trace/ftrace.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index 65a5d36463e0..d04552c0c275 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -2763,6 +2763,19 @@ void __weak ftrace_arch_code_modify_post_process(void) { } +static int update_ftrace_func(ftrace_func_t func) +{ + static ftrace_func_t save_func; + + /* Avoid updating if it hasn't changed */ + if (func == save_func) + return 0; + + save_func = func; + + return ftrace_update_ftrace_func(func); +} + void ftrace_modify_all_code(int command) { int update = command & FTRACE_UPDATE_TRACE_FUNC; @@ -2783,7 +2796,7 @@ void ftrace_modify_all_code(int command) * traced. */ if (update) { - err = ftrace_update_ftrace_func(ftrace_ops_list_func); + err = update_ftrace_func(ftrace_ops_list_func); if (FTRACE_WARN_ON(err)) return; } @@ -2799,7 +2812,7 @@ void ftrace_modify_all_code(int command) /* If irqs are disabled, we are in stop machine */ if (!irqs_disabled()) smp_call_function(ftrace_sync_ipi, NULL, 1); - err = ftrace_update_ftrace_func(ftrace_trace_function); + err = update_ftrace_func(ftrace_trace_function); if (FTRACE_WARN_ON(err)) return; }