From patchwork Wed Aug 7 22:15:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Desaulniers X-Patchwork-Id: 170770 Delivered-To: patch@linaro.org Received: by 2002:a92:512:0:0:0:0:0 with SMTP id q18csp7704851ile; Wed, 7 Aug 2019 15:16:02 -0700 (PDT) X-Google-Smtp-Source: APXvYqwH1LMD+m12b9M6N70gDxoYatCOINorVyZPgAdC7mIorvFKPZwI5ZJa1KEN7DczrCat4APG X-Received: by 2002:a62:f20b:: with SMTP id m11mr11768819pfh.125.1565216162020; Wed, 07 Aug 2019 15:16:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565216162; cv=none; d=google.com; s=arc-20160816; b=iDMz0VsYDf3LNUnTzY4Hw30gaC9oN8NeC+OPPdiGwMuWA2h3i70SXDx2a0OQpAU+7b 02GRm9IfjsCJlbvF23QtUau4Tvac57aQ7RNht4LLk86ZxEKnGDZmrrG5Jq5u5zCvIJHK 69QJFvn2F86FYQ8Zs5VXh+NB4jegz/+bgX6M+r7e4EsoMN0EfSsl4eyRc8/o+zIAznYM GvsYWBwrM7IyB42Vj/2w6Fj3sgJMkvQW/tiLipLIBnc1Lc+ewBQVxkTdlv58YXrqa4Zp oQAV9t8DdUZB8qk1e2LmTOT82ATSWi56J+4jKJi7Y//QzLSs9BNN0dH0A9l3VZg8o7Am yxRw== 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=O7ZtR6QD4RFJAye0JbMOqjbvPAFE8alBTupXCiubdkM=; b=jopjS6Ot4IFxQ0nKx17nzVigOTXqTCyaGreH6WmoAL26cqc8JP8NvoKU4RrAxOUROt UtxgdBxfJ+QBhx5WHwpaliTmISmkaWJLyfvk2RCQ8+4CUvxrzs4BIJuJc9WaOlye086B ObmDhAs7z0ni1xoDXWkTs8MdD7rYCaEU6kiwbhG7SgWPgMyBM9+fJ3/kTlihvqwGqusT dd0d6amWJU0QnxP+8S3XH3BNMDj4U8pUQghCPp4U/aXseZjRr0zaGJO8BwVlF4nx6PaH NwdwwjGN1WnzyVIWA5cHYhQcy8U4DPEUOqImObU0k/Q0bFobIMu3AZs5gwG1N3XHRlQT ogZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=L0jfbkWl; 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 a17si45964640pfn.38.2019.08.07.15.16.01; Wed, 07 Aug 2019 15:16:02 -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=L0jfbkWl; 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 S2388400AbfHGWP7 (ORCPT + 28 others); Wed, 7 Aug 2019 18:15:59 -0400 Received: from mail-vk1-f202.google.com ([209.85.221.202]:46252 "EHLO mail-vk1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729624AbfHGWP7 (ORCPT ); Wed, 7 Aug 2019 18:15:59 -0400 Received: by mail-vk1-f202.google.com with SMTP id j63so39691316vkc.13 for ; Wed, 07 Aug 2019 15:15:58 -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=O7ZtR6QD4RFJAye0JbMOqjbvPAFE8alBTupXCiubdkM=; b=L0jfbkWlYSycFtmRuN6qqndAzaKahqkihHir07QALKvEDvcBYpFwYQSrhKZF1rmog8 hqDPJkgxiKgXnd7P9tk4+s2/SaCu+ymqgypyS/OMVD+v7Qj1at0KMqLXX6bYeOpsb2pu WXGnll0nWDJfzQT1g4H12gJN5XXq44d37mydS2UejLL8/8IkdiWTMq4GSIhE8whGACEJ 37DXPbWLXeLpJLrlJFao6oGjEkYqBPDxHs+1B0kuUGgHTLKvTjNVhT8PZKqDc3gnazmG 3apGES28G+ZT8B6h6W7lT6F+RzBmcDj5JZS7NseQJJMdGsjjwWd4X6iFSEVizyQszSQo eqkA== 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=O7ZtR6QD4RFJAye0JbMOqjbvPAFE8alBTupXCiubdkM=; b=Ud0v/YDH1X59ao5YgQ8U2O/OsP3GxBRIG+3MLg0H4Lxf0vZw8XIMyzpe2ywn0WfnNO SdobWGtvj+vZ9jBV5rGDgQS0XvCWybNewfkO9ib+p9CgAdPcxqrJjYE2ZUPD56PdwA5C G004gfqDUq0W020piPqC2VQ0n9qDS4C2qcn9eAQOO3stf8jewL8BCov/iHEvyWyUetAf 4FF1HtLFmqjowS3AXMBWeozM+SK36iOhfzkxa3SyBaPD/KIzX5LAow+RRLfvy5iE5OGX 0IrCVRAkoVKuSEUqQeK48rPGMxyt2Eg6zZYowuBRdcJKetsG2KZG32+5rxljmvRzdRjm Qa1g== X-Gm-Message-State: APjAAAXWs8risHaRMEzjY/sOGSVa97OKF5N1nZs09vsQC4vRpIMuwYxt 81feLT1pT02eiAq/mfDDEO0higMtMwZhLieKrUg= X-Received: by 2002:a1f:5945:: with SMTP id n66mr4514236vkb.58.1565216157744; Wed, 07 Aug 2019 15:15:57 -0700 (PDT) Date: Wed, 7 Aug 2019 15:15:32 -0700 Message-Id: <20190807221539.94583-1-ndesaulniers@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.22.0.770.g0f2c4a37fd-goog Subject: [PATCH v5 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 , stable@vger.kernel.org, Vaibhav Rustagi , Manoj Gupta , Alistair Delva , "H. Peter Anvin" , x86@kernel.org, Allison Randal , Enrico Weigelt , Chao Fan , Greg Kroah-Hartman , Alexios Zavras 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") Cc: stable@vger.kernel.org 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 v4 -> v5: * None 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.770.g0f2c4a37fd-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); -}