Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp234837rdb; Thu, 21 Dec 2023 07:47:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IGg3aOQIKk1VzWgs5APm/feP90+6MpX3mt8Z678vEQCQQR4yvk75Jt4GbH01CzDYTUoQA2D X-Received: by 2002:aa7:8606:0:b0:6d4:262f:f064 with SMTP id p6-20020aa78606000000b006d4262ff064mr5038834pfn.31.1703173666777; Thu, 21 Dec 2023 07:47:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703173666; cv=none; d=google.com; s=arc-20160816; b=NLSQKJmfCWXc+/6IFz7Yay1FJoRk73qcm0hW0vJ7fKe93Awq8EYpJ7SwJ8/5U8pLnb i9idPQs6svp3/ksvQSyfEm9NlPzhty72ZdxP4wL41vOsiwfxJsbbsPvd22WS4BPOYwW4 l1EAYCfsW5nc4QiVKDjo84sUaWcREvIvE8g1D7ic/kx0Ju975SfeGgl4H8gMzxxVVI24 yWstUCCyt0axlcbybL/r68LC/vUvbFjyM8w8ca1z3nGkH4Tj/6mo6nXmBrYVQhMeDFAI 3IP3WaPWLRAkEKgG+Uqhnt7VATHVK+v5mzjpas6Qml9lTyebo/OuBF35c2ivUvdv/dc9 nNXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=1k+/B4guT896qKtota1MGQ4SSG0753NJP6gkGq0fwOM=; fh=3oLi19GBkyhJDnfPDVreUUqO1j4ttvrJvxlwBFT/HZY=; b=hbKxKjrb/seGOa6nGE9fgQZZW/1fufiAmBPxXKPffyGjHKbZb0O0oR9c9tm6JyIuwJ 9Q8VXoK/L64C1SSaO+ly85PFphhpZU9EUWexih5uBL2030pB3E6bD6Mh2qWtj86TqyUP L5/T803p8Ef4JxNLIRmL11AqKpE59658aO27A3UsEJRmjR51NYs4AQmUJMA1lbJGtbvp EHsX3qEB6lf7nNFnlx0YUy14I7khx29LodgREVtE+Me/IIEgfzglDqQ0WZX9GeQZAhZ/ YXyNkC14QON11UxtwvFE+UPUECpzyMDEMoC5EjUylxU1alaCsYaIasH/G2GtPpiS/kO8 KG6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="W0Lb/onC"; spf=pass (google.com: domain of linux-kernel+bounces-8696-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-8696-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id e5-20020a62ee05000000b006d97dbf7d2bsi510344pfi.244.2023.12.21.07.47.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Dec 2023 07:47:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-8696-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="W0Lb/onC"; spf=pass (google.com: domain of linux-kernel+bounces-8696-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-8696-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 7CF70B2501D for ; Thu, 21 Dec 2023 15:45:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9EEC662803; Thu, 21 Dec 2023 15:43:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="W0Lb/onC" X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CA71E627F6; Thu, 21 Dec 2023 15:43:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 54B74C433C9; Thu, 21 Dec 2023 15:43:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1703173382; bh=MWfBIeGV77ZPzBms8I8+8JYz2lrVS9wQV+KpBlK+plk=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=W0Lb/onCPnWS0E5hgE4x6WdFMxQ58Or6FUWxq/OSmNPXNwii2WmeCJnDV3xmnzq63 dpprRWXsgzVCexyIyzaPWn8RroSwdqs33A4Yc5+7rVEes8I3SGaorNATbQm5hndX1i eVkALAkqwpcDyiJdeePD0xkX0ACVAohAzifciRfMu83zR2IYoNk6SGS91GF+ysh541 0pm9tAkmDo4i1mluLF5Epv+PGKSmSTPAZ+PjJXI/z10w2XapnKpB+EIjMF4UCFpogy WIFpZjXCgriO3/tk56SjdZY6HSyyW3oSll3Vm75VVFwv/jQRTQknkzC6ggbRRlgI7G jXrBAZlOF2L/Q== Received: by mail-oa1-f50.google.com with SMTP id 586e51a60fabf-1f03d9ad89fso640217fac.1; Thu, 21 Dec 2023 07:43:02 -0800 (PST) X-Gm-Message-State: AOJu0YyLF3XeX/aTAwi4sUWyTANupZeBIQJDeyKllq2ZgmRyNryZVPUt j8EI99KGlUMcNhC0M9DnI0OCsflLWSg9ocYOk24= X-Received: by 2002:a05:6871:b0c:b0:204:31a1:9b84 with SMTP id fq12-20020a0568710b0c00b0020431a19b84mr950136oab.64.1703173381696; Thu, 21 Dec 2023 07:43:01 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20231122221814.139916-1-deller@kernel.org> In-Reply-To: From: Masahiro Yamada Date: Fri, 22 Dec 2023 00:42:24 +0900 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 0/4] Section alignment issues? To: deller@kernel.org Cc: linux-kernel@vger.kernel.org, Arnd Bergmann , linux-modules@vger.kernel.org, linux-arch@vger.kernel.org, Luis Chamberlain Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Dec 21, 2023 at 10:40=E2=80=AFPM Masahiro Yamada wrote: > > On Thu, Nov 23, 2023 at 7:18=E2=80=AFAM wrote: > > > > From: Helge Deller > > > > While working on the 64-bit parisc kernel, I noticed that the __ksymtab= [] > > table was not correctly 64-bit aligned in many modules. > > The following patches do fix some of those issues in the generic code. > > > > But further investigation shows that multiple sections in the kernel an= d in > > modules are possibly not correctly aligned, and thus may lead to perfor= mance > > degregations at runtime (small on x86, huge on parisc, sparc and others= which > > need exception handlers). Sometimes wrong alignments may also be simply= hidden > > by the linker or kernel module loader which pulls in the sections by lu= ck with > > a correct alignment (e.g. because the previous section was aligned alre= ady). > > > > An objdump on a x86 module shows e.g.: > > > > ./kernel/net/netfilter/nf_log_syslog.ko: file format elf64-x86-64 > > Sections: > > Idx Name Size VMA LMA File of= f Algn > > 0 .text 00001fdf 0000000000000000 0000000000000000 0000004= 0 2**4 > > CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE > > 1 .init.text 000000f6 0000000000000000 0000000000000000 0000202= 0 2**4 > > CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE > > 2 .exit.text 0000005c 0000000000000000 0000000000000000 0000212= 0 2**4 > > CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE > > 3 .rodata.str1.8 000000dc 0000000000000000 0000000000000000 000021= 80 2**3 > > CONTENTS, ALLOC, LOAD, READONLY, DATA > > 4 .rodata.str1.1 0000030a 0000000000000000 0000000000000000 000022= 5c 2**0 > > CONTENTS, ALLOC, LOAD, READONLY, DATA > > 5 .rodata 000000b0 0000000000000000 0000000000000000 0000258= 0 2**5 > > CONTENTS, ALLOC, LOAD, READONLY, DATA > > 6 .modinfo 0000019e 0000000000000000 0000000000000000 0000263= 0 2**0 > > CONTENTS, ALLOC, LOAD, READONLY, DATA > > 7 .return_sites 00000034 0000000000000000 0000000000000000 000027c= e 2**0 > > CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA > > 8 .call_sites 0000029c 0000000000000000 0000000000000000 0000280= 2 2**0 > > CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA > > > > In this example I believe the ".return_sites" and ".call_sites" should = have > > an alignment of at least 32-bit (4 bytes). > > > > On other architectures or modules other sections like ".altinstructions= " or > > "__ex_table" may show up wrongly instead. > > > > In general I think it would be beneficial to search for wrong alignment= s at > > link time, and maybe at runtime. > > > > The patch at the end of this cover letter > > - adds compile time checks to the "modpost" tool, and > > - adds a runtime check to the kernel module loader at runtime. > > And it will possibly show false positives too (!!!) > > I do understand that some of those sections are not performce critical > > and thus any alignment is OK. > > > > The modpost patch will emit at compile time such warnings (on x86-64 ke= rnel build): > > > > WARNING: modpost: vmlinux: section .initcall7.init (type 1, flags 2) ha= s alignment of 1, expected at least 4. > > Maybe you need to add ALIGN() to the modules.lds file (or fix modpost) = ? > > WARNING: modpost: vmlinux: section .altinstructions (type 1, flags 2) h= as alignment of 1, expected at least 2. > > WARNING: modpost: vmlinux: section .initcall6.init (type 1, flags 2) ha= s alignment of 1, expected at least 4. > > WARNING: modpost: vmlinux: section .initcallearly.init (type 1, flags 2= ) has alignment of 1, expected at least 4. > > WARNING: modpost: vmlinux: section .rodata.cst2 (type 1, flags 18) has = alignment of 2, expected at least 64. > > WARNING: modpost: vmlinux: section .static_call_tramp_key (type 1, flag= s 2) has alignment of 1, expected at least 8. > > WARNING: modpost: vmlinux: section .con_initcall.init (type 1, flags 2)= has alignment of 1, expected at least 8. > > WARNING: modpost: vmlinux: section __bug_table (type 1, flags 3) has al= ignment of 1, expected at least 4. > > ... > > > > > modpost acts on vmlinux.o instead of vmlinux. > > > vmlinux.o is a relocatable ELF, which is not a real layout > because no linker script has been considered yet at this > point. > > > vmlinux is an executable ELF, produced by a linker, > with the linker script taken into consideration > to determine the final section/symbol layout. > > > So, checking this in modpost is meaningless. > > > > I did not check the module checking code, but > modules are also relocatable ELF. Sorry, I replied too early. (Actually I replied without reading your modpost code). Now, I understand what your checker is doing. I did not test how many false positives are produced, but it catches several suspicious mis-alignments. However, I am not convinced with this warning. + warn("%s: section %s (type %d, flags %lu) has alignment of %d, expected at least %d.\n" + "Maybe you need to add ALIGN() to the modules.lds file (or fix modpost) ?\n", + modname, sec, sechdr->sh_type, sechdr->sh_flags, is_shalign, should_shalign); + } Adding ALGIN() hides the real problem. I think the real problem is that not enough alignment was requested in the code. For example, the right fix for ".initcall7.init" should be this: diff --git a/include/linux/init.h b/include/linux/init.h index 3fa3f6241350..650311e4b215 100644 --- a/include/linux/init.h +++ b/include/linux/init.h @@ -264,6 +264,7 @@ extern struct module __this_module; #define ____define_initcall(fn, __stub, __name, __sec) \ __define_initcall_stub(__stub, fn) \ asm(".section \"" __sec "\", \"a\" \n" \ + ".balign 4 \n" \ __stringify(__name) ": \n" \ ".long " __stringify(__stub) " - . \n" \ ".previous \n"); \ Then, "this section requires at least 4 byte alignment" is recorded in the sh_addralign field. Then, the rest is the linker's job. We should not tweak the linker script. -- Best Regards Masahiro Yamada