From adf96e6f514a9e87aa3d26c8c9c03eca5be53df0 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Wed, 27 Feb 2013 09:59:50 -0800 Subject: [PATCH] sysrq: don't depend on weak undefined arrays to have an address that compares as NULL When taking an address of an extern array, gcc quite naturally should be able to say "an address of an object can never be NULL" and just optimize away the test entirely. However, the new alternate sysrq reset code (commit 154b7a489a5b: "Input: sysrq - allow specifying alternate reset sequence") did exactly that, and declared platform_sysrq_reset_seq[] as a weak array, and expecting that testing the address of the array would show whether it actually got linked against something or not. And that doesn't work with all gcc versions. Clearly it works with *some* versions of gcc, and maybe it's even supposed to work, but it really is a very fragile concept. So instead of testing the address of the weak variable, just create a weak instance of that array that is empty. If some platform then has a real platform_sysrq_reset_seq[] that overrides our weak one, the linker will switch to that one, and it all works without any run-time conditionals at all. Reported-by: Dave Airlie Cc: David Howells Cc: Russell King Cc: Dmitry Torokhov Acked-by: Mathieu Poirier Signed-off-by: Linus Torvalds --- drivers/tty/sysrq.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c index 814655ee2d61..3687f0cad642 100644 --- a/drivers/tty/sysrq.c +++ b/drivers/tty/sysrq.c @@ -870,21 +870,20 @@ static struct input_handler sysrq_handler = { static bool sysrq_handler_registered; +unsigned short platform_sysrq_reset_seq[] __weak = { KEY_RESERVED }; + static inline void sysrq_register_handler(void) { - extern unsigned short platform_sysrq_reset_seq[] __weak; unsigned short key; int error; int i; - if (platform_sysrq_reset_seq) { - for (i = 0; i < ARRAY_SIZE(sysrq_reset_seq); i++) { - key = platform_sysrq_reset_seq[i]; - if (key == KEY_RESERVED || key > KEY_MAX) - break; + for (i = 0; i < ARRAY_SIZE(sysrq_reset_seq); i++) { + key = platform_sysrq_reset_seq[i]; + if (key == KEY_RESERVED || key > KEY_MAX) + break; - sysrq_reset_seq[sysrq_reset_seq_len++] = key; - } + sysrq_reset_seq[sysrq_reset_seq_len++] = key; } error = input_register_handler(&sysrq_handler); -- 2.20.1