From patchwork Tue Jul 23 21:24:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Desaulniers X-Patchwork-Id: 169558 Delivered-To: patch@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp9366626ilk; Tue, 23 Jul 2019 14:24:32 -0700 (PDT) X-Google-Smtp-Source: APXvYqxIY+Lzzku7ZnchhyvlMF5yWv+ELNILCOuW4AFPwsW2PfhyxoSNOxEHyMdZhAlfLZV6ppLX X-Received: by 2002:a17:902:2baa:: with SMTP id l39mr83351411plb.280.1563917072055; Tue, 23 Jul 2019 14:24:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563917072; cv=none; d=google.com; s=arc-20160816; b=VRhTsMsF6wizDqwIKb4HeCMgI1G1A97oxP/FLaQe4YAk846T5FXy/lgVGfB8f3bzOs 5jQdsHOerNTFbi+szyCdn8duE2Xn64lfPeW6J5sBlVwZ9aX0xsunN9hrlfgRkPSRqOoS quSA2uhIVLoGKQlvFKnaLnL+K4U3ZDF9smtFncWRC1R5Zw1U/R13K4w9CNPfkXoIU0qe ejkc6dEIM7FQ3U1bBwfkTWLBdX3im7LvojLpOlHH88lYJMLauMrIrWo4pyJvG3seIWHW W8UGtgN+fmL6bZeDzMduucMyjb3ry80Db98SLFZCYVK73B0vhdy4dzYkH4Fg9nVDbJrh 0o4A== 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=Hpr0yuvpw6CUvnD+l1IHEq2uOMPPyrhe5BbCI8J3Zt4=; b=G78jTx3SHfgEb8BLqJp4T8J/4E0AZY+t70P/7IhS0UrTrrV8ZtYGvL9kXGdA0unfK6 0XsOSpf0EdfaV5j/6pUqdXapClwMzPt1Xoo+f1o/Rsww8a2Oyq0eYPDsCx5A9TNxbCNs Qkw7VKPkVjjOpsxb/vbYTwBktOX6Z+XNCyd0WtYeMj+Z9Wc0o8h6ziaek5U/v/+oVpNU EILKqz0DjdHZ6YROt/HXAv1Rba/OPWZ+EsM2lxmHER2a3s57cZgnTAMTBK2qXn8h1SqJ UyT/0Kl4lnPRg9f0gLXblZGw8V3iwSHuMoBC+fo7KwHMKRbd9EHJrzHa9GWedVZHaln3 UMjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=VT0Ofx24; 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 k22si13961530pfi.289.2019.07.23.14.24.31; Tue, 23 Jul 2019 14:24:32 -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=VT0Ofx24; 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 S2392017AbfGWVY3 (ORCPT + 29 others); Tue, 23 Jul 2019 17:24:29 -0400 Received: from mail-pl1-f202.google.com ([209.85.214.202]:54400 "EHLO mail-pl1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726950AbfGWVY3 (ORCPT ); Tue, 23 Jul 2019 17:24:29 -0400 Received: by mail-pl1-f202.google.com with SMTP id u10so22730509plq.21 for ; Tue, 23 Jul 2019 14:24:28 -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=Hpr0yuvpw6CUvnD+l1IHEq2uOMPPyrhe5BbCI8J3Zt4=; b=VT0Ofx24XZsOi/DWgdLf2x1p5ZVFNWYNnm62NavnjPOiJD05DZXcapm7QaSZE1TDlA trA+JJpYbnyIZO0I4IHApXrgNEmDUOiHtIROcvVBasPHwY9XhEoVZddSoCAQrJANDWAI /Zh1uh5Xkf6oL/rKjoj8nX9ZY/M8JYQnwsLN6fnzrhyxsXO1O0KpK2qJKsF4MTey0Mlf 7/vQXADbjEYoT78sc9OMqSHlbcbSbqRlzVtVlAKpjKoBFd7uObCMP2o9GP6zKrFXC+Mb vY1TDxrcTgp8liTP5ylCHq5DlNxNCSBHrSRtQmTf2i3awU26ece+iICLO5iG42bTqwc2 3Bkg== 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=Hpr0yuvpw6CUvnD+l1IHEq2uOMPPyrhe5BbCI8J3Zt4=; b=GJv3JQHiIHSV8VvWwgEDWSYR4G4/RY7glL0icjF5AbDeCffzAx/gdsSFlgLKuP5J2K +17KBSkdybQHGac7utN9jsnjn0pk1a4WTYazE+Pb2m3Z5nmTF0YsFWCie3rtkD+njI01 MCOLg3vcLbCy6TEEEmQbg5vkEsPlXvXt8u1mseLkZDYrlLW3ipDaxjUC/NoP1KU1OBcv UZCLTeyPTOPT288zuTVrEhF6VUnZU3MOcIeuJdYDqv1bvBCO+kptInk11FSKi2PexvZA U2kbncoWjLfMuQkzYnPbi2sN1gm/kAkznBkc/Vx1S/qIvt/53FbMW35GlgTJHs+Z/vmY 7J6Q== X-Gm-Message-State: APjAAAUK5p924O6zQTPii4tW1SrMogPyPEogH1nxHdsYV98qtEnae/n2 fRYTwKwe7AMMkxI8aeN3YwSLRs1W3RLZZu3HcN0= X-Received: by 2002:a63:2026:: with SMTP id g38mr72977845pgg.172.1563917068137; Tue, 23 Jul 2019 14:24:28 -0700 (PDT) Date: Tue, 23 Jul 2019 14:24:10 -0700 Message-Id: <20190723212418.36379-1-ndesaulniers@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.22.0.709.g102302147b-goog Subject: [PATCH v3 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, Enrico Weigelt , Allison Randal , Uros Bizjak , 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: Vaibhav Rustagi Signed-off-by: Nick Desaulniers --- 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 | 7 +++++++ arch/x86/purgatory/Makefile | 3 +++ arch/x86/purgatory/purgatory.c | 6 ++++++ arch/x86/purgatory/string.c | 23 ----------------------- 4 files changed, 16 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..4c364cf63432 100644 --- a/arch/x86/boot/string.c +++ b/arch/x86/boot/string.c @@ -37,6 +37,13 @@ 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); -}