stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/8] of: fix linker-section match-table corruption
       [not found] <20201123102319.8090-1-johan@kernel.org>
@ 2020-11-23 10:23 ` Johan Hovold
  2020-12-04 14:02   ` Johan Hovold
  0 siblings, 1 reply; 2+ messages in thread
From: Johan Hovold @ 2020-11-23 10:23 UTC (permalink / raw)
  To: Rob Herring, Greg Kroah-Hartman, Jessica Yu, Linus Torvalds
  Cc: Frank Rowand, Arnd Bergmann, Geert Uytterhoeven, Dmitry Torokhov,
	David Miller, Jakub Jelinek, Peter Zijlstra, Thomas Gleixner,
	Steven Rostedt, Daniel Kurtz, linux-arch, linux-m68k,
	linux-kernel, Johan Hovold, stable

Specify type alignment when declaring linker-section match-table entries
to prevent gcc from increasing alignment and corrupting the various
tables with padding (e.g. timers, irqchips, clocks, reserved memory).

This is specifically needed on x86 where gcc (typically) aligns larger
objects like struct of_device_id with static extent on 32-byte
boundaries which at best prevents matching on anything but the first
entry. Specifying alignment when declaring variables suppresses this
optimisation.

Here's a 64-bit example where all entries are corrupt as 16 bytes of
padding has been inserted before the first entry:

	ffffffff8266b4b0 D __clk_of_table
	ffffffff8266b4c0 d __of_table_fixed_factor_clk
	ffffffff8266b5a0 d __of_table_fixed_clk
	ffffffff8266b680 d __clk_of_table_sentinel

And here's a 32-bit example where the 8-byte-aligned table happens to be
placed on a 32-byte boundary so that all but the first entry are corrupt
due to the 28 bytes of padding inserted between entries:

	812b3ec0 D __irqchip_of_table
	812b3ec0 d __of_table_irqchip1
	812b3fa0 d __of_table_irqchip2
	812b4080 d __of_table_irqchip3
	812b4160 d irqchip_of_match_end

Verified on x86 using gcc-9.3 and gcc-4.9 (which uses 64-byte
alignment), and on arm using gcc-7.2.

Note that there are no in-tree users of these tables on x86 currently
(even if they are included in the image).

Fixes: 54196ccbe0ba ("of: consolidate linker section OF match table declarations")
Fixes: f6e916b82022 ("irqchip: add basic infrastructure")
Cc: stable <stable@vger.kernel.org>     # 3.9
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 include/linux/of.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/include/linux/of.h b/include/linux/of.h
index 5d51891cbf1a..af655d264f10 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -1300,6 +1300,7 @@ static inline int of_get_available_child_count(const struct device_node *np)
 #define _OF_DECLARE(table, name, compat, fn, fn_type)			\
 	static const struct of_device_id __of_table_##name		\
 		__used __section("__" #table "_of_table")		\
+		__aligned(__alignof__(struct of_device_id))		\
 		 = { .compatible = compat,				\
 		     .data = (fn == (fn_type)NULL) ? fn : fn  }
 #else
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2 1/8] of: fix linker-section match-table corruption
  2020-11-23 10:23 ` [PATCH v2 1/8] of: fix linker-section match-table corruption Johan Hovold
@ 2020-12-04 14:02   ` Johan Hovold
  0 siblings, 0 replies; 2+ messages in thread
From: Johan Hovold @ 2020-12-04 14:02 UTC (permalink / raw)
  To: Rob Herring, Greg Kroah-Hartman, Jessica Yu, Linus Torvalds
  Cc: Frank Rowand, Arnd Bergmann, Geert Uytterhoeven, Dmitry Torokhov,
	David Miller, Jakub Jelinek, Peter Zijlstra, Thomas Gleixner,
	Steven Rostedt, Daniel Kurtz, linux-arch, linux-m68k,
	linux-kernel, Johan Hovold, stable

Rob,

On Mon, Nov 23, 2020 at 11:23:12AM +0100, Johan Hovold wrote:
> Specify type alignment when declaring linker-section match-table entries
> to prevent gcc from increasing alignment and corrupting the various
> tables with padding (e.g. timers, irqchips, clocks, reserved memory).
> 
> This is specifically needed on x86 where gcc (typically) aligns larger
> objects like struct of_device_id with static extent on 32-byte
> boundaries which at best prevents matching on anything but the first
> entry. Specifying alignment when declaring variables suppresses this
> optimisation.
> 
> Here's a 64-bit example where all entries are corrupt as 16 bytes of
> padding has been inserted before the first entry:
> 
> 	ffffffff8266b4b0 D __clk_of_table
> 	ffffffff8266b4c0 d __of_table_fixed_factor_clk
> 	ffffffff8266b5a0 d __of_table_fixed_clk
> 	ffffffff8266b680 d __clk_of_table_sentinel
> 
> And here's a 32-bit example where the 8-byte-aligned table happens to be
> placed on a 32-byte boundary so that all but the first entry are corrupt
> due to the 28 bytes of padding inserted between entries:
> 
> 	812b3ec0 D __irqchip_of_table
> 	812b3ec0 d __of_table_irqchip1
> 	812b3fa0 d __of_table_irqchip2
> 	812b4080 d __of_table_irqchip3
> 	812b4160 d irqchip_of_match_end
> 
> Verified on x86 using gcc-9.3 and gcc-4.9 (which uses 64-byte
> alignment), and on arm using gcc-7.2.
> 
> Note that there are no in-tree users of these tables on x86 currently
> (even if they are included in the image).
> 
> Fixes: 54196ccbe0ba ("of: consolidate linker section OF match table declarations")
> Fixes: f6e916b82022 ("irqchip: add basic infrastructure")
> Cc: stable <stable@vger.kernel.org>     # 3.9
> Signed-off-by: Johan Hovold <johan@kernel.org>
> ---

Could you pick this one up for 5.11?

Johan

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-12-04 14:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20201123102319.8090-1-johan@kernel.org>
2020-11-23 10:23 ` [PATCH v2 1/8] of: fix linker-section match-table corruption Johan Hovold
2020-12-04 14:02   ` Johan Hovold

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).