Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp2495766rdb; Thu, 21 Sep 2023 23:25:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF77xZZ9gcEPNEBCUmB3BpMFW/fCouRJtK//xELH97/DmrVUo8aY74mKObe2KDiYFbBGIOP X-Received: by 2002:a05:6a20:5530:b0:159:c24f:5fa4 with SMTP id ko48-20020a056a20553000b00159c24f5fa4mr6340492pzb.1.1695363947317; Thu, 21 Sep 2023 23:25:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695363947; cv=none; d=google.com; s=arc-20160816; b=UFSUkQu7V86R2Q2E6HW9nl227lmjIpAh/uKDL3FinzZQwyK4AnafuERoEat1uunosf c8A4q0+MUYmPVocQkdR2n8blFSkP2LLZQsoxdN0wXbUzyLe2h7ohzNW9Ju7mEEobRKGm gEjSE4hR3pprdw5XHEcMdhBOuw5YQeGmsv9iGeErE+QM9/Qo4Yw4LEMDifNnabOtLaS6 zo02BG7FXQTr2inmIxULHFzRHTmuC5J94psLcR/2xEGiG+GtVPEbHqxFqcgMbL82ee9l YrYhqwYnhU/ACp5zy8UTNWIBFlweM/1DKJ+Cz66/m4OwWPqWgLJd2FCbg+vZD6G5Jsho kSIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=7Js1zpV1/5tyHr20INs8L7sC4PojIEf8avVZr3ovZtM=; fh=6Ehczo5O4WybEwBzzPazhHSVRLr3am9XSs/0QBaAom8=; b=K+9WrWIowi+UE/yUWV6oa6Mu3EwZBrD0FRZhqY/2cGKn09qddqj4VXM04LpAx8ZGGX dOrh39OCTF/lw2PMaPJ4R2zOKJ+62ilNxoLddEj7ic6XSRFwPDGAEUwaVIMHeXAUh5cx 4+Hl+XaPLyFwrgHfftkdaXUPPRvVVJQmcT8fTUCuSs8ykzVDm8411nQQjpO43IVqCHyu 0pjrqPsE6e/AL6ZFO5u5AWx0t/id5PNqijp9QFuWedAGDUofWH4N/8fcnWXP3q3MOwmY 2xJEA6lB4ihqdb1kdN4mcfMhv90DUxdVfXXEdwHtu5PFLI3PTDBvlOPkOLYEGpz2X9aN xczw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=kmnRhCS9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id ca18-20020a17090af31200b00274c5572179si3317452pjb.2.2023.09.21.23.25.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Sep 2023 23:25:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=kmnRhCS9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 2C88A84EE56E; Thu, 21 Sep 2023 13:40:23 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231774AbjIUUkV (ORCPT + 99 others); Thu, 21 Sep 2023 16:40:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232191AbjIUUji (ORCPT ); Thu, 21 Sep 2023 16:39:38 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47B3D8BD25; Thu, 21 Sep 2023 10:41:55 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 3597E338AD; Thu, 21 Sep 2023 10:33:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1695292382; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=7Js1zpV1/5tyHr20INs8L7sC4PojIEf8avVZr3ovZtM=; b=kmnRhCS9M0QXPtX1OhDAV3Y6l/pa1Pf+cx8XBXHzVcqRqVppnoYl8lDjYbqXemFVhBta93 g7wNEKMNHZ/6Jgh58uPFoVTjG5v0WeyzrOhE9KTPXO7ea114eMDNExMkTiCvW7kSgEnPYY 1HMq4IqDhyEtX7laXgnRERz03en9Jgw= Received: from suse.cz (pmladek.udp.ovpn2.prg.suse.de [10.100.201.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 3405F2C14E; Thu, 21 Sep 2023 10:33:00 +0000 (UTC) Date: Thu, 21 Sep 2023 12:33:00 +0200 From: Petr Mladek To: "Leizhen (ThunderTown)" Cc: Yonghong Song , Kees Cook , Nick Desaulniers , Song Liu , Steven Rostedt , Fangrui Song , kernel-team@fb.com, Leizhen , linux-kernel@vger.kernel.org, llvm@lists.linux.dev, kernel test robot , live-patching@vger.kernel.org Subject: Re: [PATCH] kallsyms: Fix kallsyms_selftest failure Message-ID: References: <20230825034659.1037627-1-yonghong.song@linux.dev> <95a7d98c-b227-7929-b833-f6adc3b7e3ca@huaweicloud.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <95a7d98c-b227-7929-b833-f6adc3b7e3ca@huaweicloud.com> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Thu, 21 Sep 2023 13:40:23 -0700 (PDT) Adding live-patching list into Cc. On Fri 2023-08-25 15:19:10, Leizhen (ThunderTown) wrote: > On 2023/8/25 11:46, Yonghong Song wrote: > > Kernel test robot reported a kallsyms_test failure when clang lto is > > enabled (thin or full) and CONFIG_KALLSYMS_SELFTEST is also enabled. > > I can reproduce in my local environment with the following error message > > with thin lto: > > [ 1.877897] kallsyms_selftest: Test for 1750th symbol failed: (tsc_cs_mark_unstable) addr=ffffffff81038090 > > [ 1.877901] kallsyms_selftest: abort > > > > It appears that commit 8cc32a9bbf29 ("kallsyms: strip LTO-only suffixes > > from promoted global functions") caused the failure. Commit 8cc32a9bbf29 > > changed cleanup_symbol_name() based on ".llvm." instead of '.' where > > ".llvm." is appended to a before-lto-optimization local symbol name. > > We need to propagate such knowledge in kallsyms_selftest.c as well. > > > > Further more, compare_symbol_name() in kallsyms.c needs change as well. > > In scripts/kallsyms.c, kallsyms_names and kallsyms_seqs_of_names are used > > to record symbol names themselves and index to symbol names respectively. > > For example: > > kallsyms_names: > > ... > > __amd_smn_rw._entry <== seq 1000 > > __amd_smn_rw._entry.5 <== seq 1001 > > __amd_smn_rw.llvm. <== seq 1002 > > ... > > > > kallsyms_seqs_of_names are sorted based on cleanup_symbol_name() through, so > > the order in kallsyms_seqs_of_names actually has > > > > index 1000: seq 1002 <== __amd_smn_rw.llvm. (actual symbol comparison using '__amd_smn_rw') > > index 1001: seq 1000 <== __amd_smn_rw._entry > > index 1002: seq 1001 <== __amd_smn_rw._entry.5 > > > > Let us say at a particular point, at index 1000, symbol '__amd_smn_rw.llvm.' > > is comparing to '__amd_smn_rw._entry' where '__amd_smn_rw._entry' is the one to > > search e.g., with function kallsyms_on_each_match_symbol(). The current implementation > > will find out '__amd_smn_rw._entry' is less than '__amd_smn_rw.llvm.' and > > then continue to search e.g., index 999 and never found a match although the actual > > index 1001 is a match. > > > > To fix this issue, let us do cleanup_symbol_name() first and then do comparison. > > In the above case, comparing '__amd_smn_rw' vs '__amd_smn_rw._entry' and > > '__amd_smn_rw._entry' being greater than '__amd_smn_rw', the next comparison will > > be > index 1000 and eventually index 1001 will be hit an a match is found. > > > > For any symbols not having '.llvm.' substr, there is no functionality change > > for compare_symbol_name(). > > Reviewed-by: Zhen Lei > > > > > Fixes: 8cc32a9bbf29 ("kallsyms: strip LTO-only suffixes from promoted global functions") > > Reported-by: kernel test robot > > Closes: https://lore.kernel.org/oe-lkp/202308232200.1c932a90-oliver.sang@intel.com > > Signed-off-by: Yonghong Song > > --- > > kernel/kallsyms.c | 17 +++++++---------- > > kernel/kallsyms_selftest.c | 23 +---------------------- > > 2 files changed, 8 insertions(+), 32 deletions(-) > > > > diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c > > index 016d997131d4..e12d26c10dba 100644 > > --- a/kernel/kallsyms.c > > +++ b/kernel/kallsyms.c > > @@ -188,16 +188,13 @@ static bool cleanup_symbol_name(char *s) > > > > static int compare_symbol_name(const char *name, char *namebuf) > > { > > - int ret; > > - > > - ret = strcmp(name, namebuf); > > - if (!ret) > > - return ret; > > - > > - if (cleanup_symbol_name(namebuf) && !strcmp(name, namebuf)) > > - return 0; > > - > > - return ret; > > + /* The kallsyms_seqs_of_names is sorted based on names after > > + * cleanup_symbol_name() (see scripts/kallsyms.c) if clang lto is enabled. > > + * To ensure correct bisection in kallsyms_lookup_names(), do > > + * cleanup_symbol_name(namebuf) before comparing name and namebuf. > > + */ > > + cleanup_symbol_name(namebuf); > > + return strcmp(name, namebuf); > > } Hmm, I think that this is not the right fix. The problem is that compare_symbol_name() does not longer allow to match the full name of the extra .llwm. symbols. I think that the problem is that the problem is that the symbols are sorted using cleanup_symbol_name(). They should be sorted by using the full name. Note that the original compare_symbol_name() returned return value when comparing the non-stripped name. It will work correctly when the non-stripped names are sorted. I believe that the correct fix is: diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 653b92f6d4c8..da1f8ae68999 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -339,25 +339,6 @@ static int symbol_absolute(const struct sym_entry *s) return s->percpu_absolute; } -static void cleanup_symbol_name(char *s) -{ - char *p; - - /* - * ASCII[.] = 2e - * ASCII[0-9] = 30,39 - * ASCII[A-Z] = 41,5a - * ASCII[_] = 5f - * ASCII[a-z] = 61,7a - * - * As above, replacing the first '.' in ".llvm." with '\0' does not - * affect the main sorting, but it helps us with subsorting. - */ - p = strstr(s, ".llvm."); - if (p) - *p = '\0'; -} - static int compare_names(const void *a, const void *b) { int ret; @@ -533,10 +514,6 @@ static void write_src(void) printf("\n"); } - if (lto_clang) - for (i = 0; i < table_cnt; i++) - cleanup_symbol_name((char *)table[i]->sym); - sort_symbols_by_name(); output_label("kallsyms_seqs_of_names"); for (i = 0; i < table_cnt; i++) Unfortunately, I could not check it easily because I do not have any experience with building kernel with C-lang. Anyway, what do you think, please? Best Regards, Petr