From patchwork Wed Nov 2 08:49:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhen Lei X-Patchwork-Id: 14097 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp3494449wru; Wed, 2 Nov 2022 01:54:31 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5lJScjYGLN4Sg4fZ3lI0TwM5XsFoyZgCNMO4VdzDsFlm+myVGOngi3ULVnQGSetjwdw6JC X-Received: by 2002:a17:903:4d7:b0:178:8564:f754 with SMTP id jm23-20020a17090304d700b001788564f754mr23795299plb.60.1667379271096; Wed, 02 Nov 2022 01:54:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667379271; cv=none; d=google.com; s=arc-20160816; b=Tov9gE+po5EE2SZ9HEScxHJLJj5rxdwxAIFC15Tvv23EnU9DnflVjFhetZqNy0+lnS IKKM2SPKmtFsH5ATEBNFh5hrH2ITe1CgSYcR2ltbfq1tUKmB4XJcCDDls0zuJ9RMxg64 Uza3V++IJGUIB1Y0tcrB6ziJdI+ORdUIuLkUvQF7D6mwza7q1ID8LWGuongx00PX98Ih CkD/9COf4m0Zc2gSqqc2QxpMCBShSjJ34D9ISOr9LbhDNE7og6IN9HMrzN1QBaG3dk3n TZ1ftIp3/10Jk3h5p1pSxeHzcLps0egQLqesn/6yQJYaw7t9/00m7MMG7sNaNZRHu28b d9+g== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=O4EM+Z9u9BAqy+uGwOyXQNwfT3jl2sLXypQlQV1N480=; b=IGx1U5PfYXDdho+55jR6fpww8ULjEcGLfjf9Bo9BUeTOW/i2zMu/aaYFRVO+oRIf05 XQ4gOmDUMxrFfyDHze2/cDkt1bXNwmDde1C/iMP1k0/Ow+95QbN2JH4Q0xGy1gD7ceK0 QUCLhQUnOityQ46lKOyPyCb32v8b/STNKhvEcCGZ7iYybRqa+9bbSaqdvz1CY/XRP5IF AXl2NOnqdWJyJTkEHO9B7kVvUVJvqSJKevy+iL5lZcmoBfk0xDUExioJ58k3BpIvtKFr axDQhliMMbuzvMHZ8hoGaoJZouEU3I3xOTEvNzg06W+1vkZAj6RJ+pGHxgNhNitMHa9f N+xA== 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; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s1-20020a170903200100b00172cb948c68si13054262pla.227.2022.11.02.01.54.18; Wed, 02 Nov 2022 01:54:31 -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; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231244AbiKBIvc (ORCPT + 99 others); Wed, 2 Nov 2022 04:51:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231204AbiKBIu6 (ORCPT ); Wed, 2 Nov 2022 04:50:58 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EEAE127DDF; Wed, 2 Nov 2022 01:50:55 -0700 (PDT) Received: from dggpemm500023.china.huawei.com (unknown [172.30.72.55]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4N2LFR329DzHvXx; Wed, 2 Nov 2022 16:50:35 +0800 (CST) Received: from dggpemm500006.china.huawei.com (7.185.36.236) by dggpemm500023.china.huawei.com (7.185.36.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 2 Nov 2022 16:50:31 +0800 Received: from thunder-town.china.huawei.com (10.174.178.55) by dggpemm500006.china.huawei.com (7.185.36.236) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 2 Nov 2022 16:50:30 +0800 From: Zhen Lei To: Josh Poimboeuf , Jiri Kosina , Miroslav Benes , Petr Mladek , Joe Lawrence , , , Masahiro Yamada , Alexei Starovoitov , Jiri Olsa , Kees Cook , Andrew Morton , "Luis Chamberlain" , , "Steven Rostedt" , Ingo Molnar CC: Zhen Lei , David Laight Subject: [PATCH v8 6/9] livepatch: Use kallsyms_on_each_match_symbol() to improve performance Date: Wed, 2 Nov 2022 16:49:18 +0800 Message-ID: <20221102084921.1615-7-thunder.leizhen@huawei.com> X-Mailer: git-send-email 2.37.3.windows.1 In-Reply-To: <20221102084921.1615-1-thunder.leizhen@huawei.com> References: <20221102084921.1615-1-thunder.leizhen@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.174.178.55] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500006.china.huawei.com (7.185.36.236) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS 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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748373886459417505?= X-GMAIL-MSGID: =?utf-8?q?1748373886459417505?= Based on the test results of kallsyms_on_each_match_symbol() and kallsyms_on_each_symbol(), the average performance can be improved by more than 1500 times. Signed-off-by: Zhen Lei --- kernel/livepatch/core.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index 9ada0bc5247be5d..50bfc3481a4ee38 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -153,6 +153,24 @@ static int klp_find_callback(void *data, const char *name, return 0; } +static int klp_match_callback(void *data, unsigned long addr) +{ + struct klp_find_arg *args = data; + + args->addr = addr; + args->count++; + + /* + * Finish the search when the symbol is found for the desired position + * or the position is not defined for a non-unique symbol. + */ + if ((args->pos && (args->count == args->pos)) || + (!args->pos && (args->count > 1))) + return 1; + + return 0; +} + static int klp_find_object_symbol(const char *objname, const char *name, unsigned long sympos, unsigned long *addr) { @@ -167,7 +185,7 @@ static int klp_find_object_symbol(const char *objname, const char *name, if (objname) module_kallsyms_on_each_symbol(klp_find_callback, &args); else - kallsyms_on_each_symbol(klp_find_callback, &args); + kallsyms_on_each_match_symbol(klp_match_callback, name, &args); /* * Ensure an address was found. If sympos is 0, ensure symbol is unique;