From patchwork Mon Aug 17 15:12:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 247859 Delivered-To: patch@linaro.org Received: by 2002:a92:cc90:0:0:0:0:0 with SMTP id x16csp2888479ilo; Mon, 17 Aug 2020 12:04:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxuMdxMlQ/4O8CdXfVqmZaPEVt/uf+XFIr4ZeP2n9yXHqWkIjv72gaDfW1Ard9NmwQVg44E X-Received: by 2002:a17:906:ca8c:: with SMTP id js12mr17013093ejb.195.1597691097464; Mon, 17 Aug 2020 12:04:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597691097; cv=none; d=google.com; s=arc-20160816; b=mk5zlEHvFszJ5LSi3fltmf3p7b4A0IpMENa9DQn216xvQDneVag+QOF/A8Ct728AdF wcND3btfx88itZmccWiUu8FhEpjwGUuUrV+y2PbUjxChfs2M9yPfmWXMokqBXeFsdGPD AGQbLwZYDRzYWJ6vMFvWIeMioOFFGp8FVdT/Om+gK6psgWZpRKk7+1psFBCumrEC6/FU M4sD2RnvkCU8fM++uuWBLx5CRoAkn0Ahj5x9vyYyWzSMyLeEHYW7ylc5WgA9hGPOM+yg oPNUdHGGPwSzgWo2AYbbRjcuZBLcpX4n6WkTdyjT0yEY51HliRvLxrzD/S1JdKmHvr6z Zx7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=dCrAXBpd+MTsZ75enP1QSifoGti7vZlU+EOIjgqmEMg=; b=Ra6h8hZD5RSk5vhldwtsGPbkEohb54xJ8rhM3uRD6EzzFtFbuzlaErNLXdSm6TzWqj BsXkW4Il6BIdp3zx7jGgL7cnVTxs0/Fgq0KXPxJathwuRPnJsotRs3/9vBcJmlltTA3G ERGXiv/8KGF9sEhzXcWFSGHfHGIJdeGY9loYWLxYwGTxokHT59W5Q2VHYvJxgIlA50QK rjZQOH6lmEDvOcGxwKsmoNl26IaPVFPCSfI9rJw3DqvwYkXyiUk13v1NOHVEPSKp9s/A 39CZoNFuEB5/+GL4e4OQ1EGdV1yYl17wlGFW3UYu32XwRVs5VzBQYLs9fY2LjePEAh6+ 1A9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=rpdce3zT; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y7si11263142ejr.228.2020.08.17.12.04.57; Mon, 17 Aug 2020 12:04:57 -0700 (PDT) Received-SPF: pass (google.com: domain of stable-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=rpdce3zT; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730818AbgHQTEc (ORCPT + 15 others); Mon, 17 Aug 2020 15:04:32 -0400 Received: from mail.kernel.org ([198.145.29.99]:56992 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730658AbgHQPph (ORCPT ); Mon, 17 Aug 2020 11:45:37 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 5D9E0206FA; Mon, 17 Aug 2020 15:45:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1597679136; bh=+k++uvc5navQ2uKqFX6t6jOMhAz3QjELVmlOd/6ryvQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rpdce3zTVhpMaA7R/AXxE1xhEoK7EiejnBGyxexrFfG0C/oaRSQorD236dRCBHuCy Ouu9h+Ih6fkzahjethJVGqx/YY1ItvU28EA6E+LY4P/OSNo1VuZOOwoAOzYyhyOXH2 QgVQy02JiRTHMbIEM0oET2HO0PZsAAyQw0ZOrJT0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Arnd Bergmann , David Woodhouse , Dmitry Golovin , Linus Torvalds , Nick Desaulniers , Thomas Gleixner , Sedat Dilek , Dennis Zhou , Sasha Levin Subject: [PATCH 5.7 108/393] x86/uaccess: Make __get_user_size() Clang compliant on 32-bit Date: Mon, 17 Aug 2020 17:12:38 +0200 Message-Id: <20200817143824.853544986@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200817143819.579311991@linuxfoundation.org> References: <20200817143819.579311991@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Nick Desaulniers [ Upstream commit 158807de5822d1079e162a3762956fd743dd483e ] Clang fails to compile __get_user_size() on 32-bit for the following code: long long val; __get_user(val, usrptr); with: error: invalid output size for constraint '=q' GCC compiles the same code without complaints. The reason is that GCC and Clang are architecturally different, which leads to subtle issues for code that's invalid but clearly dead, i.e. with code that emulates polymorphism with the preprocessor and sizeof. GCC will perform semantic analysis after early inlining and dead code elimination, so it will not warn on invalid code that's dead. Clang strictly performs optimizations after semantic analysis, so it will warn for dead code. Neither Clang nor GCC like this very much with -m32: long long ret; asm ("movb $5, %0" : "=q" (ret)); However, GCC can tolerate this variant: long long ret; switch (sizeof(ret)) { case 1: asm ("movb $5, %0" : "=q" (ret)); break; case 8:; } Clang, on the other hand, won't accept that because it validates the inline asm for the '1' case before the optimisation phase where it realises that it wouldn't have to emit it anyway. If LLVM (Clang's "back end") fails such as during instruction selection or register allocation, it cannot provide accurate diagnostics (warnings / errors) that contain line information, as the AST has been discarded from memory at that point. While there have been early discussions about having C/C++ specific language optimizations in Clang via the use of MLIR, which would enable such earlier optimizations, such work is not scoped and likely a multi-year endeavor. It was discussed to change the asm output constraint for the one byte case from "=q" to "=r". While it works for 64-bit, it fails on 32-bit. With '=r' the compiler could fail to chose a register accessible as high/low which is required for the byte operation. If that happens the assembly will fail. Use a local temporary variable of type 'unsigned char' as output for the byte copy inline asm and then assign it to the real output variable. This prevents Clang from failing the semantic analysis in the above case. The resulting code for the actual one byte copy is not affected as the temporary variable is optimized out. [ tglx: Amended changelog ] Reported-by: Arnd Bergmann Reported-by: David Woodhouse Reported-by: Dmitry Golovin Reported-by: Linus Torvalds Signed-off-by: Nick Desaulniers Signed-off-by: Thomas Gleixner Tested-by: Sedat Dilek Acked-by: Linus Torvalds Acked-by: Dennis Zhou Link: https://bugs.llvm.org/show_bug.cgi?id=33587 Link: https://github.com/ClangBuiltLinux/linux/issues/3 Link: https://github.com/ClangBuiltLinux/linux/issues/194 Link: https://github.com/ClangBuiltLinux/linux/issues/781 Link: https://lore.kernel.org/lkml/20180209161833.4605-1-dwmw2@infradead.org/ Link: https://lore.kernel.org/lkml/CAK8P3a1EBaWdbAEzirFDSgHVJMtWjuNt2HGG8z+vpXeNHwETFQ@mail.gmail.com/ Link: https://lkml.kernel.org/r/20200720204925.3654302-12-ndesaulniers@google.com Signed-off-by: Sasha Levin --- arch/x86/include/asm/uaccess.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) -- 2.25.1 diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h index d8f283b9a569c..d1323c73cf6d2 100644 --- a/arch/x86/include/asm/uaccess.h +++ b/arch/x86/include/asm/uaccess.h @@ -314,11 +314,14 @@ do { \ #define __get_user_size(x, ptr, size, retval) \ do { \ + unsigned char x_u8__; \ + \ retval = 0; \ __chk_user_ptr(ptr); \ switch (size) { \ case 1: \ - __get_user_asm(x, ptr, retval, "b", "=q"); \ + __get_user_asm(x_u8__, ptr, retval, "b", "=q"); \ + (x) = x_u8__; \ break; \ case 2: \ __get_user_asm(x, ptr, retval, "w", "=r"); \