From patchwork Thu Mar 10 18:19:54 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Pitre X-Patchwork-Id: 8559321 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id C475CC0553 for ; Thu, 10 Mar 2016 18:22:20 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E379B2014A for ; Thu, 10 Mar 2016 18:22:19 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 040742012B for ; Thu, 10 Mar 2016 18:22:19 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ae5CB-0002MQ-22; Thu, 10 Mar 2016 18:20:23 +0000 Received: from mail-qg0-x236.google.com ([2607:f8b0:400d:c04::236]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1ae5C7-0001CZ-AY for linux-arm-kernel@lists.infradead.org; Thu, 10 Mar 2016 18:20:21 +0000 Received: by mail-qg0-x236.google.com with SMTP id w104so77697214qge.1 for ; Thu, 10 Mar 2016 10:19:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:user-agent:mime-version; bh=HAWjJ6XKomT5bHM/2X1oIrDsGBJAr5hznv5/ZabddrA=; b=K2UnH5Uvz4cj91zXxptlqHM+Zt7QXq0lbi9eX1W2X8CDhJUHy8aMDXK9LcyxMeOgBl 2zJSN8cLndmK7jmbTP4z593pYiPVNVfhXm7jC/LtmrBEFJQuifUTGjG/EK+MYDUEmo+0 czd2ZpHlIf1or1fihFv7Sa6/kd9NrkFmmFSXs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:cc:subject:message-id:user-agent :mime-version; bh=HAWjJ6XKomT5bHM/2X1oIrDsGBJAr5hznv5/ZabddrA=; b=SlVXTV5tBNM1SbQjwYdgNVZEx2+XLdB0PXyNL+QMgeFwK/aE4Mn0Ss6OTcXUj0Gw92 O8hAac/GklrZBi5ffiesWTKtF/BNMSLQodY9Pd5uCERl0CuMBnSHntMr/a4S0Waf0eQ8 SsFtJSIQW8j0MTI85VwcoBmbST29pdAXz+h/0YJZ0GXBjOTGXOBt70TQ8Mv3nd1OpZW6 vA4/h80iWCu5HHkCsRmuhVpO6CFBOH94Moz0q6CrQhHNf8scw5/4opHfsygotTfRt8yq Vb+FQKhf9eHn5/hDs4mN4bTw589UZg3GQX/kVFx3LOyFxkNcrT96El41KD5GeDNxKfPn Tx+g== X-Gm-Message-State: AD7BkJIgn4W0XGI/pXVEU4f9CRRhG9a47RBQZDnCBmBgeTsjPUJuAv0RUyQz2Y34ED4i//qH X-Received: by 10.140.228.68 with SMTP id y65mr6386146qhb.78.1457633996731; Thu, 10 Mar 2016 10:19:56 -0800 (PST) Received: from xanadu.home ([2607:fa48:6e39:d410:feaa:14ff:fea7:ed77]) by smtp.gmail.com with ESMTPSA id f83sm2190295qkb.25.2016.03.10.10.19.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 10 Mar 2016 10:19:55 -0800 (PST) Date: Fri, 11 Mar 2016 01:19:54 +0700 (ICT) From: Nicolas Pitre To: linux-arm-kernel@lists.infradead.org Subject: [PATCH] harden idiv patching against undefined gcc behavior Message-ID: User-Agent: Alpine 2.20 (LFD 67 2015-01-07) MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160310_102019_596958_59DF306E X-CRM114-Status: GOOD ( 12.06 ) X-Spam-Score: -2.7 (--) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Marcin Juszkiewicz , Arnd Bergmann Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP It was reported that a kernel with CONFIG_ARM_PATCH_IDIV=y stopped booting when compiled with the upcoming gcc 6. Turns out that turning a function address into a writable array is undefined and gcc 6 decided it was OK to omit the store to the first word of the function while still preserving the store to the second word. Even though gcc 6 is now fixed to behave more coherently, it is a mystery that gcc 4 and gcc 5 actually produce wanted code in the kernel. And in fact the reduced test case to illustrate the issue does indeed break with gcc < 6 as well. In any case, let's guard the kernel against undefined compiler behavior by hiding the nature of the array location as suggested by gcc developers. Reference: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70128 Signed-off-by: Nicolas Pitre Reported-by: Marcin Juszkiewicz diff --git a/arch/arm/kernel/setup.c b/arch/arm/kernel/setup.c index 7d0cba6f1c..c86ea8aac2 100644 --- a/arch/arm/kernel/setup.c +++ b/arch/arm/kernel/setup.c @@ -430,11 +430,13 @@ static void __init patch_aeabi_idiv(void) pr_info("CPU: div instructions available: patching division code\n"); fn_addr = ((uintptr_t)&__aeabi_uidiv) & ~1; + asm ("" : "+g" (fn_addr)); ((u32 *)fn_addr)[0] = udiv_instruction(); ((u32 *)fn_addr)[1] = bx_lr_instruction(); flush_icache_range(fn_addr, fn_addr + 8); fn_addr = ((uintptr_t)&__aeabi_idiv) & ~1; + asm ("" : "+g" (fn_addr)); ((u32 *)fn_addr)[0] = sdiv_instruction(); ((u32 *)fn_addr)[1] = bx_lr_instruction(); flush_icache_range(fn_addr, fn_addr + 8);