From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 4A95E2DF6E9 for ; Tue, 5 May 2026 14:38:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777991881; cv=none; b=mP7Q3TaYczHA1+I8GuMwZ/rxrkidF218pbTliRev5b3Wp0Ke/tBcBW3/GW+sT642Ict6kfprq93kxPstZQWjBWT0D2Fc2mYovVAzGRYFLn4T08k2kTVNSy37CJuPfl4yn5TMzn43nzCVG1Smz3bmvGU1STPYgPZVohSEvGwqNmU= 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.51 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-f51.google.com with SMTP id 5b1f17b1804b1-488af96f6b2so68718225e9.0 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=XONWXcNYabLaWAQNsbOwItzmn1kyXQW21zF0sqyBupwTlb/AIZBd98Ns2HHS9qjI6k yAeoewcLdorVJHLLNjTURP3E7X/nW2pEXa0Tksdd1npI4r7UjlH4jf+ScQUVUyt8ALZw yNA4msxmzB5/2Ybvrw1N6+pAAawviLOnyT5vNCAG/pVWnL/DK02KHbEqXTufW+zcQspI G9dCYqFbvLBLeFwIBVLvNjT1MNDq9hZ6GLMSIi1zmnL+4cHKDQcQjWm53x8mkvyFCBtT YQmbIzysANmVTaFGrDv1toDxmemn1CKRA0Ar3kIBqOzb/3F35/5ZkeiLqVqDsUzJVUtz PfOA== X-Forwarded-Encrypted: i=1; AFNElJ8+IIUKBgTCBs+0w99KV4wY53uqzYgbjBBe8t8QC16/DgXOldWL+UfMUjG3Z387uTCDIpMGqaBXQ2JVbHs=@vger.kernel.org X-Gm-Message-State: AOJu0Yyu7J318r4rHlDTXxOcqfgW/uTATO9l8FOtB1tLhGpvB1mC9YpT mxcRVpbc2qC4bsRvA8oJwC7VtyzxIa0RMECQxMH7WTDSzZKzv2hB7vAUXCH4hS121zY= X-Gm-Gg: AeBDiet50xVC+zTUtmtpyJSfDWMlnIlPZZTfPBegwJjax4yfdpNteuQ4qnJTfMV1LP3 8X1HO7xslN2EziTy6t9sgfDBdv2SYEJ9aM/yp4QW4LeHsjOurwC5qxY4aTi770hLRNOWk1tRJLC FnrmKcJQNbvdQtb7wc2CljbJllQEuBCPsle6C9F96P/PFmPNF6fDJV+ev6uiHTX3d7pvVNLJaga X4F2vO4T5haFCsgco3q1XTUFhMCbjD9VKyhJfxcR3RjQof4QcsfmHoncibn6sY6o35276BxyCui CgHn2njvCzOa5BwskGsA5mUZmH8cPqMtq+20DkUjB/zTGBXKDfjSfTjEoBaf+B9Qmpvx0pZXNcW 0n0zcPzryjwHTEwoekfnZaJUeSpMnMecnYmSZnKiL9TfLDBFzGV5d9rYxFPZa3GM90dNpQUZFKB RHIfckpcukUmEd00pweKxK7z0ziJx6CgxZ5SPAK1z3/+W2lRnLoM/8rasvW1wS5fbImiHVhGoDe xJkBcCQwAKL77rf+txb8GZVBz7Is3Gu20XdqTqgHcjsN2MxF8YPxEb0kcgKIjWOklrKzeCHu7So z+O4vgGPE35cB4M= X-Received: by 2002:a05: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-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