From patchwork Sat Apr 15 02:17:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 83637 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp788472vqo; Fri, 14 Apr 2023 19:37:58 -0700 (PDT) X-Google-Smtp-Source: AKy350YA6feCeeOu/ucKsCvUnVaLOhL4WIcycYiCq9tgTx3jkj0GxXlAcrPhscR/w8tEKR/IjDsM X-Received: by 2002:a17:902:bb92:b0:1a6:9ec6:6a92 with SMTP id m18-20020a170902bb9200b001a69ec66a92mr3930752pls.55.1681526277982; Fri, 14 Apr 2023 19:37:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681526277; cv=none; d=google.com; s=arc-20160816; b=Yhl43JCFLL/Fc0DLucAp+myrgedUhsXEjilzYdz3aWnAps3CKHVR4iz8PVGyHHjrFD U99wniLkDVc/rj27PkihaJEDo+gXvMVWsIxNuVQIzoGbqmuA2Eeegip5rb/bbuk4SvW8 BwFWt7K4AGLuFChZaCvrZk+fCVXSIYuzj/4qjZNIgoh2ql0KjicTFYOZKkBt/a52SJJ5 XgrxGmNpJXJvk/YyJixV3XTAUooDJ7FZA/a9jV+byrd98ER5D+SCWHCfJ7qFUVYUKqvx sA3+IRiExaZvg3CmGMghT3YDUUZeYAPVRzyFz7YHOxK7b4reEKjuAKBieEe5z2XlCO27 S7Mg== 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=gTRSGaM9kVQ6P7Zqo+pfU/VLgELqiupePomeEojRoZ4=; b=VLXh412zjSMqL51BlqW7z0whgo+BTSBLb4Ot6oV2lZAs+77vQjNA2rMSmGsAWJk1jF E4Jz7kafqep1UvZ1yVtqDZHN7PEQrgLtAK/2LiqQzd0nr/3cFifpa+QX7/Igq4htdCDa CGOxa1gb/8OzanhHdLwesaq0D8L7KIH0IHZLlAHl9ClRrQ9ADiJmPM/bpgEBH9OePpt7 HuIR960VJytiD4TH32sZ2v77hiCjcU0Y3V/y5Q+qwlTF9lSucSgiWD/p/oZwXntN7QyX lfAikHKSNNg7UrtHXIduDN0wmttfsYIa2cY4Zf2miSQy9jwXwlSi26NxMC0kUIIr72ah fpDA== 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 a1-20020a170902900100b001a52a50e167si6001353plp.573.2023.04.14.19.37.45; Fri, 14 Apr 2023 19:37:57 -0700 (PDT) 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 S230032AbjDOCRK (ORCPT + 99 others); Fri, 14 Apr 2023 22:17:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229569AbjDOCRJ (ORCPT ); Fri, 14 Apr 2023 22:17:09 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1124F55B3; Fri, 14 Apr 2023 19:17:07 -0700 (PDT) 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 dfw.source.kernel.org (Postfix) with ESMTPS id A0F4D64934; Sat, 15 Apr 2023 02:17:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6FBD7C433EF; Sat, 15 Apr 2023 02:17:05 +0000 (UTC) Date: Fri, 14 Apr 2023 22:17:02 -0400 From: Steven Rostedt To: LKML , Linux trace kernel Cc: Masami Hiramatsu , Mark Rutland , Yafang Shao Subject: [PATCH] tracing: Add generic test_recursion_try_acquire() Message-ID: <20230414221702.554c44fe@rorschach.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, T_SCC_BODY_TEXT_LINE 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?1763208098425527681?= X-GMAIL-MSGID: =?utf-8?q?1763208098425527681?= From: "Steven Rostedt (Google)" The ftrace_test_recursion_trylock() also disables preemption. This is not required, but was a clean up as every place that called it also disabled preemption, and making the two tightly coupled appeared to make the code simpler. But the recursion protection can be used for other purposes that do not require disabling preemption. As the recursion bits are attached to the task_struct, it follows the task, so there's no need for preemption being disabled. Add test_recursion_try_acquire/release() functions to be used generically, and separate it from being associated with ftrace. It also removes the "lock" name, as there is no lock happening. Keeping the "lock" for the ftrace version is better, as it at least differentiates that preemption is being disabled (hence, "locking the CPU"). Link: https://lore.kernel.org/linux-trace-kernel/20230321020103.13494-1-laoar.shao@gmail.com/ Cc: Yafang Shao Signed-off-by: Steven Rostedt (Google) Acked-by: Yafang Shao --- include/linux/trace_recursion.h | 47 ++++++++++++++++++++++++--------- kernel/trace/ftrace.c | 2 ++ 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/include/linux/trace_recursion.h b/include/linux/trace_recursion.h index d48cd92d2364..80de2ee7b4c3 100644 --- a/include/linux/trace_recursion.h +++ b/include/linux/trace_recursion.h @@ -150,9 +150,6 @@ extern void ftrace_record_recursion(unsigned long ip, unsigned long parent_ip); # define trace_warn_on_no_rcu(ip) false #endif -/* - * Preemption is promised to be disabled when return bit >= 0. - */ static __always_inline int trace_test_and_set_recursion(unsigned long ip, unsigned long pip, int start) { @@ -182,18 +179,11 @@ static __always_inline int trace_test_and_set_recursion(unsigned long ip, unsign val |= 1 << bit; current->trace_recursion = val; barrier(); - - preempt_disable_notrace(); - return bit; } -/* - * Preemption will be enabled (if it was previously enabled). - */ static __always_inline void trace_clear_recursion(int bit) { - preempt_enable_notrace(); barrier(); trace_recursion_clear(bit); } @@ -205,12 +195,18 @@ static __always_inline void trace_clear_recursion(int bit) * tracing recursed in the same context (normal vs interrupt), * * Returns: -1 if a recursion happened. - * >= 0 if no recursion. + * >= 0 if no recursion and preemption will be disabled. */ static __always_inline int ftrace_test_recursion_trylock(unsigned long ip, unsigned long parent_ip) { - return trace_test_and_set_recursion(ip, parent_ip, TRACE_FTRACE_START); + int bit; + + bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_FTRACE_START); + if (unlikely(bit < 0)) + return bit; + preempt_disable_notrace(); + return bit; } /** @@ -220,6 +216,33 @@ static __always_inline int ftrace_test_recursion_trylock(unsigned long ip, * This is used at the end of a ftrace callback. */ static __always_inline void ftrace_test_recursion_unlock(int bit) +{ + preempt_enable_notrace(); + trace_clear_recursion(bit); +} + +/** + * test_recursion_try_acquire - tests for recursion in same context + * + * This will detect recursion of a function. + * + * Returns: -1 if a recursion happened. + * >= 0 if no recursion + */ +static __always_inline int test_recursion_try_acquire(unsigned long ip, + unsigned long parent_ip) +{ + return trace_test_and_set_recursion(ip, parent_ip, TRACE_FTRACE_START); +} + +/** + * test_recursion_release - called after a success of test_recursion_try_acquire() + * @bit: The return of a successful test_recursion_try_acquire() + * + * This releases the recursion lock taken by a non-negative return call + * by test_recursion_try_acquire(). + */ +static __always_inline void test_recursion_release(int bit) { trace_clear_recursion(bit); } diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index db8532a4d5c8..1b117ab057ed 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -7299,6 +7299,7 @@ __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, if (bit < 0) return; + preempt_disable(); do_for_each_ftrace_op(op, ftrace_ops_list) { /* Stub functions don't need to be called nor tested */ if (op->flags & FTRACE_OPS_FL_STUB) @@ -7320,6 +7321,7 @@ __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, } } while_for_each_ftrace_op(op); out: + preempt_enable(); trace_clear_recursion(bit); }