From patchwork Thu Jul 25 20:06:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Desaulniers X-Patchwork-Id: 169749 Delivered-To: patch@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp12070461ilk; Thu, 25 Jul 2019 13:06:47 -0700 (PDT) X-Google-Smtp-Source: APXvYqwsVLz/5ffPT5KA8nbIsfFiErlUzpVBTZoD1OjquMnc9mUx8AqOw94i7W3Dd94pLQRauS6H X-Received: by 2002:a63:9e54:: with SMTP id r20mr53449809pgo.64.1564085207558; Thu, 25 Jul 2019 13:06:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564085207; cv=none; d=google.com; s=arc-20160816; b=I8PpDJlnEjG15f0v63fVyfBkoaqjXoDKO84jM82ZZRTEOypMVsUuY8UaLr+yMVUOiU 0jbYxBy6iRxv2QnTNN0VRIwtX2u2/UvXqO1GQGKUq1raV5c0bjx4TD7E1M6Uv5ZemBll C617kyO7TQECTSpiLHK9U+Rs1co6SsObCPbXhmwT3GCOEDLyZhZx0JTbJuDWfEUjT+3H myKMTuCO+kpLD3g7z+tkcfoRKW3pyBc7BtcVrziAUg9y3T54T1L/a7CWw95goQdfrqqH k23udunP2n3XmtO1liUOSaJ0LFVDrCJ/udKw/9SKnIbV7R1oCyP1bhV5wjsFWH5IRxDX CcYQ== 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=bLZyVx2d9+4mutQVBosXKh2ZSsxE7Nf0cA/U+GJuG+A=; b=jllgKToHiF+953wvJY0D0IKlD0aG8WgAY0T3cr+62BR1IxOJqORQIAtIK4ZUFPT5R2 Dmtcgns3Rvc1vzPCXXWRmbRKxHBaHaAvs0SLWMuwZoEQknl0FdhoLzr3iR8WwNr5tOBx JKHZTAdYJBE1pX8kr6idizKwbJfaggdo18kY+tULwRdllzUVl5MIxhwmUqq/8tGjI77b zUvLtaODNOpFI45f5Gv5phCeJ8x5NzwVYd+vvEue7afMZF6fsM/j69X43oKd2KiW6D/I HwziecRwpHhlmvrG4RtDNzUo9NNFuHKrg58rh84+BQAnybZO0pjFFQLp57cV9dvYnfd5 2Gwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=m+T3VBZI; 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 a59si18184192plc.319.2019.07.25.13.06.47; Thu, 25 Jul 2019 13:06:47 -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=m+T3VBZI; 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 S1726516AbfGYUGp (ORCPT + 29 others); Thu, 25 Jul 2019 16:06:45 -0400 Received: from mail-pf1-f202.google.com ([209.85.210.202]:42063 "EHLO mail-pf1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726166AbfGYUGp (ORCPT ); Thu, 25 Jul 2019 16:06:45 -0400 Received: by mail-pf1-f202.google.com with SMTP id 21so31593411pfu.9 for ; Thu, 25 Jul 2019 13:06:44 -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=bLZyVx2d9+4mutQVBosXKh2ZSsxE7Nf0cA/U+GJuG+A=; b=m+T3VBZIJljmJqRqMRQDcbzAAQaa/9uOr8Q/ElYMWK1MgCq5Qv2UQckai6yVrCbHv2 zJxRaIWjigCrCQ6bVKnnhM3sZuQmugPabgGX/mdzGe4TaQsRVUb6STzU3xWyJISmQ+1m 5MA3V19qnAuxHoayJsCsP8wjTc9GYvpeL4psfoJSqkCaqHKnsS0OTxyb/NfJFitxA7j1 vH+tl/UfuEdxiydFhJ8d5VsEvRrMrHKdNGqZIwy7E4QS39cujJ3+GXOu/Xl2mkmnlLQE ZMmqNehZbAuLZYZ2DentulKe+kfFpiGueOUoRWEO+IiHSlc7AyQwFiDOnjIK68b5Fk3z WOKg== 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=bLZyVx2d9+4mutQVBosXKh2ZSsxE7Nf0cA/U+GJuG+A=; b=ozgQknvhF0Js2LnqX1HI24I9NiIVogFBO49pm4yQiE4lO/N29490iNIUltK8Fcrm8J /Y8cZT3ecjAGioMFEdCOi6ffq4hg+GPvTLnkFw9U8S/+f2GEHUF5+mgQAyXSVck3LoEK 476dYfMlIFldH1tETwIwEwO6HdfeLdqrRxBAvyK9qVwn2y0uuXjhJo1aPR0ZpYSa9xB6 o2P1FmtXxSaCTyupgpDjkP0xXmHIXHftcbIiHVzdC2fxJBncG+pfzoiNSSRU9w7Nh7jh fId4wVX5RHy4v2iDusb4rCtcbk4L2cRNrlV8FTFQ5rkK0RfrsAjFgm0m3+K6sbZrMW1r zAtQ== X-Gm-Message-State: APjAAAUOCF+FXDgc0F4rmAlzgDa4Zz/PmpzGJ+5/HUWtSVibMO8ZKs45 0qfISZ7dqfJrGuduUTWhXhZ8eZUODCXqm/yQKCY= X-Received: by 2002:a63:6901:: with SMTP id e1mr57594206pgc.390.1564085203941; Thu, 25 Jul 2019 13:06:43 -0700 (PDT) Date: Thu, 25 Jul 2019 13:06:17 -0700 Message-Id: <20190725200625.174838-1-ndesaulniers@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.22.0.709.g102302147b-goog Subject: [PATCH v4 1/2] x86/purgatory: do not use __builtin_memcpy and __builtin_memset From: Nick Desaulniers To: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de Cc: peterz@infradead.org, clang-built-linux@googlegroups.com, linux-kernel@vger.kernel.org, yamada.masahiro@socionext.com, Nick Desaulniers , Vaibhav Rustagi , Manoj Gupta , Alistair Delva , "H. Peter Anvin" , x86@kernel.org, Enrico Weigelt , Chao Fan , Uros Bizjak , Alexios Zavras , Greg Kroah-Hartman , Allison Randal Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Implementing memcpy and memset in terms of __builtin_memcpy and __builtin_memset is problematic. GCC at -O2 will replace calls to the builtins with calls to memcpy and memset (but will generate an inline implementation at -Os). Clang will replace the builtins with these calls regardless of optimization level. $ llvm-objdump -dr arch/x86/purgatory/string.o | tail 0000000000000339 memcpy: 339: 48 b8 00 00 00 00 00 00 00 00 movabsq $0, %rax 000000000000033b: R_X86_64_64 memcpy 343: ff e0 jmpq *%rax 0000000000000345 memset: 345: 48 b8 00 00 00 00 00 00 00 00 movabsq $0, %rax 0000000000000347: R_X86_64_64 memset 34f: ff e0 Such code results in infinite recursion at runtime. This is observed when doing kexec. Instead, reuse an implementation from arch/x86/boot/compressed/string.c if we define warn as a symbol. Also, Clang may lower memcmp's that compare against 0 to bcmp's, so add a small definition, too. See also: commit 5f074f3e192f ("lib/string.c: implement a basic bcmp") Fixes: 8fc5b4d4121c ("purgatory: core purgatory functionality") Link: https://bugs.chromium.org/p/chromium/issues/detail?id=984056 Reported-by: Vaibhav Rustagi Debugged-by: Vaibhav Rustagi Debugged-by: Manoj Gupta Suggested-by: Alistair Delva Signed-off-by: Nick Desaulniers Tested-by: Vaibhav Rustagi --- Changes v3 -> v4: * (style) open brace on newline * drop Vaibhav's SOB tag that was accidentally copy+pasta'd from v1. * Carry Vaibhav's tested by tag from v3 since v4 is strictly stylistic change from v3. * Drop cc'ing stable. Sasha's bot reports v1 doesn't cherry pick cleanly 5.1, so this series will require manual backports. Changes v2 -> v3: * Add bcmp implementation. * Drop tested-by tag (Vaibhav will help retest). * Cc stable Changes v1 -> v2: * Add Fixes tag. * Move this patch to first in the series. arch/x86/boot/string.c | 8 ++++++++ arch/x86/purgatory/Makefile | 3 +++ arch/x86/purgatory/purgatory.c | 6 ++++++ arch/x86/purgatory/string.c | 23 ----------------------- 4 files changed, 17 insertions(+), 23 deletions(-) delete mode 100644 arch/x86/purgatory/string.c -- 2.22.0.709.g102302147b-goog diff --git a/arch/x86/boot/string.c b/arch/x86/boot/string.c index 401e30ca0a75..8272a4492844 100644 --- a/arch/x86/boot/string.c +++ b/arch/x86/boot/string.c @@ -37,6 +37,14 @@ int memcmp(const void *s1, const void *s2, size_t len) return diff; } +/* + * Clang may lower `memcmp == 0` to `bcmp == 0`. + */ +int bcmp(const void *s1, const void *s2, size_t len) +{ + return memcmp(s1, s2, len); +} + int strcmp(const char *str1, const char *str2) { const unsigned char *s1 = (const unsigned char *)str1; diff --git a/arch/x86/purgatory/Makefile b/arch/x86/purgatory/Makefile index 3cf302b26332..91ef244026d2 100644 --- a/arch/x86/purgatory/Makefile +++ b/arch/x86/purgatory/Makefile @@ -6,6 +6,9 @@ purgatory-y := purgatory.o stack.o setup-x86_$(BITS).o sha256.o entry64.o string targets += $(purgatory-y) PURGATORY_OBJS = $(addprefix $(obj)/,$(purgatory-y)) +$(obj)/string.o: $(srctree)/arch/x86/boot/compressed/string.c FORCE + $(call if_changed_rule,cc_o_c) + $(obj)/sha256.o: $(srctree)/lib/sha256.c FORCE $(call if_changed_rule,cc_o_c) diff --git a/arch/x86/purgatory/purgatory.c b/arch/x86/purgatory/purgatory.c index 6d8d5a34c377..b607bda786f6 100644 --- a/arch/x86/purgatory/purgatory.c +++ b/arch/x86/purgatory/purgatory.c @@ -68,3 +68,9 @@ void purgatory(void) } copy_backup_region(); } + +/* + * Defined in order to reuse memcpy() and memset() from + * arch/x86/boot/compressed/string.c + */ +void warn(const char *msg) {} diff --git a/arch/x86/purgatory/string.c b/arch/x86/purgatory/string.c deleted file mode 100644 index 01ad43873ad9..000000000000 --- a/arch/x86/purgatory/string.c +++ /dev/null @@ -1,23 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-only -/* - * Simple string functions. - * - * Copyright (C) 2014 Red Hat Inc. - * - * Author: - * Vivek Goyal - */ - -#include - -#include "../boot/string.c" - -void *memcpy(void *dst, const void *src, size_t len) -{ - return __builtin_memcpy(dst, src, len); -} - -void *memset(void *dst, int c, size_t len) -{ - return __builtin_memset(dst, c, len); -}