From patchwork Thu Dec 14 06:39:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Kochetkov X-Patchwork-Id: 178543 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp8376327dys; Wed, 13 Dec 2023 23:37:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IHxGkwLhcw1aRMzlqrIGIZazcL3kb0uxvh0jxLVG1V6/uhcLMscDLrBIsypDEJzPlLi7din X-Received: by 2002:a05:6a20:5487:b0:18f:97c:4f48 with SMTP id i7-20020a056a20548700b0018f097c4f48mr6326397pzk.84.1702539474006; Wed, 13 Dec 2023 23:37:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702539473; cv=none; d=google.com; s=arc-20160816; b=OCVqGDHQZEMqXpYJLllUMFz0BKmTDIQsUv7HuGvllosiyORzuIhxzgkHunhhUApd1l ClgC5O7e3R4e3Jq7QKaNboSQIbWB+8wgbF11aWuAlP3VK/H8dpZrJSFddJvHP/f8H0fJ usu1YLfxu8mEH+XWmL+YgeYJyJSNOO1hpPmY22wS5c/3IiQwq1C25+67gun1dDLefCUQ yEB72qXRjpnjalzGlzMtaG0ynDQFfQ8isK8CmJMxVNqQlgLmas5taiuybDs4xdt8GBSa TXXpYAhcSdvt8pQUfmJRzjkwO+7tC9w1jNQq9PqL24oMldU5xJ9k45k+7suurPwVNYbl w11A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature:dkim-signature; bh=MUmKhkzD6Lqw/sRqo4MpOesl2qfmIn0n56k8Dpw1hBc=; fh=EBoyQDjGS4h62ASFEF1SxWcD5MjpsLU0fQDX+XanSvU=; b=ZHnAFzstDhnRDjk4mLbLeZQLQMH8++/Y5Gao7iQgU4cNIm7LbkCEdQwjQnZaKeXvj0 6wC7U224BB7cNxU/+4JOIDlXRB4aSTDNjC1u05fV49Fx7zonk9bwe8F1+wwSoUfkqpai IOwskRfK3a9NyLI7+HCLzDU91klAAQhsG2Y42nLdP5tmvV/G0MIDWBZncbSjN85EkEAp zq28KCdq+DmBR2NY78o6VebwR2Uboh1rp3EYjmxd9X31Gk3EEZ4Jaih6VcN8TS38mahH nT9mh4dhNFPsiTXVbSo6z2WAq3umF7BUr8HmBcyYAIVyLNK/WlK5priBHMH5aeydf+G3 pGFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@inbox.ru header.s=mail4 header.b=qA8IR23U; dkim=fail header.i=@inbox.ru header.s=mail4 header.b="Eyg1D/AN"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=inbox.ru Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id n9-20020a170903110900b001d09c54485esi11120382plh.568.2023.12.13.23.37.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 23:37:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@inbox.ru header.s=mail4 header.b=qA8IR23U; dkim=fail header.i=@inbox.ru header.s=mail4 header.b="Eyg1D/AN"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=inbox.ru Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 61A1880295FE; Wed, 13 Dec 2023 23:37:52 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230117AbjLNHhm (ORCPT + 99 others); Thu, 14 Dec 2023 02:37:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229461AbjLNHhl (ORCPT ); Thu, 14 Dec 2023 02:37:41 -0500 X-Greylist: delayed 3491 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Wed, 13 Dec 2023 23:37:46 PST Received: from fallback20.i.mail.ru (fallback20.i.mail.ru [79.137.243.76]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3418EA0 for ; Wed, 13 Dec 2023 23:37:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=inbox.ru; s=mail4; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject:Cc:To:From:From:Subject:Content-Type:Content-Transfer-Encoding:To:Cc; bh=MUmKhkzD6Lqw/sRqo4MpOesl2qfmIn0n56k8Dpw1hBc=; t=1702539466;x=1702629466; b=qA8IR23UGi4k39sFZ69siviiFi6wU8lrSWCadDbUf9m2j/K1fLKOerr4r51wA/iz7ML3vu+kVwrm9ScIc8tl6K+Zm2v27u2X/b1tvd0ZoKiVtpG8XASOK0Jq1aIuHOEPmix6kpc6FI0lTWI9tvLxbHc1IKwvZ6pa+7M3Wj3b2o6hRPrk5kAEgqIsu/oXxIXEFAm+RkBwbRxpu24uxBVSolW56ImKdyZSmeSEZGuBZOArEwcPXwMINFwsGoBAQu9h9nyf+2XNskC8R0T5Jvga6w1XgUNIv2QL7pW8sOZ9tpMN9V3xHWXUwDOwyueJx2WI8K8eSeiF/R8y13BY6vK1Ug==; Received: from [10.12.4.1] (port=59058 helo=smtp3.i.mail.ru) by fallback20.i.mail.ru with esmtp (envelope-from ) id 1rDfNc-000VmM-UQ for linux-kernel@vger.kernel.org; Thu, 14 Dec 2023 09:39:33 +0300 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=inbox.ru; s=mail4; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject:Cc: To:From:From:Sender:Reply-To:To:Cc:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive: X-Cloud-Ids:Disposition-Notification-To; bh=MUmKhkzD6Lqw/sRqo4MpOesl2qfmIn0n56k8Dpw1hBc=; t=1702535972; x=1702625972; b=Eyg1D/AN0/7jBvTnwj7Gpc9S+He4t+G8fm6jVdVRjjoZSsB98HwKulPG6FOBDKCtBnASWrk+Eso hlRsH47XtZ7usi/hAggSyD5InNW/HsAHYhFAXUY/JbTv/4w70t9WXBr+t9h4BbBLxKYlnnYBUvSxt xEbjA4N/wPQW22Q47qTVhH70sflrpXJaGt5pIsRauu4KSSN0Bt0w6fRrDAL+hm5Pj2r3m4ZDVxJ1a P5b7y4bEd3IXTQjcGw5wnL9QaYhCUuKNzp/+fv8ci7J9L1rV4PGqWP4nZ5ovjr59kYuTCexyzJDDu tkBylX22vQTDrYLKitPyex5mYoya5l3JGM4w==; Received: by smtp3.i.mail.ru with esmtpa (envelope-from ) id 1rDfNN-009naG-1j; Thu, 14 Dec 2023 09:39:18 +0300 From: Maxim Kochetkov To: linux-riscv@lists.infradead.org Cc: linux-kernel@vger.kernel.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, charlie@rivosinc.com, ajones@ventanamicro.com, conor.dooley@microchip.com, jszhang@kernel.org, kernel@esmil.dk, Maxim Kochetkov , Amma Lee Subject: [PATCH v5 1/1] riscv: optimize ELF relocation function in riscv Date: Thu, 14 Dec 2023 09:39:06 +0300 Message-Id: <20231214063906.13612-1-fido_max@inbox.ru> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Mailru-Src: smtp X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD97533543916A0F71AD286D97BFFE3DA5DB7E67E5414E74A44CD62213F67905E7A9584602B0A0B67F79DAC4348BB0AD5402F1DF3D5CE2C97D047EEACAE9851930B X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE70A10A23A3B64B805EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006378B67D2947F927BCB8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D89139C7D2CD1B56E1D787F01A6F3A20516F9789CCF6C18C3F8528715B7D10C86878DA827A17800CE764603B5C71CE8B8F9FA2833FD35BB23D9E625A9149C048EEB1593CA6EC85F86DF6B57BC7E64490618DEB871D839B7333395957E7521B51C2DFABB839C843B9C08941B15DA834481F8AA50765F79006370BDD70ABAC747F53389733CBF5DBD5E9B5C8C57E37DE458B9E9CE733340B9D5F3BBE47FD9DD3FB595F5C1EE8F4F765FC8C7ADC89C2F0B2A5E2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8BB07C9E286C61B7F975ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-C1DE0DAB: 0D63561A33F958A5D0916945B0B188A8508253F83738EB50E009ED81786A57A7F87CCE6106E1FC07E67D4AC08A07B9B062B3BD3CC35DA5889C5DF10A05D560A950611B66E3DA6D700B0A020F03D25A0997E3FB2386030E77 X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF77DD89D51EBB7742D3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CF8C7B53F24711718A4D14E3609780158DF677858F0EEB1B3502793B85883C3870E547CA128C9EF43861E41F2809366B3878243DF2BCB178E1BE19B5793D4E595721BEC6C0C71ED4F84C41F94D744909CEE921556F0E976A29E6EC0772259F8F8F8815B87D7EC76CB9 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojtsCAqtdyux0lKPxRzspCxg== X-Mailru-Sender: 689FA8AB762F7393590D8C940224AE339267D7193F259203476718A195E4972498CC072019C18A892CA7F8C7C9492E1F2F5E575105D0B01ADBE2EF17B331888EEAB4BC95F72C04283CDA0F3B3F5B9367 X-Mras: Ok X-7564579A: B8F34718100C35BD X-77F55803: 6242723A09DB00B4504CF8691E74CC1EE045C20511AAF4EE20659E52C60CB635049FFFDB7839CE9E27692693F3A214146632D4FDD6EF8EF520C337D8FF41615A053D9B0B9A65156E X-7FA49CB5: 0D63561A33F958A5FE572B1DBC45F354BD1E1CE930F8FC0E293031C103444D49CACD7DF95DA8FC8BD5E8D9A59859A8B6D1AAE889B2A4D82D X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5xhPKz0ZEsZ5k6NOOPWz5QAiZSCXKGQRq3/7KxbCLSB2ESzQkaOXqCBFZPLWFrEGlV1shfWe2EVcxl5toh0c/aCGOghz/frdRhzMe95NxDFdfQ7/oSSXYuSsOysMKPXHPQ== X-Mailru-MI: C000000000000800 X-Mras: Ok X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 13 Dec 2023 23:37:52 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785242031534914592 X-GMAIL-MSGID: 1785242031534914592 The patch can optimize the running times of insmod command by modify ELF relocation function. In the 5.10 and latest kernel, when install the riscv ELF drivers which contains multiple symbol table items to be relocated, kernel takes a lot of time to execute the relocation. For example, we install a 3+MB driver need 180+s. We focus on the riscv architecture handle R_RISCV_HI20 and R_RISCV_LO20 type items relocation function in the arch\riscv\kernel\module.c and find that there are two-loops in the function. If we modify the begin number in the second for-loops iteration, we could save significant time for installation. We install the same 3+MB driver could just need 2s. Signed-off-by: Amma Lee Signed-off-by: Maxim Kochetkov Reviewed-by: Charlie Jenkins --- Changes in v5: - rebase onto latest linux-next Changes in v4: - use 'while' loop instead of 'for' loop to avoid code duplicate --- arch/riscv/kernel/module.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c index aac019ed63b1..a8a01df1cc17 100644 --- a/arch/riscv/kernel/module.c +++ b/arch/riscv/kernel/module.c @@ -779,6 +779,7 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, Elf_Sym *sym; void *location; unsigned int i, type; + unsigned int j_idx = 0; Elf_Addr v; int res; unsigned int num_relocations = sechdrs[relsec].sh_size / sizeof(*rel); @@ -829,9 +830,10 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, v = sym->st_value + rel[i].r_addend; if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) { - unsigned int j; + unsigned int j = j_idx; + bool found = false; - for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) { + do { unsigned long hi20_loc = sechdrs[sechdrs[relsec].sh_info].sh_addr + rel[j].r_offset; @@ -860,16 +862,26 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, hi20 = (offset + 0x800) & 0xfffff000; lo12 = offset - hi20; v = lo12; + found = true; break; } - } - if (j == sechdrs[relsec].sh_size / sizeof(*rel)) { + + j++; + if (j > sechdrs[relsec].sh_size / sizeof(*rel)) + j = 0; + + } while (j_idx != j); + + if (!found) { pr_err( "%s: Can not find HI20 relocation information\n", me->name); return -EINVAL; } + + /* Record the previous j-loop end index */ + j_idx = j; } if (reloc_handlers[type].accumulate_handler)