Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp25587458rwd; Sun, 2 Jul 2023 20:59:37 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ59dkoUIVkbVkk7lV++CZv+f0kVgbFk0ykWtaddrqloqEvOm+UrB9HoGK952q6kwEw9gjPZ X-Received: by 2002:a05:6830:118a:b0:6b8:6a83:2b17 with SMTP id u10-20020a056830118a00b006b86a832b17mr9119862otq.33.1688356776933; Sun, 02 Jul 2023 20:59:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688356776; cv=none; d=google.com; s=arc-20160816; b=FHGtg7UfYdI3CGP2Ssol+k6aDudZedMToX3AWdK4UkDD3/kCP5qXXiUa0fv2FSxfY8 E9y2YccWt3FBDLmcZ9Dm+wnAVvdILlzDthrQXXF7d+ysRHU1UIYzjhIDO5lW8p4Rkkph Rbkj0B9PGRkDB7IF/72p5VgY5ATGXU/g4Ghg+tqZmW/LcFYtso94R7P5neqS4S6YWx9l p9uN+EB5mGah8Qc5AJ+zOP8YqnF65yHrX6+aTEFKJUJX5dxnP2hTawrfMATh1RK6Iq1M qxIcFtevlMXMKG/dq/J9oxDfq/1m3xSvFuUiStdshACjVUgyWzHIcoqZMN5okl26xFmc Zyxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from; bh=72vU+h1jjcJj/zZm28AkDezwNWwrzOTevJOjXit6Oh4=; fh=X4eyHQAAPTLChXk2X5RMAwW0Me0Nl+j6rgQZ2DNqTV0=; b=kShFK+eHFoB/UM+38Abbq9DTUnIotN9d0y5PJLEhcyEY8K5LdOuHd7wJFHY0ofDP5+ ioQ7gAWiGEs9kEvRQbAmOP5Bme90vDXk49q1ENi42SwSUm4vrzi1DxJ106PF/nZfQs3u +syC/bneYWkHOMKWKdWv2vZgYtmg55tqFoPIyFGxtM6fY1s1MiwkW2K8IlOK89+7HqK5 64hMow7igaqX/HQchML4g/zv0kBsg2oUs5wn9Y4R3uJcxZVx2lwq6eSq/XKMU9bN/Mfd +l1xlJ+BRP2Bh2ifb+q6G2Pz7kGSolYmXMen9CasRqV+hT3KTa5gA2d5+r40HeBtHdym a6TQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s23-20020a63f057000000b00543a6ce8c33si17062608pgj.78.2023.07.02.20.59.25; Sun, 02 Jul 2023 20:59:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229960AbjGCDdM (ORCPT + 99 others); Sun, 2 Jul 2023 23:33:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60374 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229759AbjGCDdH (ORCPT ); Sun, 2 Jul 2023 23:33:07 -0400 Received: from out28-146.mail.aliyun.com (out28-146.mail.aliyun.com [115.124.28.146]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8764B123 for ; Sun, 2 Jul 2023 20:33:04 -0700 (PDT) X-Alimail-AntiSpam: AC=CONTINUE;BC=0.07437741|-1;CH=green;DM=|CONTINUE|false|;DS=CONTINUE|ham_system_inform|0.0371712-0.0010859-0.961743;FP=0|0|0|0|0|-1|-1|-1;HT=ay29a033018047192;MF=lixiaoyun@binary-semi.com;NM=1;PH=DS;RN=7;RT=7;SR=0;TI=SMTPD_---.TjpOQK9_1688355167; Received: from localhost.localdomain(mailfrom:lixiaoyun@binary-semi.com fp:SMTPD_---.TjpOQK9_1688355167) by smtp.aliyun-inc.com; Mon, 03 Jul 2023 11:32:58 +0800 From: Amma Lee To: paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu Cc: lixiaoyun@binary-semi.com, xiezx@binary-semi.com, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH] riscv: optimize ELF relocation function in riscv Date: Mon, 3 Jul 2023 11:32:12 +0800 Message-Id: <1688355132-62933-1-git-send-email-lixiaoyun@binary-semi.com> X-Mailer: git-send-email 1.9.1 X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,HK_RANDOM_ENVFROM, HK_RANDOM_FROM,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY 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 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 --- arch/riscv/kernel/module.c | 67 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 3 deletions(-) diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c index 7c651d5..b8df144 100755 --- a/arch/riscv/kernel/module.c +++ b/arch/riscv/kernel/module.c @@ -345,13 +345,13 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, int (*handler)(struct module *me, u32 *location, Elf_Addr v); Elf_Sym *sym; u32 *location; - unsigned int i, type; + unsigned int i, type, j_idx; Elf_Addr v; int res; pr_debug("Applying relocate section %u to %u\n", relsec, sechdrs[relsec].sh_info); - + j_idx = 0; for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { /* This is where to make the change */ location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr @@ -386,7 +386,15 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) { unsigned int j; - for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) { + /*In the second for-loops, each traversal for j is + * starts from 0 to the symbol table item index which + * is detected. By the tool "readelf", we find that all + * the symbol table items about R_RISCV_PCREL_HI20 type + * are incrementally added in order. It means that we + * could interate the j with the previous loop end + * value(j_idx) as the begin number in the next loop; + */ + for (j = j_idx; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) { unsigned long hi20_loc = sechdrs[sechdrs[relsec].sh_info].sh_addr + rel[j].r_offset; @@ -420,11 +428,64 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, } } if (j == sechdrs[relsec].sh_size / sizeof(*rel)) { + if (j_idx == 0) { pr_err( "%s: Can not find HI20 relocation information\n", me->name); return -EINVAL; +} + + /*If the last j-loop have been traversed to the + * maximum value but never match the + * corresponding symbol relocation item, the + * j-loop will execute the second loop which + * is begin from 0 to the prerious index (j_idx) + * unless the previous j_idx == 0; + * */ + for (j = 0; j < j_idx; j++) { + unsigned long hi20_loc = + sechdrs[sechdrs[relsec].sh_info].sh_addr + + rel[j].r_offset; + u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info); + + /* Find the corresponding HI20 relocation entry */ + if (hi20_loc == sym->st_value + && (hi20_type == R_RISCV_PCREL_HI20 + || hi20_type == R_RISCV_GOT_HI20)) { + s32 hi20, lo12; + Elf_Sym *hi20_sym = + (Elf_Sym *)sechdrs[symindex].sh_addr + + ELF_RISCV_R_SYM(rel[j].r_info); + unsigned long hi20_sym_val = + hi20_sym->st_value + + rel[j].r_addend; + + /* Calculate lo12 */ + size_t offset = hi20_sym_val - hi20_loc; + if (IS_ENABLED(CONFIG_MODULE_SECTIONS) + && hi20_type == R_RISCV_GOT_HI20) { + offset = module_emit_got_entry( + me, hi20_sym_val); + offset = offset - hi20_loc; + } + hi20 = (offset + 0x800) & 0xfffff000; + lo12 = offset - hi20; + v = lo12; + + break; + } } + + if (j == j_idx) { + pr_err( + "%s: Can not find HI20 relocation information\n", + me->name); + return -EINVAL; + } + } + + /* Record the previous j-loop end index */ + j_idx = j; } res = handler(me, location, v); -- 1.9.1