Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp2541529pxb; Sun, 23 Jan 2022 07:58:43 -0800 (PST) X-Google-Smtp-Source: ABdhPJyC6GfBIDbNNfNg6A6CDm7wdWMJ0qeFSrF3e1sXmYr9hytVa2Mkp0ZiBPwcCXSH5EpYB/k3 X-Received: by 2002:a17:90b:1bc3:: with SMTP id oa3mr9444832pjb.131.1642953523091; Sun, 23 Jan 2022 07:58:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642953523; cv=none; d=google.com; s=arc-20160816; b=ttcwgPQz4ziKxwZUQ4Kku1eXhF/LuThhiKpNRcAPDB4SFp3DikE77na/wu50TrRfpM mepdq7aDmKHQL6kndhXPhsoVw2vW4Pi9zhbPRixzJN4Iaqjbw8S9m/9Ucg/ou5BNrxWM HGXSj3be3eb/ecpz2MCMr/6jfcq2ZSDsCnEZqcRLOj53KpfXD0qlgsI4uUJc0h5nTzqX RlxJEL/pDhQHtzslKRaDIuUD0VeK9FQV9rsAiINJLKaSuDQf7yze0nLlR2YLk9rcyKff Eo4h9HXdpiNfKNDC+UZcRa63INTfhjx8+jAP2/bwHkflcp2mb8HFhdkGVBmtS4WsKLE9 +wtw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=acgAkle8xoBTvtH1UMjY/cNHDObGSDA6DvWE/JyvkgM=; b=rqj3vQgplOoeA/H3Jcwh0XR/FmPYwsRJihyCRHaquHofP3MOh2xYPqVb9ptGg0hxhE XZXMon9LIPJg5jaF4RxdTh2wLpJ2TRSHqnQrZUxdMAfKYO+Ba+Zpvu9Rz3V7bvD0wqWG 5v3Y7AyOmjByUCDZVvEgAeXPJx79BXYgY6dq5SZiIwWnF3tJQL6uCts+WBKygHBNeAmS Hs9r9EkmxU+efAWSUcvBJRb9jX83n+9E83M2eViUDH9PJKtVvoL/lzTXYplpOpubLgx+ dX0UqHcBO3S5lBTu+xVmNWL8XcQ0JKzHB0+i60U9QZ762w4MIsaEfDGS5eYQFm4EMWrF uYCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.co.jp header.s=amazon201209 header.b=QTSmhUc4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.co.jp Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 62si10628058pff.325.2022.01.23.07.58.31; Sun, 23 Jan 2022 07:58:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-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=@amazon.co.jp header.s=amazon201209 header.b=QTSmhUc4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.co.jp Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231818AbiAWB61 (ORCPT + 99 others); Sat, 22 Jan 2022 20:58:27 -0500 Received: from smtp-fw-9103.amazon.com ([207.171.188.200]:32077 "EHLO smtp-fw-9103.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231614AbiAWB60 (ORCPT ); Sat, 22 Jan 2022 20:58:26 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.jp; i=@amazon.co.jp; q=dns/txt; s=amazon201209; t=1642903106; x=1674439106; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=acgAkle8xoBTvtH1UMjY/cNHDObGSDA6DvWE/JyvkgM=; b=QTSmhUc48jdi2i0i+LIZVFdgjihn45HH+U5Eyn/PCe8Z7Y5AiZXtX3+h W+B0Qr4pwnwffiPp8hSqvKI52454seVgbIyll8QQW7oqIRgyZFzR9g4lz fPIWIe3jXA4UP+LutYZ9nROKiMa/x7XtP4fTN5R4HeqFbLTW72axKJF01 0=; X-IronPort-AV: E=Sophos;i="5.88,309,1635206400"; d="scan'208";a="986676307" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-pdx-2c-9ec26c6c.us-west-2.amazon.com) ([10.25.36.210]) by smtp-border-fw-9103.sea19.amazon.com with ESMTP; 23 Jan 2022 01:58:25 +0000 Received: from EX13MTAUWB001.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan2.pdx.amazon.com [10.236.137.194]) by email-inbound-relay-pdx-2c-9ec26c6c.us-west-2.amazon.com (Postfix) with ESMTPS id 4552F419CB; Sun, 23 Jan 2022 01:58:25 +0000 (UTC) Received: from EX13D04ANC001.ant.amazon.com (10.43.157.89) by EX13MTAUWB001.ant.amazon.com (10.43.161.207) with Microsoft SMTP Server (TLS) id 15.0.1497.28; Sun, 23 Jan 2022 01:58:24 +0000 Received: from 88665a182662.ant.amazon.com (10.43.161.142) by EX13D04ANC001.ant.amazon.com (10.43.157.89) with Microsoft SMTP Server (TLS) id 15.0.1497.28; Sun, 23 Jan 2022 01:58:15 +0000 From: Kuniyuki Iwashima To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen CC: Benjamin Herrenschmidt , Kuniyuki Iwashima , Kuniyuki Iwashima , , Subject: [PATCH] x86/boot: Avoid redundant address overlap tests in memcpy(). Date: Sun, 23 Jan 2022 10:58:07 +0900 Message-ID: <20220123015807.45005-1-kuniyu@amazon.co.jp> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.43.161.142] X-ClientProxiedBy: EX13D49UWC002.ant.amazon.com (10.43.162.215) To EX13D04ANC001.ant.amazon.com (10.43.157.89) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When 'dest' and 'src' overlap, the boot time memcpy() calls memmove(), which tests the same condition again. GCC does not optimise it for now. Let's split the copy part of memmove() as ____memmove() and call it from memcpy(). Signed-off-by: Kuniyuki Iwashima --- arch/x86/boot/compressed/string.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/arch/x86/boot/compressed/string.c b/arch/x86/boot/compressed/string.c index 81fc1eaa3..4c0edb5d6 100644 --- a/arch/x86/boot/compressed/string.c +++ b/arch/x86/boot/compressed/string.c @@ -50,26 +50,31 @@ void *memset(void *s, int c, size_t n) return s; } -void *memmove(void *dest, const void *src, size_t n) +void *____memmove(void *dest, const void *src, size_t n) { unsigned char *d = dest; const unsigned char *s = src; - if (d <= s || d - s >= n) - return ____memcpy(dest, src, n); - while (n-- > 0) d[n] = s[n]; return dest; } -/* Detect and warn about potential overlaps, but handle them with memmove. */ +void *memmove(void *dest, const void *src, size_t n) +{ + if (dest <= src || dest - src >= n) + return ____memcpy(dest, src, n); + + return ____memmove(dest, src, n); +} + +/* Detect and warn about potential overlaps, but handle them with ____memmove(). */ void *memcpy(void *dest, const void *src, size_t n) { if (dest > src && dest - src < n) { warn("Avoiding potentially unsafe overlapping memcpy()!"); - return memmove(dest, src, n); + return ____memmove(dest, src, n); } return ____memcpy(dest, src, n); } -- 2.30.2