Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4690717iob; Sun, 8 May 2022 22:08:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxm9vt9JwOHwDo6E6YVmSS7+93NuR7V5MgJfj2j4IsghD14VREGIlXSQkxbrfdkW7VyWJvj X-Received: by 2002:a17:902:e543:b0:15e:fbaf:ba8c with SMTP id n3-20020a170902e54300b0015efbafba8cmr8495389plf.78.1652072919604; Sun, 08 May 2022 22:08:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652072919; cv=none; d=google.com; s=arc-20160816; b=KlnXqPuZz8KSJ2cw09bmhZNMNGViIRYMOjnV0Z2CDKK4wEPbE0zPkaB+MnqS/hqvxg wsYg0ygoEad4HZEzRz3XF0tMAgVdoVj5e5UW3TZqkQ27q9cUoxqjRpm4OShP6LVlz2/I axz0vox94ZJoA8WF5D3NnrG+nZasvE5NfNMsuVxIINeR5ABwnQpmJ53mJYha4wNGA27x HRnGdRrKq/MV04m82ZTAii46gLplJiUCxxeJDIzV8Awu9lf5N80q6tQ9yzDLB7wKq9tj kvDSOYb3ZPTSMu/mVztNs1qNTcN7ff6jbCBztXROqIKesUGBPynG49wnYq2ony7udwpf tBUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=rCTLTbLP/HCPwEyRHnS4Xntt1B1zffntB8D6OmsmIYM=; b=FIQONxu1fEvr9BtS6OmjPMIo9SOQbs6ggrzGzv7He7Eug5HSBxVSNfXCLafYf+NbKt alyZ9AS2H9NMdwLxr5mg+PnPjI4x6F1jJAr7TuJfDYlX7ec5OmkHQaWQI++VPoRItqm6 IuM0/lgN90dr6tp3L+qO72/La8HrthJaOrGkSN64BjzFE3vomAzpYMvSfVJbPjITOEvy ehwsCL/pg2vERpHtosoVDCUhJZ4CZ6cKdfua4QUQjsTGzLu4/EHdbjyjaa/m3stYy7lV 57CrrRREW+ayb7uEShqE/cAuzzhcEWmaby/eo4fHNBPbV6WOaYXpt1VGbOz6o7QyqzkK sgLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="pg0/Yt3Q"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id b12-20020a056a00114c00b0050ddce187edsi14652849pfm.331.2022.05.08.22.08.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 May 2022 22:08:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="pg0/Yt3Q"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 9D5A218395; Sun, 8 May 2022 22:01:26 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1444683AbiEFWSm (ORCPT + 99 others); Fri, 6 May 2022 18:18:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241939AbiEFWSj (ORCPT ); Fri, 6 May 2022 18:18:39 -0400 Received: from mail-io1-xd2d.google.com (mail-io1-xd2d.google.com [IPv6:2607:f8b0:4864:20::d2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D5C06540F; Fri, 6 May 2022 15:14:54 -0700 (PDT) Received: by mail-io1-xd2d.google.com with SMTP id e3so9521370ios.6; Fri, 06 May 2022 15:14:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=rCTLTbLP/HCPwEyRHnS4Xntt1B1zffntB8D6OmsmIYM=; b=pg0/Yt3QJEJeyRkqIy1cKjUnl4Q5IC5OVFEgPVFVI9uYOt7Xmm7edvsl6gBEjCELEg SnFEiHqFqtJNoimavdRivrQjd4ET896Y+7gS9WthGdnonHoggAlP9MkvbgbP6KcJMIQl YiQ0o0q2L55cEB6cjE892PJ3+EiEAMxf9ocexcwjUGMYC//ZCMkg/ArE9S/uU5/bN94+ zToBvUNywhFlS719I9+9aq7K/fkCLLnebbKWxol21h11RibuL5bzBKeS/i2GsyXDmcIk rBHYqK3W9jk/APsnpp+ivf6nQ8PcPYp76lKw6ULV4lYD7s1gQQ+RYnIi8T821cOhijoG bRAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=rCTLTbLP/HCPwEyRHnS4Xntt1B1zffntB8D6OmsmIYM=; b=QE1ZPlHhmfmfyaUe41Zlp99F0TlYN5cqXwHXfxtk1cWEPrvMul4AMQr1kZ/bFK0D+q RBsRgwxZO8Ydrs+Rc/NCdgGLOT7RwMOJH72Pa7Df+nKwNRRTGaKbo/at2/bhtUubhIJD LSuuFT9OM2Feu7BZa2Lwy9WkcJT1RL+32+txfq5S5JHVaSCrThhDs5uwb0vWTQpJ4jnc Afq+DOz83nXufHY3Nbb88h7O8/iNDTpXDYY4rfEp6s0jgt+08m0Zg8qsxF6ca0v91M7J xr5y8dh0s+kXomn2D1YG9BiRgUf7cFEs6A5mWt1KZNbB4nWTLTU5Yw6SYmOwBW1jzVFX xjUw== X-Gm-Message-State: AOAM5332SyDtiWvYTLnVv2Au1cdbR5oiox3VIDj4HJC5xQcWDTVyODht qDDBr/1nUF+yFKnWMwH80AK7+xKjm7jV8h+a+QC9goWp X-Received: by 2002:a05:6638:33a1:b0:32b:8e2b:f9ba with SMTP id h33-20020a05663833a100b0032b8e2bf9bamr2370798jav.93.1651875293774; Fri, 06 May 2022 15:14:53 -0700 (PDT) MIME-Version: 1.0 References: <20220503150410.2d9e88aa@rorschach.local.home> In-Reply-To: <20220503150410.2d9e88aa@rorschach.local.home> From: Andrii Nakryiko Date: Fri, 6 May 2022 15:14:43 -0700 Message-ID: Subject: Re: : [PATCH] ftrace/x86: Add FTRACE_MCOUNT_MAX_OFFSET to avoid adding weak functions To: Steven Rostedt Cc: LKML , Ingo Molnar , Andrew Morton , Masami Hiramatsu , Jiri Olsa , Alexei Starovoitov , Daniel Borkmann , Networking , bpf , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Peter Zijlstra , X86 ML Content-Type: text/plain; charset="UTF-8" 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,T_SCC_BODY_TEXT_LINE 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 On Tue, May 3, 2022 at 12:04 PM Steven Rostedt wrote: > > From: "Steven Rostedt (Google)" > > If an unused weak function was traced, it's call to fentry will still > exist, which gets added into the __mcount_loc table. Ftrace will use > kallsyms to retrieve the name for each location in __mcount_loc to display > it in the available_filter_functions and used to enable functions via the > name matching in set_ftrace_filter/notrace. Enabling these functions do > nothing but enable an unused call to ftrace_caller. If a traced weak > function is overridden, the symbol of the function would be used for it, > which will either created duplicate names, or if the previous function was > not traced, it would be incorrectly listed in available_filter_functions > as a function that can be traced. > > This became an issue with BPF[1] as there are tooling that enables the > direct callers via ftrace but then checks to see if the functions were > actually enabled. The case of one function that was marked notrace, but > was followed by an unused weak function that was traced. The unused > function's call to fentry was added to the __mcount_loc section, and > kallsyms retrieved the untraced function's symbol as the weak function was > overridden. Since the untraced function would not get traced, the BPF > check would detect this and fail. > > The real fix would be to fix kallsyms to not show address of weak > functions as the function before it. But that would require adding code in > the build to add function size to kallsyms so that it can know when the > function ends instead of just using the start of the next known symbol. > > In the mean time, this is a work around. Add a FTRACE_MCOUNT_MAX_OFFSET > macro that if defined, ftrace will ignore any function that has its call > to fentry/mcount that has an offset from the symbol that is greater than > FTRACE_MCOUNT_MAX_OFFSET. > > If CONFIG_HAVE_FENTRY is defined for x86, define FTRACE_MCOUNT_MAX_OFFSET > to zero, which will have ftrace ignore all locations that are not at the > start of the function. > > [1] https://lore.kernel.org/all/20220412094923.0abe90955e5db486b7bca279@kernel.org/ > > Signed-off-by: Steven Rostedt (Google) > --- > arch/x86/include/asm/ftrace.h | 5 ++++ > kernel/trace/ftrace.c | 50 +++++++++++++++++++++++++++++++++-- > 2 files changed, 53 insertions(+), 2 deletions(-) > Thanks for investigating and fixing this! I guess we'll need ENDBR handling, but otherwise it looks good to me!