Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp858075rwb; Fri, 23 Sep 2022 05:14:14 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5JoA8bKfOcE3HsMGDqMCOhWx972KgiN9rrwZsD58oKA9Lpb3rcOy34A7kLUwZGPHrxW+Xn X-Received: by 2002:a17:907:97cd:b0:780:6829:cb9d with SMTP id js13-20020a17090797cd00b007806829cb9dmr7019514ejc.344.1663935253965; Fri, 23 Sep 2022 05:14:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663935253; cv=none; d=google.com; s=arc-20160816; b=XkyAzOjzxssWQec+2Zlq6atwvcAgEjT893n9PVBKH/kai3CeXNdlhmqynXJz9PANQ/ Qd5mDqhy0TFH36jpF5RWEMa0HkjTcms+QrsJjtqOhmR/NsUdgAQ15uuB3khoSv/tLPyu 0h4ZILvknPShtJ0HhhlzAErbjQ8OIXyYL5AuYEYXngmPUmsx2Vyl+0Ew1OEMWobmWuSw av3l+GuGBHLmPkMaKvpfKgHZZFzS9PKjbTscFZa7YbvCLMxor3OKU39t1IyLG/2RE595 8JK9D9FmfiBP+z1WE0gWKTzcXTzunOvOpjKJzWPIhRyhYKRUT4WnMtqqSFdl5VwkQg6X qAKA== 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=OlEpG0BhCHS4LLiOHM1jnX+R0Oz7iM9XlQYf9Ns/hbw=; b=PJh2DxlFtSVkvxtet0C0/NglDnUQ5OnBtVwNh09xCkCHPl7DdNVccSlNGUFWkml4rJ kcz689Hm9T+/hrEL1PB9tSP1dZ+00P/QbUMzVTCAP9kLfNCniUSKk8c2QTmluhW2fSwy 8eRX0RSo4GuaGHAUCTE10nbJx6pPUXM2F/gHf0KTdWU5OTBnglhCX5wc5EOy5HV0WBQQ SLlqfrBWSYxBQpk+hvN35wCkLsXCj4lWixhx/Qparr9lEJeNVCj27RlzK4nBFqk0lqVU rJjjEtl2t1CX+c8ebh7a4jvxXvs5qsdAkHdIfWC140c92I+vCfLic3JrJHLi5kpAJsAT EuWA== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d9-20020aa7ce09000000b00453833fe083si6313742edv.418.2022.09.23.05.13.48; Fri, 23 Sep 2022 05:14:13 -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 S230175AbiIWLWl (ORCPT + 99 others); Fri, 23 Sep 2022 07:22:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48246 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232159AbiIWLWD (ORCPT ); Fri, 23 Sep 2022 07:22:03 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9FF6D13792C; Fri, 23 Sep 2022 04:21:58 -0700 (PDT) Received: from dggpemm500021.china.huawei.com (unknown [172.30.72.57]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4MYqPy2GsnzWgnK; Fri, 23 Sep 2022 19:17:58 +0800 (CST) Received: from dggpemm500006.china.huawei.com (7.185.36.236) by dggpemm500021.china.huawei.com (7.185.36.109) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 23 Sep 2022 19:21:56 +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; Fri, 23 Sep 2022 19:21:56 +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 Subject: [PATCH v5 07/10] livepatch: Use kallsyms_on_each_match_symbol() to improve performance Date: Fri, 23 Sep 2022 19:20:30 +0800 Message-ID: <20220923112033.1958-8-thunder.leizhen@huawei.com> X-Mailer: git-send-email 2.26.0.windows.1 In-Reply-To: <20220923112033.1958-1-thunder.leizhen@huawei.com> References: <20220923112033.1958-1-thunder.leizhen@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.174.178.55] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) 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 Based on the test results of kallsyms_on_each_match_symbol() and kallsyms_on_each_symbol(), the average performance can be improved by 20 to 30 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 42f7e716d56bf72..31b57ccf908017e 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; -- 2.25.1