Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp455380pxb; Wed, 11 Nov 2020 07:49:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJz5UZdYwL1D+dXg3T4P3MEtoeidlv8TfDwlxMu3ifwe6Nwj/OQwvRmd9/9ct8vWsR6hnIM9 X-Received: by 2002:a50:d2d3:: with SMTP id q19mr5938896edg.173.1605109774667; Wed, 11 Nov 2020 07:49:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605109774; cv=none; d=google.com; s=arc-20160816; b=zZhaddsuYG9fWj0yjrBXEJR+qOEjZmvXyraJ0tVdsAD/cftEepDhUNeNF6nDlTFvC3 PG09fsGtYfe38PB7iPV7kQc0ctn3J5L+0FPrkWaaYNT0qkY7VjrQbz8TKIzbBKVX4Ojz XB7G2dsgmgtx74I8r9kO8fJG9UJ2UZbTE3YTnP1/ZFgEDbejFra+ufGKkZtPmrJHI0dz G22Ccdm7HkHqi3W7rPuwUkqT56NEBcZk64Z39yfzCu0npd2ARmRwaZxzhLSKCfBwEVUG Qawupk/ISeOlt4B2eUapYvKNkikZrp8SChbuH4IjrfGH2z6WAnnlndRZKH7cky1IiGVi VVAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=9YfkkQ9wkyZZeXSsWCszRktU6AOG71Q42S1YPmEDuW4=; b=hR6OfUngPnLXuKojw6w1MHJUPW5eQLPcJIZMc6g39dms2i/YKicZ6ZbRfslCfpBHC2 An9tMQgGI8I2PGC1aXVYSBUzJeanqTwQC8SCKe6WtrLJesafTsvBY098tRXwxagFdSG2 HqiqhOhTPg/Nnncia+2NYcvXdxfHjSuF6w27TbbcLpzrrML0V2wSZuHX3XQZidC5R8lU xT3+MMbHE3TMCRDhudJuToJW77+wEYBlR0VlQRGCBQ8XzIz8WPpV5OGYbjfYlh+uOMop iDsW78dv41U2NJ5cgESSwQzB9vehFFThMc0TnZUPKLCioqaK+kndVOzrlKXJZKi9ijSK emSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=yH0r3uT7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x4si1585684ejf.537.2020.11.11.07.49.10; Wed, 11 Nov 2020 07:49:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=yH0r3uT7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727041AbgKKPr1 (ORCPT + 99 others); Wed, 11 Nov 2020 10:47:27 -0500 Received: from mail.kernel.org ([198.145.29.99]:42714 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726274AbgKKPrY (ORCPT ); Wed, 11 Nov 2020 10:47:24 -0500 Received: from linux-8ccs (p57a236d4.dip0.t-ipconnect.de [87.162.54.212]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C8B4E20709; Wed, 11 Nov 2020 15:47:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1605109643; bh=9YfkkQ9wkyZZeXSsWCszRktU6AOG71Q42S1YPmEDuW4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=yH0r3uT7ZVJ/rL/JLJzzPkKUzrrJfgOhruyaFU7b+LW4L6jPFinMWadlrS66zQmBi zxDYjCri6wVsNVwJcrHvbzjg3iMCLuUrhhTcmtScnN7I9MqWu2Kx3DRJ+vrt6Ip3iL TtxAITaYRnJ3F/ZNAdHzXE+3gnZPfu1GDZ4ZqaWc= Date: Wed, 11 Nov 2020 16:47:16 +0100 From: Jessica Yu To: Johan Hovold Cc: linux-kernel@vger.kernel.org, Linus Torvalds , Rob Herring , Frank Rowand , Greg Kroah-Hartman , Nick Desaulniers , Arnd Bergmann , Geert Uytterhoeven , Dmitry Torokhov , David Miller , Jakub Jelinek , Peter Zijlstra , Thomas Gleixner , Steven Rostedt , Daniel Kurtz , linux-arch@vger.kernel.org, linux-m68k@lists.linux-m68k.org Subject: Re: [PATCH 0/8] linker-section array fix and clean ups Message-ID: <20201111154716.GB5304@linux-8ccs> References: <20201103175711.10731-1-johan@kernel.org> <20201106160344.GA12184@linux-8ccs.fritz.box> <20201106164537.GD4085@localhost> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20201106164537.GD4085@localhost> X-OS: Linux linux-8ccs 4.12.14-lp150.12.61-default x86_64 User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org +++ Johan Hovold [06/11/20 17:45 +0100]: >On Fri, Nov 06, 2020 at 05:03:45PM +0100, Jessica Yu wrote: >> +++ Johan Hovold [03/11/20 18:57 +0100]: >> >We rely on the linker to create arrays for a number of things including >> >kernel parameters and device-tree-match entries. >> > >> >The stride of these linker-section arrays obviously needs to match the >> >expectations of the code accessing them or bad things will happen. >> > >> >One thing to watch out for is that gcc is known to increase the >> >alignment of larger objects with static extent as an optimisation (on >> >x86), but this can be suppressed by using the aligned attribute when >> >declaring entries. >> > >> >We've been relying on this behaviour for 16 years for kernel parameters >> >(and other structures) and it indeed hasn't changed since the >> >introduction of the aligned attribute in gcc 3.1 (see align_variable() >> >in [1]). >> > >> >Occasionally this gcc optimisation do cause problems which have instead >> >been worked around in various creative ways including using indirection >> >through an array of pointers. This was originally done for tracepoints >> >[2] after a number of failed attempts to create properly aligned arrays, >> >and the approach was later reused for module-version attributes [3] and >> >earlycon entries. >> >> >[2] https://lore.kernel.org/lkml/20110126222622.GA10794@Krystal/ > >> So unfortunately, I am not familiar enough with the semantics of gcc's >> aligned attribute. AFAICT from the patch you linked in [2], the >> original purpose of the pointer indirection workaround was to avoid >> relying on (potentially inconsistent) compiler-specific behavior with >> respect to the aligned attribute. The main concern was potential >> up-alignment being done by gcc (or the linker) despite the desired >> alignment being specified. Indeed, the gcc documentation also states >> that the aligned attribute only specifies the *minimum* alignment, >> although there's no guarantee that up-alignment wouldn't occur. >> >> So I guess my question is, is there some implicit guarantee that >> specifying alignment by type via __alignof__ that's supposed to >> prevent gcc from up-aligning? Or are we just assuming that gcc won't >> increase the alignment? The gcc docs don't seem to clarify this >> unfortunately. > >It's simply specifying alignment when declaring the variable that >prevents this optimisation. The relevant code is in the function >align_variable() in [1] where DATA_ALIGNMENT() is never called in case >an alignment has been specified (!DECL_USER_ALIGN(decl)). > >There's no mention in the documentation of this that I'm aware of, but >this is the way the aligned attribute has worked since its introduction >judging from the commit history. > >As mentioned above, we've been relying on this for kernel parameters and >other structures since 2003-2004 so if it ever were to change we'd find >out soon enough. > >It's about to be used for scheduler classes as well. [2] > >Johan > >[1] https://github.com/gcc-mirror/gcc/blob/master/gcc/varasm.c >[2] https://lore.kernel.org/r/160396870486.397.377616182428528939.tip-bot2@tip-bot2 Thanks for providing the links and references. Your explanation and this reply from Jakub [1] clarified things for me. I was not aware of the distinction gcc made between aligned attributes on types vs. on variables. So from what I understand now, gcc suppresses the optimization when the alignment is specified in the variable declaration, but not necessarily when the aligned attribute is just on the type. Even though it's been in use for a long time, I think it would be really helpful if this gcc quirk was explained just a bit more in the patch changelogs, especially since this is undocumented behavior. I found the explanation in [1] (as well as in your cover letter) to be sufficient. Maybe something like "GCC suppresses any optimizations increasing alignment when the alignment is specified in the variable declaration, as opposed to just on the type definition. Therefore, explicitly specify type alignment when declaring entries to prevent gcc from increasing alignment." In any case, I can take the module and moduleparam.h patches through my tree, but I will wait a few days in case there are any objections. Thanks, Jessica [1] https://lore.kernel.org/lkml/20201021131806.GA2176@tucnak/