From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 49B9F248F64 for ; Tue, 5 May 2026 14:38:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777991881; cv=none; b=UOQQzmiSXnxvqmO+Aln655L36NA2A31Wl4vI9UiLLjKyYrQy/1PaIh+FPk2rpiv3wQUYNd+YrqD7mYy/LDFsThy76hnKeQAxNKqNq8qNluacMpVOkCyLqi2rcaNQxKh9HhgAxG4izQXDPN/cRghPW+AbHkwOJw2ArZbQdV9ohCI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777991881; c=relaxed/simple; bh=JvUqTfqiAW9yC35/8X+aOZcYzssa6BCdLInlTa1/lkM=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=oADHQADUv7RL7QVczy0VXTBXbPhHuHR7SKUOVwyS07ecuxHAg3PMj2M4sgQlQ2HuXPCoAJ2xhZepib8NGEUrdt9QMA8lH3ISSRoIM1QOxuTL6WFsyI6IyKrNPZcLp+5Ifpr5zBvJ6PJsc7Rfb8mbhjNk693kmo5XG6P3yLlaU4c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=bfOCfQwx; arc=none smtp.client-ip=209.85.128.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="bfOCfQwx" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-488a88aeec9so60387015e9.2 for ; Tue, 05 May 2026 07:38:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1777991879; x=1778596679; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=rW/yHyxsN9QI7AveMhZMYM5ISMvZ0yqJBxdD/EhC5LQ=; b=bfOCfQwxrawu82lUm1DRP/xOV/wBNJhWMJG6XVRH3eyQ8GucyHVVfJoXgIrrCTxt4l A4WUC3p14+oyf0DBh8Q2uB8ZuK5O6D74Eg/k+BYtaQkG2f/nyMjmkhtup6UcM6LoPnmd PK7ieHiGSTakBdqqZfr5yFNLdwIXU2oupKKg0y1uVGrSajVabDHBv56QJXPKyMMh6sHh kSKTQSh398izPQkFWhRhHnH6D0aYnO+PSCXZbwEi9AhJs8GJkb/W0SH0RO1h7yLj+Ezw Qe1VTjq1CsABEKRrKDQqU1GUuHYVCBYJr8ZrXRn7tNJITOr5OzFRNBev9pYcxc+W9+q3 XL/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777991879; x=1778596679; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=rW/yHyxsN9QI7AveMhZMYM5ISMvZ0yqJBxdD/EhC5LQ=; b=GsynKiyJWFMmM/NmtPGxpD3H8erkCjsJMMJ6+8wzj+t1QcT5JR95JUh8J8l3C4Yfvc bMD7MNnSZKSY+HqIesk7j6w/eUqsnS9L9rKF8BeRhSeunCL756B1LbvyOSNoL+MgesQJ PfVis/SNRslBjiuaTqMiFNRRu9j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:600c:4e43:b0:480:1d0b:2d32 with SMTP id 5b1f17b1804b1-48a98638227mr256258935e9.12.1777991878651; Tue, 05 May 2026 07:37:58 -0700 (PDT) Received: from ?IPV6:2a00:1028:838d:271e:8e3b:4aff:fe4c:a100? (dynamic-2a00-1028-838d-271e-8e3b-4aff-fe4c-a100.ipv6.o2.cz. [2a00:1028:838d:271e:8e3b:4aff:fe4c:a100]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48a8ebb2fa5sm309081525e9.12.2026.05.05.07.37.57 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 05 May 2026 07:37:58 -0700 (PDT) Message-ID: <28bb0f74-8721-4e53-ad89-87b2a78623b2@suse.com> Date: Tue, 5 May 2026 16:37:56 +0200 Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 2/2] module/kallsyms: sort function symbols and use binary search To: Petr Mladek Cc: Stanislaw Gruszka , linux-modules@vger.kernel.org, Sami Tolvanen , Luis Chamberlain , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, live-patching@vger.kernel.org, Daniel Gomez , Aaron Tomlin , Steven Rostedt , Masami Hiramatsu , Jordan Rome , Viktor Malik , Miroslav Benes , Josh Poimboeuf , Joe Lawrence References: <20260327110005.16499-1-stf_xl@wp.pl> <20260327110005.16499-2-stf_xl@wp.pl> Content-Language: en-US From: Petr Pavlu In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 5/5/26 2:24 PM, Petr Mladek wrote: > On Fri 2026-03-27 12:00:05, Stanislaw Gruszka wrote: >> Module symbol lookup via find_kallsyms_symbol() performs a linear scan >> over the entire symtab when resolving an address. The number of symbols >> in module symtabs has grown over the years, largely due to additional >> metadata in non-standard sections, making this lookup very slow. >> >> Improve this by separating function symbols during module load, placing >> them at the beginning of the symtab, sorting them by address, and using >> binary search when resolving addresses in module text. >> >> This also should improve times for linear symbol name lookups, as valid >> function symbols are now located at the beginning of the symtab. >> >> The cost of sorting is small relative to module load time. In repeated >> module load tests [1], depending on .config options, this change >> increases load time between 2% and 4%. With cold caches, the difference >> is not measurable, as memory access latency dominates. >> >> The sorting theoretically could be done in compile time, but much more >> complicated as we would have to simulate kernel addresses resolution >> for symbols, and then correct relocation entries. That would be risky >> if get out of sync. >> >> The improvement can be observed when listing ftrace filter functions. >> >> Before: >> >> root@nano:~# time cat /sys/kernel/tracing/available_filter_functions | wc -l >> 74908 >> >> real 0m1.315s >> user 0m0.000s >> sys 0m1.312s >> >> After: >> >> root@nano:~# time cat /sys/kernel/tracing/available_filter_functions | wc -l >> 74911 >> >> real 0m0.167s >> user 0m0.004s >> sys 0m0.175s >> >> (there are three more symbols introduced by the patch) >> >> For livepatch modules, the symtab layout is preserved and the existing >> linear search is used. For this case, it should be possible to keep >> the original ELF symtab instead of copying it 1:1, but that is outside >> the scope of this patch. > > What is the exact motivation for the special handling of livepatch modules, > please? > > Honestly, I am always a bit lost in the various symbol tables. It is > possile that I have got something wrong. > > Anyway, my understanding is that there are two aspects which are important > for livepatches: > > 1. Livepatches need to preserve special symbols which are used to > relocate symbols which were local in the original code, see > Documentation/livepatch/module-elf-format.rst > > IMHO, this is why layout_symtab() computes space for all core > symbols in livepatch modules and copies them in add_kallsyms(). > > The symtab is normally released when the module is loaded. > But livepatch modules make its own copy of the important > parts, see copy_module_elf(). > > IMHO, the sorting of function symbols vs other symbols does > not matter here. I believe that the special relocation > symbols are not affected by this. I'm not sure if I fully follow the conclusion in this point. My understanding is that .klp.rela sections still refer to their special symbols in the symbol table via Elf_Rela::r_info. If the symbol table is filtered or reordered, these references will end up pointing to incorrect symbols. This is also described in Documentation/livepatch/module-elf-format.rst, section "4.1 A livepatch module's symbol table". > > > 2. Livepatches _rely on the sorting_ of symbols in the module. > The special relocation symbols might define a symbol position, > see > > .klp.sym.objname.symbol_name,sympos > > in the documentation. It defines the position of the symbol > when there are more symbols of the same name, see > klp_match_callback() in kernel/livepatch/core.c. > > I am afraid that _this patch might break_ this when it moves > function symbols before non-funciton ones. I guess that > the symbols of the same name will not longer be groupped. I see. So if the module loader sorts the symbol table in a regular module and a livepatch module exists for that module, the livepatch may no longer function correctly. This means that the loader cannot even reorder the symbol table in regular modules. -- Thanks, Petr