From patchwork Thu Oct 27 19:57:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 13022678 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 3A3A6FA3743 for ; Thu, 27 Oct 2022 19:58:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236317AbiJ0T61 (ORCPT ); Thu, 27 Oct 2022 15:58:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236787AbiJ0T60 (ORCPT ); Thu, 27 Oct 2022 15:58:26 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3DB6876AB; Thu, 27 Oct 2022 12:58:23 -0700 (PDT) Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 29RHn0qW019874; Thu, 27 Oct 2022 19:58:16 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2022-7-12; bh=hafRT3cbaR0JvZQBfXnlGBpP2JcVzJVAiWG91/wkYnA=; b=flD9g9Dk8Z5QPAKyrsPGB+MFaKmVfX9eprOiYkU3nzrnmnDXJyXPoMdpx2qvNhCYchUn p0iIyGScAP1ixkQ7SVJ/jIwgHtsWJrco884lgThJ6tec0JvPzU4mq/9ut0a28UERLs+x lA1ENUhit4Vw/O4zkvhVgLyjcQKQnI+X5hUjW8jL/QxvfdDnWQ/PQ5aagl5W4clKlnOD 9+aY6ReZzq+bG+BUEXFFQ4r5/v8gfzG/9O4e8GAhExzPMzH/Er+jNfuOV9kGfZCvSXF0 gDlS00x5Kk9VSGSOYeNCgZzdlQDL9a25jJbI1vEFdcyFETCLAFtSu3ki45buMTOLefsX +w== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3kfax7u2yh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 27 Oct 2022 19:58:16 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 29RJvqx4006906; Thu, 27 Oct 2022 19:58:15 GMT Received: from nam04-dm6-obe.outbound.protection.outlook.com (mail-dm6nam04lp2046.outbound.protection.outlook.com [104.47.73.46]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3kfaghb12u-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 27 Oct 2022 19:58:15 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jrAwNqqzubprugX7VbFfhALB7OCYK/EfoHKRrRy472x7AvUrLuHOebjf0VZpz5bqbnMxFdagNJjBVGgPAjwMAcKAoGKo+yiXSnK/5I9xvWmbxlO6RShdJgdm4oyDY/NA3Z6i+RFi2kRFUkTT5W2aGhzvxmtsGfJshAFclVP6TlRVpyizSitch58F8vMzWc2hhzPVtx9Sto4SSx5+xRYEC2u696GBM1RsapW8a7AgOcBu5qD10DaMFzbP2mKXB55rCtVh46W6oJrgvgVytBYNFZT6y5WkGYgUU7ozFHdXXv7BrLc4gTW46gRRKmNVZ7YXCoTovsg8/SXoCh78jTZcvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=hafRT3cbaR0JvZQBfXnlGBpP2JcVzJVAiWG91/wkYnA=; b=RbR47NKpFKz3EoyQbfkCcIGO4wy6RumQCv0jviK1wlVTpq0ZZVAqi7k7C9ClDRaVoZjFoLFtOyEfBmwmViJeD1FbNxKpbRH/0LreCb+83Ykxqnsa2GwF4vVLHzzEogXRF1sXDHva0VdBp54Qn2dMo2jxtfvVQQx6wfDMNq97RkkJFkVyTzeQ1vNXGdmTpdLEGMrlf+FQilRin+Qs5va5HhK0pptesm+a0WSiUV321izN67xdlI/QGs1Y6xwlMQ5OlvDBjxQeTwlrjuYR5K3pyePRIHdNcq4LeWwozRTzUu0ZGhAP7dxQ9dB8nYneT4xo+ah2x801bCfgsu7QbJkHtQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=hafRT3cbaR0JvZQBfXnlGBpP2JcVzJVAiWG91/wkYnA=; b=Q8CIEog23HS/cmr5Zqcr382zx2kvBvMnpRycnCiUOJo2bH/NGzbgg/gBzVN5TwjCpddl0bkSqnQwIi3UYMkI1OZzMNFv0mpbkOVsdmjQqKIiBxmOQjbVHlvJv6vYiGm/geFbsmA/Zjyz2EiNUuQE/oxOJY3i4ORgv9440xEKT2c= Received: from DS0PR10MB6798.namprd10.prod.outlook.com (2603:10b6:8:13c::20) by BL3PR10MB6042.namprd10.prod.outlook.com (2603:10b6:208:3b2::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.24; Thu, 27 Oct 2022 19:58:13 +0000 Received: from DS0PR10MB6798.namprd10.prod.outlook.com ([fe80::ff03:d749:df80:38e1]) by DS0PR10MB6798.namprd10.prod.outlook.com ([fe80::ff03:d749:df80:38e1%7]) with mapi id 15.20.5746.028; Thu, 27 Oct 2022 19:58:13 +0000 From: Nick Alcock To: mcgrof@kernel.org, rostedt@goodmis.org, masahiroy@kernel.org Cc: linux-trace-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, akpm@linux-foundation.org, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v9 1/8] kbuild: bring back tristate.conf Date: Thu, 27 Oct 2022 20:57:47 +0100 Message-Id: <20221027195754.100039-2-nick.alcock@oracle.com> X-Mailer: git-send-email 2.38.0.266.g481848f278 In-Reply-To: <20221027195754.100039-1-nick.alcock@oracle.com> References: <20221027195754.100039-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P265CA0108.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:c::24) To DS0PR10MB6798.namprd10.prod.outlook.com (2603:10b6:8:13c::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS0PR10MB6798:EE_|BL3PR10MB6042:EE_ X-MS-Office365-Filtering-Correlation-Id: 9a94aa39-c8c6-4c73-6863-08dab8559465 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: OyTdRbLn8N0tTX9NkQGUQA+/SotDEnMn7fAcBHzgFwTUO3TFa79STkfv0A09AaqYGNH6FINYe4D5NwTLpQEE5NJj0MlUrCsdsJR2TLwQSC0HYLdFkjGdF/mFlKXeEdoVhmNHceNqvA2UDbYxDzYV2JrPSNH8Pj08nLqv3dKxY6hH4lix2ktlHnAfQJ56k+esKQsFRp3kz2FU5AyMLDVd4o4Cl73izvdGlIc3AdE8qkn55xP9aFxJ/3KUsrOcI/mv+izywdwPv09jFw7V/2RuIYuQw7v6orVntPaYBaVMKSbiLrFl/GCywfArjmSOMO54YufYbPP2ffHzJK+ekUHrlhPUKD0z1MyZooHNjHe3YWF6kyiLECy6cX7zFzEUL6tdkIUinEWRUKSHz4uo4IFS0yp4G9qZtKxZzJMQkZM26HFiJ1IQ3YHw9SdBcWxHL2CsoZ7yxuTMfUS6AEMMvD2RE6qQ9/pWDUu6g6HOpfdO1WzHI7yGdEAfoELXc1zq3jCClNuxY2h9FHmQjUd7ueYai9EjS2TYf7Jq/D5GtNSBumxku88nfoU01mHau6klQW56SjgScNZcVeW2lTh93p2WmBcE+TLKn3YXNwzShm1vtuxX2vo4Fdwy4HkGiU3Xw3+5hcnKQZ8ub+WAWWsw2a8YDV/vZrtU0Awt+I4PJY/oujLCEYKx1RJT9f4meQH24oy2SyK2f5sm9EDcAd7set3gSw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS0PR10MB6798.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(39860400002)(366004)(376002)(346002)(136003)(396003)(451199015)(83380400001)(41300700001)(478600001)(66476007)(6506007)(6486002)(8936002)(316002)(6666004)(5660300002)(2616005)(4326008)(66946007)(36756003)(2906002)(6512007)(66556008)(86362001)(107886003)(44832011)(8676002)(186003)(1076003)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: /FeXlbXKKU9FwLsxQX1ANNfdOcK+vlYoJId+sR6bPd5GNezRuEEJU5CrCHTmHsA3cCJr+GqgTH+q22fFzkXgCY/NxmeFY06aoqdi+GLJACTe5pXVW69XGm+t82Jj3MRE0yLb7wIkrNRYnumKWzqo0IWmwN0DkIC8WPpY09awfe5SvtkZHAvmI+I/WkRqQxVeHcmKO0psETS5Cq/JwCKXbKSKSaUWaAH3AbAADAqzcKorO5zyEZRy9tCr6TqTD8Pltk+284LYB8w31mwgzLU0LEPTmu3eynTeFz/3eDVS1kEX6nTSYLFO8jPDFPbELg1/zCHORae6u38aQrSq7twNm24svgTKCdQmY3hWZer4SBfNpOTzPuO8OV3N+gWjTRMHEri8eRfWVTMzgzpBV+X382r00A4neNBkWKKQtAUTBHJOaK+r0U+3aTJiwRXPnbGXgM42fmghql4LfoVv+sMF0S1vVUqNjftqn8hhTU65N6bITMjwRuYEu9SrBOBEdGPH4hSjy9XQJfzb+MAXsVtVqnxnzosu99b/FvTFMjc5ZP8HLho8ARCHCK+5HEu4xpDApfOIpaK7FZvMBzmO3W+ixw2eu8tDgLdoNEqCQXAgRGLJoOb1g+I/L7a2xGSOI09+0efD/vMhSN1lKzOR2F9kdvMcmQYMudr5cp6UBmYCHA/3QN715OEI38uuGFuZxDkRg8k2qn76VWw57x046eVgNCiMZ3KymWCwjZOF6sE3PjjUaKzbhlP+0ggPDos3049uwYzlCL7j6Mq1obvrw9REVEjHl01TrQ9cd9fpwQMz6AbrhBLeOMLizD5CCbL+7EO45n5PnfWSPKXSTCDvxRBKqgtNDbdxAMlLBC4TT8jSWFeIZqXz0Y9l6jM49b93W5+2+57iJNWiScJX9qRFxyfHPQDpDNEEPIAnpciI0nRbsl65d2gzfsQCWeVnVSvfk1VY8mJYEyF5h9cdPf5dAH3P9L9ziE35kncimRu20yqJmUQfv97pez0wzyA+0rkyh2OTyMC4u287eFHVRkB5FUN9oScn8sL4+2qsAzamStwikFAVdmpg/gLHqNg0rALAdQXMek2ESNiFJcwQwcHp5ZmP28csiBVLZDMc64Zs+rzmJoDyxhszqbtjOKQ1wv+6p1SirqPx6r2gbAs31UQqgYuAOoCYwaAQzeqWApGemrkyMIa+rdkKY2UzhZetpECdBM7l33QsC1Jgswt94DHj6tYzepjJWPsz52q/u/wHKiYp+F3HtzEElg7pSmVe8AZwwNIkB5irC8tjOS7wsPc8EN9mMoGZFi5LAK8mpZE3TpgK4QY8GKsdzfXy1uJCgPPZOPWilJQcyD9XDvo09mP/gQsgXvEeUv1AIyRm+q7LXx68XB9TvfV/xeq9X4FobSQefPYfpUKdp8O1yLg4quQ5CatnzZUi4OiC0nmjO1n4vuoA9pcCAHdaJazaZ7IHR6Ftr7G2N8IsSV4fdPUdjnqd6PZSg5GNFTnu/n2zOomgC0Pk2g1RB9oi1I9enzYu8OmhLzxoRgTBeFZU5hlhu1wOPGiKevtKOhyVfgQopb4YQqBxPgENez7zQyZjzr7QWJ8bK4HqjVZo+aUbwpDWuylcbUHXZg== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9a94aa39-c8c6-4c73-6863-08dab8559465 X-MS-Exchange-CrossTenant-AuthSource: DS0PR10MB6798.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Oct 2022 19:58:12.9626 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: G2pwYrsyUNkhJbnhoW8Mufcc8YYthHDLkA7YQ6wkqCkGiXWQEUvHkN03wrenyWHd+jwSEkW3woVYlKXgu1j29g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR10MB6042 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-10-27_07,2022-10-27_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 spamscore=0 suspectscore=0 phishscore=0 adultscore=0 mlxscore=0 mlxlogscore=999 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2210270112 X-Proofpoint-GUID: QztShrEen4mrEhanTBNP1v3u2_RVymMk X-Proofpoint-ORIG-GUID: QztShrEen4mrEhanTBNP1v3u2_RVymMk Precedence: bulk List-ID: X-Mailing-List: linux-trace-kernel@vger.kernel.org tristate.conf was dropped because it is not needed to build a modules.builtin (although dropping it introduces a few false positives into modules.builtin support), and doing so avoids one round of recursion through the build tree to build it. But kallmodsyms support requires building a mapping from object file name to built-in module name for all builtin modules: this seems to me impossible to accomplish without parsing all makefiles under the influence of tristate.conf, since the makefiles are the only place this mapping is recorded. So bring it back for this purpose. (Thanks to the refactoring in the 5.16 timeframe, this is basically a reimplementation of commit 8b41fc4454e36fbfdbb23f940d023d4dece2de29 rather than a simple reversion.) Signed-off-by: Nick Alcock Reviewed-by: Victor Erminpour Reviewed-by: Kris Van Hees --- Notes: v7: rewrite in terms of the new confdata refactoring v8: adjust for changes in 5.17 merge window Documentation/kbuild/kconfig.rst | 5 ++++ Makefile | 2 +- scripts/kconfig/confdata.c | 41 +++++++++++++++++++++++++++----- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/Documentation/kbuild/kconfig.rst b/Documentation/kbuild/kconfig.rst index 5967c79c3baa..e2c78760d442 100644 --- a/Documentation/kbuild/kconfig.rst +++ b/Documentation/kbuild/kconfig.rst @@ -162,6 +162,11 @@ KCONFIG_AUTOCONFIG This environment variable can be set to specify the path & name of the "auto.conf" file. Its default value is "include/config/auto.conf". +KCONFIG_TRISTATE +---------------- +This environment variable can be set to specify the path & name of the +"tristate.conf" file. Its default value is "include/config/tristate.conf". + KCONFIG_AUTOHEADER ------------------ This environment variable can be set to specify the path & name of the diff --git a/Makefile b/Makefile index d148a55bfd0f..5d26447fecb8 100644 --- a/Makefile +++ b/Makefile @@ -793,7 +793,7 @@ $(KCONFIG_CONFIG): # # Do not use $(call cmd,...) here. That would suppress prompts from syncconfig, # so you cannot notice that Kconfig is waiting for the user input. -%/config/auto.conf %/config/auto.conf.cmd %/generated/autoconf.h %/generated/rustc_cfg: $(KCONFIG_CONFIG) +%/config/auto.conf %/config/auto.conf.cmd %/generated/autoconf.h %/generated/rustc_cfg %/tristate.conf: $(KCONFIG_CONFIG) $(Q)$(kecho) " SYNC $@" $(Q)$(MAKE) -f $(srctree)/Makefile syncconfig else # !may-sync-config diff --git a/scripts/kconfig/confdata.c b/scripts/kconfig/confdata.c index b7c9f1dd5e42..160d12b69957 100644 --- a/scripts/kconfig/confdata.c +++ b/scripts/kconfig/confdata.c @@ -223,6 +223,13 @@ static const char *conf_get_rustccfg_name(void) return name ? name : "include/generated/rustc_cfg"; } +static const char *conf_get_tristate_name(void) +{ + char *name = getenv("KCONFIG_TRISTATE"); + + return name ? name : "include/config/tristate.conf"; +} + static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p) { char *p2; @@ -670,8 +677,12 @@ static char *escape_string_value(const char *in) enum output_n { OUTPUT_N, OUTPUT_N_AS_UNSET, OUTPUT_N_NONE }; +#define PRINT_ESCAPE 0x01 +#define PRINT_UPCASE 0x02 +#define PRINT_TRISTATE_ONLY 0x04 + static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n, - bool escape_string) + int flags) { const char *val; char *escaped = NULL; @@ -679,6 +690,9 @@ static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n, if (sym->type == S_UNKNOWN) return; + if (flags & PRINT_TRISTATE_ONLY && sym->type != S_TRISTATE) + return; + val = sym_get_string_value(sym); if ((sym->type == S_BOOLEAN || sym->type == S_TRISTATE) && @@ -688,29 +702,38 @@ static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n, return; } - if (sym->type == S_STRING && escape_string) { + if (sym->type == S_STRING && flags & PRINT_ESCAPE) { escaped = escape_string_value(val); val = escaped; } - fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val); + if (flags & PRINT_UPCASE) + fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*val)); + else + fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val); free(escaped); } static void print_symbol_for_dotconfig(FILE *fp, struct symbol *sym) { - __print_symbol(fp, sym, OUTPUT_N_AS_UNSET, true); + __print_symbol(fp, sym, OUTPUT_N_AS_UNSET, PRINT_ESCAPE); } static void print_symbol_for_autoconf(FILE *fp, struct symbol *sym) { - __print_symbol(fp, sym, OUTPUT_N_NONE, false); + __print_symbol(fp, sym, OUTPUT_N_NONE, 0); +} + +static void print_symbol_for_tristate(FILE *fp, struct symbol *sym) +{ + __print_symbol(fp, sym, OUTPUT_N_NONE, PRINT_ESCAPE | PRINT_UPCASE | + PRINT_TRISTATE_ONLY); } void print_symbol_for_listconfig(struct symbol *sym) { - __print_symbol(stdout, sym, OUTPUT_N, true); + __print_symbol(stdout, sym, OUTPUT_N, PRINT_ESCAPE); } static void print_symbol_for_c(FILE *fp, struct symbol *sym) @@ -1207,6 +1230,12 @@ int conf_write_autoconf(int overwrite) if (ret) return ret; + ret = __conf_write_autoconf(conf_get_tristate_name(), + print_symbol_for_tristate, + &comment_style_pound); + if (ret) + return ret; + /* * Create include/config/auto.conf. This must be the last step because * Kbuild has a dependency on auto.conf and this marks the successful From patchwork Thu Oct 27 19:57:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 13022680 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 19BE1FA3743 for ; Thu, 27 Oct 2022 19:58:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236317AbiJ0T6m (ORCPT ); Thu, 27 Oct 2022 15:58:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236799AbiJ0T6k (ORCPT ); Thu, 27 Oct 2022 15:58:40 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A063C88A0A; Thu, 27 Oct 2022 12:58:29 -0700 (PDT) Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 29RHmxq8019860; Thu, 27 Oct 2022 19:58:20 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2022-7-12; bh=1z6C9gtkixehzLN/UpkpEQAvPNA7JZPk/QsoJZ6Xg60=; b=OPHsqupzJ1T8gVmYGI6SMTFjOb/ESTuVFdt10eRD4k9+3YUbSQjrqVPNwnu72O3qlazJ EzBADwUn6JIGdJBnIUGaWjU2n1VKP/2HlUL1Ev9TRn/zMStzUIPjAWasBeAus/nqxmn0 F1ci7VNqeOJFb9FlMZvaBk9/VnfCWEm6rPxRjuNevTWWvy8i+gRvX4h0sDX2M/MpzvIT MrN7e2J7fG33Bip/EAWbN7bNzQuVCDcF/QNq+3kyLYh7jQQoKSZLu2CL28XKM9J6N0bU advv3+V8w5hSmcq7wAF+mcINq5ZbAFTfcxYPeRH2cRR74vm6HW+waOWEAziEgt5lVYP/ 3Q== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3kfax7u304-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 27 Oct 2022 19:58:20 +0000 Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 29RHgu9m027030; Thu, 27 Oct 2022 19:58:19 GMT Received: from nam04-dm6-obe.outbound.protection.outlook.com (mail-dm6nam04lp2043.outbound.protection.outlook.com [104.47.73.43]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3kfagqc21j-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 27 Oct 2022 19:58:19 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ifkSBCGd+4L8Rxb33Dmy17I0UsRxG1q+RbjtHj577e2pLeyeqK3si+Von8bQ1nny1Eag2oi12EuwZ1UinL95LF3Iwdlobr9RXfce2nIwAOMGRggStVF9GYBeYCOvmtK4CvWlL09xnPMYRNCdPdlJ4o54cziIDNMaLf2hse2vwwrospwHNTrMucmfNstLfl05TQvRKmhFHqbWvHi0hZLaQ/pVxXhkONuBR6OkoIhduoOE9Sc4xqw2/0zt9hXlUcxUV0vB+8hkTKkjmJ8pAFIeTIcLn6nLV0133iwOd6ck1R6rZnqjlfsXnS+Fho2Y7lOPXD09haCl0D5iYltVmoUPKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=1z6C9gtkixehzLN/UpkpEQAvPNA7JZPk/QsoJZ6Xg60=; b=XZQXef9Cq1uqCQeIfTNCETvELweZx/4ca5TaeNwXnQN7MW0rrr8aI8x7mRUipZQ4qdKqJ5BEzhGnsemK1WqUVeshgc0XMG+C6aXe08kcK6KsHa9qVnrt11zgiRwFZQkiSHMJKmm6hJKglfoDND+5uJ/ZCIu5zmmZhY7ZRUxVid6Y4GJNIksgyR3iavgNGTcj3kFna40PphkaC73tUBwB3zFbP5ZQJlMwPK7/kGhWvCOhBS2Kn0qGQ3NKEmFGY352FtwLeeDECCMceRyJrFrYlDaY0MKn4uvWbzvKGHocozZt/fqhQGykTRvF6DvoU1wwaA4MUvN7S1xxFHHUk5sPrw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1z6C9gtkixehzLN/UpkpEQAvPNA7JZPk/QsoJZ6Xg60=; b=TK8R/oRo6dIfjS5TORKo+nCpqQODuXXIjDbAwQaMTmQ9bgvXSEXkiqwugDakI4HGK7sl+/Xy5miQ45yCZX9spDdC19zVENRSVKN9FvI1kRzRHEx20Im2mAEj9JzzR7NPsGQ21zRwLcqZJhWfli7M4zKP0go23lfkxgzHa2zxOI8= Received: from DS0PR10MB6798.namprd10.prod.outlook.com (2603:10b6:8:13c::20) by BL3PR10MB6042.namprd10.prod.outlook.com (2603:10b6:208:3b2::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.24; Thu, 27 Oct 2022 19:58:17 +0000 Received: from DS0PR10MB6798.namprd10.prod.outlook.com ([fe80::ff03:d749:df80:38e1]) by DS0PR10MB6798.namprd10.prod.outlook.com ([fe80::ff03:d749:df80:38e1%7]) with mapi id 15.20.5746.028; Thu, 27 Oct 2022 19:58:17 +0000 From: Nick Alcock To: mcgrof@kernel.org, rostedt@goodmis.org, masahiroy@kernel.org Cc: linux-trace-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, akpm@linux-foundation.org, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v9 2/8] kbuild: add modules_thick.builtin Date: Thu, 27 Oct 2022 20:57:48 +0100 Message-Id: <20221027195754.100039-3-nick.alcock@oracle.com> X-Mailer: git-send-email 2.38.0.266.g481848f278 In-Reply-To: <20221027195754.100039-1-nick.alcock@oracle.com> References: <20221027195754.100039-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P265CA0001.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:62::13) To DS0PR10MB6798.namprd10.prod.outlook.com (2603:10b6:8:13c::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS0PR10MB6798:EE_|BL3PR10MB6042:EE_ X-MS-Office365-Filtering-Correlation-Id: 59044170-0c19-46a8-6cc2-08dab85596ca X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 3pr4bI0c9chPuuG9JX3JIGpeFVF60HYY5LSu/9tt1OQyufGLdKx/jlRcC62ATU7DBQxNUANIybsPr7tkhuHhyoQPK+R96BkTpUnN4P6pOEMwRbCDwCwv3ktn48wqAdx5M5/dYKGvof8oz0K+Z7m0ntSTEH/yRcJzbqJ3ye3l90AEQoAsdW11+7v9h5xnxFXhivZQQiapr3RoLh68eNmOGAPdkQR3qfSliQUB1NLCE+B79aWNIGa6yYFfO1YwLWUxbJ2ZYNF+7FHqlrg4B8qJkGIAb1XEYNX05ttAzV5ZYeBbvzZa7U5ut/EvoqcI8TrYJDaG4gSVS6Mpqd23xdHeJ570Q8AxZTgfkyxjQZBfN8xKGgeeRcZJdGGiOLZTxUsS0qYkqR8KVx3Z7QK0O+eA/5TQ6soSrWtYy24vZTY2KK9XRZNV1z035V5P7CVVXoJo2kVpit4sejZZySW6l0gO7/Lh6Us6SyEPgaeaFxj4ljmpYN96XS1tijHTGKc/Hh95Yo3eYHAlXyC630OumdGnnLz+4QoSODMBJql/TJQgEPDWDZffIAvKcyV6SkWKwyMnTQEi4+mbJGiJCOKTPsgn5bqn4x6vn8ulnxm/3lr/er/JPQ3RYh2QlGSlXphuwdR5u4+BQ3XN7rYb4dPMSgzHta0aeBs5k+JZ9J3p9iwXanrYUj/ssIFzfgRG8ZVMfrKZyzD4eb+l8WMLBJGBC+v4R7ndhLuyEdW2v9isaGXlrXhmzarDKbk6s8UbZ5pTGqU3 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS0PR10MB6798.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(39860400002)(366004)(376002)(346002)(136003)(396003)(451199015)(83380400001)(41300700001)(478600001)(66476007)(6506007)(6486002)(8936002)(316002)(6666004)(30864003)(5660300002)(2616005)(4326008)(66946007)(36756003)(2906002)(6512007)(66556008)(86362001)(107886003)(44832011)(8676002)(186003)(1076003)(38100700002)(2004002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 5fE7nt3mWh99CnWRIOdD/qMD+zX1f4xEj7+FRVuhli+Heb5+okFWrdlCjpzjE/y/lVfbW8vXQrbWYaO6C8MK00IICJq4sfPfpc6fWeXU5/8ElVV0yJHDS2Zb71E16nnPPKXvStO8R4Hx2CddMqH033NUQQioXbqX94/kroY4rB3IXxQoAPvbSjZw/m5Po5KPXJbcZdw+jCvqepP4rlsVXoa14wTSa9Y/fYht4X8Eg8QONkHNwqCe/4rklAZiQIqGo4KsqolRnbLtXCDrGU1phDZOnaYoHkD4YSM2IdHFAJHVW7XxkxEogptlqFYip1zzslNmVRVA+ULUuuZDdc3BcD69o5YsCV7TsF2aFQECRYexYf/LlGP4bXQAY8EahUXSZC+VrjMleXJo1i1GIiiMtTAgfr2QYi7L4hw9OGnAwujq0H0ATgICH5pjTyvmfWJ5WwiAuNV/38i+bOpUfX8ge4LckjDrBXqvA1rrLonKvIV7xIYcByQsntO6Wfg4VIqAzwxZx79Y2MeeMI4O2P4yvBU4/lrUjb7IB/CUaPKPU+eT2qn9EwFFj2P4QSufQmdcAXtDrPbZXFSd2D7IUdM3wi62nNIPcsUC/Ivg4LpgErhFIMzE2+YSKS8Jtde6NFSEm7m8tA3f5+/helxdclwLBzbil29F3G/OFzjDLAVUu7nShlmkPZWupOFZ2opstuXU3tBJd6a/HUJ8lkqNwCYSGQ3DtokUxxJvQ4YjkBEPuu2PDrHSpKzCOQf294QmesZ7WIgJXNGMLrRl81IDHcCeZ4izT+Ktb+tHevQWK566psHCPY1eoebx5uKvVykpFagKqDIkcqJJUuDyMp7zQTI7GwgZmTh6163En5RRKA5LM+M5dcxi4O2hg2Vz+jSbo1d4HVI345I+f69YFkgzDCaBTJpqhma68puC1H7iiBKkUXUyfxAigEEY/fpryc9nLy3BfKON/limB86102U/31Qb+UYUOnJbRGjMM0kEvsGpEqr001P9N7laIWrUpADybNwQMmqWWi+Q5dg1FTcVcafKfgNbf6DdIB43+Eyq0MrHRpgMZpjTs0CfuieJMc5he7yhsTykeaSyBpq6e+m9AtLKX7SGbttaUB3bzEY4d6aAeEaHudDvRqMsYtQm38vF5p8I7MyErLf89YyV/d6PkP+nqcPVDVxBZNgHT8PNav6KSXIn+eimxm/EpqHtxekhDgCNhcMt46cpy4Ifyx+mMKAPY9XiCsAVmXUPBp5eGTuqAzzwOiCQcT3uT6A7WU+p0s2J3QE8/wEVfYKG0pvDHFpEt2eWvo6uFKCIAy+oila4RUJ22QOdLKOwte+KDIq45Dnxpo3T4xwYKHNIemadMGbalTKqtvZAVXd0PyFaK0dJHFVCkzWoF8oNjQwC4+9IqSSFDgxDAdxEm7crtkh0pso+wvLYtkX3gBB/1dpNuSHPp1p5xI4cGsN/vOi5TVjLQKvTI85/OEJekKXrPkhEU0g1TtmBdP3adoBTHyEb3xipGD0dyCvQlwxmW5w3GzTrPD6nk1uehoi6JsKm7xhR/LeLG+gXUSJgVD28QD6+CF/d8DPlNbitPrgiK59R7LCiZgggGE6Rp7sT9QXlsRaLhdlNvg== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 59044170-0c19-46a8-6cc2-08dab85596ca X-MS-Exchange-CrossTenant-AuthSource: DS0PR10MB6798.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Oct 2022 19:58:16.9176 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: J9k/vYNvxt+8opKnGZkTdhOiUKDLHvKoM3Da/YkrrX0T7z2ivcaj/In+PLlb/f0olXUjL841zhMXkjlr5lLVTA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR10MB6042 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-10-27_07,2022-10-27_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 mlxscore=0 mlxlogscore=999 spamscore=0 phishscore=0 bulkscore=0 malwarescore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2210270112 X-Proofpoint-GUID: pbMprv2Kh78r1IYdRthWe2SPiCtFaAEM X-Proofpoint-ORIG-GUID: pbMprv2Kh78r1IYdRthWe2SPiCtFaAEM Precedence: bulk List-ID: X-Mailing-List: linux-trace-kernel@vger.kernel.org This is similar to modules.builtin, and constructed in a similar way to the way that used to be built before commit 8b41fc4454e36fbfdbb23f940d023d4dece2de29, via tristate.conf inclusion and recursive concatenation up the tree. Unlike modules.builtin, modules_thick.builtin gives the names of the object files that make up modules that are comprised of more than one object file, using a syntax similar to that of makefiles, e.g.: crypto/crypto.o: crypto/api.o crypto/cipher.o crypto/compress.o crypto/memneq.o crypto/crypto_algapi.o: crypto/algapi.o crypto/proc.o crypto/scatterwalk.o crypto/aead.o: crypto/geniv.o: (where the latter two are single-file modules). An upcoming commit will use this mapping to populate /proc/kallmodsyms. A parser is included that yields a stram of (module, objfile name[]) mappings: it's a bit baroque, but then parsing text files in C is quite painful, and I'd rather put the complexity in here than in its callers. The parser is not built in this commit, nor does it have any callers yet; nor is any rule added that causes modules_thick.builtin to actually be constructed. (Again, see a later commit for that.) I am not wedded to the approach used to construct this file, but I don't see any other way to do it despite spending a week or so trying to tie it into Kbuild without using a separate Makefile.modbuiltin: unlike the names of builtin modules (which are also recorded in the source files themseves via MODULE_*() macros) the mapping from object file name to built-in module name is not recorded anywhere but in the makefiles themselves, so we have to at least reparse them with something to indicate the builtin-ness of each module (i.e., tristate.conf) if we are to figure out which modules are built-in and which are not. Signed-off-by: Nick Alcock Reviewed-by: Kris Van Hees --- Notes: v9: move modules_thick.builtin generation into the top-level Kbuild .gitignore | 1 + Documentation/dontdiff | 1 + Kbuild | 16 +++ Makefile | 2 +- scripts/Kbuild.include | 6 ++ scripts/Makefile.modbuiltin | 56 ++++++++++ scripts/modules_thick.c | 200 ++++++++++++++++++++++++++++++++++++ scripts/modules_thick.h | 48 +++++++++ 8 files changed, 329 insertions(+), 1 deletion(-) create mode 100644 scripts/Makefile.modbuiltin create mode 100644 scripts/modules_thick.c create mode 100644 scripts/modules_thick.h diff --git a/.gitignore b/.gitignore index 5da004814678..f129bf52cbd4 100644 --- a/.gitignore +++ b/.gitignore @@ -52,6 +52,7 @@ *.zst Module.symvers modules.order +modules_thick.builtin # # Top-level generic files diff --git a/Documentation/dontdiff b/Documentation/dontdiff index 352ff53a2306..077d43b9675d 100644 --- a/Documentation/dontdiff +++ b/Documentation/dontdiff @@ -183,6 +183,7 @@ modules.builtin modules.builtin.modinfo modules.nsdeps modules.order +modules_thick.builtin modversions.h* nconf nconf-cfg diff --git a/Kbuild b/Kbuild index 464b34a08f51..a84e8312c174 100644 --- a/Kbuild +++ b/Kbuild @@ -97,3 +97,19 @@ obj-$(CONFIG_SAMPLES) += samples/ obj-$(CONFIG_NET) += net/ obj-y += virt/ obj-y += $(ARCH_DRIVERS) + +# Generate modules_thick.builtin if needed. +# +# modules_thick.builtin maps from kernel modules (or rather the object file +# names they would have had had they not been built in) to their constituent +# object files: we can use this to determine which modules any given object +# file is part of. (We cannot eliminate the slight redundancy here without +# double-expansion.) + +modthickbuiltin-files := $(addsuffix modules_thick.builtin, $(filter %/,$(obj-y))) + +$(modthickbuiltin-files): include/config/tristate.conf + $(Q)$(MAKE) $(modbuiltin)=$(patsubst %/modules_thick.builtin,%,$@) builtin-file=modules_thick.builtin + +modules_thick.builtin: $(modthickbuiltin-files) $(obj-y) + $(Q)$(AWK) '!x[$$0]++' $(patsubst %/built-in.a, %/$@, $(filter %/built-in.a,$(obj-y))) > $@ diff --git a/Makefile b/Makefile index 5d26447fecb8..21117f9d4202 100644 --- a/Makefile +++ b/Makefile @@ -2008,7 +2008,7 @@ clean: $(clean-dirs) -o -name '*.lex.c' -o -name '*.tab.[ch]' \ -o -name '*.asn1.[ch]' \ -o -name '*.symtypes' -o -name 'modules.order' \ - -o -name '.tmp_*' \ + -o -name '.tmp_*' -o -name modules_thick.builtin \ -o -name '*.c.[012]*.*' \ -o -name '*.ll' \ -o -name '*.gcno' \ diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include index 2bc08ace38a3..28d5eb7a9b61 100644 --- a/scripts/Kbuild.include +++ b/scripts/Kbuild.include @@ -78,6 +78,12 @@ endef # $(Q)$(MAKE) $(build)=dir build := -f $(srctree)/scripts/Makefile.build obj +### +# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.modbuiltin obj= +# Usage: +# $(Q)$(MAKE) $(modbuiltin)=dir +modbuiltin := -f $(srctree)/scripts/Makefile.modbuiltin obj + ### # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.dtbinst obj= # Usage: diff --git a/scripts/Makefile.modbuiltin b/scripts/Makefile.modbuiltin new file mode 100644 index 000000000000..a27b692ea795 --- /dev/null +++ b/scripts/Makefile.modbuiltin @@ -0,0 +1,56 @@ +# SPDX-License-Identifier: GPL-2.0 +# ========================================================================== +# Generating modules_thick.builtin +# ========================================================================== + +src := $(obj) + +PHONY := __modbuiltin +__modbuiltin: + +include include/config/auto.conf +# tristate.conf sets tristate variables to uppercase 'Y' or 'M' +# That way, we get the list of built-in modules in obj-Y +include include/config/tristate.conf + +include scripts/Kbuild.include + +ifdef building_out_of_srctree +# Create output directory if not already present +_dummy := $(shell [ -d $(obj) ] || mkdir -p $(obj)) +endif + +# The filename Kbuild has precedence over Makefile +kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src)) +kbuild-file := $(if $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Kbuild,$(kbuild-dir)/Makefile) +include $(kbuild-file) + +include scripts/Makefile.lib + +modthickbuiltin-subdirs := $(patsubst %,%/modules_thick.builtin, $(subdir-ym)) +modthickbuiltin-target := $(obj)/modules_thick.builtin + +__modbuiltin: $(obj)/$(builtin-file) $(subdir-ym) + @: + +$(modthickbuiltin-target): $(subdir-ym) FORCE + $(Q) rm -f $@ + $(Q) $(foreach mod-o, $(filter %.o,$(obj-Y)),\ + printf "%s:" $(addprefix $(obj)/,$(mod-o)) >> $@; \ + printf " %s" $(sort $(strip $(addprefix $(obj)/,$($(mod-o:.o=-objs)) \ + $($(mod-o:.o=-y)) $($(mod-o:.o=-Y))))) >> $@; \ + printf "\n" >> $@; ) \ + cat /dev/null $(modthickbuiltin-subdirs) >> $@; + +PHONY += FORCE + +FORCE: + +# Descending +# --------------------------------------------------------------------------- + +PHONY += $(subdir-ym) +$(subdir-ym): + $(Q)$(MAKE) $(modbuiltin)=$@ builtin-file=$(builtin-file) + +.PHONY: $(PHONY) diff --git a/scripts/modules_thick.c b/scripts/modules_thick.c new file mode 100644 index 000000000000..9a15e99c1330 --- /dev/null +++ b/scripts/modules_thick.c @@ -0,0 +1,200 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * A simple modules_thick reader. + * + * (C) 2014, 2021 Oracle, Inc. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#include +#include +#include +#include + +#include "modules_thick.h" + +/* + * Read a modules_thick.builtin file and translate it into a stream of + * name / module-name pairs. + */ + +/* + * Construct a modules_thick.builtin iterator. + */ +struct modules_thick_iter * +modules_thick_iter_new(const char *modules_thick_file) +{ + struct modules_thick_iter *i; + + i = calloc(1, sizeof(struct modules_thick_iter)); + if (i == NULL) + return NULL; + + i->f = fopen(modules_thick_file, "r"); + + if (i->f == NULL) { + fprintf(stderr, "Cannot open builtin module file %s: %s\n", + modules_thick_file, strerror(errno)); + return NULL; + } + + return i; +} + +/* + * Iterate, returning a new null-terminated array of object file names, and a + * new dynamically-allocated module name. (The module name passed in is freed.) + * + * The array of object file names should be freed by the caller: the strings it + * points to are owned by the iterator, and should not be freed. + */ + +char ** __attribute__((__nonnull__)) +modules_thick_iter_next(struct modules_thick_iter *i, char **module_name) +{ + size_t npaths = 1; + char **module_paths; + char *last_slash; + char *last_dot; + char *trailing_linefeed; + char *object_name = i->line; + char *dash; + int composite = 0; + + /* + * Read in all module entries, computing the suffixless, pathless name + * of the module and building the next arrayful of object file names for + * return. + * + * Modules can consist of multiple files: in this case, the portion + * before the colon is the path to the module (as before): the portion + * after the colon is a space-separated list of files that should be + * considered part of this module. In this case, the portion before the + * name is an "object file" that does not actually exist: it is merged + * into built-in.a without ever being written out. + * + * All module names have - translated to _, to match what is done to the + * names of the same things when built as modules. + */ + + /* + * Reinvocation of exhausted iterator. Return NULL, once. + */ +retry: + if (getline(&i->line, &i->line_size, i->f) < 0) { + if (ferror(i->f)) { + fprintf(stderr, "Error reading from modules_thick file:" + " %s\n", strerror(errno)); + exit(1); + } + rewind(i->f); + return NULL; + } + + if (i->line[0] == '\0') + goto retry; + + /* + * Slice the line in two at the colon, if any. If there is anything + * past the ': ', this is a composite module. (We allow for no colon + * for robustness, even though one should always be present.) + */ + if (strchr(i->line, ':') != NULL) { + char *name_start; + + object_name = strchr(i->line, ':'); + *object_name = '\0'; + object_name++; + name_start = object_name + strspn(object_name, " \n"); + if (*name_start != '\0') { + composite = 1; + object_name = name_start; + } + } + + /* + * Figure out the module name. + */ + last_slash = strrchr(i->line, '/'); + last_slash = (!last_slash) ? i->line : + last_slash + 1; + free(*module_name); + *module_name = strdup(last_slash); + dash = *module_name; + + while (dash != NULL) { + dash = strchr(dash, '-'); + if (dash != NULL) + *dash = '_'; + } + + last_dot = strrchr(*module_name, '.'); + if (last_dot != NULL) + *last_dot = '\0'; + + trailing_linefeed = strchr(object_name, '\n'); + if (trailing_linefeed != NULL) + *trailing_linefeed = '\0'; + + /* + * Multifile separator? Object file names explicitly stated: + * slice them up and shuffle them in. + * + * The array size may be an overestimate if any object file + * names start or end with spaces (very unlikely) but cannot be + * an underestimate. (Check for it anyway.) + */ + if (composite) { + char *one_object; + + for (npaths = 0, one_object = object_name; + one_object != NULL; + npaths++, one_object = strchr(one_object + 1, ' ')); + } + + module_paths = malloc((npaths + 1) * sizeof(char *)); + if (!module_paths) { + fprintf(stderr, "%s: out of memory on module %s\n", __func__, + *module_name); + exit(1); + } + + if (composite) { + char *one_object; + size_t i = 0; + + while ((one_object = strsep(&object_name, " ")) != NULL) { + if (i >= npaths) { + fprintf(stderr, "%s: num_objs overflow on module " + "%s: this is a bug.\n", __func__, + *module_name); + exit(1); + } + + module_paths[i++] = one_object; + } + } else + module_paths[0] = i->line; /* untransformed module name */ + + module_paths[npaths] = NULL; + + return module_paths; +} + +/* + * Free an iterator. Can be called while iteration is underway, so even + * state that is freed at the end of iteration must be freed here too. + */ +void +modules_thick_iter_free(struct modules_thick_iter *i) +{ + if (i == NULL) + return; + fclose(i->f); + free(i->line); + free(i); +} diff --git a/scripts/modules_thick.h b/scripts/modules_thick.h new file mode 100644 index 000000000000..f5edcaf9550c --- /dev/null +++ b/scripts/modules_thick.h @@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * A simple modules_thick reader. + * + * (C) 2014, 2021 Oracle, Inc. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#ifndef _LINUX_MODULES_THICK_H +#define _LINUX_MODULES_THICK_H + +#include +#include + +/* + * modules_thick.builtin iteration state. + */ +struct modules_thick_iter { + FILE *f; + char *line; + size_t line_size; +}; + +/* + * Construct a modules_thick.builtin iterator. + */ +struct modules_thick_iter * +modules_thick_iter_new(const char *modules_thick_file); + +/* + * Iterate, returning a new null-terminated array of object file names, and a + * new dynamically-allocated module name. (The module name passed in is freed.) + * + * The array of object file names should be freed by the caller: the strings it + * points to are owned by the iterator, and should not be freed. + */ + +char ** __attribute__((__nonnull__)) +modules_thick_iter_next(struct modules_thick_iter *i, char **module_name); + +void +modules_thick_iter_free(struct modules_thick_iter *i); + +#endif From patchwork Thu Oct 27 19:57:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 13022679 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 EC1C3FA3746 for ; Thu, 27 Oct 2022 19:58:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236803AbiJ0T6n (ORCPT ); Thu, 27 Oct 2022 15:58:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236804AbiJ0T6k (ORCPT ); Thu, 27 Oct 2022 15:58:40 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2731788A32; Thu, 27 Oct 2022 12:58:30 -0700 (PDT) Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 29RHmxuT019842; Thu, 27 Oct 2022 19:58:24 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2022-7-12; bh=aV9EStDlUFiVpRAMrN0YzrYdZBeYHzIysbC7g5GURjs=; b=HgFNgAk0y2tVKS3NI402PhIkDE4KblsoKzkm9zoMRMtZbmurv8YefPBHLBlisMp5Ygzu Pw0/w7/LUabxyv3PDCqIcbd7GzfBabkKUlMhxYqwCaIJ+uYfx9BpaGpJOnOcsibXZYv0 0GwxIMgcXWpjtf8XFZo3viZ8ozUkdrdx3A0Z7mXQa9zJJwLtYGjR5JSwiTBGR5V4P82+ h4Md8/D+Q8ajg9I0fH6ixSNHDkCyVLelJqky5wPDRSoRGfyWJbguqgPH/GgbOAep/8yp x+7do9dsj2ThYk9aq00HEnMKzGade+NZSeJ586KPNf4U6i0+HJTK2UDNF4c4uvkViqy5 LQ== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3kfax7u30e-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 27 Oct 2022 19:58:24 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 29RIZSAA006561; Thu, 27 Oct 2022 19:58:23 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2173.outbound.protection.outlook.com [104.47.57.173]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3kfaghb17d-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 27 Oct 2022 19:58:23 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=h/svT/OtwozeGFSY6LJyEFMflZK6vkRdMJxCkYOVnr4P+Q3sJn1x6wFrUUcC3j5ygyB3DudywKzlvghtJrGetxBwF/538typzRoweVCtMCaH23lYhhEWvWWur8mcejmQmNH3uebWK0ga/eSAoE1Pmgd6WO1Gp/Lex8ZD54BJYjq0aoGGHqypw3fts5x56bmiefRRFEwsFXS3P8desSaLGR98+n8FERt6+sAiHMcZdpMxilzpuM6j61J7oEwKOdMlT24qQYj/t4a4dWU577yxd7QKdsa3PUaQPwjDKGPkKwQQeU35J9RBb4WlIO38QsZ+aupo+Cwgq1UvVaVspsD0Iw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=aV9EStDlUFiVpRAMrN0YzrYdZBeYHzIysbC7g5GURjs=; b=MVbU0YygvqGywF8rlpKk7poVrDp3qGTFAKi11r8fB3b3keeXefYPfiesYrri+Yf+BlehUSTiKmtYHWa5zYQ9EQk8UcWvzrXyEs+L+WjokxDqvbfYCiKuKvmmokVYxbfuD+EzAXoxJG/Ov+JSQMSj0W68jHNGyqAyzC+iRnwF0nk2QGOv1a5dqQ/ohRnFtHNwjIp8PBHycO3eNpCyqsXrkAqaaxGeHEefl5fa5/JIAndWNRwj4wHc1czNHJ5GmmtBV0v1kVMm/lwjalggMl5Y8pyRko5/wqWYut0DgJgWIaSk7qinqfhQ0h5qvjIAHOEK+6ki3Ian8r3L460ZAn7A3Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=aV9EStDlUFiVpRAMrN0YzrYdZBeYHzIysbC7g5GURjs=; b=U1TfqlGeuIGMGxzRn1HTeywkSqEqTfE71nj/FG0M3wM65uoZdkr8O0I6mCOYtVeBfegmiuT8bFJVp25nJz6/i0aODVM52PfWXJkwG1c8nEDbtKMkrbOzaBSHr0fFZ5+AmYdA+TvPI9atIZTQxgnw3v5w2UhhMJC76X27H9ZczVg= Received: from DS0PR10MB6798.namprd10.prod.outlook.com (2603:10b6:8:13c::20) by BL3PR10MB6042.namprd10.prod.outlook.com (2603:10b6:208:3b2::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.24; Thu, 27 Oct 2022 19:58:21 +0000 Received: from DS0PR10MB6798.namprd10.prod.outlook.com ([fe80::ff03:d749:df80:38e1]) by DS0PR10MB6798.namprd10.prod.outlook.com ([fe80::ff03:d749:df80:38e1%7]) with mapi id 15.20.5746.028; Thu, 27 Oct 2022 19:58:21 +0000 From: Nick Alcock To: mcgrof@kernel.org, rostedt@goodmis.org, masahiroy@kernel.org Cc: linux-trace-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, akpm@linux-foundation.org, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v9 3/8] kbuild: generate an address ranges map at vmlinux link time Date: Thu, 27 Oct 2022 20:57:49 +0100 Message-Id: <20221027195754.100039-4-nick.alcock@oracle.com> X-Mailer: git-send-email 2.38.0.266.g481848f278 In-Reply-To: <20221027195754.100039-1-nick.alcock@oracle.com> References: <20221027195754.100039-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO4P265CA0169.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:312::6) To DS0PR10MB6798.namprd10.prod.outlook.com (2603:10b6:8:13c::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS0PR10MB6798:EE_|BL3PR10MB6042:EE_ X-MS-Office365-Filtering-Correlation-Id: a670a0db-a62b-4dbc-be0e-08dab8559936 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: dNG75im7d9OgLSMnrqVEUXpHGmbRSb54H41Kr3NFrqzkXhBJaCwE9ozGyFKnkc5NAKvFfTru8bShAboLe0vvC0bsUm5M3P7EBVN+Vc8wTdJa9i3fkgteQbooNNboi8ISznompBu8leVLiUOwR1ffsl5cozTKlGfKEImQhb14FJGT8gyTztSlVDGghNZrZCwaOnhHS4rirYLtSKJnUJ7K/oHmRcKqYj5Gojm84K29aAk8uiJgZHCKN4Sb55l8IJx2GM7pjVCusaUfmvw3PJsDDy8Lo93774i+zkcWN4g3/+iRzdwjFIX05dOYyS9VPpbBiJ8MU3DIXdC27FUeqDfDd0JBYfMjjEmv9YCQR7mIcDCD2vAdf7SHh7oB4njbRGUuLeO2NibJ3Kx1o76FMdGZH5Ks0a/Bzoe6y3BmSZm5jsA7tXE+bTb2L1MtK2pG2ItxuauHAx2AEUpiqZt9gAjWjLSyTh/6h993YNeFBhhg9CERVGx5TAJFDR4PPdUqc8xwKfBu+oCFdM1GR6atWAckFd52PYndRtRpOtr4ds7V6VyYuxrH7PGbpZS3/2xkJrw/U79Js0rkvKhgNzTccatz4EeySx1F5ak0EUutn6SZ0TX4lzkUaxQZj0LETB++K7froJUyeeZM003e8YBSCdk34Mq91gkggud1SBwCAahTz5ByIknwHwBM3iudo0mT91M8qKoF0UmJcybRgoHa3g60Ag== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS0PR10MB6798.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(39860400002)(366004)(376002)(346002)(136003)(396003)(451199015)(83380400001)(41300700001)(478600001)(66476007)(6506007)(6486002)(8936002)(316002)(6666004)(5660300002)(2616005)(4326008)(66946007)(36756003)(2906002)(6512007)(66556008)(86362001)(107886003)(44832011)(8676002)(186003)(1076003)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: M4mizavXVZYbWvcW5troWGZN8FS9bTY5tdhJCxRqWjCKJxn+7asWrKPh0Beg/8Cc+Q3O1D+XJIL6KFtCIVYuZy7qqL1UIjGliKHkZbyaHIMH8dH+4AYN4LHbR7vRDxbSq6JYDSNldoqjqSEJtmcxU7BWQGxVAWCxBTggfRfrXgca6bcuIWoWuvEFrQXOIoOfvTgQUxwLBoVcuQQPQMnWF5xmUqvt18w+nz+jw86Kz38I6FWe74yne3qqnmmEP8TbNhU2g4tqdcYZnRx1nw+VyWB0AGBR0m5X0YJfAGHmCBZqSFQj5/Jl3d2Xi1UCwJB9CdpCsgTEmCqfoyrpXRHI9NwxzyGGc9/ts0ZrOm1m8J77/C+L6pbFqL+YicVkllrv/iYWxLqkBnT7oWnynQJL0GmwZ3tjinqPGB8zSjlTVvmH51WqFcl7RavT9DSjASFTDR6Hfxhd8Lz/4ev8jC/qXa/5aVETHN6/LWzjT7LW7RfVC/7KN5AqU24bRTuZF8cTKiJo7wAr3cjr7DSRCHRftEZVthlHbw38Ovru8hs4fWsOUrlYFv91AgogXC+iMu5429q6KcsF5s0ElF3LVfBI2L1Zs/j3x9zJkcjr+sRGzlqkvYj2uiiJMAWZ3NulSvPFII+1SzwDXvVALut+HalYA0INkxKHEQH9ZJqYGumycZ8RxJBlV1Bn3vqB1BevSWqZtQNcWS05DqCaPNE090EMolKLplciquQWXPrLP33bo70KMtqOHNGk+w8Ep9Wgokbxkv/agDefoDK+J6oVij7SqIRSdTs/uSNEXB7AVwfTrhuRFo8h3pRNaKzm/jCO60fyz0qajzrPyoRJwIqzjyslD8geDCtRpNlR1jNXaiLW911b6jODdXJCbD/Yg04CcRHCEa3Uq+01Ye2wJrDZvTqoJA3SCJZSZnoWcqY60xO0/qE7XU7ufoIqPdG3B2jaPKTtOrUJVbkHCyrfrARM29PHuOeO7Vc+JIrllPUYUAMEz8vhQom5t0y4QMCicMfduvxBV97p9ouMjYzXGUS5kWNu8OIKWyDZSD9YR6dIRxTg1wUFf5kRXLIAxF+Qt8PbuyDpX5vyk73sh3TRfokX8lfRW194QNqEaJNPIIN83bFKeU2Zodn8GU66wNWj91E6aygMhudQunzjBpUUQOLiwnhwu7cyjTgGf0QiulyEd4T6nJjPhqM+AM/zX46BMEx/qyonWlWWtTSwAOItPBalL4IgwRihjtDoX9etCLHMkEHav6Fo4EDd2YyHTPNMoIWEiyc+jBTTgaXNhMGwivt4Ovsi71JuMukrmZ9hFvNZpCfInNJcspcA8M0tgEIUWV8NRn07D7oHc4ti4S40Akw8GYkCSGqXtTGQBYXOjZTyy/y2y8ui3B3MhOZdz9GtA7ZJZ7YKtR2Jv5rzJ6Gy7Kamcs4E1cL92nPAfF7NprMmkwoWIpoe38g2edcGJrjhdNPkkWnMLTSFjbMqBYvAfVsIqZIVdNpwI8mCjW3bsQ2KQyeOGTBsJxRtcNF08GuSIrjmmae+VRApZq1L+gZwPy2DxxrPBywSb3Auy0/lDD+7X3aPV/Woh+PuUU5P8QdbH+EVn5qk6PdTxeSNB0CKsGoT9pl3Xw== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: a670a0db-a62b-4dbc-be0e-08dab8559936 X-MS-Exchange-CrossTenant-AuthSource: DS0PR10MB6798.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Oct 2022 19:58:21.0454 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: zSaqiuo//QvR0OyLsd9gCfqsRSn02DgadPjuPEzv4Ui4cJ4Q5mJQmIk1YuA0n53slX8yR/R3+ufHKH9VuDIHbg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR10MB6042 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-10-27_07,2022-10-27_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 spamscore=0 suspectscore=0 phishscore=0 adultscore=0 mlxscore=0 mlxlogscore=999 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2210270112 X-Proofpoint-GUID: xw32ieTS-9rGNQZnWGHFSMwtENveWB-h X-Proofpoint-ORIG-GUID: xw32ieTS-9rGNQZnWGHFSMwtENveWB-h Precedence: bulk List-ID: X-Mailing-List: linux-trace-kernel@vger.kernel.org This emits a new file, .tmp_vmlinux.ranges, which maps address range/size pairs in vmlinux to the object files which make them up, e.g., in part: 0x0000000000000000 0x30 arch/x86/kernel/cpu/common.o 0x0000000000001000 0x1000 arch/x86/events/intel/ds.o 0x0000000000002000 0x4000 arch/x86/kernel/irq_64.o 0x0000000000006000 0x5000 arch/x86/kernel/process.o 0x000000000000b000 0x1000 arch/x86/kernel/cpu/common.o 0x000000000000c000 0x5000 arch/x86/mm/cpu_entry_area.o 0x0000000000011000 0x10 arch/x86/kernel/espfix_64.o 0x0000000000011010 0x2 arch/x86/kernel/cpu/common.o [...] In my simple tests this seems to work with clang too, but if I'm not sure how stable the format of clang's linker mapfiles is: if it turns out not to work in some versions, the mapfile-massaging awk script added here might need some adjustment. Signed-off-by: Nick Alcock Reviewed-by: Kris Van Hees --- Notes: v6: use ${wl} where appropriate to avoid failure on UML scripts/link-vmlinux.sh | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh index 918470d768e9..287a2b2c4d46 100755 --- a/scripts/link-vmlinux.sh +++ b/scripts/link-vmlinux.sh @@ -101,7 +101,7 @@ vmlinux_link() ${ld} ${ldflags} -o ${output} \ ${wl}--whole-archive ${objs} ${wl}--no-whole-archive \ ${wl}--start-group ${libs} ${wl}--end-group \ - $@ ${ldlibs} + ${wl}-Map=.tmp_vmlinux.map $@ ${ldlibs} } # generate .BTF typeinfo from DWARF debuginfo @@ -144,6 +144,19 @@ kallsyms() { local kallsymopt; + # read the linker map to identify ranges of addresses: + # - for each *.o file, report address, size, pathname + # - most such lines will have four fields + # - but sometimes there is a line break after the first field + # - start reading at "Linker script and memory map" + # - stop reading at ".brk" + ${AWK} ' + /\.o$/ && start==1 { print $(NF-2), $(NF-1), $NF } + /^Linker script and memory map/ { start = 1 } + /^\.brk/ { exit(0) } + ' .tmp_vmlinux.map | sort > .tmp_vmlinux.ranges + + # get kallsyms options if is_enabled CONFIG_KALLSYMS_ALL; then kallsymopt="${kallsymopt} --all-symbols" fi From patchwork Thu Oct 27 19:57:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 13022681 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 71E32FA3744 for ; Thu, 27 Oct 2022 19:58:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236818AbiJ0T6p (ORCPT ); Thu, 27 Oct 2022 15:58:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236817AbiJ0T6l (ORCPT ); Thu, 27 Oct 2022 15:58:41 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4DD4588DC0; Thu, 27 Oct 2022 12:58:36 -0700 (PDT) Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 29RHmxqB019860; Thu, 27 Oct 2022 19:58:29 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2022-7-12; bh=7hNpr2jAHme90wG89ywYaxbkWPRr1q/sz64Wtpn03MQ=; b=GJ/XyybDWHoT0ZdN9zP44bfmMqjTfyIxNAOZG8Sn7Iynm1gWs7VDsvXP8xm1Bdk6uMQc T110fUOXYMHo6VJs3qm6VLSuNMvi13n52++mhwJ/LHKU7TJH3VSWHGibb99uJ48xjkRq TUszh35U5TXnwcgAYdxGfCfVZizx4NAcR5nuxREpZwEfl391YJbff5r/dkO1MslGW6MB GO91jfHzoAV8d0DgLA0gP50zACXNmw37EBWDHypm4DyB/Gj5EvZX5QZAEi0yEMG3xYrk 5q7zKnxu4/YTDb87ONq/FaQ5G4zM36kIAFyVJknQ0BfuLZIy7jbkoEYXi8G9BmRWcz4a Ww== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3kfax7u30k-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 27 Oct 2022 19:58:28 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 29RIYxhJ006786; Thu, 27 Oct 2022 19:58:27 GMT Received: from nam04-dm6-obe.outbound.protection.outlook.com (mail-dm6nam04lp2043.outbound.protection.outlook.com [104.47.73.43]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3kfaghb194-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 27 Oct 2022 19:58:27 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Vi9ie9blxQ4SBrMgpPI1/ADOtRNAfF+UAP6/FjeMPuQ4oXuHT7t43MxOq/Yje3xkPXiW7QkHKKS1MB/zocJHv98btaDWMEUXFDFpawhE8U1CYq5Pr06YXvkFAIWk7vkI7gUlS3A/apa5mHefQlYcc8hWKNHkBgBWt355CILxc1CLQUUBF831yBhT+j5MVdv37XDo6HHCioSzMndNsYPa32aiDSeEiK5ZH8REKuG6UvHSyK4qpEaMH6YlwT8O2WX53psw0PICRKWWAqJBxRejzxiCp06RrKFNebJqZjpOxracwuB3EFbgfYvdKvXjyhBpe9CUZlrYOjMQRhdhRoP47w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=7hNpr2jAHme90wG89ywYaxbkWPRr1q/sz64Wtpn03MQ=; b=kifMZNMPShUab8reZZNCg0c09qYddtBd21HB5mgodVC85j7kSfNKj9ZjjnUnwpsb6nDNZaxY5nM9wZBZI46SMvr9EJJtRvZhvKHMzPzac156dnSLPNFojT5bTRE/2GyD6vXXLo9DsC81AmuS/LgN6xAVur0uTZBBPFkTZp+i0Ti9NuiUUrDWj9cdx7f07IBuSTwzP7FjzSGNjpGJWyN41lBeCg2GJYItTtPzyOKhdKMubaD/9LDyqamRFz4JG35CIJ4Y4TNyvd/STRYjXBn2Nv5GJanPwiCSaXgE7GJAc0ssXKA/FLbaSvWD429BWlTB14FDzGoDj92hHqAef8l1hg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=7hNpr2jAHme90wG89ywYaxbkWPRr1q/sz64Wtpn03MQ=; b=punZCtka2nBZsfX4Ga75kK5/s8AAcKtR5O4H6Ms6SWBnQTzU8zPCP5uqwxa2IZ79zlEoN81NUPamkZSVM3k3rhk4QEtMna9xUN8Vi0g3Jf0W4/F3Leap1jbDeEYIwWOTmlXjIeM6EE9cjY+iueYzBt3lsQ+Pb10ijQNgCAGzQp8= Received: from DS0PR10MB6798.namprd10.prod.outlook.com (2603:10b6:8:13c::20) by BL3PR10MB6042.namprd10.prod.outlook.com (2603:10b6:208:3b2::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.24; Thu, 27 Oct 2022 19:58:25 +0000 Received: from DS0PR10MB6798.namprd10.prod.outlook.com ([fe80::ff03:d749:df80:38e1]) by DS0PR10MB6798.namprd10.prod.outlook.com ([fe80::ff03:d749:df80:38e1%7]) with mapi id 15.20.5746.028; Thu, 27 Oct 2022 19:58:25 +0000 From: Nick Alcock To: mcgrof@kernel.org, rostedt@goodmis.org, masahiroy@kernel.org Cc: linux-trace-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, akpm@linux-foundation.org, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v9 4/8] kallsyms: introduce sections needed to map symbols to built-in modules Date: Thu, 27 Oct 2022 20:57:50 +0100 Message-Id: <20221027195754.100039-5-nick.alcock@oracle.com> X-Mailer: git-send-email 2.38.0.266.g481848f278 In-Reply-To: <20221027195754.100039-1-nick.alcock@oracle.com> References: <20221027195754.100039-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P265CA0316.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a4::16) To DS0PR10MB6798.namprd10.prod.outlook.com (2603:10b6:8:13c::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS0PR10MB6798:EE_|BL3PR10MB6042:EE_ X-MS-Office365-Filtering-Correlation-Id: 87c53c49-fddf-4e63-3200-08dab8559bb8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Gy5ryFqY/+vIRCschS3FqHhi8QtQ6/brFbgges94poEQx8jaT0yUeg2VxDU3xrrTUmjcznvqrnW6WD6PhG/PI6UY3ncOJp0n1AO5K3oSP0RgYmVHCLOOSYCYBPMpb92GtFuLSQ2jMxXW3G4+2cRs1USL04dfjd0ONs9NbK52Dru5HabLM3W1uiDbwo9AAabe6nqGyHA3aPHLO91akJNMgvxbv+/u2vHns5BmSyyZF0EVBkXfrSSh2Zj+WQRdX4CKnQOdPIuO5n4bMRwm7dIV7Gx9krnWOMMjrGPr0R1a6J3N1aAH5tP+Y6vlUmf3n6jkpR1+HQk6HFgJqFPcOI4ACbfdVG4PaiE2Gcr4rBKGdTSrHT/lAwOj5FEuq+o6quU236jDgHZpbvzGf5PAkPz/YJh4DU5HiZbzvemEyOd0gJ+l8oMbUDhr5/Tng+UEP9m6W8GbjMPJEPBDXZGqKrRFqEu7WsyNE/620XqHGS0t6siexL11+3BEHlUp/9bIUhqPC8S7drGHeW8bUy0y2A68hMWZJMdRLuqtnB468LlMBI95UbFz+DeBlHa+MVpiUj5w7sXa+YUOxl0W8jsFo4EqabX2O3BkF5ukyEQXZ2dnF+0WvXMDUpCHAYDJlGMwwuQu+YemG5NXWeZkovPpB1WflZRJOjmwpG1hJ0fN6V4N7Il+IqJDFDcHbtI2qqntqJo5aiwlu++i3TS/NHQRj3SeJw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS0PR10MB6798.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(39860400002)(366004)(376002)(346002)(136003)(396003)(451199015)(83380400001)(41300700001)(478600001)(66476007)(6506007)(45080400002)(6486002)(8936002)(316002)(6666004)(30864003)(5660300002)(2616005)(4326008)(66946007)(36756003)(2906002)(6512007)(66556008)(86362001)(107886003)(44832011)(8676002)(186003)(1076003)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: zVk6t87IE4z/dsWtuvMFLiCjB0HUt7uUMeWRJU3BmnrzSB6RWxl4WirkjMWTxhY1bFU0sgcqFvOUxgmbmPInnzXgbK79A/UnfEG+pPF8HqPtadEIVhyTjToxpzaRgl0PBU9dDnXNML0aEJwx3ZCzItXrHIICf5LPBMdcSU9BYNS1/r/xDYPd1Kte0txb16AHlMRwb/ee0/n59ZT+Xa7t+l0iaEnY8A8DMZxCoTP4WOGyYrqspZ1FyJBxgTerhmyhZbrFOpBCrpk5YTyvvksg5LsbBjGxdjfMMbd2CkeymLMDDFkWajWP3ZGXTm4ojIt+MaYYFra/J/vS9aDAbrz8h07ypOfDU2jeVg6OlgD+KhFs+Nu8B3sLj8Zaca6a909EakjnfBbw9nMnc1wKnM0lxrt7r1NYj5kaSuas8xn3Br47pAbt4XdP6dzDFs9mtv1ArPvi3MTBYvoU5wTCq/P3KEHOkQyRiOuyVnLqv5trrJqxi7qZQGsrWZ3OrU6rRVg0sVK+Ym5toaWtxgStzGiQf5HFJe4XJ5vP7iipNYZqcQSQdL0yk0RjKQ7LjpKZwYJWRR9oo1gUhOPdn4iN4l/uE6Xac0GI29oIaCjdcwi3Kmpc8lltf3b8oN7J2fao/LyTn+jGsu5b2bDjSRCAUbqp0+GBSY1eFw+pJfzfJXroGO512D6M6Xqe3gjf/itRYxFfTWU1y/FtH5ObZfku6KtuGGxv+Gl+by73hASMFj/jkakPvlu0tTZQdtlAvMGBocPWPtL7yW9PK9VEyhUmfQ7Wq/3hB+qtR1G/LFcEwvCDI3p5xO+1hxLZFg0Akto5ibCtqlLGXrUSR+yrq2oKOOjCZOJO4Ju/nc6r08zJ9+EWC+lzo49u017K9HwxP57CfZPbgOW9dUVJ1N7STO3n760yHhtEvtqYfziagpA5mmatQrJyy1gwBwsYWZtBes7BDE8FPqX97GeR+x9HCYz3QGehX53ghZB6B8loKx9IU/MgvLgypamv2w2wtSMeu1oivQridaLseMPvHAzv2OSQ6pqyEynJKXLMIrrJQwBHnlnUC6EUONkolDIup4yl0dAD6LvbG1Fj+B+Xd8uM8CO5jDRj1MzwgKzqoEgvKbUIjKn+XSFUEvkykgH9s/MRl5BKkLTNLZ1jjpt3A4UH/7u2ibrHpeq4luhyD2NvXy9918Pxwq97qet9YElxd5yGsb4OMqW7W14Ug8jdSCsfRd7AOra13J7PEgfTk/W5/OTWP4rmnYDR5aLWsqDBrUx7stI+sY8KBXitdpI8JU2f6ZErV9iAfxzwJvuYdZ1IrMeyF9EZnC/NgUxT7OHDql9NbEZoVWwNsDUdPP5mcemNm5hnt5fdp5WhPw926GxjU6ugyQIIUp+Yr723RP5gQT0ZJP9iL6hry4dyZ1J9zQEU6orkbqW6OpZFVXHCQOdVpZDe45fHbWxd0iOGnDJvizL9kOlc1UFHFgrmfxpxmOoxNVbjcsp7HYztanbzUhW1/4P4A93Ec24JK0OPx1l004PVN0cYFgExa7IZvtQQtZfM5Sz6ofdPKsy5lkGJ1JZH9xL3rI4JIt4M/h72Uv05w4ZI6vRo0a6f00o67p0wk5MDss6IwusfYw== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 87c53c49-fddf-4e63-3200-08dab8559bb8 X-MS-Exchange-CrossTenant-AuthSource: DS0PR10MB6798.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Oct 2022 19:58:25.2515 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: /7irT/wjy0hGx8fdUP8Jcs948DC1PrrvQjMyimOnnvMME5TSIt3hVCcS22EcN13jmuOa6+aT38ll0pLn55hBnw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR10MB6042 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-10-27_07,2022-10-27_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 spamscore=0 suspectscore=0 phishscore=0 adultscore=0 mlxscore=0 mlxlogscore=999 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2210270112 X-Proofpoint-GUID: iJUBUnFmhWAXk4fWaozOjEj8YacY33X0 X-Proofpoint-ORIG-GUID: iJUBUnFmhWAXk4fWaozOjEj8YacY33X0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-kernel@vger.kernel.org The mapping consists of three new symbols, computed by integrating the information in the (just-added) .tmp_vmlinux.ranges and modules_thick.builtin: taken together, they map address ranges (corresponding to object files on the input) to the names of zero or more modules containing those address ranges. - kallsyms_module_addresses/kallsyms_module_offsets encodes the address/offset of each object file (derived from the linker map), in exactly the same way as kallsyms_addresses/kallsyms_offsets does for symbols. There is no size: instead, the object files are assumed to tile the address space. (This is slightly more space-efficient than using a size). Non-text-section addresses are skipped: for now, all the users of this interface only need module/non-module information for instruction pointer addresses, not absolute-addressed symbols and the like. This restriction can easily be lifted in future. (Regarding the name: right now the entries correspond pretty closely to object files, so we could call the section kallsyms_objfiles or something, but the optimizer added in the next commit will change this.) - kallsyms_mod_objnames encodes the name of each module in a modified form of strtab: notably, if an object file appears in *multiple* modules, all of which are built in, this is encoded via a zero byte, a one-byte module count, then a series of that many null-terminated strings. As a special case, the table starts with a single zero byte which does *not* represent the start of a multi-module list. (The name is "objnames" because in an upcoming commit it will store some object file names too.) - kallsyms_modules connects the two, encoding a table associated 1:1 with kallsyms_module_addresses / kallsyms_module_offsets, pointing at an offset in kallsyms_module_names describing which module (or modules, for a multi-module list) the code occupying this address range is part of. If an address range is part of no module (always built-in) it points at 0 (the null byte at the start of the kallsyms_module_names list). There is no optimization yet: kallsyms_modules and kallsyms_module_names will almost certainly contain many duplicate entries, and kallsyms_module_{addresses,offsets} may contain consecutive entries that point to the same place. The size hit is fairly substantial as a result, though still much less than a naive implementation mapping each symbol to a module name would be: 50KiB or so. Since this commit is the first user of modules_thick.builtin, introduce rules to actually build it when CONFIG_KALLMODSYMS is set (similarly to modules.order, it is named in the top-level makefile purely for documentation purposes, then reiterated in the makefile where it is actually built, in this case the top-level Kbuild). Since it's also the first user of the new Kconfig symbol to enable compiling-out of /proc/kallmodsyms support, introduce that symbol too. Signed-off-by: Nick Alcock Reviewed-by: Kris Van Hees --- Notes: v9: Rename .kallsyms_module_names to .kallsyms_mod_objnames now that it contains object file names too. Adjustments to the Kconfig wording; adjustments to modules_thick.builtin rules. Adjust to getopt_long use in scripts/kallsyms. Kbuild | 6 + Makefile | 5 +- init/Kconfig | 9 ++ scripts/Makefile | 6 + scripts/kallsyms.c | 375 ++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 392 insertions(+), 9 deletions(-) diff --git a/Kbuild b/Kbuild index a84e8312c174..2bd36178ae64 100644 --- a/Kbuild +++ b/Kbuild @@ -113,3 +113,9 @@ $(modthickbuiltin-files): include/config/tristate.conf modules_thick.builtin: $(modthickbuiltin-files) $(obj-y) $(Q)$(AWK) '!x[$$0]++' $(patsubst %/built-in.a, %/$@, $(filter %/built-in.a,$(obj-y))) > $@ + +ifdef CONFIG_KALLMODSYMS +ifdef need-builtin +extra-y += modules_thick.builtin +endif +endif diff --git a/Makefile b/Makefile index 21117f9d4202..71f4d7abd6ed 100644 --- a/Makefile +++ b/Makefile @@ -1232,7 +1232,7 @@ vmlinux.o modules.builtin.modinfo modules.builtin: vmlinux_o @: PHONY += vmlinux -vmlinux: vmlinux.o $(KBUILD_LDS) modpost +vmlinux: vmlinux.o $(KBUILD_LDS) modules_thick.builtin modpost $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.vmlinux # The actual objects are generated when descending, @@ -1562,6 +1562,9 @@ __modinst_pre: endif # CONFIG_MODULES +modules_thick.builtin: $(build-dir) + @: + ### # Cleaning is done on three levels. # make clean Delete most generated files diff --git a/init/Kconfig b/init/Kconfig index abf65098f1b6..70cc2e67bef7 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1570,6 +1570,15 @@ config POSIX_TIMERS If unsure say y. +config KALLMODSYMS + default y + bool "Enable support for /proc/kallmodsyms" if EXPERT + depends on KALLSYMS + help + This option enables the /proc/kallmodsyms file, which unambiguously + maps built-in kernel symbols and their associated object files and + modules to addresses. + config PRINTK default y bool "Enable support for printk" if EXPERT diff --git a/scripts/Makefile b/scripts/Makefile index 1575af84d557..acd46bfeedc3 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -32,6 +32,12 @@ ifdef CONFIG_BUILDTIME_MCOUNT_SORT HOSTCFLAGS_sorttable.o += -DMCOUNT_SORT_ENABLED endif +kallsyms-objs := kallsyms.o + +ifdef CONFIG_KALLMODSYMS +kallsyms-objs += modules_thick.o +endif + # The following programs are only built on demand hostprogs += unifdef diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 03fa07ad45d9..6b9654a151fb 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -5,7 +5,10 @@ * This software may be used and distributed according to the terms * of the GNU General Public License, incorporated herein by reference. * - * Usage: nm -n vmlinux | scripts/kallsyms [--all-symbols] > symbols.S + * Usage: nm -n vmlinux + * | scripts/kallsyms [--all-symbols] [--absolute-percpu] + * [--base-relative] [--builtin=modules_thick.builtin] + * > symbols.S * * Table compression uses all the unused char codes on the symbols and * maps these to the most used substrings (tokens). For instance, it might @@ -25,6 +28,10 @@ #include #include #include +#include +#include "modules_thick.h" + +#include "../include/generated/autoconf.h" #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) @@ -84,11 +91,118 @@ static int token_profit[0x10000]; static unsigned char best_table[256][2]; static unsigned char best_table_len[256]; +#ifdef CONFIG_KALLMODSYMS +static unsigned int strhash(const char *s) +{ + /* fnv32 hash */ + unsigned int hash = 2166136261U; + + for (; *s; s++) + hash = (hash ^ *s) * 0x01000193; + return hash; +} + +#define OBJ2MOD_BITS 10 +#define OBJ2MOD_N (1 << OBJ2MOD_BITS) +#define OBJ2MOD_MASK (OBJ2MOD_N - 1) +struct obj2mod_elem { + char *obj; + char *mods; /* sorted module name strtab */ + size_t nmods; /* number of modules in "mods" */ + size_t mods_size; /* size of all mods together */ + int mod_offset; /* offset of module name in .kallsyms_mod_objnames */ + struct obj2mod_elem *obj2mod_next; +}; + +/* + * Map from object files to obj2mod entries (a unique mapping). + */ + +static struct obj2mod_elem *obj2mod[OBJ2MOD_N]; +static size_t num_objfiles; + +/* + * An ordered list of address ranges and the objfile that occupies that range. + */ +struct addrmap_entry { + char *obj; + unsigned long long addr; + unsigned long long end_addr; + struct obj2mod_elem *objfile; +}; +static struct addrmap_entry *addrmap; +static int addrmap_num, addrmap_alloced; + +static void obj2mod_init(void) +{ + memset(obj2mod, 0, sizeof(obj2mod)); +} + +static struct obj2mod_elem *obj2mod_get(const char *obj) +{ + int i = strhash(obj) & OBJ2MOD_MASK; + struct obj2mod_elem *elem; + + for (elem = obj2mod[i]; elem; elem = elem->obj2mod_next) { + if (strcmp(elem->obj, obj) == 0) + return elem; + } + return NULL; +} + +/* + * Note that a given object file is found in some module, interning it in the + * obj2mod hash. Should not be called more than once for any given (module, + * object) pair. + */ +static void obj2mod_add(char *obj, char *mod) +{ + int i = strhash(obj) & OBJ2MOD_MASK; + struct obj2mod_elem *elem; + + elem = obj2mod_get(obj); + if (!elem) { + elem = malloc(sizeof(struct obj2mod_elem)); + if (!elem) + goto oom; + memset(elem, 0, sizeof(struct obj2mod_elem)); + elem->obj = strdup(obj); + if (!elem->obj) + goto oom; + elem->mods = strdup(mod); + if (!elem->mods) + goto oom; + + elem->obj2mod_next = obj2mod[i]; + obj2mod[i] = elem; + num_objfiles++; + } else { + elem->mods = realloc(elem->mods, elem->mods_size + + strlen(mod) + 1); + if (!elem->mods) + goto oom; + strcpy(elem->mods + elem->mods_size, mod); + } + + elem->mods_size += strlen(mod) + 1; + elem->nmods++; + if (elem->nmods > 255) { + fprintf(stderr, "kallsyms: %s: too many modules associated with this object file\n", + obj); + exit(EXIT_FAILURE); + } + return; +oom: + fprintf(stderr, "kallsyms: out of memory\n"); + exit(1); +} +#endif /* CONFIG_KALLMODSYMS */ static void usage(void) { fprintf(stderr, "Usage: kallsyms [--all-symbols] [--absolute-percpu] " - "[--base-relative] in.map > out.S\n"); + "[--base-relative] [--builtin=modules_thick.builtin] " + " in.map > out.S\n"); exit(1); } @@ -112,10 +226,16 @@ static bool is_ignored_symbol(const char *name, char type) "kallsyms_offsets", "kallsyms_relative_base", "kallsyms_num_syms", + "kallsyms_num_modules", "kallsyms_names", "kallsyms_markers", "kallsyms_token_table", "kallsyms_token_index", + "kallsyms_module_offsets", + "kallsyms_module_addresses", + "kallsyms_modules", + "kallsyms_mod_objnames", + "kallsyms_mod_objnames_len", /* Exclude linker generated symbols which vary between passes */ "_SDA_BASE_", /* ppc */ "_SDA2_BASE_", /* ppc */ @@ -262,8 +382,8 @@ static struct sym_entry *read_symbol(FILE *in) return sym; } -static int symbol_in_range(const struct sym_entry *s, - const struct addr_range *ranges, int entries) +static int addr_in_range(unsigned long long addr, + const struct addr_range *ranges, int entries) { size_t i; const struct addr_range *ar; @@ -271,7 +391,7 @@ static int symbol_in_range(const struct sym_entry *s, for (i = 0; i < entries; ++i) { ar = &ranges[i]; - if (s->addr >= ar->start && s->addr <= ar->end) + if (addr >= ar->start && addr <= ar->end) return 1; } @@ -285,8 +405,8 @@ static int symbol_valid(const struct sym_entry *s) /* if --all-symbols is not specified, then symbols outside the text * and inittext sections are discarded */ if (!all_symbols) { - if (symbol_in_range(s, text_ranges, - ARRAY_SIZE(text_ranges)) == 0) + if (addr_in_range(s->addr, text_ranges, + ARRAY_SIZE(text_ranges)) == 0) return 0; /* Corner case. Discard any symbols with the same value as * _etext _einittext; they can move between pass 1 and 2 when @@ -378,6 +498,121 @@ static void output_address(unsigned long long addr) printf("\tPTR\t_text - %#llx\n", _text - addr); } +#ifdef CONFIG_KALLMODSYMS +/* Output the .kallmodsyms_mod_objnames symbol content. */ +static void output_kallmodsyms_mod_objnames(void) +{ + struct obj2mod_elem *elem; + size_t offset = 1; + size_t i; + + /* + * Traverse and emit, updating mod_offset accordingly. Emit a single \0 + * at the start, to encode non-modular objfiles. + */ + output_label("kallsyms_mod_objnames"); + printf("\t.byte\t0\n"); + for (i = 0; i < OBJ2MOD_N; i++) { + for (elem = obj2mod[i]; elem; + elem = elem->obj2mod_next) { + const char *onemod; + size_t i; + + elem->mod_offset = offset; + onemod = elem->mods; + + /* + * Technically this is a waste of space: we could just + * as well implement multimodule entries by pointing one + * byte further back, to the trailing \0 of the previous + * entry, but doing it this way makes it more obvious + * when an entry is a multimodule entry. + */ + if (elem->nmods != 1) { + printf("\t.byte\t0\n"); + printf("\t.byte\t%zi\n", elem->nmods); + offset += 2; + } + + for (i = elem->nmods; i > 0; i--) { + printf("\t.asciz\t\"%s\"\n", onemod); + offset += strlen(onemod) + 1; + onemod += strlen(onemod) + 1; + } + } + } + printf("\n"); + output_label("kallsyms_mod_objnames_len"); + printf("\t.long\t%zi\n", offset); +} + +static void output_kallmodsyms_objfiles(void) +{ + size_t i = 0; + size_t emitted_offsets = 0; + size_t emitted_objfiles = 0; + + if (base_relative) + output_label("kallsyms_module_offsets"); + else + output_label("kallsyms_module_addresses"); + + for (i = 0; i < addrmap_num; i++) { + long long offset; + int overflow; + + if (base_relative) { + if (!absolute_percpu) { + offset = addrmap[i].addr - relative_base; + overflow = (offset < 0 || offset > UINT_MAX); + } else { + offset = relative_base - addrmap[i].addr - 1; + overflow = (offset < INT_MIN || offset >= 0); + } + if (overflow) { + fprintf(stderr, "kallsyms failure: " + "objfile %s at address %#llx out of range in relative mode\n", + addrmap[i].objfile ? addrmap[i].objfile->obj : + "in always-built-in object", table[i]->addr); + exit(EXIT_FAILURE); + } + printf("\t.long\t0x%x\n", (int)offset); + } else + printf("\tPTR\t%#llx\n", addrmap[i].addr); + emitted_offsets++; + } + + output_label("kallsyms_modules"); + + for (i = 0; i < addrmap_num; i++) { + struct obj2mod_elem *elem = addrmap[i].objfile; + /* + * Address range cites no modular object file: point at 0, the + * built-in module. + */ + if (addrmap[i].objfile == NULL) { + printf("\t.long\t0x0\n"); + emitted_objfiles++; + continue; + } + + /* + * Zero offset is the initial \0, there to catch uninitialized + * obj2mod entries, and is forbidden. + */ + assert(elem->mod_offset != 0); + + printf("\t.long\t0x%x\n", elem->mod_offset); + emitted_objfiles++; + } + + assert(emitted_offsets == emitted_objfiles); + output_label("kallsyms_num_modules"); + printf("\t.long\t%zi\n", emitted_objfiles); + printf("\n"); +} +#endif /* CONFIG_KALLMODSYMS */ + /* uncompress a compressed symbol. When this function is called, the best table * might still be compressed itself, so the function needs to be recursive */ static int expand_symbol(const unsigned char *data, int len, char *result) @@ -477,6 +712,11 @@ static void write_src(void) printf("\n"); } +#ifdef CONFIG_KALLMODSYMS + output_kallmodsyms_mod_objnames(); + output_kallmodsyms_objfiles(); +#endif + output_label("kallsyms_num_syms"); printf("\t.long\t%u\n", table_cnt); printf("\n"); @@ -784,7 +1024,7 @@ static void make_percpus_absolute(void) unsigned int i; for (i = 0; i < table_cnt; i++) - if (symbol_in_range(table[i], &percpu_range, 1)) { + if (addr_in_range(table[i]->addr, &percpu_range, 1)) { /* * Keep the 'A' override for percpu symbols to * ensure consistent behavior compared to older @@ -811,13 +1051,123 @@ static void record_relative_base(void) } } +#ifdef CONFIG_KALLMODSYMS +/* + * Read the linker map. + */ +static void read_linker_map(void) +{ + unsigned long long addr, size; + char *obj; + FILE *f = fopen(".tmp_vmlinux.ranges", "r"); + + if (!f) { + fprintf(stderr, "Cannot open '.tmp_vmlinux.ranges'.\n"); + exit(1); + } + + addrmap_num = 0; + addrmap_alloced = 4096; + addrmap = malloc(sizeof(*addrmap) * addrmap_alloced); + if (!addrmap) + goto oom; + + /* + * For each address range, add to addrmap the address and the objfile + * entry to which the range maps. Only add entries relating to text + * ranges. + * + * Ranges that do not correspond to a built-in module, but to an + * always-built-in object file, have no obj2mod_elem and point at NULL + * instead. Their obj member is still filled out. + */ + + while (fscanf(f, "%llx %llx %ms\n", &addr, &size, &obj) == 3) { + struct obj2mod_elem *elem = obj2mod_get(obj); + + if (addr == 0 || size == 0 || + !addr_in_range(addr, text_ranges, ARRAY_SIZE(text_ranges))) { + free(obj); + continue; + } + + if (addrmap_num >= addrmap_alloced) { + addrmap_alloced *= 2; + addrmap = realloc(addrmap, + sizeof(*addrmap) * addrmap_alloced); + if (!addrmap) + goto oom; + } + + addrmap[addrmap_num].addr = addr; + addrmap[addrmap_num].end_addr = addr + size; + addrmap[addrmap_num].objfile = elem; + addrmap[addrmap_num].obj = obj; + addrmap_num++; + } + fclose(f); + return; + +oom: + fprintf(stderr, "kallsyms: out of memory\n"); + exit(1); +} + +/* + * Read "modules_thick.builtin" (the list of built-in modules). Construct the + * obj2mod hash to track objfile -> module mappings. Read ".tmp_vmlinux.ranges" + * (the linker map) and build addrmap[], which maps address ranges to built-in + * module names (using obj2mod). + */ +static void read_modules(const char *modules_builtin) +{ + struct modules_thick_iter *i; + char *module_name = NULL; + char **module_paths; + + obj2mod_init(); + /* + * Iterate over all modules in modules_thick.builtin and add each. + */ + i = modules_thick_iter_new(modules_builtin); + if (i == NULL) { + fprintf(stderr, "Cannot iterate over builtin modules.\n"); + exit(1); + } + + while ((module_paths = modules_thick_iter_next(i, &module_name))) { + char **walk = module_paths; + while (*walk) { + obj2mod_add(*walk, module_name); + walk++; + } + free(module_paths); + } + + free(module_name); + modules_thick_iter_free(i); + + /* + * Read linker map. + */ + read_linker_map(); +} +#else +static void read_modules(const char *unused) {} +#endif /* CONFIG_KALLMODSYMS */ + int main(int argc, char **argv) { + char *modules_builtin = "modules_thick.builtin"; + while (1) { + static int has_modules_builtin; + static struct option long_options[] = { {"all-symbols", no_argument, &all_symbols, 1}, {"absolute-percpu", no_argument, &absolute_percpu, 1}, {"base-relative", no_argument, &base_relative, 1}, + {"builtin", required_argument, &has_modules_builtin, 1}, {}, }; @@ -827,12 +1177,21 @@ int main(int argc, char **argv) break; if (c != 0) usage(); + + if (has_modules_builtin) { + modules_builtin = strdup(optarg); + if (!modules_builtin) { + fprintf(stderr, "Out of memory parsing args\n"); + exit(1); + } + } } if (optind >= argc) usage(); read_map(argv[optind]); + read_modules(modules_builtin); shrink_table(); if (absolute_percpu) make_percpus_absolute(); From patchwork Thu Oct 27 19:57:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 13022682 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 D882EFA3740 for ; Thu, 27 Oct 2022 19:59:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236813AbiJ0T7B (ORCPT ); Thu, 27 Oct 2022 15:59:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236828AbiJ0T6n (ORCPT ); Thu, 27 Oct 2022 15:58:43 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB5B48991C; Thu, 27 Oct 2022 12:58:40 -0700 (PDT) Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 29RHmppt001419; Thu, 27 Oct 2022 19:58:33 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2022-7-12; bh=RdKfiUQzQZfWe2qNENmLmxoQLh+dBt776HmM3eMhbLc=; b=CVIM6S30VzM/0+wc8dyxuvA1CkBePuVjsJAw2r7qA/U4+ZeqoSegytBe4K8lXbITIEdv 4Y+HaMBM1W7WzlI6snhSL234zcmf4pxAC2vphzeBC54uBKlcdD+QTnQdrLxrIeqK6gPk bS2NkmAxezx40atzoWcIpGrGaOVVuRDtvQEpSSDO3dmJvy7Gt9Cz5SffRity+dgleZu0 sRmb8wG4an0ZHeJ23L9jYI7OxgDISR0zbwSFLiUtC9S9RF3iWpAGQi74Wpzk1PpQF9aJ V+GqEP3Kn48dvpa2MgmhRf2iR0HwQ2zrHScGQH6/0aSgqfdj3ImTj0liIZ16ViTOfu8a Sw== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3kfawru8cm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 27 Oct 2022 19:58:33 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 29RJvqxD006906; Thu, 27 Oct 2022 19:58:32 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2168.outbound.protection.outlook.com [104.47.57.168]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3kfaghb1bq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 27 Oct 2022 19:58:32 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ce9qVwzX3C+ZE+SvBp1MSOOF1zx93+XNoXiMUPNGnV1dmlboREOW97tVY9DdnBxOs0Pq4+tsAjMi/k8KV74fLGyQcgbcEJDD+Q81k3Rgz5aCOAHGKrPgFSjMuaLR0WRGl4i1srb7m2nICPPu9fE/Rtpiprvg4vOzm4+avth29Fv6pe9FUjWRDrg9SCDEUdNfd1lWkgkE6AMSb2mTUHxx+DOegPuvIovVvVg5VEz4SvMFeNV6sOgxJ5R2ydSl+ZLv7hLt129sPb7B2Tq62CQyElA0dcQZVrnrSRwVzXc7HZHsDhjqbD7Va7pgTNtsrL56JExZFsCRULPN6YNIGCo5gA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=RdKfiUQzQZfWe2qNENmLmxoQLh+dBt776HmM3eMhbLc=; b=mXazQ5nqW7Mibs3wRhwT6pefqcNkSaPlthwAgouNSwKPH9RnutpRRhr76Q/x4VzDU5RECnPTI//X6TCh5AL3uW/lFXMyyKAiJvtmaAGwWDcwlEE1B/f0P9LraKWi/uqYFLQzPto12see/+B+HGiE7ujRv/wKhVTnxe/wTXbuyTeATbaDhotmmbzO1nOns7JgUaRLkA/V7oto+OEUJVi1mMwIx3ysJAVt9E11+eFiX5HnTa4+f2TcixOrgGVXvEuqkYsd4ZenKtnwkLNIgkYQL1/5VQmLs8GqmJuZOvGXsOy31ar60HK0AnwuFT0xGhJJcp+pBXPYSO1lTv/ZYZVXXQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=RdKfiUQzQZfWe2qNENmLmxoQLh+dBt776HmM3eMhbLc=; b=hIMQ0QCY7kT+gse5BTIK51R16OCQypDb2YtqjabrrLSC6IMUa9EJInOt8XDzcmEIEMj3d3NseEB/6dq4VAdOL2u6wL0jjcWd959dBi8PkH44LvgEoGVj93TuH0WB8Wj8Ai6FAilK7A1jQVINtIGRFAEqCEuayZj0pMyAWg1+ORE= Received: from DS0PR10MB6798.namprd10.prod.outlook.com (2603:10b6:8:13c::20) by BL3PR10MB6042.namprd10.prod.outlook.com (2603:10b6:208:3b2::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.24; Thu, 27 Oct 2022 19:58:29 +0000 Received: from DS0PR10MB6798.namprd10.prod.outlook.com ([fe80::ff03:d749:df80:38e1]) by DS0PR10MB6798.namprd10.prod.outlook.com ([fe80::ff03:d749:df80:38e1%7]) with mapi id 15.20.5746.028; Thu, 27 Oct 2022 19:58:29 +0000 From: Nick Alcock To: mcgrof@kernel.org, rostedt@goodmis.org, masahiroy@kernel.org Cc: linux-trace-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, akpm@linux-foundation.org, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v9 5/8] kallsyms: optimize .kallsyms_modules* Date: Thu, 27 Oct 2022 20:57:51 +0100 Message-Id: <20221027195754.100039-6-nick.alcock@oracle.com> X-Mailer: git-send-email 2.38.0.266.g481848f278 In-Reply-To: <20221027195754.100039-1-nick.alcock@oracle.com> References: <20221027195754.100039-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO2P265CA0131.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:9f::23) To DS0PR10MB6798.namprd10.prod.outlook.com (2603:10b6:8:13c::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS0PR10MB6798:EE_|BL3PR10MB6042:EE_ X-MS-Office365-Filtering-Correlation-Id: 7b4183e0-48e8-4e49-f704-08dab8559e64 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: lK6QMBd6DZSGocZ7cpue/lAYtddl8wikWcyNBwWfsoykTPwpwrnL6eXiX3zJSPA5iAK+rOh+qNaBXJnXulcuivZGBcMO+8ll2xJI99HURaENVQ61/HVW5rUYYH9ZkeUVgszkqlaBA3P+bWtnMCJ5/Dh9K9CRUtcth69E4+rERrVFR8LDOBs0i8HW09J8hx5DAqw89QGVT83fNm/TGP4ICu+nfIauq0IjGz2diw2bkxwNSv6gqNeye8eH7UcyalagnSvSwXlaxD6EmBXoHDG/l6/J8KHV5zQTB4t/DLHGLtvCU8F9estZdHGw5NNCkIEWogaNSSF4t8iThjcKKijqksp4BZSyBDkAxQjOTRbMkEjuOpZFSHdxFjzhY+KjRtsVPvCIWfQ5ShhPDV8yaoCfmXusJRJjIPn38qXgNZwbt7rzgeoiepfMG2wEDnV3/mjK12oxzAxMK/LOjijoTXBoi5s/W6wRw+hecyTcn+HlLJ0mYdsOulrGV/vEv7LMdSkTmNZFs5Lx25/bWw3LQFOitcGGUTG/iwBHJoFJdo7EBodNVWwlcORPzthjqmETGcgDpzNY+FWEnvQ5DmiycFGR5yJ/QddaWRnKS7x1ayMO490/3/ckjxpn5i2j+0FgaBN2xQXsBrCS9cFOJL8Hc09CFsUaV4+oy7F6VyqoVmpVh4f/CGNUBS//k5CcSjNX5Wc2/THl9mDc+cQ73tTRjEd6dSHTOsJO3R3MuZRzlz6UABpRXRGMJF7+otn+NRnwqmLG/5Xc35yNpQWpGOV1/ZMAOg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS0PR10MB6798.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(39860400002)(366004)(376002)(346002)(136003)(396003)(451199015)(83380400001)(41300700001)(478600001)(66476007)(6506007)(6486002)(8936002)(316002)(6666004)(30864003)(5660300002)(2616005)(4326008)(66946007)(36756003)(2906002)(6512007)(66556008)(86362001)(107886003)(44832011)(8676002)(186003)(1076003)(38100700002)(142923001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: A92M2PWs5iVhEXkkT9EDFmkYw6lpQQJZCL3o0cc4CD4b2qqjiKncIJ+o66yePikR8omRTh+90L5qEUdUZMspEG0kb3jKUW92KF7NJ48FzbEpd1A+j/DOnkq3kEOrbo1j8o1gUqtfSqvLsn71Ne8U3v8QtHmps8IkIh+iX0MYMq33zMY0EhB/kCmnj/bbjym6T2NqA9j2EnWH5imylBOfIIVdLwDBoBnIZImC+sQUU8ARp7mYdtJcJ0awX1mIYKb1/D9z/AeRktpjuC8H4fWEC5ItZHsQ1EtcBRd2tBcDBRVoRDyAf4zBVoWzAWU4gpMtNIvMwSsALXhZ6CbcQw54YGyJEk9gYJt2yHUaRfylxZs5SdKSDfQ+C/IFDUSg+kaOd1dn455m5IuCZORNUxKrsIoEyq4Zo0xhG2aR03CGElYxK7Y1nuzfn2swBaQhciMJyzLBNriPUbky7InWAiRSkYlewYR6ufTvMRBQQc2mXjII8YGJ27b9UGFaA20Iul/fifN+3zhv5G0BtT6kh2UzfCRb7INZis1rrTwnq6sUEt9WKsMM/MMDVInEo5o7qgtUhCQqLNbX+p5hRKZl9lfFoP8CLvCIPvSQTGBUqAG2kqn0Xut1na1HccCGHVfxnZhhS+kTy71jWhoWmsUvUJGwCcDnP+/GRvyXtQnxvCeeeT+2KSxoDTm+8S76EyZX1p/NWkVKHa5wUkAHG6Ufe7NwJDG1Bz12hfMlvizE9u/EYMy7k0dyD5Dwxu8ulT/AQQfAaAfAy1IvkkkT6M6nOE4UTpN4hbkaUkokN/mfo7RHWVP/YEYhEVsmcJ4PePCbXBnyRdikFYnDEvE2HUWjzUp1opnaqNwzsjK1wFrGqDbXGj8gzgbS9g9T8ZCGtzHgtfp5LjxJDovBoFFFdFFBSCRdgVGzrH9VLJ0jy1k1iG7eL9Oeran6M3pceT6H+h5T7PPpACkdzLl0edGybpny7ZBxhbuvLrzoutX0aIDXGUzZcDnnXektdFjEnuLa8650nmi0bO7d8n5TyhlXcCZ6iYquIKu9WfOlBdUmOf1i32TfJ4+/W2fF7OqPLs7V9DImzUJF1AwMJW8546WCFYx4a8r1HsnsVwRokYrJoiqtgs8/qK/nMZDCu+ReLhK3bjF7r513RWzaQdVgdgbm2w3J5O/e0mvqDMg7sR9I4HVQ3ivOsDmYQmcFmQdDoDDoNR+OBWjOPIiggOQmVQjEcgve+5uIaG4fhjIfaNH72cLriOoIRr7rFwnXQ13vAy6hQnw0ewzJrusHbinLwNJBHXktWpvA9IIqYPKEvZJroWdU4gZ7co+moBQiv8R8uIOi2eLPNfHLOftchdP2cSKcuxeZq8fEQRU/5IAucvXXc5rPXeOYkIrwBgfzPsfaNNZ/VjkeASeylgR0qZ9h3xuJPjdAeoKpVb4GKM7kOfU9DWz3jmfOmIUuq2i8GF5ogr0kLmCcOAuLNvZBUm0AdQHz4Zn78JvtxDm2rwoBhiRuhssBVhq14JlWSsd211q4qnENTB622gTBlHIRNNodAfH2TPpSliqiwB20xR9Yj6/FSHKirPXD3//7Hy4cUDg4dbibfzppAjNsVfQCfYsECOLpFK9pcCqedw== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7b4183e0-48e8-4e49-f704-08dab8559e64 X-MS-Exchange-CrossTenant-AuthSource: DS0PR10MB6798.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Oct 2022 19:58:29.7369 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: ECWjrEwWxrbpgWyGDpHD3PB5sB3ACvJn59FmW2tMOTubfs4GJcyJilN+ibXqujCpHU34Zegzr1hb1xjWS0qjxQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR10MB6042 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-10-27_07,2022-10-27_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 spamscore=0 suspectscore=0 phishscore=0 adultscore=0 mlxscore=0 mlxlogscore=999 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2210270112 X-Proofpoint-ORIG-GUID: tzipA8SOygMJefgUWXDxW3kYAiQ6VwDb X-Proofpoint-GUID: tzipA8SOygMJefgUWXDxW3kYAiQ6VwDb Precedence: bulk List-ID: X-Mailing-List: linux-trace-kernel@vger.kernel.org These symbols are terribly inefficiently stored at the moment. Add a simple optimizer which fuses obj2mod_elem entries and uses this to implement three cheap optimizations: - duplicate names are eliminated from .kallsyms_module_names. - entries in .kallsyms_modules which point at single-file modules which also appear in a multi-module list are redirected to point inside that list, and the single-file entry is dropped from .kallsyms_module_names. Thus, modules which contain some object files shared with other modules and some object files exclusive to them do not double up the module name. (There might still be some duplication between multiple multi-module lists, but this is an extremely marginal size effect, and resolving it would require an extra layer of lookup tables which would be even more complex, and incompressible to boot). - Entries in .kallsyms_modules that would contain the same value after the above optimizations are fused together, along with their corresponding .kallsyms_module_addresses/offsets entries. Due to this fusion process, and because object files can be split apart into multiple parts by the linker for hot/cold partitioning and the like, entries in .kallsyms_module_addresses/offsets no longer correspond 1:1 to object files, but more to some contiguous range of addresses which are guaranteed to belong to a single built-in module, but which may well stretch over multiple object files. The optimizer's time complexity is O(log n) in the number of objfiles at most (and probably much lower), so, given the relatively low number of objfiles, its runtime overhead is in the noise. Optimization reduces the overhead of the kallmodsyms tables by about 7500 items, dropping the .tmp_kallsyms2.o object file size by about 33KiB, leaving it 8672 bytes larger than before: a gain of .4%. The vmlinux size is not yet affected because the variables are not used and are eliminated by the linker: but if they were used (after the next commit but one), the size impact of all of this on the final kernel is minimal: in my testing, vmlinux grew by 0.17% (10824 bytes), and the compressed vmlinux only grew by 0.08% (7552 bytes): though this is very configuration-dependent, it seems likely to scale roughly with the kernel as a whole. (The next commit changes these numbers a bit, but not much.) Signed-off-by: Nick Alcock Reviewed-by: Kris Van Hees --- Notes: v9: Fix a bug in optimize_obj2mod that prevented proper reuse of module names for object files appearing in both multimodule modules and single-module modules. Adjustments to allow for objfile support. Tiny style fixes. scripts/kallsyms.c | 297 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 288 insertions(+), 9 deletions(-) diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 6b9654a151fb..f89f569eb3c9 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -102,6 +102,17 @@ static unsigned int strhash(const char *s) return hash; } +static unsigned int memhash(char *s, size_t len) +{ + /* fnv32 hash */ + unsigned int hash = 2166136261U; + size_t i; + + for (i = 0; i < len; i++) + hash = (hash ^ *(s + i)) * 0x01000193; + return hash; +} + #define OBJ2MOD_BITS 10 #define OBJ2MOD_N (1 << OBJ2MOD_BITS) #define OBJ2MOD_MASK (OBJ2MOD_N - 1) @@ -111,14 +122,35 @@ struct obj2mod_elem { size_t nmods; /* number of modules in "mods" */ size_t mods_size; /* size of all mods together */ int mod_offset; /* offset of module name in .kallsyms_mod_objnames */ + + /* + * Hash values of all module names in this elem, combined: used for + * rapid comparisons. Populated quite late, at optimize_obj2mod time. + */ + unsigned int modhash; + + /* + * If set at emission time, this points at another obj2mod entry that + * contains the module name we need (possibly at a slightly later + * offset, if the entry is for an objfile that appears in many modules). + */ + struct obj2mod_elem *xref; + + /* + * Chain links for object -> module and module->object mappings. + */ struct obj2mod_elem *obj2mod_next; + struct obj2mod_elem *mod2obj_next; }; /* - * Map from object files to obj2mod entries (a unique mapping). + * Map from object files to obj2mod entries (a unique mapping), and vice versa + * (not unique, but entries for objfiles in more than one module in this hash + * are ignored). */ static struct obj2mod_elem *obj2mod[OBJ2MOD_N]; +static struct obj2mod_elem *mod2obj[OBJ2MOD_N]; static size_t num_objfiles; /* @@ -162,6 +194,8 @@ static void obj2mod_add(char *obj, char *mod) elem = obj2mod_get(obj); if (!elem) { + int j = strhash(mod) & OBJ2MOD_MASK; + elem = malloc(sizeof(struct obj2mod_elem)); if (!elem) goto oom; @@ -175,8 +209,15 @@ static void obj2mod_add(char *obj, char *mod) elem->obj2mod_next = obj2mod[i]; obj2mod[i] = elem; + elem->mod2obj_next = mod2obj[j]; + mod2obj[j] = elem; num_objfiles++; } else { + /* + * objfile appears in multiple modules. mod2obj for this entry + * will be ignored from now on, except insofar as it is needed + * to maintain the hash chain. + */ elem->mods = realloc(elem->mods, elem->mods_size + strlen(mod) + 1); if (!elem->mods) @@ -196,6 +237,162 @@ static void obj2mod_add(char *obj, char *mod) fprintf(stderr, "kallsyms: out of memory\n"); exit(1); } + +static int qstrcmp(const void *a, const void *b) +{ + return strcmp((const char *) a, (const char *) b); +} + +static int qmodhash(const void *a, const void *b) +{ + struct obj2mod_elem * const *el_a = a; + struct obj2mod_elem * const *el_b = b; + if ((*el_a)->modhash < (*el_b)->modhash) + return -1; + else if ((*el_a)->modhash > (*el_b)->modhash) + return 1; + return 0; +} + +/* + * Associate all object files in obj2mod which refer to the same module with a + * single obj2mod entry for emission, preferring to point into the module list + * in a multi-module objfile. + */ +static void optimize_obj2mod(void) +{ + size_t i; + size_t n = 0; + struct obj2mod_elem *elem; + struct obj2mod_elem *dedup; + + /* An array of all obj2mod_elems, later sorted by hashval. */ + struct obj2mod_elem **uniq; + struct obj2mod_elem *last; + + /* + * Canonicalize all module lists by sorting them, then compute their + * hash values. + */ + uniq = malloc(sizeof(struct obj2mod_elem *) * num_objfiles); + if (uniq == NULL) + goto oom; + + for (i = 0; i < OBJ2MOD_N; i++) { + for (elem = obj2mod[i]; elem; elem = elem->obj2mod_next) { + if (elem->nmods >= 2) { + char **sorter; + char *walk; + char *tmp_mods; + size_t j; + + tmp_mods = malloc(elem->mods_size); + sorter = malloc(sizeof(char *) * elem->nmods); + if (sorter == NULL || tmp_mods == NULL) + goto oom; + memcpy(tmp_mods, elem->mods, elem->mods_size); + + for (j = 0, walk = tmp_mods; j < elem->nmods; + j++) { + sorter[j] = walk; + walk += strlen(walk) + 1; + } + qsort(sorter, elem->nmods, sizeof (char *), + qstrcmp); + for (j = 0, walk = elem->mods; j < elem->nmods; + j++) { + strcpy(walk, sorter[j]); + walk += strlen(walk) + 1; + } + free(tmp_mods); + free(sorter); + } + + uniq[n] = elem; + uniq[n]->modhash = memhash(elem->mods, elem->mods_size); + n++; + } + } + + qsort(uniq, num_objfiles, sizeof (struct obj2mod_elem *), qmodhash); + + /* + * Work over multimodule entries. These must be emitted into + * .kallsyms_mod_objnames as a unit, but we can still optimize by + * reusing some other identical entry. Single-file modules are amenable + * to the same optimization, but we avoid doing it for now so that we + * can prefer to point them directly inside a multimodule entry. + */ + for (i = 0, last = NULL; i < num_objfiles; i++) { + const char *onemod; + size_t j; + + if (uniq[i]->nmods < 2) + continue; + + /* Duplicate multimodule. Reuse the first we saw. */ + if (last != NULL && last->modhash == uniq[i]->modhash && + memcmp(uniq[i]->mods, last->mods, + uniq[i]->mods_size) == 0) { + uniq[i]->xref = last; + continue; + } + + /* + * Single-module entries relating to modules also emitted as + * part of this multimodule entry can refer to it: later, we + * will hunt down the right specific module name within this + * multimodule entry and point directly to it. + */ + onemod = uniq[i]->mods; + for (j = uniq[i]->nmods; j > 0; j--) { + int h = strhash(onemod) & OBJ2MOD_MASK; + + for (dedup = mod2obj[h]; dedup; + dedup = dedup->mod2obj_next) { + if (dedup->nmods > 1) + continue; + + if (strcmp(dedup->mods, onemod) != 0) + continue; + dedup->xref = uniq[i]; + assert(uniq[i]->xref == NULL); + } + onemod += strlen(onemod) + 1; + } + + last = uniq[i]; + } + + /* + * Now traverse all single-module entries, xreffing every one that + * relates to a given module to the first one we saw that refers to that + * module. + */ + for (i = 0, last = NULL; i < num_objfiles; i++) { + if (uniq[i]->nmods > 1) + continue; + + if (uniq[i]->xref != NULL) + continue; + + /* Duplicate module name. Reuse the first we saw. */ + if (last != NULL && last->modhash == uniq[i]->modhash && + memcmp(uniq[i]->mods, last->mods, uniq[i]->mods_size) == 0) { + uniq[i]->xref = last; + assert(last->xref == NULL); + continue; + } + last = uniq[i]; + } + + free(uniq); + + return; +oom: + fprintf(stderr, "kallsyms: out of memory optimizing module list\n"); + exit(EXIT_FAILURE); +} #endif /* CONFIG_KALLMODSYMS */ static void usage(void) @@ -507,8 +704,8 @@ static void output_kallmodsyms_mod_objnames(void) size_t i; /* - * Traverse and emit, updating mod_offset accordingly. Emit a single \0 - * at the start, to encode non-modular objfiles. + * Traverse and emit, chasing xref and updating mod_offset accordingly. + * Emit a single \0 at the start, to encode non-modular objfiles. */ output_label("kallsyms_mod_objnames"); printf("\t.byte\t0\n"); @@ -517,9 +714,25 @@ static void output_kallmodsyms_mod_objnames(void) elem = elem->obj2mod_next) { const char *onemod; size_t i; + struct obj2mod_elem *out_elem = elem; - elem->mod_offset = offset; - onemod = elem->mods; + /* + * Single-module ref to a multimodule: will be emitted + * as a whole, so avoid emitting pieces of it (which + * would go unreferenced in any case). + */ + if (elem->xref && + elem->nmods == 1 && elem->xref->nmods > 1) + continue; + + if (elem->xref) + out_elem = elem->xref; + + if (out_elem->mod_offset != 0) + continue; /* Already emitted. */ + + out_elem->mod_offset = offset; + onemod = out_elem->mods; /* * Technically this is a waste of space: we could just @@ -528,13 +741,14 @@ static void output_kallmodsyms_mod_objnames(void) * entry, but doing it this way makes it more obvious * when an entry is a multimodule entry. */ - if (elem->nmods != 1) { + if (out_elem->nmods != 1) { printf("\t.byte\t0\n"); - printf("\t.byte\t%zi\n", elem->nmods); + printf("\t.byte\t%zi\n", out_elem->nmods); offset += 2; } - for (i = elem->nmods; i > 0; i--) { + for (i = out_elem->nmods; i > 0; i--) { + printf("/* 0x%lx */\n", offset); printf("\t.asciz\t\"%s\"\n", onemod); offset += strlen(onemod) + 1; onemod += strlen(onemod) + 1; @@ -561,6 +775,13 @@ static void output_kallmodsyms_objfiles(void) long long offset; int overflow; + /* + * Fuse consecutive address ranges citing the same object file + * into one. + */ + if (i > 0 && addrmap[i-1].objfile == addrmap[i].objfile) + continue; + if (base_relative) { if (!absolute_percpu) { offset = addrmap[i].addr - relative_base; @@ -586,6 +807,13 @@ static void output_kallmodsyms_objfiles(void) for (i = 0; i < addrmap_num; i++) { struct obj2mod_elem *elem = addrmap[i].objfile; + int orig_nmods; + const char *orig_modname; + int mod_offset; + + if (i > 0 && addrmap[i-1].objfile == addrmap[i].objfile) + continue; + /* * Address range cites no modular object file: point at 0, the * built-in module. @@ -596,13 +824,63 @@ static void output_kallmodsyms_objfiles(void) continue; } + orig_nmods = elem->nmods; + orig_modname = elem->mods; + + /* + * Chase down xrefs, if need be. There can only be one layer of + * these: from single-module entry to other single-module + * entry, or from single- or multi-module entry to another + * multi-module entry. Single -> single and multi -> multi + * always points at the start of the xref target, so its offset + * can be used as is. + */ + if (elem->xref) + elem = elem->xref; + + if (elem->nmods == 1 || orig_nmods > 1) { + + if (elem->nmods == 1) + printf("/* 0x%llx--0x%llx: module %s */\n", + addrmap[i].addr, addrmap[i].end_addr, + elem->mods); + else + printf("/* 0x%llx--0x%llx: multimodule */\n", + addrmap[i].addr, addrmap[i].end_addr); + + mod_offset = elem->mod_offset; + } else { + /* + * If this is a reference from a single-module entry to + * a multi-module entry, hunt down the offset to this + * specific module's name (which is guaranteed to be + * present: see optimize_obj2mod). + */ + + size_t j = elem->nmods; + const char *onemod = elem->mods; + mod_offset = elem->mod_offset; + + for (; j > 0; j--) { + if (strcmp(orig_modname, onemod) == 0) + break; + onemod += strlen(onemod) + 1; + } + assert(j > 0); + /* + * +2 to skip the null byte and count at the start of + * the multimodule entry. + */ + mod_offset += onemod - elem->mods + 2; + } + /* * Zero offset is the initial \0, there to catch uninitialized * obj2mod entries, and is forbidden. */ assert(elem->mod_offset != 0); - printf("\t.long\t0x%x\n", elem->mod_offset); + printf("\t.long\t0x%x\n", mod_offset); emitted_objfiles++; } @@ -1146,6 +1424,7 @@ static void read_modules(const char *modules_builtin) free(module_name); modules_thick_iter_free(i); + optimize_obj2mod(); /* * Read linker map. From patchwork Thu Oct 27 19:57:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 13022683 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 3D9D9FA3740 for ; Thu, 27 Oct 2022 19:59:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236859AbiJ0T7U (ORCPT ); Thu, 27 Oct 2022 15:59:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236861AbiJ0T67 (ORCPT ); Thu, 27 Oct 2022 15:58:59 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBC468983B; Thu, 27 Oct 2022 12:58:47 -0700 (PDT) Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 29RHmpRv001412; Thu, 27 Oct 2022 19:58:40 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2022-7-12; bh=2/AXzHCvqQXde74LhZFpYpeNnEDDNsHTMzasoJKQahE=; b=PtFoJwn1obR5qRdAJOsHLuWDy7TudRrO8xCMGUhy8wzEZmJ0aA6/ZfxBOYNgypxDtK6c s1cQxmH3HVBIk9ar5AvnxKiGBSjyH5pvqMB5GA1+ergYcaWvJmHYkoh0dV9VBNXnnIOG s5daC+NeKlaDz5GuJh+eFbJHYf2jyeSLLSWR3eJC/CElw+m29TXH6oPeDaLElcKD/V9r fx7sOcOBKomKzI3XeSfl3Zl8giMHv5oKINpHig0c0BTQIKPCCTPOhoZ/I54kA+JxrmJr UoD9pGSZcloB/YfD3gOaHdcoeGNwMvy7DXA8gczvkPhp9rH/SSBmNBn5FqfJGeTQyJEP tA== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3kfawru8d0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 27 Oct 2022 19:58:40 +0000 Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 29RIF3wS033247; Thu, 27 Oct 2022 19:58:40 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2173.outbound.protection.outlook.com [104.47.57.173]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3kfagnbybx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 27 Oct 2022 19:58:39 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XWb0g6ZgdPCtcgK/Lcd06XVEKnQINsqLBrzPfJZeZegvKwEIsV6La8846L2uZx8ObTEsDGKHfY76yMvWSno4XKS9k19nux51CN2TNs/RdLinbQX+iSKXTrPUIl8tpPu7vkauI//KuMmHMBf/4S82Vn0t1VKSVAV6PKqfVUXuvwg/Fzkm1lA9jNBVu0grm7iOLo6YuHt602MhOuNMl0hv/udLddkH9hEh4kFexUVnoKN3uJtL+bJqYfACPUCMh2Q5qw7sj+93eEL+fy1aeXWv9ak3OOIfMEbqK7aY3LYQv/TfPA4yby7+F3T7CFsEd55LjimN9cRriw4EHaPRt5l9Kw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=2/AXzHCvqQXde74LhZFpYpeNnEDDNsHTMzasoJKQahE=; b=miIcHUJtxiSMgOQPU5I45t+VwTAvSKMTE2aYB/Qc5gAM2gXaf3rrjCdaV1zjRUII2P64iNBPswOuNgk1BiIpQmWMGa1VJ02dBH2X7i5LjKbZyRdRuJ7AChDdFGzhjwyHYqI0wFpe0ZaLkrdJkBq9NQSFd5M91ows8GzibgZU8WFkSW/Kzfl91DDoxTPCslazFKoxL7io5nro33BkTzt4OxSRKxXxSXzUinXnZxW90656oe3jVpiwUuNqakLnIpnRHKQKlH3B2Og+A+KEX7v6W5CzeUytAksy3HIINPMR9PmpmRY7767pNuq7YgE0GpaSI268VcxYNpOHg4h5jJDgUQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=2/AXzHCvqQXde74LhZFpYpeNnEDDNsHTMzasoJKQahE=; b=a1KJJ0cC9OF1hL0YxRndxAvrp7kOSsh2BmK/opy711eCzs499MCi+jE9vLIHmEmxFul+5pflykjr63VRD0HIUCAs2HH1LXcdk2Rf8od2oj+tCTBGN7tTLXk/NUvS7RoUmrua4nQxyo/azO4uswqB62z/hCv+4t/k3HMQaOmgBkY= Received: from DS0PR10MB6798.namprd10.prod.outlook.com (2603:10b6:8:13c::20) by BL3PR10MB6042.namprd10.prod.outlook.com (2603:10b6:208:3b2::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.24; Thu, 27 Oct 2022 19:58:34 +0000 Received: from DS0PR10MB6798.namprd10.prod.outlook.com ([fe80::ff03:d749:df80:38e1]) by DS0PR10MB6798.namprd10.prod.outlook.com ([fe80::ff03:d749:df80:38e1%7]) with mapi id 15.20.5746.028; Thu, 27 Oct 2022 19:58:34 +0000 From: Nick Alcock To: mcgrof@kernel.org, rostedt@goodmis.org, masahiroy@kernel.org Cc: linux-trace-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, akpm@linux-foundation.org, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v9 6/8] kallsyms: distinguish text symbols fully using object file names Date: Thu, 27 Oct 2022 20:57:52 +0100 Message-Id: <20221027195754.100039-7-nick.alcock@oracle.com> X-Mailer: git-send-email 2.38.0.266.g481848f278 In-Reply-To: <20221027195754.100039-1-nick.alcock@oracle.com> References: <20221027195754.100039-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO6P123CA0048.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:310::15) To DS0PR10MB6798.namprd10.prod.outlook.com (2603:10b6:8:13c::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS0PR10MB6798:EE_|BL3PR10MB6042:EE_ X-MS-Office365-Filtering-Correlation-Id: 18dc075e-4052-4ca1-318d-08dab855a0eb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: agFAz8s+dCwKMe8dfW+VWPHNqRYTXzY9tgo6wNQnBAwN2F9jhXrgfT5StiiOBySklKQAApz2Hp6wvZddq/MRkB+DrOwgUTwb1YCB0n7ePXpgbU4ckVUPXcqN17gqTzpsedD9dpEdj+PX4ZtgJ6CV1dv4F5XgeZypVvlUzjrhV2+xquq+bVcaSSx+MUDszAbTHdY0Nd7Tk6T3CRbKd7Q9HFIb+/b5qRADvVGZPBWviwqQQBQbUbtAG3DDlbIRiKtz5dzwKdO4+pvmp1iWlmwH4C+zdJWclysjtUMSt0+9YVwjae4gvVjRDXf3ndKW+VRYvyEGnl8+JHYEqPSr+SVx56PqQNGez0qSIXpOzn7aFtcd8EYCQgOmYwlhkE/je4jIDuIIxo1Uz7Skjh19DkEFpu1PkEQnxaSDsMEUY9B8ByDPEEO5OqKDieLoX4ts3BKsSEhAEINGFsWj5zx7oncRX9pZ3kP4H8njmvmI2koBpnV4TyA1j2dtI5mWcAjZceBcvPk7INfosSgmORUh1m2NKhXHCvN0AlDErkybVhE/vU5u6QQtPE0UE2HFNHkI2/Fg0zOIkx4XQslViEgoYG0fCE2E0/17Gnrn/wjeXQP+WkdS7GV3UYe7xdsimqvLOcWLbSupvCP3MPc94Ta1lfEpK2Zuv8Vk0B8SRbgqgkHff7d6PXcIX16m+yVyd7S4ic3zmacTA9Rn1mfU+24te3lh5g== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS0PR10MB6798.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(39860400002)(366004)(376002)(346002)(136003)(396003)(451199015)(83380400001)(41300700001)(478600001)(66476007)(6506007)(6486002)(8936002)(316002)(6666004)(30864003)(5660300002)(2616005)(4326008)(66946007)(36756003)(2906002)(6512007)(66556008)(86362001)(107886003)(44832011)(8676002)(186003)(1076003)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: JbmLr643Chda4oBb71sYKEYZV0PCFPkSuWzdFNyYppkyIwsVQNt7tp4K5kMUYuKFarI6YarFSz0qb+WC1xjghDnod0vaGdJR0juOEJt7IJKMFOCWaw3RD7i+gos6vqTB7eqeCneU30z31bMvbgZ5ClqpN08Go+qnk2hJbv59Ejd11lqRKfZQ/yuzHHwM7aCSFRVidAm7zR9MtB+CAkvm0Cge4DPSPR7RsATJbmMzT9bxC2UkZaEIHH7ZAflgP2pOt+OypYY7rN700t+ZwZ3hTwBW4rgcpz5sjao+PczHti+bktudveKpSGdW3iNieVllXmCcE3wcdD4uEjtGCkW630eP9X2r1DJsA6n+l6heWq/QfL4yBHSNTXp86+JHBywpZSIEtJlVD3l97m0RTjMzQjCmLeJPlf1FvuKshZpFbs+ylgGoRXgHsuFGCjBP2wX/OSX9n+6HNftyboGILKDJ3xjPMZ92Tg79rUr/VKH6K9q10/T7Un22pBiB6UbaOc4BhkIO0rnvWS6sn8J0M4NGhwgCd/asmX2FliJDiti+0N46fa09fBLgWX7ytUHdVle7vZUpfXgzIgvcz99C2G2D3d+dbyFh6t4hkdr6WjDUbp6byzHM4aKYT9x/GDN+Fnhy4ylBGf9HVY/t+H1ux6SE/O5jKfwL0M7cAcCIGeIY8j/OVphldTLMMS6nA2JrX62yq/u1pS678YYfZwor/hp2wEkFiuxgf58qIO0DDhKigAknCyKmZYz4NKwBg/dn9Fo4QcPgY6gJbO1t0lkbiLHnSQ7cQVzHAPhyBo5mBvR9b5ochpvnIlMbjzFvBDXFLybxaJfql5Kgr9OlbgGVWaqCZtXY3to/JEAebHsy55Y4hIYuvXCpcx/drDyreBB5tAiMLWvPHSSg2VQU37KRXInrOdV0yqnjr6eLqMD0iJMhnrAr/GxnEHcOtAhJy9beZ/vawvosP+qdGy/HVJvmJ8YFo7Vwy6Ii7vS3vKUE2d6iw2OP72sAB5VN8jDZkgIERPeqzj1sG4RpAhO83S+XCnLKJdsZ7vvrdohVUJsFQ//sxDwuJ0PsMU2A/Y3/usnL+qQ6ZwlEkIG54pIyVTQeCF3XzPvUq1M5YEzWpxLjsMjxSjR9QEXJvF0CUVoPbYD5r/80RjZc+whZGPMrgZOD7FL3Loni6OO1t2xfNvUu5ki+WT/HOKJUlfMfoIoehdJ0jCpIu0LnEtONoh+6bHLYPsA//hHuXtpOf1jFNplTH1+Q8uQo6CptZTyJc3pv1f4wRDq/TJBCYr/hbtv5NgUpmiTgJK6er5sXsaGyOZ6xcwBRx2IjSe8Jz1ObBDmC2vHuD6Yf3twRwNdmpDUxeG1IBxiVOms4Syxd113LIkQgFD+q91qJL5oTfpbbW8KmScPhCg16g9lUCKRQ3BstpWBFfZbIrHRyjWVV2PyKs+Y/uGo/LqZAcw6sW0mLmKhOJHrX0BvPphtwGhd7jzpTmU6RT/lc3cE1MmcNRBusv3j1tymVM5FtAK95JXrzfCM8QlOzURgjfYHUtixKLPZsrwCBNqzSx1BtDJZtvGlYJdRiaASPvNJHoUC3o4l9rWCCvLni8fD+QdtGkMlepkAlDU4iuW3w3w== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 18dc075e-4052-4ca1-318d-08dab855a0eb X-MS-Exchange-CrossTenant-AuthSource: DS0PR10MB6798.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Oct 2022 19:58:33.9720 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: D08Os3ZqYMaJ/5a4TWOfSZxKDUDlWqyC7GnaakUaGPWV1LQTjPp0QneAw8sEQyg4RfQbmymOGFKV/K8F6COArg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR10MB6042 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-10-27_07,2022-10-27_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 adultscore=0 mlxlogscore=999 malwarescore=0 suspectscore=0 phishscore=0 mlxscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2210270112 X-Proofpoint-ORIG-GUID: vu63DnCKBYpU37u9PQPldqKAEBV0Fd-v X-Proofpoint-GUID: vu63DnCKBYpU37u9PQPldqKAEBV0Fd-v Precedence: bulk List-ID: X-Mailing-List: linux-trace-kernel@vger.kernel.org The commits before this one allow you to distinguish identically-named text symbols located in different built-in object files from each other; but identically-named symbols can appear in any object files at all, including object files that cannot be built as modules. We already have nearly all the machinery to disambiguate these symbols as well. Since any given object file can contain at most one definition of a given symbol, it suffices to name the object files containing any symbol which is otherwise ambiguous. (No others need be named, saving a bunch of space). We associate address ranges with object file names using a new .kallsyms_objfiles section just like the previously-added .kallsyms_modules section. But that's not quite enough. Even the object file name is ambiguous in some cases: e.g. there are a lot of files named "core.o" in the kernel. We could just store the full pathname for every object file, but this is needlessly wasteful: doing this eats more than 50KiB in object file names alone, and nearly all the content of every name is repeated for many names. But if we store the object file names in the same section as the built-in module names, drop the .o, and store minimal path suffixes, we can save almost all that space. (For example, "core.o" would be stored as "core" unless there are ambiguous symbols in two different object files both named "core", in which case they'd be named "sched/core" and "futex/core", etc, possibly re-extending to "kernel/sched/core" if still ambiguous). We do this by a repeated-rehashing process. First, we compute a hash value for symbol\0modhash for every symbol (the modhash is ignored if this is a built-in symbol). Any two symbols with the same such hash are identically-named: add the maximally-shortened (one-component, .o-stripped) object file name for all such symbols, and rehash, this time hashing symbol\0objname\0modhash. Any two symbols which still have the same hash are still ambiguous: lengthen the name given to one of the symbols' object files and repeat. Eventually, all the ambiguity will go away. (We do have to take care not to re-lengthen anything we already lengthened in any given hashing round.) This involves multiple sorting passes but the impact on compilation time appears to be nearly zero, and the impact on space in the running kernel is noticeable: only a few dozen names need lengthening, so we can completely ignore the overhead from storing repeated path components because there are hardly any of them. But that's not all. We can also do similar optimization tricks to what was done with .kallsyms_modules, reusing module names and names of already-emitted object files: so any given object file name only appears once in the strtab, and can be cited by many address ranges and even by module entries. Put all this together and the net overhead of this in my testing is about 3KiB of new object file names in the .kallsyms_mod_objnames table and 6KiB for the .kallsyms_objfiles table (mostly zeroes: in future maybe we can find a way to elide some of those, but 6KiB is small enough that it's not worth taking too much effort). No ambiguous textual symbols remain outside actual modules (which can still contain identically-named symbols in different object files because kallsyms doesn't run over them so none of these tables can be built for them. At least, it doesn't yet.) Signed-off-by: Nick Alcock Reviewed-by: Kris Van Hees --- Notes: v9: new. scripts/kallsyms.c | 559 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 546 insertions(+), 13 deletions(-) diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index f89f569eb3c9..ffb69a8f6ff8 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -113,6 +113,9 @@ static unsigned int memhash(char *s, size_t len) return hash; } +/* + * Object file -> module and shortened object file name tracking machinery. + */ #define OBJ2MOD_BITS 10 #define OBJ2MOD_N (1 << OBJ2MOD_BITS) #define OBJ2MOD_MASK (OBJ2MOD_N - 1) @@ -143,15 +146,40 @@ struct obj2mod_elem { struct obj2mod_elem *mod2obj_next; }; +/* + * Shortened object file names. These are only ever consulted after checking + * the obj2mod hashes: names that already exist in there are used directly from + * there (pointed to via the mod_xref field) rather than being re-emitted. + * Entries that do not exist there are added to the end of the mod_objnames + * list. + */ +struct obj2short_elem { + const char *obj; + char *desuffixed; /* objname sans suffix */ + const char *short_obj; /* shortened at / and suffix */ + int short_offset; /* offset of short name in .kallsyms_mod_objnames */ + int last_rehash; /* used during disambiguate_hash_syms */ + + struct obj2mod_elem *mod_xref; + struct obj2short_elem *short_xref; + struct obj2short_elem *short_next; +}; + /* * Map from object files to obj2mod entries (a unique mapping), and vice versa * (not unique, but entries for objfiles in more than one module in this hash - * are ignored). + * are ignored); also map from object file names to shortened names for them + * (also unique: there is no point storing both longer and shorter forms of one + * name, so if a longer name is needed we consistently use it instead of the + * shorter form.) + * + * obj2short is populated very late, at disambiguate_syms time. */ static struct obj2mod_elem *obj2mod[OBJ2MOD_N]; static struct obj2mod_elem *mod2obj[OBJ2MOD_N]; -static size_t num_objfiles; +static struct obj2short_elem *obj2short[OBJ2MOD_N]; +static size_t num_objfiles, num_shortnames; /* * An ordered list of address ranges and the objfile that occupies that range. @@ -165,6 +193,9 @@ struct addrmap_entry { static struct addrmap_entry *addrmap; static int addrmap_num, addrmap_alloced; +static void disambiguate_syms(void); +static void optimize_objnames(void); + static void obj2mod_init(void) { memset(obj2mod, 0, sizeof(obj2mod)); @@ -182,6 +213,18 @@ static struct obj2mod_elem *obj2mod_get(const char *obj) return NULL; } +static struct obj2short_elem *obj2short_get(const char *obj) +{ + int i = strhash(obj) & OBJ2MOD_MASK; + struct obj2short_elem *elem; + + for (elem = obj2short[i]; elem; elem = elem->short_next) { + if (strcmp(elem->obj, obj) == 0) + return elem; + } + return NULL; +} + /* * Note that a given object file is found in some module, interning it in the * obj2mod hash. Should not be called more than once for any given (module, @@ -254,6 +297,12 @@ static int qmodhash(const void *a, const void *b) return 0; } +static int qobj2short(const void *a, const void *b) +{ + return strcmp((*(struct obj2short_elem **)a)->short_obj, + (*(struct obj2short_elem **)b)->short_obj); +} + /* * Associate all object files in obj2mod which refer to the same module with a * single obj2mod entry for emission, preferring to point into the module list @@ -393,6 +442,336 @@ static void optimize_obj2mod(void) fprintf(stderr, "kallsyms: out of memory optimizing module list\n"); exit(EXIT_FAILURE); } + +/* + * Associate all short-name entries in obj2short that refer to the same short + * name with a single entry for emission, either (preferably) a module that + * shares that name or (alternatively) the first obj2short entry referencing + * that name. + */ +static void optimize_objnames(void) +{ + size_t i; + size_t num_objnames = 0; + struct obj2short_elem *elem; + struct obj2short_elem **uniq; + struct obj2short_elem *last; + + uniq = malloc(sizeof(struct obj2short_elem *) * num_shortnames); + if (uniq == NULL) { + fprintf(stderr, "kallsyms: out of memory optimizing object file name list\n"); + exit(EXIT_FAILURE); + } + + /* + * Much like optimize_obj2mod, except there is no need to canonicalize + * anything or handle multimodule entries, and we need to chase down + * possible entries in mod2obj first (so as not to duplicate them in the + * final kallsyms_mod_objnames strtab). + */ + for (i = 0; i < OBJ2MOD_N; i++) + for (elem = obj2short[i]; elem; elem = elem->short_next) + uniq[num_objnames++] = elem; + + qsort(uniq, num_objnames, sizeof(struct obj2short_elem *), qobj2short); + + for (i = 0, last = NULL; i < num_objnames; i++) { + int h = strhash(uniq[i]->short_obj) & OBJ2MOD_MASK; + struct obj2mod_elem *mod_elem; + + for (mod_elem = mod2obj[h]; mod_elem; + mod_elem = mod_elem->mod2obj_next) { + /* + * mod_elem entries are only valid if they are for + * single-module objfiles: see obj2mod_add + */ + if (mod_elem->nmods > 1) + continue; + + if (strcmp(mod_elem->mods, uniq[i]->short_obj) != 0) + continue; + uniq[i]->mod_xref = mod_elem; + break; + } + + /* + * Only look for a short_xref match if we don't already have one + * in mod_xref. (This means that multiple objfiles with the + * same short name that is also a module name all chain directly + * to the module name via mod_xref, rather than going through a + * chain of short_xrefs.) + */ + if (uniq[i]->mod_xref) + continue; + + if (last != NULL && strcmp(last->short_obj, + uniq[i]->short_obj) == 0) { + uniq[i]->short_xref = last; + continue; + } + + last = uniq[i]; + } + + free(uniq); +} + +/* + * Used inside disambiguate_syms to identify colliding symbols. We spot this by + * hashing symbol\0modhash (or just the symbol name if this is in the core + * kernel) and seeing if that collides. (This means we don't need to bother + * canonicalizing the module list, since optimize_obj2mod already did it for + * us.) + * + * If that collides, we try disambiguating by adding ever-longer pieces of the + * object file name before the modhash until we no longer collide. The result + * of this repeated addition becomes the obj2short hashtab. + */ +struct sym_maybe_collides { + struct sym_entry *sym; + struct addrmap_entry *addr; + struct obj2short_elem *short_objname; + unsigned int symhash; +}; + +static int qsymhash(const void *a, const void *b) +{ + const struct sym_maybe_collides *el_a = a; + const struct sym_maybe_collides *el_b = b; + if (el_a->symhash < el_b->symhash) + return -1; + else if (el_a->symhash > el_b->symhash) + return 1; + return 0; +} + +static int find_addrmap(const void *a, const void *b) +{ + const struct sym_entry *sym = a; + const struct addrmap_entry *map = b; + + if (sym->addr < map->addr) + return -1; + else if (sym->addr >= map->end_addr) + return 1; + return 0; +} + +/* + * Allocate or lengthen an object file name for a symbol that needs it. + */ +static int lengthen_short_name(struct sym_maybe_collides *sym, int hash_cycle) +{ + struct obj2short_elem *short_objname = obj2short_get(sym->addr->obj); + + if (!short_objname) { + int i = strhash(sym->addr->obj) & OBJ2MOD_MASK; + char *p; + + short_objname = malloc(sizeof(struct obj2short_elem)); + if (short_objname == NULL) + goto oom; + + /* + * New symbol: try maximal shortening, which is just the object + * file name (no directory) with the suffix removed (the suffix + * is useless for disambiguation since it is almost always .o). + * + * Add a bit of paranoia to allow for names starting with /, + * ending with ., and names with no suffix. (At least two of + * these are most unlikely, but possible.) + */ + + memset(short_objname, 0, sizeof(struct obj2short_elem)); + short_objname->obj = sym->addr->obj; + + p = strrchr(sym->addr->obj, '.'); + if (p) + short_objname->desuffixed = strndup(sym->addr->obj, + p - sym->addr->obj); + else + short_objname->desuffixed = strdup(sym->addr->obj); + + if (short_objname->desuffixed == NULL) + goto oom; + + p = strrchr(short_objname->desuffixed, '/'); + if (p && p[1] != 0) + short_objname->short_obj = p + 1; + else + short_objname->short_obj = short_objname->desuffixed; + + short_objname->short_next = obj2short[i]; + short_objname->last_rehash = hash_cycle; + obj2short[i] = short_objname; + + num_shortnames++; + return 1; + } + + /* + * Objname already lengthened by a previous symbol clash: do nothing + * until we rehash again. + */ + if (short_objname->last_rehash == hash_cycle) + return 0; + short_objname->last_rehash = hash_cycle; + + /* + * Existing symbol: lengthen the objname we already have. + */ + + if (short_objname->desuffixed == short_objname->short_obj) { + fprintf(stderr, "Cannot disambiguate %s: objname %s is " + "max-length but still colliding\n", + sym->sym->sym, short_objname->short_obj); + return 0; + } + + /* + * Allow for absolute paths, where the first byte is '/'. + */ + + if (short_objname->desuffixed >= short_objname->short_obj - 2) + short_objname->short_obj = short_objname->desuffixed; + else { + for (short_objname->short_obj -= 2; + short_objname->short_obj > short_objname->desuffixed && + *short_objname->short_obj != '/'; + short_objname->short_obj--); + + if (*short_objname->short_obj == '/') + short_objname->short_obj++; + } + return 1; + oom: + fprintf(stderr, "Out of memory disambiguating syms\n"); + exit(EXIT_FAILURE); +} + +/* + * Do one round of disambiguation-check symbol hashing, factoring in the current + * set of applicable shortened object file names for those symbols that need + * them. + */ +static void disambiguate_hash_syms(struct sym_maybe_collides *syms) +{ + size_t i; + for (i = 0; i < table_cnt; i++) { + struct obj2short_elem *short_objname = NULL; + char *tmp, *p; + size_t tmp_size; + + if (syms[i].sym == NULL) { + syms[i].symhash = 0; + continue; + } + + short_objname = obj2short_get(syms[i].addr->obj); + + tmp_size = strlen((char *) &(syms[i].sym->sym[1])) + 1; + + if (short_objname) + tmp_size += strlen(short_objname->short_obj) + 1; + + if (syms[i].addr->objfile) + tmp_size += sizeof(syms[i].addr->objfile->modhash); + + tmp = malloc(tmp_size); + if (tmp == NULL) { + fprintf(stderr, "Out of memory disambiguating syms\n"); + exit(EXIT_FAILURE); + } + + p = stpcpy(tmp, (char *) &(syms[i].sym->sym[1])); + p++; + if (short_objname) { + p = stpcpy(p, short_objname->short_obj); + p++; + } + if (syms[i].addr->objfile) + memcpy(p, &(syms[i].addr->objfile->modhash), + sizeof(syms[i].addr->objfile->modhash)); + + syms[i].symhash = memhash(tmp, tmp_size); + free(tmp); + } + + qsort(syms, table_cnt, sizeof (struct sym_maybe_collides), qsymhash); +} + +/* + * Figure out which object file names are necessary to disambiguate all symbols + * in the linked kernel: transform them for minimum length while retaining + * disambiguity: point to them in obj2short. + */ +static void disambiguate_syms(void) +{ + size_t i; + int retry; + int hash_cycle = 0; + unsigned int lasthash; + struct sym_maybe_collides *syms; + + syms = calloc(table_cnt, sizeof(struct sym_maybe_collides)); + + if (syms == NULL) + goto oom; + + /* + * Initial table population: symbol-dependent things not affected by + * disambiguation rounds. + */ + for (i = 0; i < table_cnt; i++) { + struct addrmap_entry *addr; + + /* + * Only bother doing anything for function symbols. + */ + if (table[i]->sym[0] != 't' && table[i]->sym[0] != 'T' && + table[i]->sym[0] != 'w' && table[i]->sym[0] != 'W') + continue; + + addr = bsearch(table[i], addrmap, addrmap_num, + sizeof(struct addrmap_entry), find_addrmap); + + /* + * Some function symbols (section start symbols, discarded + * non-text-range symbols, etc) don't appear in the linker map + * at all. + */ + if (addr == NULL) + continue; + + syms[i].sym = table[i]; + syms[i].addr = addr; + } + + do { + hash_cycle++; + retry = 0; + lasthash = 0; + disambiguate_hash_syms(syms); + + for (i = 0; i < table_cnt; i++) { + if (syms[i].sym == NULL) + continue; + if (syms[i].symhash == lasthash) { + if (lengthen_short_name(&syms[i], hash_cycle)) + retry = 1; + } + lasthash = syms[i].symhash; + } + } while (retry); + + free(syms); + return; + oom: + fprintf(stderr, "kallsyms: out of memory disambiguating syms\n"); + exit(EXIT_FAILURE); + +} + #endif /* CONFIG_KALLMODSYMS */ static void usage(void) @@ -424,6 +803,7 @@ static bool is_ignored_symbol(const char *name, char type) "kallsyms_relative_base", "kallsyms_num_syms", "kallsyms_num_modules", + "kallsyms_num_objfiles", "kallsyms_names", "kallsyms_markers", "kallsyms_token_table", @@ -431,6 +811,7 @@ static bool is_ignored_symbol(const char *name, char type) "kallsyms_module_offsets", "kallsyms_module_addresses", "kallsyms_modules", + "kallsyms_objfiles", "kallsyms_mod_objnames", "kallsyms_mod_objnames_len", /* Exclude linker generated symbols which vary between passes */ @@ -700,6 +1081,7 @@ static void output_address(unsigned long long addr) static void output_kallmodsyms_mod_objnames(void) { struct obj2mod_elem *elem; + struct obj2short_elem *short_elem; size_t offset = 1; size_t i; @@ -755,15 +1137,75 @@ static void output_kallmodsyms_mod_objnames(void) } } } + + /* + * Module names are done; now emit objfile names that don't match + * objfile names. They go in the same section to enable deduplication + * between (maximally-shortened) objfile names and module names. + * (This is another reason why objfile names drop the suffix.) + */ + for (i = 0; i < OBJ2MOD_N; i++) { + for (short_elem = obj2short[i]; short_elem; + short_elem = short_elem->short_next) { + + /* Already emitted? */ + if (short_elem->mod_xref) + continue; + + if (short_elem->short_xref) + short_elem = short_elem->short_xref; + + if (short_elem->short_offset != 0) + continue; + + printf("/* 0x%lx: shortened from %s */\n", offset, + short_elem->obj); + + short_elem->short_offset = offset; + printf("\t.asciz\t\"%s\"\n", short_elem->short_obj); + offset += strlen(short_elem->short_obj) + 1; + } + } + printf("\n"); output_label("kallsyms_mod_objnames_len"); printf("\t.long\t%zi\n", offset); } +/* + * Return 1 if this address range cites the same built-in module and objfile + * name as the previous one. + */ +static int same_kallmodsyms_range(int i) +{ + struct obj2short_elem *last_short; + struct obj2short_elem *this_short; + if (i == 0) + return 0; + + last_short = obj2short_get(addrmap[i-1].obj); + this_short = obj2short_get(addrmap[i].obj); + + if (addrmap[i-1].objfile == addrmap[i].objfile) { + + if ((last_short == NULL && this_short != NULL) || + (last_short != NULL && this_short == NULL)) + return 0; + + if (last_short == NULL && this_short == NULL) + return 1; + + if (strcmp(last_short->short_obj, this_short->short_obj) == 0) + return 1; + } + return 0; +} + static void output_kallmodsyms_objfiles(void) { size_t i = 0; size_t emitted_offsets = 0; + size_t emitted_modules = 0; size_t emitted_objfiles = 0; if (base_relative) @@ -775,12 +1217,15 @@ static void output_kallmodsyms_objfiles(void) long long offset; int overflow; - /* - * Fuse consecutive address ranges citing the same object file - * into one. - */ - if (i > 0 && addrmap[i-1].objfile == addrmap[i].objfile) - continue; + printf("/* 0x%llx--0x%llx: %s */\n", addrmap[i].addr, + addrmap[i].end_addr, addrmap[i].obj); + + /* + * Fuse consecutive address ranges citing the same built-in + * module and objfile name into one. + */ + if (same_kallmodsyms_range(i)) + continue; if (base_relative) { if (!absolute_percpu) { @@ -807,11 +1252,12 @@ static void output_kallmodsyms_objfiles(void) for (i = 0; i < addrmap_num; i++) { struct obj2mod_elem *elem = addrmap[i].objfile; + struct obj2mod_elem *orig_elem = NULL; int orig_nmods; const char *orig_modname; int mod_offset; - if (i > 0 && addrmap[i-1].objfile == addrmap[i].objfile) + if (same_kallmodsyms_range(i)) continue; /* @@ -819,8 +1265,10 @@ static void output_kallmodsyms_objfiles(void) * built-in module. */ if (addrmap[i].objfile == NULL) { + printf("/* 0x%llx--0x%llx: %s: built-in */\n", + addrmap[i].addr, addrmap[i].end_addr, addrmap[i].obj); printf("\t.long\t0x0\n"); - emitted_objfiles++; + emitted_modules++; continue; } @@ -835,8 +1283,10 @@ static void output_kallmodsyms_objfiles(void) * always points at the start of the xref target, so its offset * can be used as is. */ - if (elem->xref) + if (elem->xref) { + orig_elem = elem; elem = elem->xref; + } if (elem->nmods == 1 || orig_nmods > 1) { @@ -872,6 +1322,19 @@ static void output_kallmodsyms_objfiles(void) * the multimodule entry. */ mod_offset += onemod - elem->mods + 2; + + /* + * If this was the result of an xref chase, store this + * mod_offset in the original entry so we can just reuse + * it if an objfile shares this name. + */ + + printf("/* 0x%llx--0x%llx: %s: single-module ref to %s in multimodule at %x */\n", + addrmap[i].addr, addrmap[i].end_addr, + orig_elem->mods, onemod, elem->mod_offset); + + if (orig_elem) + orig_elem->mod_offset = mod_offset; } /* @@ -881,12 +1344,68 @@ static void output_kallmodsyms_objfiles(void) assert(elem->mod_offset != 0); printf("\t.long\t0x%x\n", mod_offset); - emitted_objfiles++; + emitted_modules++; } - assert(emitted_offsets == emitted_objfiles); + assert(emitted_offsets == emitted_modules); output_label("kallsyms_num_modules"); + printf("\t.long\t%zi\n", emitted_modules); + + output_label("kallsyms_objfiles"); + + for (i = 0; i < addrmap_num; i++) { + struct obj2short_elem *elem; + int mod_offset; + + if (same_kallmodsyms_range(i)) + continue; + + /* + * No corresponding objfile name: no disambiguation needed; + * point at 0. + */ + elem = obj2short_get(addrmap[i].obj); + + if (elem == NULL) { + printf("/* 0x%llx--0x%llx: %s: unambiguous */\n", + addrmap[i].addr, addrmap[i].end_addr, + addrmap[i].obj); + printf("\t.long\t0x0\n"); + emitted_objfiles++; + continue; + } + + /* + * Maybe the name is also used for a module: if it is, it cannot + * be a multimodule. + */ + + if (elem->mod_xref) { + assert(elem->mod_xref->nmods == 1); + mod_offset = elem->mod_xref->mod_offset; + printf("/* 0x%llx--0x%llx: %s: shortened as %s, references module */\n", + addrmap[i].addr, addrmap[i].end_addr, + addrmap[i].obj, elem->short_obj); + } else { + /* + * A name only used for objfiles. Chase down xrefs to + * reuse existing entries. + */ + if (elem->short_xref) + elem = elem->short_xref; + + mod_offset = elem->short_offset; + printf("/* 0x%llx--0x%llx: %s: shortened as %s */\n", + addrmap[i].addr, addrmap[i].end_addr, + addrmap[i].obj, elem->short_obj); + } + printf("\t.long\t0x%x\n", mod_offset); + emitted_objfiles++; + } + assert(emitted_offsets == emitted_objfiles); + output_label("kallsyms_num_objfiles"); printf("\t.long\t%zi\n", emitted_objfiles); + printf("\n"); } #endif /* CONFIG_KALLMODSYMS */ @@ -1430,6 +1949,20 @@ static void read_modules(const char *modules_builtin) * Read linker map. */ read_linker_map(); + + /* + * Now the modules are sorted out and we know their address ranges, use + * the modhashes computed in optimize_obj2mod to identify any symbols + * that are still ambiguous and set up the minimal representation of + * their objfile name to disambiguate them. + */ + disambiguate_syms(); + + /* + * Now we have objfile names, optimize the objfile list. + */ + optimize_objnames(); + } #else static void read_modules(const char *unused) {} From patchwork Thu Oct 27 19:57:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 13022685 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 D6C19FA3742 for ; Thu, 27 Oct 2022 19:59:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236896AbiJ0T7X (ORCPT ); Thu, 27 Oct 2022 15:59:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236868AbiJ0T67 (ORCPT ); Thu, 27 Oct 2022 15:58:59 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7EAF189806; Thu, 27 Oct 2022 12:58:48 -0700 (PDT) Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 29RHmpSZ001416; Thu, 27 Oct 2022 19:58:41 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2022-7-12; bh=I1d1bqXhzhDNq6Kusl94sLc+gKjQzctiUPQdP4RW9Ac=; b=GjKTItGRUI8kN5aekgaKvKQ5PQSrDvAFYUoz3Xiv3Bfk1DgtsFCVXaIUq8M8Mq+GNSN4 mYomXUQ/X7kTHPtg3YKq/00ouLV2VggOaeiy+TEbghISqE8FDhmJzpJY90yYTTTDSZig qVtYiaKhBdcqKOzb4luyfQ16WTx6eVp9PkuR3WBifwY9Cz+kVUo5gf8Zrs2dM0aELCmo UOu8W8h82XWpMNguz55yCp2JR/B1TlugUyMlfctKA9xy5bl1K0zsGWfL+K1k0IxjfnNV 6sHYl1siy8mC+JvdOg9QO2Rho0CtfdNR93N9XsAXG+Qut1nNRPXk8U8fYJf5ildsI8Q2 Cg== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3kfawru8d2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 27 Oct 2022 19:58:41 +0000 Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 29RIfcS0009431; Thu, 27 Oct 2022 19:58:40 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2102.outbound.protection.outlook.com [104.47.58.102]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3kfagfbcth-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 27 Oct 2022 19:58:40 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=F3fA/HGXbD7E+7wlWilGc/xZEsLNm34hEfGpTNi3gPOqu79/sYM+r4u00JuSn4FGgwZPCQPgH3vE+mdeuis+M/gtKyLCejL81d/XVNFhJHj0spF+KmMuKsrrob1TJmdNfhE1sVAjk8MzvqJM3h1D1KPl2EXo1oQJvEg+eyd4ZEEUfk/SxlCqH++QQ0HrBiUUIjt62tDGCkTmuTI7UTBHJr7BxnaypJzkdsCEqlPNkoEgGfOgyC2/siFDyLmVmGOyfVkLmv7fjZn/Fd32UxEsgPtR2CExrFdEOtPUdhbxRj/c9gYuInpsX7iY+Kc/eefamgvm/8R9XMqPUxH15KEyYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=I1d1bqXhzhDNq6Kusl94sLc+gKjQzctiUPQdP4RW9Ac=; b=ONFNnALnRdq0mxsXzyEzRdNRsZHRtvkk7S4sOjgSEAlJcGOGqknMrv3WJgcqejFJDOjO/28dFe07u9b0t6DDDo1D0fiuboapKiFQX+C5dp+NhshWVqu/CSENmoLeCIFKBZlxf6XQD0+EkonTMRFM5eFNL2ZL4+DUOYZbcgrHhbtRJE1scqmS/EQ2T+RKaAy1eNo+Ik85Mkbci8marmTFittvR3RYlw+Qr58y72Fhd4hbADwEJGT3PIp8XzqXUleUT4MwieZqGxa+KogTYhEju6tR3NPd4v1q03eiDvpt6n6Z8BKP75tnSgg2rqMnZM6JX96VTW7Nhtd7m+PZPQu06g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=I1d1bqXhzhDNq6Kusl94sLc+gKjQzctiUPQdP4RW9Ac=; b=CSon0jwImoxGjG3nuwGkx7IWAYONDS+ytmaMiSo2A2nsmyH2XJRCS3sxH2x2uEtdWWN8R+Akv8SZVdOFcwM/VyZhAa6B3hGNZZnP8CcdRpb4EWz3xTO01dFElm5JI0nX3oXGx1qT7i3mTQYXduUtWtkyL3ybDOO0SNFSfECcYv4= Received: from DS0PR10MB6798.namprd10.prod.outlook.com (2603:10b6:8:13c::20) by CY8PR10MB6778.namprd10.prod.outlook.com (2603:10b6:930:99::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5746.28; Thu, 27 Oct 2022 19:58:37 +0000 Received: from DS0PR10MB6798.namprd10.prod.outlook.com ([fe80::ff03:d749:df80:38e1]) by DS0PR10MB6798.namprd10.prod.outlook.com ([fe80::ff03:d749:df80:38e1%7]) with mapi id 15.20.5746.028; Thu, 27 Oct 2022 19:58:37 +0000 From: Nick Alcock To: mcgrof@kernel.org, rostedt@goodmis.org, masahiroy@kernel.org Cc: linux-trace-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, akpm@linux-foundation.org, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v9 7/8] kallsyms: add /proc/kallmodsyms for text symbol disambiguation Date: Thu, 27 Oct 2022 20:57:53 +0100 Message-Id: <20221027195754.100039-8-nick.alcock@oracle.com> X-Mailer: git-send-email 2.38.0.266.g481848f278 In-Reply-To: <20221027195754.100039-1-nick.alcock@oracle.com> References: <20221027195754.100039-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO4P123CA0220.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:1a6::9) To DS0PR10MB6798.namprd10.prod.outlook.com (2603:10b6:8:13c::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS0PR10MB6798:EE_|CY8PR10MB6778:EE_ X-MS-Office365-Filtering-Correlation-Id: 121745df-ccf6-43a5-f0dc-08dab855a303 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: RV8dQD7nPx4zfe9PqsQNL7TdVw0NNx9vfgD62eXSZwQ9lkL0HJmx9zBs1+K7wt1QiKw9faKXKuHVhKOnv+9sAGEGsB/RHdcj16XOiMJDLpoLGk3ALfm5i0+nUqluWJuzHcwxvTtIzInhAZFSvSdv4xv73VxtFg6Gl5SH3thBAqrnrLfuU2ZANp1BgSzfMGlTwa3otKr/JL1aYfemsgx17APT5tXCl3MamkTX73k57NDqDXX7+obxEmFWFrC/52XY1udPX2kpkIJ4IEBmNIYJXoy2dDNLKjckYDZjnlds4MJPiXjzO4i8kpZGfQZZgoRo1ZuK7i5zkfAoL3z5k9Yrpg0WMP24G3uhTWKpGQdmWRN/doTOiba7jstY58Y/3GQ7cOTPFOQ6Wk2PWApYiPa5hTJGUpAXbEMdwmbZKtQPUzGnABXusJFYVkjWcPWoske60QOcPdUL2vw6AvMk0GQ16fRopAdMG5CMwX019FKj0yfPjFrJPFFSiVVCXhpRS2oFRnVl+/moftJt/FxwlsplJDPBwP9sgQ6o3/gsiAPy/UdMMGC4wM+AWl0CVWFY/HiGBmB9xlqNKMM3HrGvOk+m4AYf6pQ2GvtSIKFoM/YMmstvBRcsyPwNRKxdPGiY9WNlF3+8+DLhMxhZSXAEo67F+sSgUqr4XZe/2MGodnqAxlrOhoytvsb8Ut9jaOrmSSINx1RidJwzzPa7nH1giCE0pA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS0PR10MB6798.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(376002)(366004)(396003)(346002)(39860400002)(136003)(451199015)(6486002)(83380400001)(41300700001)(5660300002)(30864003)(478600001)(8676002)(6666004)(107886003)(66946007)(4326008)(66476007)(316002)(44832011)(8936002)(66556008)(1076003)(86362001)(2616005)(6512007)(186003)(2906002)(36756003)(38100700002)(6506007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Fy+xnDPJMUmaOUCj1ixlFoOAuI4QuL1jZhfSeXO0gvHjf7I85+XhCGGyz/vX27IWMosiMqc5Nun6ugiaFiN726h7628wiTYDTQfxEPmj2hueQGtz65Cw/5T6QdB4NKafe6VDj1QBh8ZC1TgNCXte+m9vf4SNDini4pYeiEX2cWqf3kWbk3n/C75SoQpOFr5sXiTS7oXgit3pC8wruT3yBsU6EnnvCmAwZEq8raQjwVcBocrpOH7jc9hz4mFhzQqzD7gVW1rKUT7sC5f0xolZPOdDmJYhaR/Rb/DdvvsCU7uhsIl3xpRwYAidOIvu2b0XGIrG21EcppWvoSgNfJJBD3T3TxnSqdiuVEnZTgK+hlUoi25Z/TZsgPj6/uw3/LVI6z/geTQE3cg9XSH4bkN67iZbndcyL9r6z+O09NvTyfIQ8MZli8QdXoN5Z/v/o/4N65Vveq6UuJoP76Vxj2EVKkkFgSdaTrZ1wDKjqouiE8Lht//ExDQ7Efl9EG8NjvVEwnDfT8AEChJQ2Jjw1vX90RKGbDh1/sKhfwIE0/f53mZUYUj5zcWkbPtUmV7X4lRTcNyntnJRcu4LQFMirhrAaS4EZP97SQn3gkQ8Hz6dlbgQsiUlNLu1c8Lf0H72iZ5COhWyUUBj41DgAoK/WsWA0RUBcpMrT7ZKCZKWe15xKtUShlfoN+yyQErjNTnqNjxjcoY2788vBQ8Id4idCIVX3MxoUx8sPGwVExBcTIYx2YXtuJ+92vm40S83GrNbF+HEImdzxKdYl1VYEk6ETpX3xqzobvAnj59jP2UcNmLQxHmn74t8U46fpF+tGLyhfllUZ8VQfDWNKrKLJ2xH0bX0YWa7v2mf+oCOpdSgVzsh6pqZS6VZlRbhVqLXgD2QDDLucF5wqix+ZK8PM1HOpE76Ce5jJphZwvrho98VY23Q5Auix7LEZpQizvpIc/hnD3vtPvEC+CgynN66l4M52Go3n7+eQH/qzPoc/RAhWpfsCyWROj1Uaizaw3MhYZRP65XycjnXfM/7jqz6WBXxQiegZ8dpeTc/x6+FSNGohmYPqw09+pfhK5VmVgpvuwVc224yBgjAKjw44m2REv/bRpo2zbr00UwetSvtk7tG8VymKPgfwLrLLUYFrwQjixw8O717vNP6/WsnzwZ+F0WB42PBRd+e5HotlQ6NjZJANApVw85kG8aecY7aPXpEOaQsBK14laT/drb38UiweIdDYrWpYVHuhbI4q4SMoK7qNh3iDzZKV0szDhfX8PO+DA94mYPWg5IPKPHCke1AhpvNayVxfakA7Ag/707AXah0kI2hShsbT81NKS1VGmrNpkEgjWt0APGlBYsW1QRe+qutBVbHIfEiqe7nPokVxPZWs7avxOZ04W6r4zEZm1g1PLzhRP8mveTxrGPEMiP/J7Ntf7BagVVo5ypPtBpne/7gCS/ibHav5ByWPB7k3+fOgeIq50AHFgvuQjmWHf8owpmhOUHuS5hi4taqC1ykbSnUbwvQ3x8bs6AGeDcEh1obM4cdEZ3ACjJk1JviG5ueAMduzZFgl1hPC/oTKeU7QJtWlDgJLFzZ5bQwd9sw+gHiK7tNVMwNW0lPIswYOOuEnzef7GgT/w== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 121745df-ccf6-43a5-f0dc-08dab855a303 X-MS-Exchange-CrossTenant-AuthSource: DS0PR10MB6798.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Oct 2022 19:58:37.5053 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: R3GrvOQHS/3hspnQ/CZBKkpjGQVKimBtJyEjvhmjC+Bn/6PT+skIXJ4DUF68H32UJeIQRmlPa/PcFSgHKkoEHQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR10MB6778 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-10-27_07,2022-10-27_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxlogscore=999 suspectscore=0 mlxscore=0 spamscore=0 phishscore=0 malwarescore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2210270112 X-Proofpoint-ORIG-GUID: bonEz2udTTi81SlRd0DzxMQLkvkrIsNF X-Proofpoint-GUID: bonEz2udTTi81SlRd0DzxMQLkvkrIsNF Precedence: bulk List-ID: X-Mailing-List: linux-trace-kernel@vger.kernel.org Use the tables added in the previous commits to introduce a new /proc/kallmodsyms, in which [module names] are also given for things that *could* have been modular had they not been built in to the kernel. So symbols that are part of, say, ext4 are reported as [ext4] even if ext4 happens to be buiilt in to the kernel in this configuration. This helps disambiguate symbols with identical names when some are in built-in modules are some are not, but if symbols are still ambiguous, {object file names} are added as needed to disambiguate them. The object file names are only shown if they would prevent ambiguity, and are minimized by chopping off as many leading path components as possible without (symbol, module, objfile) combinations becoming ambiguous again. (Not every symbol with an {object file name} is necessarily ambiguous, but at least one symbol in any such object file would have been ambiguous if the object file was not mentioned.) Symbols that are part of multiple modules at the same time are shown with [multiple] [module names]: consumers will have to be ready to handle such lines. Also, kernel symbols for built-in modules will be sorted by address, as usual for the core kernel, so will probably appear interspersed with other symbols that are part of different modules and non-modular always-built-in symbols, which, as usual, have no square-bracketed module denotation. This differs from /proc/kallsyms; even though /proc/kallsyms shows the same symbols as /proc/kallmodsyms in the same order, the only modules it names are loadable ones, which are necessarily in single contiguous blocks and thus shown contiguously. The result looks like this: ([...] to show where lines are omitted for brevity): ffffffff97606e50 t not_visible ffffffff97606e70 T perf_msr_probe ffffffff97606f80 t test_msr [rapl] ffffffff97606fa0 t __rapl_pmu_event_start [rapl] [...] ffffffffa6007350 t rapl_pmu_event_stop [rapl] ffffffffa6007440 t rapl_pmu_event_del [rapl] ffffffffa6007460 t rapl_hrtimer_handle [rapl] ffffffffa6007500 t rapl_pmu_event_read [rapl] ffffffffa6007520 t rapl_pmu_event_init [rapl] ffffffffa6007630 t rapl_cpu_offline [rapl] ffffffffa6007710 t amd_pmu_event_map {core.o} ffffffffa6007750 t amd_pmu_add_event {core.o} ffffffffa6007760 t amd_put_event_constraints_f17h {core.o} The [rapl] notation is emitted even if rapl is built into the kernel (but, obviously, not if it's not in the .config at all, or is in a loadable module that is not loaded). The {core.o} is an object file name. Further down, we see what happens when object files are reused by multiple modules, all of which are built in to the kernel, and some of which have symbols that would be ambiguous without an object file name attached in addition to the module names: ffffffff97d7aed0 t liquidio_pcie_mmio_enabled [liquidio] ffffffff97d7aef0 t liquidio_pcie_resume [liquidio] ffffffff97d7af00 t liquidio_ptp_adjtime [liquidio] ffffffff97d7af50 t liquidio_ptp_enable [liquidio] ffffffff97d7af70 t liquidio_get_stats64 [liquidio] ffffffff97d7b0f0 t liquidio_fix_features [liquidio] ffffffff97d7b1c0 t liquidio_get_port_parent_id [liquidio] [...] ffffffff97d824c0 t lio_vf_rep_modinit [liquidio] ffffffff97d824f0 t lio_vf_rep_modexit [liquidio] ffffffff97d82520 t lio_ethtool_get_channels [liquidio] [liquidio_vf] ffffffff97d82600 t lio_ethtool_get_ringparam [liquidio] [liquidio_vf] ffffffff97d826a0 t lio_get_msglevel [liquidio] [liquidio_vf] ffffffff97d826c0 t lio_vf_set_msglevel [liquidio] [liquidio_vf] ffffffff97d826e0 t lio_get_pauseparam [liquidio] [liquidio_vf] ffffffff97d82710 t lio_get_ethtool_stats [liquidio] [liquidio_vf] ffffffff97d82e70 t lio_vf_get_ethtool_stats [liquidio] [liquidio_vf] [...] ffffffff97d91a80 t cn23xx_vf_mbox_thread [liquidio] [liquidio_vf] {cn23xx_vf_device.o} ffffffff97d91aa0 t cpumask_weight.constprop.0 [liquidio] [liquidio_vf] {cn23xx_vf_device.o} ffffffff97d91ac0 t cn23xx_vf_msix_interrupt_handler [liquidio] [liquidio_vf] {cn23xx_vf_device.o} ffffffff97d91bd0 t cn23xx_vf_get_oq_ticks [liquidio] [liquidio_vf] {cn23xx_vf_device.o} ffffffff97d91c00 t cn23xx_vf_ask_pf_to_do_flr [liquidio] [liquidio_vf] {cn23xx_vf_device.o} ffffffff97d91c70 t cn23xx_octeon_pfvf_handshake [liquidio] [liquidio_vf] {cn23xx_vf_device.o} ffffffff97d91e20 t cn23xx_setup_octeon_vf_device [liquidio] [liquidio_vf] {cn23xx_vf_device.o} ffffffff97d92060 t octeon_mbox_read [liquidio] [liquidio_vf] ffffffff97d92230 t octeon_mbox_write [liquidio] [liquidio_vf] [...] ffffffff97d946b0 t octeon_alloc_soft_command_resp [liquidio] [liquidio_vf] ffffffff97d947e0 t octnet_send_nic_data_pkt [liquidio] [liquidio_vf] ffffffff97d94820 t octnet_send_nic_ctrl_pkt [liquidio] [liquidio_vf] ffffffff97d94ab0 t liquidio_get_stats64 [liquidio_vf] ffffffff97d94c10 t liquidio_fix_features [liquidio_vf] ffffffff97d94cd0 t wait_for_pending_requests [liquidio_vf] Like /proc/kallsyms, the output is driven by address, so keeps the curious property of /proc/kallsyms that symbols may appear repeatedly with different addresses: but now, unlike in /proc/kallsyms, we can see that those symbols appear repeatedly because they are *different symbols* that ultimately belong to different modules or different object files within a module, all of which are built in to the kernel. As with /proc/kallsyms, non-root usage produces addresses that are all zero. I am not wedded to the name or format of /proc/kallmodsyms, but felt it best to split it out of /proc/kallsyms to avoid breaking existing kallsyms parsers. This is currently driven by a new config option, but now that kallmodsyms data uses very little space, this option might be something people don't want to bother with: maybe we can just control it via CONFIG_KALLSYMS or something. Internally, this uses a new kallsyms_builtin_module_address() almost identical to kallsyms_sym_address() to get the address corresponding to a given .kallsyms_modules index, and a new get_builtin_modobj_idx quite similar to get_symbol_pos to determine the index in the .kallsyms_modules and .kallsyms_objfiles arrays that relate to a given address. We save a little time by exploiting the fact that all callers will only ever traverse this list from start to end by allowing them to pass in the previous index returned from this function as a hint: thus very few bsearches are actually needed. (In theory this could change to just walk straight down kallsyms_module_addresses/offsets and not bother bsearching at all, but doing it this way is hardly any slower and much more robust.) We explicitly filter out displaying modules for non-text symbols (perhaps this could be lifted for initialized data symbols in future). There might be occasional incorrect module or objfile names for section start/end symbols. The display process is complicated a little by the weird format of the .kallsyms_mod_objnames table: we have to look for multimodule entries and print them as space-separated lists of module names. Signed-off-by: Nick Alcock Reviewed-by: Kris Van Hees --- Notes: v9: add objfile support. Commit message adjustments. kernel/kallsyms.c | 277 ++++++++++++++++++++++++++++++++++--- kernel/kallsyms_internal.h | 14 ++ 2 files changed, 274 insertions(+), 17 deletions(-) diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index 60c20f301a6b..9667962173f1 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -187,6 +187,25 @@ static bool cleanup_symbol_name(char *s) return false; } +#ifdef CONFIG_KALLMODSYMS +static unsigned long kallsyms_builtin_module_address(int idx) +{ + if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE)) + return kallsyms_module_addresses[idx]; + + /* values are unsigned offsets if --absolute-percpu is not in effect */ + if (!IS_ENABLED(CONFIG_KALLSYMS_ABSOLUTE_PERCPU)) + return kallsyms_relative_base + (u32)kallsyms_module_offsets[idx]; + + /* ...otherwise, positive offsets are absolute values */ + if (kallsyms_module_offsets[idx] >= 0) + return kallsyms_module_offsets[idx]; + + /* ...and negative offsets are relative to kallsyms_relative_base - 1 */ + return kallsyms_relative_base - 1 - kallsyms_module_offsets[idx]; +} +#endif + /* Lookup the address for this symbol. Returns 0 if not found. */ unsigned long kallsyms_lookup_name(const char *name) { @@ -293,6 +312,54 @@ static unsigned long get_symbol_pos(unsigned long addr, return low; } +/* + * The caller passes in an address, and we return an index to the corresponding + * builtin module index in .kallsyms_modules and .kallsyms_objfiles, or + * (unsigned long) -1 if none match. + * + * The hint_idx, if set, is a hint as to the possible return value, to handle + * the common case in which consecutive runs of addresses relate to the same + * index. + */ +#ifdef CONFIG_KALLMODSYMS +static unsigned long get_builtin_modobj_idx(unsigned long addr, unsigned long hint_idx) +{ + unsigned long low, high, mid; + + if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE)) + BUG_ON(!kallsyms_module_addresses); + else + BUG_ON(!kallsyms_module_offsets); + + /* + * Do a binary search on the sorted kallsyms_modules array. The last + * entry in this array indicates the end of the text section, not an + * object file. + */ + low = 0; + high = kallsyms_num_modules - 1; + + if (hint_idx > low && hint_idx < (high - 1) && + addr >= kallsyms_builtin_module_address(hint_idx) && + addr < kallsyms_builtin_module_address(hint_idx + 1)) + return hint_idx; + + if (addr >= kallsyms_builtin_module_address(low) + && addr < kallsyms_builtin_module_address(high)) { + while (high - low > 1) { + mid = low + (high - low) / 2; + if (kallsyms_builtin_module_address(mid) <= addr) + low = mid; + else + high = mid; + } + return low; + } + + return (unsigned long) -1; +} +#endif + /* * Lookup an address but don't bother to find any names. */ @@ -564,6 +631,9 @@ struct kallsym_iter { char type; char name[KSYM_NAME_LEN]; char module_name[MODULE_NAME_LEN]; + const char *builtin_module_names; + const char *builtin_objfile_name; + unsigned long hint_builtin_modobj_idx; int exported; int show_value; }; @@ -594,6 +664,9 @@ static int get_ksymbol_mod(struct kallsym_iter *iter) &iter->value, &iter->type, iter->name, iter->module_name, &iter->exported); + iter->builtin_module_names = NULL; + iter->builtin_objfile_name = NULL; + if (ret < 0) { iter->pos_mod_end = iter->pos; return 0; @@ -613,6 +686,9 @@ static int get_ksymbol_ftrace_mod(struct kallsym_iter *iter) &iter->value, &iter->type, iter->name, iter->module_name, &iter->exported); + iter->builtin_module_names = NULL; + iter->builtin_objfile_name = NULL; + if (ret < 0) { iter->pos_ftrace_mod_end = iter->pos; return 0; @@ -627,6 +703,8 @@ static int get_ksymbol_bpf(struct kallsym_iter *iter) strlcpy(iter->module_name, "bpf", MODULE_NAME_LEN); iter->exported = 0; + iter->builtin_module_names = NULL; + iter->builtin_objfile_name = NULL; ret = bpf_get_kallsym(iter->pos - iter->pos_ftrace_mod_end, &iter->value, &iter->type, iter->name); @@ -647,23 +725,74 @@ static int get_ksymbol_kprobe(struct kallsym_iter *iter) { strlcpy(iter->module_name, "__builtin__kprobes", MODULE_NAME_LEN); iter->exported = 0; + iter->builtin_module_names = NULL; + iter->builtin_objfile_name = NULL; return kprobe_get_kallsym(iter->pos - iter->pos_bpf_end, &iter->value, &iter->type, iter->name) < 0 ? 0 : 1; } /* Returns space to next name. */ -static unsigned long get_ksymbol_core(struct kallsym_iter *iter) +static unsigned long get_ksymbol_core(struct kallsym_iter *iter, int kallmodsyms) { unsigned off = iter->nameoff; - iter->module_name[0] = '\0'; + iter->exported = 0; iter->value = kallsyms_sym_address(iter->pos); iter->type = kallsyms_get_symbol_type(off); + iter->module_name[0] = '\0'; + iter->builtin_module_names = NULL; + iter->builtin_objfile_name = NULL; + off = kallsyms_expand_symbol(off, iter->name, ARRAY_SIZE(iter->name)); +#ifdef CONFIG_KALLMODSYMS + if (kallmodsyms) { + unsigned long modobj_idx = (unsigned long) -1; + + if (kallsyms_module_offsets) + modobj_idx = + get_builtin_modobj_idx(iter->value, + iter->hint_builtin_modobj_idx); + /* + * This is a built-in module iff the tables of built-in modules + * (address->module name mappings), object files (ditto), and + * module/objfile names are known, and if the address was found + * there, and if the corresponding module index is nonzero, and + * iff this is a text (or weak) symbol. All other cases mean + * off the end of the binary or in a non-modular range in + * between one or more modules. + * + * The same rules are true for kallsyms_objfiles, except that + * zero entries are much more common because we only record + * object file names if we need them to disambiguate one or more + * symbols: see scripts/kallsyms.c:disambiguate_syms. + * + * (Also guard against corrupt kallsyms_modules or + * kallsyms_objfiles arrays pointing off the end of + * kallsyms_mod_objnames.) + */ + if (kallsyms_modules != NULL && kallsyms_mod_objnames != NULL && + kallsyms_objfiles != NULL && + (iter->type == 't' || iter->type == 'T' || + iter->type == 'w' || iter->type == 'W') && + modobj_idx != (unsigned long) -1) { + + if (kallsyms_modules[modobj_idx] != 0 && + kallsyms_modules[modobj_idx] < kallsyms_mod_objnames_len) + iter->builtin_module_names = + &kallsyms_mod_objnames[kallsyms_modules[modobj_idx]]; + + if (kallsyms_objfiles[modobj_idx] != 0 && + kallsyms_objfiles[modobj_idx] < kallsyms_mod_objnames_len) + iter->builtin_objfile_name = + &kallsyms_mod_objnames[kallsyms_objfiles[modobj_idx]]; + } + iter->hint_builtin_modobj_idx = modobj_idx; + } +#endif return off - iter->nameoff; } @@ -709,7 +838,7 @@ static int update_iter_mod(struct kallsym_iter *iter, loff_t pos) } /* Returns false if pos at or past end of file. */ -static int update_iter(struct kallsym_iter *iter, loff_t pos) +static int update_iter(struct kallsym_iter *iter, loff_t pos, int kallmodsyms) { /* Module symbols can be accessed randomly. */ if (pos >= kallsyms_num_syms) @@ -719,7 +848,7 @@ static int update_iter(struct kallsym_iter *iter, loff_t pos) if (pos != iter->pos) reset_iter(iter, pos); - iter->nameoff += get_ksymbol_core(iter); + iter->nameoff += get_ksymbol_core(iter, kallmodsyms); iter->pos++; return 1; @@ -729,14 +858,14 @@ static void *s_next(struct seq_file *m, void *p, loff_t *pos) { (*pos)++; - if (!update_iter(m->private, *pos)) + if (!update_iter(m->private, *pos, 0)) return NULL; return p; } static void *s_start(struct seq_file *m, loff_t *pos) { - if (!update_iter(m->private, *pos)) + if (!update_iter(m->private, *pos, 0)) return NULL; return m->private; } @@ -745,7 +874,7 @@ static void s_stop(struct seq_file *m, void *p) { } -static int s_show(struct seq_file *m, void *p) +static int s_show_internal(struct seq_file *m, void *p, int kallmodsyms) { void *value; struct kallsym_iter *iter = m->private; @@ -756,23 +885,82 @@ static int s_show(struct seq_file *m, void *p) value = iter->show_value ? (void *)iter->value : NULL; - if (iter->module_name[0]) { + /* + * Real module, or built-in module and /proc/kallsyms being shown. + */ + if (iter->module_name[0] != '\0' || + (iter->builtin_module_names != NULL && kallmodsyms != 0)) { char type; /* - * Label it "global" if it is exported, - * "local" if not exported. + * Label it "global" if it is exported, "local" if not exported. */ type = iter->exported ? toupper(iter->type) : tolower(iter->type); - seq_printf(m, "%px %c %s\t[%s]\n", value, - type, iter->name, iter->module_name); - } else - seq_printf(m, "%px %c %s\n", value, +#ifdef CONFIG_KALLMODSYMS + if (kallmodsyms) { + /* + * /proc/kallmodsyms, built as a module. + */ + if (iter->builtin_module_names == NULL) + seq_printf(m, "%px %c %s\t[%s]", value, + type, iter->name, + iter->module_name); + /* + * /proc/kallmodsyms, single-module symbol. + */ + else if (*iter->builtin_module_names != '\0') + seq_printf(m, "%px %c %s\t[%s]", value, + type, iter->name, + iter->builtin_module_names); + /* + * /proc/kallmodsyms, multimodule symbol. Formatted + * as \0MODULE_COUNTmodule-1\0module-2\0, where + * MODULE_COUNT is a single byte, 2 or higher. + */ + else { + size_t i = *(char *)(iter->builtin_module_names + 1); + const char *walk = iter->builtin_module_names + 2; + + seq_printf(m, "%px %c %s\t[%s]", value, + type, iter->name, walk); + + while (--i > 0) { + walk += strlen(walk) + 1; + seq_printf(m, " [%s]", walk); + } + } + /* + * Possibly there is an objfile name too, if needed to + * disambiguate at least one symbol. + */ + if (iter->builtin_objfile_name) + seq_printf(m, " {%s.o}", iter->builtin_objfile_name); + + seq_printf(m, "\n"); + } else /* !kallmodsyms */ +#endif /* CONFIG_KALLMODSYMS */ + seq_printf(m, "%px %c %s\t[%s]\n", value, + type, iter->name, iter->module_name); + } else { + seq_printf(m, "%px %c %s", value, iter->type, iter->name); +#ifdef CONFIG_KALLMODSYMS + if (kallmodsyms) { + if (iter->builtin_objfile_name) + seq_printf(m, "\t{%s.o}", iter->builtin_objfile_name); + } +#endif /* CONFIG_KALLMODSYMS */ + seq_printf(m, "\n"); + } return 0; } +static int s_show(struct seq_file *m, void *p) +{ + return s_show_internal(m, p, 0); +} + static const struct seq_operations kallsyms_op = { .start = s_start, .next = s_next, @@ -780,6 +968,36 @@ static const struct seq_operations kallsyms_op = { .show = s_show }; +#ifdef CONFIG_KALLMODSYMS +static int s_mod_show(struct seq_file *m, void *p) +{ + return s_show_internal(m, p, 1); +} + +static void *s_mod_next(struct seq_file *m, void *p, loff_t *pos) +{ + (*pos)++; + + if (!update_iter(m->private, *pos, 1)) + return NULL; + return p; +} + +static void *s_mod_start(struct seq_file *m, loff_t *pos) +{ + if (!update_iter(m->private, *pos, 1)) + return NULL; + return m->private; +} + +static const struct seq_operations kallmodsyms_op = { + .start = s_mod_start, + .next = s_mod_next, + .stop = s_stop, + .show = s_mod_show +}; +#endif + #ifdef CONFIG_BPF_SYSCALL struct bpf_iter__ksym { @@ -905,7 +1123,8 @@ bool kallsyms_show_value(const struct cred *cred) } } -static int kallsyms_open(struct inode *inode, struct file *file) +static int kallsyms_open_internal(struct inode *inode, struct file *file, + const struct seq_operations *ops) { /* * We keep iterator in m->private, since normal case is to @@ -913,7 +1132,7 @@ static int kallsyms_open(struct inode *inode, struct file *file) * using get_symbol_offset for every symbol. */ struct kallsym_iter *iter; - iter = __seq_open_private(file, &kallsyms_op, sizeof(*iter)); + iter = __seq_open_private(file, ops, sizeof(*iter)); if (!iter) return -ENOMEM; reset_iter(iter, 0); @@ -926,6 +1145,18 @@ static int kallsyms_open(struct inode *inode, struct file *file) return 0; } +static int kallsyms_open(struct inode *inode, struct file *file) +{ + return kallsyms_open_internal(inode, file, &kallsyms_op); +} + +#ifdef CONFIG_KALLMODSYMS +static int kallmodsyms_open(struct inode *inode, struct file *file) +{ + return kallsyms_open_internal(inode, file, &kallmodsyms_op); +} +#endif + #ifdef CONFIG_KGDB_KDB const char *kdb_walk_kallsyms(loff_t *pos) { @@ -936,7 +1167,7 @@ const char *kdb_walk_kallsyms(loff_t *pos) reset_iter(&kdb_walk_kallsyms_iter, 0); } while (1) { - if (!update_iter(&kdb_walk_kallsyms_iter, *pos)) + if (!update_iter(&kdb_walk_kallsyms_iter, *pos, 0)) return NULL; ++*pos; /* Some debugging symbols have no name. Ignore them. */ @@ -953,9 +1184,21 @@ static const struct proc_ops kallsyms_proc_ops = { .proc_release = seq_release_private, }; +#ifdef CONFIG_KALLMODSYMS +static const struct proc_ops kallmodsyms_proc_ops = { + .proc_open = kallmodsyms_open, + .proc_read = seq_read, + .proc_lseek = seq_lseek, + .proc_release = seq_release_private, +}; +#endif + static int __init kallsyms_init(void) { proc_create("kallsyms", 0444, NULL, &kallsyms_proc_ops); +#ifdef CONFIG_KALLMODSYMS + proc_create("kallmodsyms", 0444, NULL, &kallmodsyms_proc_ops); +#endif return 0; } device_initcall(kallsyms_init); diff --git a/kernel/kallsyms_internal.h b/kernel/kallsyms_internal.h index 2d0c6f2f0243..0ee6d97b732e 100644 --- a/kernel/kallsyms_internal.h +++ b/kernel/kallsyms_internal.h @@ -22,8 +22,22 @@ __section(".rodata") __attribute__((weak)); extern const unsigned long kallsyms_relative_base __section(".rodata") __attribute__((weak)); +extern const unsigned long kallsyms_num_modules +__section(".rodata") __attribute__((weak)); + +extern const unsigned long kallsyms_num_objfiles +__section(".rodata") __attribute__((weak)); + +extern const unsigned long kallsyms_mod_objnames_len +__section(".rodata") __attribute__((weak)); + extern const char kallsyms_token_table[] __weak; extern const u16 kallsyms_token_index[] __weak; +extern const unsigned long kallsyms_module_addresses[] __weak; +extern const int kallsyms_module_offsets[] __weak; +extern const u32 kallsyms_modules[] __weak; +extern const u32 kallsyms_objfiles[] __weak; +extern const char kallsyms_mod_objnames[] __weak; extern const unsigned int kallsyms_markers[] __weak; From patchwork Thu Oct 27 19:57:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 13022684 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 315F0FA3743 for ; Thu, 27 Oct 2022 19:59:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236899AbiJ0T7Y (ORCPT ); Thu, 27 Oct 2022 15:59:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236877AbiJ0T7A (ORCPT ); Thu, 27 Oct 2022 15:59:00 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 655BF89938; Thu, 27 Oct 2022 12:58:52 -0700 (PDT) Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 29RHmpfx001409; Thu, 27 Oct 2022 19:58:45 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2022-7-12; bh=zgGkXDjkyMHjdfNQzVhUVuVo36dzABRxbA++2XsbHco=; b=dSwcGvZoJJNoeiLaE3Bs190GiWGcaknJ9OoKnDaWpFjPiD8lsJY5KbWFebrZcIKZjEwL nW01DJ9rH2PL8NPwRM1dwZZUge6KSuSZEotbJb7MCMGrOHlNUxq0Xw02XuyGBgVXIs4X /3SRk9dTeBls4igOqJqQzqCtrujJKpWFI24/40rHfWOpPSN2fdSEdm2sUxA/8k0437rT Uw7DTgl263DmBhVM1bHHsKKvepA7phRRQgyBnXZwTc/ciBVFrMC7lLttjhqUc26Iidl0 YfOXU/4Sz1OSj1wTvLYKG3Lbo3nLuLvBxSkCu9rP4zvPaYH4FPLKSkXXU6X5kU9Fwp7e TQ== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3kfawru8db-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 27 Oct 2022 19:58:45 +0000 Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 29RIR3mq033439; Thu, 27 Oct 2022 19:58:44 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2101.outbound.protection.outlook.com [104.47.58.101]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3kfagnbyf2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 27 Oct 2022 19:58:44 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=B3DX39nxGJPN1hbTAw7jz1nJWLyUquLj2N8taAom9zSnU0N8WjXywBpt0ojs4+ownjc/g8ogZuOB6RnBeg31PzUa+9L3PnmTTsinkdi/YS5GoonOxXF+kjevtZTGG1TMG/nnojI68yi+FcnFHrFTkUx/fOsu7pXWl4jV8M5bEwLNxiUyWA0QmnPn9RSYnNlM3n1hL9A7hss7VqB710prgCRX+Hwp7EgB2sdbTf+boCikSs4B7DIQlYtz1h1GSaly42aMKENKt32OMiDMgP0GGHxND8RGucBIdI+WRbuOwp++tqXpE8pk5UE0N4DRTkk5INIoGnyaf/W8c+ypKuf6bw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=zgGkXDjkyMHjdfNQzVhUVuVo36dzABRxbA++2XsbHco=; b=n+PY2AcBh5Qo8pM9+anJOpP7my3/vFOFeTXJYoIprUqLlHiKgoO2z9+d1pwnOKOzE3bppmts7Qzm0In/tOrQAu3NdNV03kGsYQRa/ocD/yovGB+xooytqantArscFj5KQ8NJLZqztp05yH05LVtYO54IvE1ETBVQrav2fX+RJtsIFWrH3c/vkFCqAEy86irsC1QHAHFQtBYIPlEDrlVubpa1hoNtJWZerDAIh5V+orQv19EYl0tcstcFXcj/qq/asVhf/QYZMLqzubACw6rjtLl6hPWtk0uJ701rS1VcbFC3I5Wfn/IElmKbykrsczxH6VKQ8COxKsC2X9KVLVwGxA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=zgGkXDjkyMHjdfNQzVhUVuVo36dzABRxbA++2XsbHco=; b=C+pem6NjRoq5t01MhOc8TaF7TIROGwil1vNnqCwRTiLuFx7aC5UbOrn8xSG05DxkMr3ZF3sJ0dahhaMPWQssKSe+IGYQ2TQrjeyw826+lZXkC5WiSuvLK134ySba7r3srIHsog2SvEbeY2QGUkB5l0n7c4cQXsxu14qN59H+Juw= Received: from DS0PR10MB6798.namprd10.prod.outlook.com (2603:10b6:8:13c::20) by CY8PR10MB6778.namprd10.prod.outlook.com (2603:10b6:930:99::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5746.28; Thu, 27 Oct 2022 19:58:41 +0000 Received: from DS0PR10MB6798.namprd10.prod.outlook.com ([fe80::ff03:d749:df80:38e1]) by DS0PR10MB6798.namprd10.prod.outlook.com ([fe80::ff03:d749:df80:38e1%7]) with mapi id 15.20.5746.028; Thu, 27 Oct 2022 19:58:41 +0000 From: Nick Alcock To: mcgrof@kernel.org, rostedt@goodmis.org, masahiroy@kernel.org Cc: linux-trace-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, akpm@linux-foundation.org, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v9 8/8] perf: proof-of-concept kallmodsyms support Date: Thu, 27 Oct 2022 20:57:54 +0100 Message-Id: <20221027195754.100039-9-nick.alcock@oracle.com> X-Mailer: git-send-email 2.38.0.266.g481848f278 In-Reply-To: <20221027195754.100039-1-nick.alcock@oracle.com> References: <20221027195754.100039-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO4P265CA0135.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:2c4::10) To DS0PR10MB6798.namprd10.prod.outlook.com (2603:10b6:8:13c::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS0PR10MB6798:EE_|CY8PR10MB6778:EE_ X-MS-Office365-Filtering-Correlation-Id: b4537590-832e-4840-7184-08dab855a54e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: AOZxeadD1fhoe7r9YqZY32bJ7z3OgDAMhvWd6gOOSKRq7OPl2YNfxSEvvOry9mPANKH7N1LPZ/By4ZS5SRUJ+x3h6xcHFSfY+kfM+X6t05ysIOnWKc4AYtFDG/yrGIA6WBgPhZrhCKDBs6M2V4FtXvSGuELfe8epRwjg6T25PFSDazUrtuJ55FpMmV8wC2cZ6Iy71YXVspyT75AiAFMoX7HKRV6/uDCV+ZIyhE+SHzFwp2+s9NR7U3IxYwgM5uImOHZyD0m5wBGw4OZgm5d9wiHH4alg/oET2V4+v6Fq6FCaxgxcLw6Kh2ekTLeoh9cQGouzBrE06+EeAteeyVPKdR0f4ishzOBFmSj/S0A8jFnto2ebB/aU5jONKpqA146gtZ8z7GGGK6foZAD5SN1zxM5HfSv10te6CJPbXiDFdmBIx9ULV+IxzijmlPXW78L3GADUgRZckGGViZz/Lw9oKbRo/5h4ZwWjUqHtnuFMqjx6fzoL74UwRoy7TqpgCwujPDWY7ZiR/QqUD0unL4QeG64cfb1RFChFYHr2yuZOAXfAdPU3n9QPpC/6zBnFsa9jja2T0+vckQVDqarhzH2dUj2cdCj5JwGrDN6zgLNz6pgXD9hUfTAaXf8m2ff/CRuJwuqR3cvKkcPF+/bUtZ2VGSJlxDqb0ts6c6MtQQrEeg8jKovmWB5k723q8Mf92leWZbEKESPvMLdAPOFqNcjIWw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS0PR10MB6798.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(376002)(366004)(396003)(346002)(39860400002)(136003)(451199015)(6486002)(83380400001)(41300700001)(5660300002)(30864003)(478600001)(8676002)(6666004)(107886003)(66946007)(4326008)(66476007)(316002)(44832011)(8936002)(66556008)(1076003)(86362001)(2616005)(6512007)(186003)(2906002)(36756003)(38100700002)(6506007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: hx/d9rCXYS6qhaqsIasoL4CHzwWt1TqbFCdLsaaSO5dJnPHA0S+ftZL5nO6zHmMY0AOR8oILcZLRy+fAa9ZqLtqQiI3vbcVZ7aITPRK/MDJbPwtsVA8dVTyb6aJrBfFZjxx9cM0BIwaREPYMHLn/jnOBvkaHZD5md1xpi3B9BFz8Sb/GDKHKrsTsbakFirA7+XgLkg2GENeErxL8G5vQqcHqFpwE668+cR7YcJu4QAW3pXE1TfhucpJtzpG2/ezA5klGmgDwGsTYylGO+ZuSt2sVn8sg+pqn7QGMiXruQxAYkMj+JKaF3qNKjBSHl8S7is2iFZjrrFI9XEDP1rZq8VSNS8iTeeVcTvm4xCkU4oMl2rbFPLYn+EOuvbBv+jqtEPNtaXaXNwBlj4UNLSJ8mcjJGQW432OskJoy4x78CpvOYgpKhcn0xoK2L0vrG3VOHptpDGFuND6scqdS8do6kplbcdlp20mYMpB+o97Gs5x5RuacFIUKTZqwCJtdR7dUcty8qhe8IsvMvdrOLv5v3bnRVk9VzI2y2KtLU4H/VVSG1v1ezKQuUJog1RI9Ee1xg3Wv7MDmUeZAx+M3+soLdJMhCpRp2sCbtsj9Kr24MEz3RPKcrgMU/e286/jId3H/Iab2vGFP8tTVDc3JKiPRubFeZBZkXt1uQEIJ9GT/1IXLrVdsSE549IIvh+9hdjNkCY2KX/8K/KUQjA7Xlk7UGAHEYVM8L05eoOP5POUu/0OBJaVaAcK6KiZeZK8X2h5yCCaTlolni1CKRx+N0Lj/8BW47InH2OhbE7oT8AltBcUkv/RwGB7vsd4s5SykDjRLZH+roaPAi/B6vU5EyOtJfcW3FYhQaTKKc9OixCy5UBvgJnrCeOYrQkrcwqgQI4/eqtXt7R+xEEAsYbUSot6DhM6AN0vZfY8r+oeOikWGvsNiPtY/BF9sCaAjD4SJMsft2x15pvLq44tW1PicVYeJYdauzww7f6GOGVstznj81hpbob4Wcj1+uG944ErnjjekHD7My+wxHte5mb0+iBC2MpOQQ4U3M+KVMBk1DL8/13nvKQkHGqZPtnShOP/h/VnZmmjhPPpsHVVe5VfRpRU5ear4o87zowEbcdACvuNlrnDHxpz5czwp96C6K3Z9tS/gKiBTfaVzfQeXwNuz1ptyxkrkgN1GKk8iUP3fVzczcUwOSu1dPtTRhXB4ZGn8BB8ir8qVBbScAOoCH85XwQQj62RdCEe11zQLBw6/PJ6g1K1G0Le6Iwr97HIM5YMMexO+SRvPm4XVhV8hV34ICtrgj4KqjPsA60YNjEc/nQwa2+OWkN6YA9IPk4qIPCebOJl4hVNNIS5BAFSnpS+miqf6irkvNwMUj3ky6i2vVXIYwYnT6EsTtnWEvr5GkYRZ3lzG9QV3iGA2OuOos5bXR+qkHjkmVTruS5sPgJrcqRHLvClBvXFBqm52m6hwx9GbjxA+E8o9kumGqNHK3LD4AnXMmBzcH9srtNjiAooNamLOa6IsTaz57qjsnFwztd2l2XhL9H5FO0bPuOn4++KwWtQ70c9eD4rkGRXLDSWE+HeY1iDZyJ3AIqSgSgccAGlx6Rc2h8Aunb+aM8onRhLZYFxK2i6aT3KtFbGGCKT/7AOXhZg= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: b4537590-832e-4840-7184-08dab855a54e X-MS-Exchange-CrossTenant-AuthSource: DS0PR10MB6798.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Oct 2022 19:58:41.3814 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: AN8YhgpMgL5K45XCzoUj4t31KwkG+uOqSN6z84F5ikAuEiwbzl45Hi7VjhV+WxfWXIvT9OdswqYuxFSOIkC/jg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR10MB6778 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-10-27_07,2022-10-27_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 adultscore=0 mlxlogscore=999 malwarescore=0 suspectscore=0 phishscore=0 mlxscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2210270112 X-Proofpoint-ORIG-GUID: HGaWHbYFNWF1Fr8xVuHpphkLbl6JQ7pv X-Proofpoint-GUID: HGaWHbYFNWF1Fr8xVuHpphkLbl6JQ7pv Precedence: bulk List-ID: X-Mailing-List: linux-trace-kernel@vger.kernel.org This is only very partial: it adds support to 'perf kallsyms', allowing you to say, e.g. % ./perf kallsyms __scsi_device_lookup __scsi_device_lookup: scsi_mod (built-in) 0xffffffff9b901de0-0xffffffff9b901e30 (0xffffffff9b901de0-0xffffffff9b901e30) and get told that this is in a built-in module. We also handle symbols that are in multiple modules at once: % ./perf kallsyms lio_set_fecparam lio_set_fecparam: liquidio, liquidio_vf (built-in) 0xffffffff9b934da0-0xffffffff9b934e10 (0xffffffff9b934da0-0xffffffff9b934e10) We do this the simplistic way, by augmenting symbols with a module array field, the members of which are atoms in a new machine.modules red-black tree (structured the same, and managed with the same code, as the existing transient tree that is used to compare /proc/modules against each other). kallsyms symbols no longer carry a [module name] around with them that needs cutting off whenever it's used (all users that relied on this adjusted, I hope) but instead have that name hived off into the per-symbol module array, with a new 'built_in' field to tell users whether this is a built-in module or not. Since we cannot use the presence of '[' to detect modules any more, we do it at kallmodsyms read time by spotting _end and considering it to denote the end of the core kernel and the start of the modular range. (I *think* this works on all arches.) Signed-off-by: Nick Alcock Reviewed-by: Kris Van Hees --- tools/perf/builtin-kallsyms.c | 35 +++++- tools/perf/util/event.c | 14 ++- tools/perf/util/machine.c | 6 +- tools/perf/util/machine.h | 1 + tools/perf/util/symbol.c | 207 +++++++++++++++++++++++++--------- tools/perf/util/symbol.h | 12 +- 6 files changed, 211 insertions(+), 64 deletions(-) diff --git a/tools/perf/builtin-kallsyms.c b/tools/perf/builtin-kallsyms.c index c08ee81529e8..6bcec2522d2d 100644 --- a/tools/perf/builtin-kallsyms.c +++ b/tools/perf/builtin-kallsyms.c @@ -35,10 +35,37 @@ static int __cmd_kallsyms(int argc, const char **argv) continue; } - printf("%s: %s %s %#" PRIx64 "-%#" PRIx64 " (%#" PRIx64 "-%#" PRIx64")\n", - symbol->name, map->dso->short_name, map->dso->long_name, - map->unmap_ip(map, symbol->start), map->unmap_ip(map, symbol->end), - symbol->start, symbol->end); + if (!symbol->modules) { + printf("%s: %s %s %#" PRIx64 "-%#" PRIx64 " (%#" PRIx64 "-%#" PRIx64")\n", + symbol->name, map->dso->short_name, map->dso->long_name, + map->unmap_ip(map, symbol->start), map->unmap_ip(map, symbol->end), + symbol->start, symbol->end); + } else { + if (!symbol->built_in) + printf("%s: %s %s %#" PRIx64 "-%#" PRIx64 " (%#" PRIx64 "-%#" PRIx64")\n", + symbol->name, map->dso->short_name, map->dso->long_name, + map->unmap_ip(map, symbol->start), map->unmap_ip(map, symbol->end), + symbol->start, symbol->end); + else if (symbol->modules[1] == 0) + printf("%s: %s (built-in) %#" PRIx64 "-%#" PRIx64 " (%#" PRIx64 "-%#" PRIx64")\n", + symbol->name, symbol->modules[0], map->unmap_ip(map, symbol->start), + map->unmap_ip(map, symbol->end), symbol->start, symbol->end); + else { /* Symbol in multiple modules at once */ + char **mod; + + printf("%s: ", symbol->name); + + for (mod = symbol->modules; *mod; mod++) { + if (mod != symbol->modules) + printf(", "); + printf("%s", *mod); + } + + printf (" (built-in) %#" PRIx64 "-%#" PRIx64 " (%#" PRIx64 "-%#" PRIx64")\n", + map->unmap_ip(map, symbol->start), map->unmap_ip(map, symbol->end), + symbol->start, symbol->end); + } + } } machine__delete(machine); diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c index 1fa14598b916..a344b35f7e38 100644 --- a/tools/perf/util/event.c +++ b/tools/perf/util/event.c @@ -97,16 +97,28 @@ static int find_symbol_cb(void *arg, const char *name, char type, u64 start) { struct process_symbol_args *args = arg; + char *chop, *tmp_alloc = NULL; + const char *tmp = name; + + if ((chop = strchr(name, '\t')) != NULL) { + tmp_alloc = strndup(name, name - chop); + if (tmp_alloc == NULL) + return -ENOMEM; + tmp = tmp_alloc; + } /* * Must be a function or at least an alias, as in PARISC64, where "_text" is * an 'A' to the same address as "_stext". */ if (!(kallsyms__is_function(type) || - type == 'A') || strcmp(name, args->name)) + type == 'A') || strcmp(tmp, args->name)) { + free(tmp_alloc); return 0; + } args->start = start; + free(tmp_alloc); return 1; } diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 76316e459c3d..2be5a3c1a267 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -173,7 +173,7 @@ struct machine *machine__new_kallsyms(void) * ask for not using the kcore parsing code, once this one is fixed * to create a map per module. */ - if (machine && machine__load_kallsyms(machine, "/proc/kallsyms") <= 0) { + if (machine && machine__load_kallsyms(machine, "/proc/kallmodsyms") <= 0) { machine__delete(machine); machine = NULL; } @@ -237,6 +237,7 @@ void machine__exit(struct machine *machine) zfree(&machine->mmap_name); zfree(&machine->current_tid); zfree(&machine->kallsyms_filename); + modules__delete_modules(&machine->modules); for (i = 0; i < THREADS__TABLE_SIZE; i++) { struct threads *threads = &machine->threads[i]; @@ -1410,7 +1411,8 @@ int machines__create_kernel_maps(struct machines *machines, pid_t pid) int machine__load_kallsyms(struct machine *machine, const char *filename) { struct map *map = machine__kernel_map(machine); - int ret = __dso__load_kallsyms(map->dso, filename, map, true); + int ret = __dso__load_kallsyms(map->dso, filename, map, &machine->modules, + true); if (ret > 0) { dso__set_loaded(map->dso); diff --git a/tools/perf/util/machine.h b/tools/perf/util/machine.h index 74935dfaa937..393063840cd1 100644 --- a/tools/perf/util/machine.h +++ b/tools/perf/util/machine.h @@ -55,6 +55,7 @@ struct machine { struct dsos dsos; struct maps *kmaps; struct map *vmlinux_map; + struct rb_root modules; u64 kernel_start; pid_t *current_tid; size_t current_tid_sz; diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index a3a165ae933a..aab7ffdd0573 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -41,10 +41,16 @@ #include #include -static int dso__load_kernel_sym(struct dso *dso, struct map *map); +static int dso__load_kernel_sym(struct dso *dso, struct map *map, + struct rb_root *modules); static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map); static bool symbol__is_idle(const char *name); +static int read_proc_modules(const char *filename, struct rb_root *modules); +static struct module_info *find_module(const char *name, + struct rb_root *modules); +static void add_module(struct module_info *mi, struct rb_root *modules); + int vmlinux_path__nr_entries; char **vmlinux_path; @@ -85,6 +91,12 @@ static enum dso_binary_type binary_type_symtab[] = { #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab) +struct module_info { + struct rb_node rb_node; + char *name; + u64 start; +}; + static bool symbol_type__filter(char symbol_type) { symbol_type = toupper(symbol_type); @@ -234,15 +246,10 @@ void symbols__fixup_end(struct rb_root_cached *symbols, bool is_kallsyms) * kernel text segment and beginning of first module's text * segment is very big. Therefore do not fill this gap and do * not assign it to the kernel dso map (kallsyms). - * - * In kallsyms, it determines module symbols using '[' character - * like in: - * ffffffffc1937000 T hdmi_driver_init [snd_hda_codec_hdmi] */ if (prev->end == prev->start) { /* Last kernel/module symbol mapped to end of page */ - if (is_kallsyms && (!strchr(prev->name, '[') != - !strchr(curr->name, '['))) + if (is_kallsyms && prev->built_in != curr->built_in) prev->end = roundup(prev->end + 4096, 4096); else prev->end = curr->start; @@ -301,6 +308,8 @@ struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char * sym->type = type; sym->binding = binding; sym->namelen = namelen - 1; + sym->modules = NULL; + sym->built_in = 0; pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n", __func__, name, start, sym->end); @@ -318,6 +327,7 @@ void symbol__delete(struct symbol *sym) annotation__exit(notes); } } + free(sym->modules); free(((void *)sym) - symbol_conf.priv_size); } @@ -716,12 +726,37 @@ static bool symbol__is_idle(const char *name) return strlist__has_entry(idle_symbols_list, name); } -static int map__process_kallsym_symbol(void *arg, const char *name, +struct process_kallsym_symbol_arg { + struct dso *dso; + struct rb_root *modules; + int seen_end; +}; + +static int map__process_kallsym_symbol(void *arg_, const char *name, char type, u64 start) { struct symbol *sym; - struct dso *dso = arg; + struct process_kallsym_symbol_arg *arg = arg_; + struct dso *dso = arg->dso; struct rb_root_cached *root = &dso->symbols; + struct rb_root *modules = arg->modules; + char *module; + const char *modulep; + int counting = 1; + size_t nmods = 0; + char **mods = NULL; + char **modp = NULL; + + /* + * Split off the modules part. + */ + if ((module = strchr(name, '\t')) != NULL) { + *module = 0; + module++; + } + + if (strcmp(name, "_end") == 0) + arg->seen_end = 1; if (!symbol_type__filter(type)) return 0; @@ -731,18 +766,88 @@ static int map__process_kallsym_symbol(void *arg, const char *name, return 0; /* - * module symbols are not sorted so we add all - * symbols, setting length to 0, and rely on - * symbols__fixup_end() to fix it up. + * non-builtin module symbols are not sorted so we add all symbols, + * setting length to 0, and rely on symbols__fixup_end() to fix it up. */ sym = symbol__new(start, 0, kallsyms2elf_binding(type), kallsyms2elf_type(type), name); if (sym == NULL) return -ENOMEM; + + sym->built_in = !arg->seen_end; + + /* + * Pass over the modules list twice: once to count the number of + * modules this symbol is part of and allocate an array to store their + * names, then again to fill it out. + * + * Arguably inefficient, due to one allocation per built-in symbol, even + * though many symbols will have the same mods array. In practice, + * it's just too small a waste to matter. The module names are pointers + * into the machine->modules rb-tree (lazily populated here). + */ + +fill: + modulep = module; + while (modulep && (modulep = strchr(modulep, '[')) != NULL) { + struct module_info *mi; + const char *end_bra = strchr(modulep, ']'); + + modulep++; + if (end_bra == NULL || end_bra <= modulep) + continue; + + if (counting) { + nmods++; + continue; + } + + /* + * Fill-out phase. + */ + + *modp = strndup(modulep, end_bra - modulep); + if (*modp == NULL) { + free(mods); + return -ENOMEM; + } + + mi = find_module(*modp, modules); + if (!mi) { + mi = zalloc(sizeof(struct module_info)); + + if (!mi) { + free (mods); + free (*modp); + return -ENOMEM; + } + mi->name = *modp; + } + else { + free(*modp); + *modp = mi->name; + } + + modp++; + } + + if (counting && nmods > 0) { + mods = calloc(nmods + 1, sizeof (char *)); + if (mods == NULL) + return -ENOMEM; + modp = mods; + + counting = 0; + goto fill; + } + + sym->modules = mods; + /* * We will pass the symbols to the filter later, in - * map__split_kallsyms, when we have split the maps per module + * map__split_kallsyms, when we have split the maps per + * (non-built-in) module */ - __symbols__insert(root, sym, !strchr(name, '[')); + __symbols__insert(root, sym, !arg->seen_end); return 0; } @@ -752,9 +857,11 @@ static int map__process_kallsym_symbol(void *arg, const char *name, * so that we can in the next step set the symbol ->end address and then * call kernel_maps__split_kallsyms. */ -static int dso__load_all_kallsyms(struct dso *dso, const char *filename) +static int dso__load_all_kallsyms(struct dso *dso, const char *filename, + struct rb_root *modules) { - return kallsyms__parse(filename, dso, map__process_kallsym_symbol); + struct process_kallsym_symbol_arg arg = {dso, modules, 0}; + return kallsyms__parse(filename, &arg, map__process_kallsym_symbol); } static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso) @@ -766,22 +873,14 @@ static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso) struct rb_root_cached *root = &dso->symbols; struct rb_node *next = rb_first_cached(root); - if (!kmaps) - return -1; - *root = RB_ROOT_CACHED; while (next) { - char *module; - pos = rb_entry(next, struct symbol, rb_node); next = rb_next(&pos->rb_node); rb_erase_cached(&pos->rb_node, &old_root); RB_CLEAR_NODE(&pos->rb_node); - module = strchr(pos->name, '\t'); - if (module) - *module = '\0'; curr_map = maps__find(kmaps, pos->start); @@ -830,19 +929,19 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, x86_64 = machine__is(machine, "x86_64"); while (next) { - char *module; - pos = rb_entry(next, struct symbol, rb_node); next = rb_next(&pos->rb_node); - module = strchr(pos->name, '\t'); - if (module) { + if (!pos->built_in && pos->modules) { if (!symbol_conf.use_modules) goto discard_symbol; - *module++ = '\0'; - - if (strcmp(curr_map->dso->short_name, module)) { + /* + * Non-built-in symbols can only be in one module at + * once. + */ + assert(pos->modules[1] == NULL); + if (strcmp(curr_map->dso->short_name, pos->modules[0])) { if (curr_map != initial_map && dso->kernel == DSO_SPACE__KERNEL_GUEST && machine__is_default_guest(machine)) { @@ -856,12 +955,12 @@ static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, dso__set_loaded(curr_map->dso); } - curr_map = maps__find_by_name(kmaps, module); + curr_map = maps__find_by_name(kmaps, pos->modules[0]); if (curr_map == NULL) { pr_debug("%s/proc/{kallsyms,modules} " "inconsistency while looking " "for \"%s\" module!\n", - machine->root_dir, module); + machine->root_dir, pos->modules[0]); curr_map = initial_map; goto discard_symbol; } @@ -971,12 +1070,6 @@ bool symbol__restricted_filename(const char *filename, return restricted; } -struct module_info { - struct rb_node rb_node; - char *name; - u64 start; -}; - static void add_module(struct module_info *mi, struct rb_root *modules) { struct rb_node **p = &modules->rb_node; @@ -995,7 +1088,7 @@ static void add_module(struct module_info *mi, struct rb_root *modules) rb_insert_color(&mi->rb_node, modules); } -static void delete_modules(struct rb_root *modules) +void modules__delete_modules(struct rb_root *modules) { struct module_info *mi; struct rb_node *next = rb_first(modules); @@ -1060,7 +1153,7 @@ static int read_proc_modules(const char *filename, struct rb_root *modules) return -1; if (modules__parse(filename, modules, __read_proc_modules)) { - delete_modules(modules); + modules__delete_modules(modules); return -1; } @@ -1101,9 +1194,9 @@ int compare_proc_modules(const char *from, const char *to) if (!from_node && !to_node) ret = 0; - delete_modules(&to_modules); + modules__delete_modules(&to_modules); out_delete_from: - delete_modules(&from_modules); + modules__delete_modules(&from_modules); return ret; } @@ -1133,7 +1226,7 @@ static int do_validate_kcore_modules(const char *filename, struct maps *kmaps) } } out: - delete_modules(&modules); + modules__delete_modules(&modules); return err; } @@ -1467,18 +1560,20 @@ static int kallsyms__delta(struct kmap *kmap, const char *filename, u64 *delta) } int __dso__load_kallsyms(struct dso *dso, const char *filename, - struct map *map, bool no_kcore) + struct map *map, struct rb_root *modules, + bool no_kcore) { struct kmap *kmap = map__kmap(map); u64 delta = 0; - if (symbol__restricted_filename(filename, "/proc/kallsyms")) + if (symbol__restricted_filename(filename, "/proc/kallsyms") && + symbol__restricted_filename(filename, "/proc/kallmodsyms")) return -1; if (!kmap || !kmap->kmaps) return -1; - if (dso__load_all_kallsyms(dso, filename) < 0) + if (dso__load_all_kallsyms(dso, filename, modules) < 0) return -1; if (kallsyms__delta(kmap, filename, &delta)) @@ -1499,9 +1594,9 @@ int __dso__load_kallsyms(struct dso *dso, const char *filename, } int dso__load_kallsyms(struct dso *dso, const char *filename, - struct map *map) + struct map *map, struct rb_root *modules) { - return __dso__load_kallsyms(dso, filename, map, false); + return __dso__load_kallsyms(dso, filename, map, modules, false); } static int dso__load_perf_map(const char *map_path, struct dso *dso) @@ -1814,12 +1909,13 @@ int dso__load(struct dso *dso, struct map *map) dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP; if (dso->kernel && !kmod) { + machine = map__kmaps(map)->machine; + if (dso->kernel == DSO_SPACE__KERNEL) - ret = dso__load_kernel_sym(dso, map); + ret = dso__load_kernel_sym(dso, map, &machine->modules); else if (dso->kernel == DSO_SPACE__KERNEL_GUEST) ret = dso__load_guest_kernel_sym(dso, map); - machine = map__kmaps(map)->machine; if (machine__is(machine, "x86_64")) machine__map_x86_64_entry_trampolines(machine, dso); goto out; @@ -2220,7 +2316,8 @@ static char *dso__find_kallsyms(struct dso *dso, struct map *map) return strdup(path); } -static int dso__load_kernel_sym(struct dso *dso, struct map *map) +static int dso__load_kernel_sym(struct dso *dso, struct map *map, + struct rb_root *modules) { int err; const char *kallsyms_filename = NULL; @@ -2282,7 +2379,7 @@ static int dso__load_kernel_sym(struct dso *dso, struct map *map) kallsyms_filename = kallsyms_allocated_filename; do_kallsyms: - err = dso__load_kallsyms(dso, kallsyms_filename, map); + err = dso__load_kallsyms(dso, kallsyms_filename, map, modules); if (err > 0) pr_debug("Using %s for symbols\n", kallsyms_filename); free(kallsyms_allocated_filename); @@ -2323,11 +2420,11 @@ static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map) if (!kallsyms_filename) return -1; } else { - sprintf(path, "%s/proc/kallsyms", machine->root_dir); + sprintf(path, "%s/proc/kallmodsyms", machine->root_dir); kallsyms_filename = path; } - err = dso__load_kallsyms(dso, kallsyms_filename, map); + err = dso__load_kallsyms(dso, kallsyms_filename, map, &machine->modules); if (err > 0) pr_debug("Using %s for symbols\n", kallsyms_filename); if (err > 0 && !dso__is_kcore(dso)) { diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h index 0b893dcc8ea6..9ca218e09acf 100644 --- a/tools/perf/util/symbol.h +++ b/tools/perf/util/symbol.h @@ -66,6 +66,11 @@ struct symbol { u8 annotate2:1; /** Architecture specific. Unused except on PPC where it holds st_other. */ u8 arch_sym; + /** Null-terminated array of pointers to names of containing modules in the + modules red-black tree. May be NULL for none. */ + char **modules; + /** Set if this symbol is built in to the core kernel. */ + int built_in; /** The name of length namelen associated with the symbol. */ char name[]; }; @@ -137,8 +142,9 @@ int dso__load_vmlinux(struct dso *dso, struct map *map, const char *vmlinux, bool vmlinux_allocated); int dso__load_vmlinux_path(struct dso *dso, struct map *map); int __dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map, - bool no_kcore); -int dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map); + struct rb_root *modules, bool no_kcore); +int dso__load_kallsyms(struct dso *dso, const char *filename, struct map *map, + struct rb_root *modules); void dso__insert_symbol(struct dso *dso, struct symbol *sym); @@ -161,6 +167,8 @@ int sysfs__read_build_id(const char *filename, struct build_id *bid); int modules__parse(const char *filename, void *arg, int (*process_module)(void *arg, const char *name, u64 start, u64 size)); +void modules__delete_modules(struct rb_root *modules); + int filename__read_debuglink(const char *filename, char *debuglink, size_t size);