From patchwork Mon Jul 29 21:10:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Desaulniers X-Patchwork-Id: 170031 Delivered-To: patch@linaro.org Received: by 2002:a92:512:0:0:0:0:0 with SMTP id q18csp1745690ile; Mon, 29 Jul 2019 14:10:46 -0700 (PDT) X-Google-Smtp-Source: APXvYqwfTcBR0DVs0P7nr3ZkGX22GihN8H7Wge3m9eyzFsVbf84zW5+P67bcXE7EnRAXi1hHod4o X-Received: by 2002:a17:902:41:: with SMTP id 59mr17915917pla.195.1564434646013; Mon, 29 Jul 2019 14:10:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564434646; cv=none; d=google.com; s=arc-20160816; b=ww0791i5fHW9g0fmjpd3V7w83sV5+wvv0wpdrlkBqzKM4Rx7UVf1gUnkYexiWpjEhu DQBaY2QXNpKwoaZfQT8wGWNsTvHytXwclkJtPYbqviNPHy4H7wJsdCQQxg/t4xd64Z7R GSSFVtF+DiJgzysWcx4VaxV0yMoq2O+2MAf9DIocI0uDFeJ6hGnhUYZ55dYyG4UpqQ60 SevU6XO3eTdknwJvtaKTJ/yWgz7RWuV04/IEBOA2N8Tcojfep/LPlmbdnnaORwCtrIea i2OuM45WKimU+sqK74vcVxnoUPZ1wj+l2KZDYhwlXWgiJlLpTtTmCkSOmTMg7ZNf5vKf LcXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=xwrp+21u5eMaNhEA/rdpeLPdIpATKFWq+XtNgIYdLsk=; b=qm3Ae0yMZreBmMCmFIMnDLq1S+cmXnlFEW4CMIDWH5sXMdKyOqEEeHyTRy/9C0ghJL b5Mx0u1oY4pQrYQGfZFaKje4o0nOVv4eJkRvaaTgA+6WAKpFeMoQNw44Rx1z7Q7JfCpA SwcOSQKcJYmjiAvplJ7W5B6tkGwzzGsTbeIRdWFt57iyZDAl7kFXnEidtDgQ/VX/Y2D8 Hl6QyaOZl47w3lELMe7zeI3sjWsDNz6RgWKiqOzSSxkysfhaQ9bV86PyFF9pynWZGXaE 1qwQp0rtyYEwL2V/WPbEKZ70jZfbYJrsI+/aEoB4K6Jumf/920u1DxfdpZhaqnWGMkSi Cpow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=nR6rThRr; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l36si1924395pgb.292.2019.07.29.14.10.45; Mon, 29 Jul 2019 14:10:46 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=nR6rThRr; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729870AbfG2VKo (ORCPT + 29 others); Mon, 29 Jul 2019 17:10:44 -0400 Received: from mail-pf1-f202.google.com ([209.85.210.202]:45362 "EHLO mail-pf1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729621AbfG2VKo (ORCPT ); Mon, 29 Jul 2019 17:10:44 -0400 Received: by mail-pf1-f202.google.com with SMTP id i27so39299400pfk.12 for ; Mon, 29 Jul 2019 14:10:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=xwrp+21u5eMaNhEA/rdpeLPdIpATKFWq+XtNgIYdLsk=; b=nR6rThRrSJlJJsQV5l5Vh48GpWZVzx3EORmd6Dy/ocAyzJewpwHUnPd/qA9KXAlHyn TgAPWGRvM0bYvV+63TWGXmoAJg9KbMwm2o0xWUiWL5nB+PeVdG/VnzJoKr6q7FwFkoxv Pfji0h7fRghn/60GhqBFvruZTa080G+fEkA+uVlztn7EBWIhJcp6aZg8P2JMvXElugLB 1gES93CDPeOKW2AjgRgsMtRrPl5UBy2JA9wRV/wERMQ/zQSQ3HSjsdagbuIuZlRR82cy rF/oqS15CbF0l3/8ay3H5ihxI2b1x1r0IwREl7OQCGD++PjnptSwlCoAhhiVyPIzRpVa mokw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=xwrp+21u5eMaNhEA/rdpeLPdIpATKFWq+XtNgIYdLsk=; b=qP3pyh+Qou+d+QIFQTFAMLKtuR9TnFOn3RZM/+mWI0EQlnLhMN4eqRn2CnRM8WXrCv UcFdNZEpxzFFAR+ZmGGiUFrkoPFw4GLCisCZJ6hcSi3wgS0rlMxZxlY1VuEHhsqFvh/m m4bpqJIgu/geUCrQSXjmjd5Misz1tpdJJ3GruMQgnxNDIS13K5efG8xE96R271L5rstl GxUdY6ILtfbooKa7osbOK3oq5Ixq9x2ZpzNbwt1dM+BGTI2AmxWdvhywPFecc2G7Jj4b gB8rtXOzQQVs9Zvtmnk0fs9urNHnhfEHVQ1m7VHCFOzpIG95ERCSrSu2hBMElnlNmrUp HLcg== X-Gm-Message-State: APjAAAVsL5IB5PUjQwEpRe4nCye3ktT+wQbQn/nninyqsfony5Iqj3pD KCxcsJRbMIl//fk/8SSDwn0K5a7fi5meY3UbS+g= X-Received: by 2002:a65:584f:: with SMTP id s15mr89215850pgr.175.1564434642782; Mon, 29 Jul 2019 14:10:42 -0700 (PDT) Date: Mon, 29 Jul 2019 14:10:12 -0700 Message-Id: <20190729211014.39333-1-ndesaulniers@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.22.0.709.g102302147b-goog Subject: [PATCH] mips: avoid explicit UB in assignment of mips_io_port_base From: Nick Desaulniers To: ralf@linux-mips.org, paul.burton@mips.com, jhogan@kernel.org Cc: Nick Desaulniers , Nathan Chancellor , Eli Friedman , "Maciej W. Rozycki" , Hassan Naveed , Stephen Kitt , Serge Semin , Mike Rapoport , Andrew Morton , Michal Hocko , linux-mips@vger.kernel.org, linux-kernel@vger.kernel.org, clang-built-linux@googlegroups.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The code in question is modifying a variable declared const through pointer manipulation. Such code is explicitly undefined behavior, and is the lone issue preventing malta_defconfig from booting when built with Clang: If an attempt is made to modify an object defined with a const-qualified type through use of an lvalue with non-const-qualified type, the behavior is undefined. LLVM is removing such assignments. A simple fix is to not declare variables const that you plan on modifying. Limiting the scope would be a better method of preventing unwanted writes to such a variable. Further, the code in question mentions "compiler bugs" without any links to bug reports, so it is difficult to know if the issue is resolved in GCC. The patch was authored in 2006, which would have been GCC 4.0.3 or 4.1.1. The minimal supported version of GCC in the Linux kernel is currently 4.6. For what its worth, there was UB before the commit in question, it just added a barrier and got lucky IRT codegen. I don't think there's any actual compiler bugs related, just runtime bugs due to UB. Link: https://github.com/ClangBuiltLinux/linux/issues/610 Fixes: 966f4406d903 ("[MIPS] Work around bad code generation for .") Reported-by: Nathan Chancellor Debugged-by: Nathan Chancellor Suggested-by: Eli Friedman Signed-off-by: Nick Desaulniers --- arch/mips/include/asm/io.h | 14 ++------------ arch/mips/kernel/setup.c | 2 +- 2 files changed, 3 insertions(+), 13 deletions(-) -- 2.22.0.709.g102302147b-goog Reviewed-by: Nathan Chancellor Tested-by: Nathan Chancellor diff --git a/arch/mips/include/asm/io.h b/arch/mips/include/asm/io.h index 97a280640daf..d58ff2229738 100644 --- a/arch/mips/include/asm/io.h +++ b/arch/mips/include/asm/io.h @@ -63,21 +63,11 @@ * instruction, so the lower 16 bits must be zero. Should be true on * on any sane architecture; generic code does not use this assumption. */ -extern const unsigned long mips_io_port_base; +extern unsigned long mips_io_port_base; -/* - * Gcc will generate code to load the value of mips_io_port_base after each - * function call which may be fairly wasteful in some cases. So we don't - * play quite by the book. We tell gcc mips_io_port_base is a long variable - * which solves the code generation issue. Now we need to violate the - * aliasing rules a little to make initialization possible and finally we - * will need the barrier() to fight side effects of the aliasing chat. - * This trickery will eventually collapse under gcc's optimizer. Oh well. - */ static inline void set_io_port_base(unsigned long base) { - * (unsigned long *) &mips_io_port_base = base; - barrier(); + mips_io_port_base = base; } /* diff --git a/arch/mips/kernel/setup.c b/arch/mips/kernel/setup.c index ab349d2381c3..675223a66d0c 100644 --- a/arch/mips/kernel/setup.c +++ b/arch/mips/kernel/setup.c @@ -76,7 +76,7 @@ static char __initdata builtin_cmdline[COMMAND_LINE_SIZE] = CONFIG_CMDLINE; * mips_io_port_base is the begin of the address space to which x86 style * I/O ports are mapped. */ -const unsigned long mips_io_port_base = -1; +unsigned long mips_io_port_base = -1; EXPORT_SYMBOL(mips_io_port_base); static struct resource code_resource = { .name = "Kernel code", };