Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp3802509pxb; Mon, 1 Nov 2021 21:32:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxoVe2QzCTGYQB0zbHloGm4DlTlVr1xJw0Kh53jdzruWxdWVfoqb37HK33NBLwAVE8Aw0Dp X-Received: by 2002:a17:906:e85:: with SMTP id p5mr42436607ejf.159.1635827563433; Mon, 01 Nov 2021 21:32:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635827563; cv=none; d=google.com; s=arc-20160816; b=TsDMezphEYWG4F3nu7Bi8cTE2w+/cKVhE/EWb7scF9yQA0W/FQgsqURMVL0VfF28qK 6NlRHzJCfwKoebxTwY23MypbCAcR13YzfPXCHBYJB8WR0eAzZktkx1LK4TwriREbacbO An9U/XN+3jF0GxP15JQuOHRSRhZSHV7oix8ayqVyXAnpIrW+gnXqfwJmKIFutXR1SLJg XMljIVQkKZnmKHW8uBx3LoGzJ/lZlxO+B31d8fGBOyA0Q6bmYcwqPLIlfKewm9ZkekaX 7kFP15MRvMmYvKJ+9AVqJ7+L5MGl2TfCurhmJRYuxAjIj/qNP+xX0YluRu/soRiMPZXV pZlQ== 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:sender:dkim-signature; bh=Q5B29hQzjMrMs8nN7SPdiKqKjScpJ832DxNf26q1Wco=; b=Pxw7veo9SRDFThR1LpAPUmK8lFqrpmGnckiXfvYIRPzuATxOxaU0s/YNIxVB5M8FSp P4CaLx4yD+mwlFbuEmayfUy2THTDZOnpUYlA3Cu46pFZcB5ao8NPRK/7pyBnVpyX4rFA +PtynORhDVX2WI31/yoQrKN8krHtODar+uBLBsWsWqie0Uz1yqDAJVLa+5F50DkCAGTY qAEqgz0rvUyki6pVaHKeH5UQOXTnpj10R3r63AsXWHErUpxz6sJQdL4pMqg6NIyuzsN7 ojfwJ31kxqYyuX7/7qoLNXS0iH6RrWoLJf9KWQOVRDPE35Ruh634zQ6QOLEHNR3vNrDc zYig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=HZZuWHI1; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l5si751425ejn.84.2021.11.01.21.32.19; Mon, 01 Nov 2021 21:32:43 -0700 (PDT) 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=@gmail.com header.s=20210112 header.b=HZZuWHI1; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229510AbhKBEdH (ORCPT + 99 others); Tue, 2 Nov 2021 00:33:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229486AbhKBEdH (ORCPT ); Tue, 2 Nov 2021 00:33:07 -0400 Received: from mail-oo1-xc2e.google.com (mail-oo1-xc2e.google.com [IPv6:2607:f8b0:4864:20::c2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2D17C061714 for ; Mon, 1 Nov 2021 21:30:32 -0700 (PDT) Received: by mail-oo1-xc2e.google.com with SMTP id o26-20020a4abe9a000000b002b74bffdef0so7006772oop.12 for ; Mon, 01 Nov 2021 21:30:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Q5B29hQzjMrMs8nN7SPdiKqKjScpJ832DxNf26q1Wco=; b=HZZuWHI13UQD9Wza9e1Atq7YO6rbIIhybqJXhIkRy3MsBtllHyeDwy5EY+phplxeMp 772qvxf7YKA595lX9+M4xEn4sNop+31qd8fJiSpSi5ZK8EWixVwJG2BfAKZ2lJUSVd/J M6+FrD04PMCviklJUwgaYw7eLzkc65eCGY2kp4C6r2T5wjAxSo5453t7dFCLiWCJ4e4R DlP4jKwYWM7eREzwUgTsTcgChOOWm2y/DYYWrMfRv0pH8SdTEnKkEwSI/85FmIQUubgq uljBfy5vDfO/FHU7g9JVXziB2AoFXCp7svEXLjCV5y7IuXdOKo3jDsFAZEgCG/JcsWYG HACA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :mime-version:content-transfer-encoding; bh=Q5B29hQzjMrMs8nN7SPdiKqKjScpJ832DxNf26q1Wco=; b=jME2z49nzNg5q1JzNZhKn5/PmkLLGp0v+9rGW7Vka7Pl878ZS4nWTo/YEUXGFlqDe9 htqB7JfJk6UwKCu4Y8SGZCvuberW0DMdH//g4cCC/z989tPLa3KKNQG7LX6TTDVlaxjf 6mlDw7YH7YKHOFdC7XSZSneuj2iKkzvQudY1eRQ6Y/a2hnJW7pgOio+l5NQYPSPycdlb obQ/n7GZqkjYRCnwLI4Mu9YiiTtRLr+pXcR6bEK+95DX5lk+72e1OeF6MFtT0PmziiPU CiBfmmm9s4kZXKsD9QSECHc34/eztQkgbli0LJpXA3s2FvRu73BcaO3TrFmGbQQXkoLg /6Lg== X-Gm-Message-State: AOAM532E7H4ucapXKpQwDeHLBLi8Y/NUpRigKF1TCUAkqoMa5++/wNok D8t74Ugo5NB5htb5eqg1WlOYEOTGOYs= X-Received: by 2002:a05:6820:504:: with SMTP id m4mr16020617ooj.62.1635827431676; Mon, 01 Nov 2021 21:30:31 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id x8sm4561273ota.75.2021.11.01.21.30.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Nov 2021 21:30:27 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: Andy Shevchenko Cc: linux-kernel@vger.kernel.org, Andrew Morton , Guenter Roeck , Linus Torvalds , Geert Uytterhoeven Subject: [PATCH] string: uninline memcpy_and_pad Date: Mon, 1 Nov 2021 21:30:24 -0700 Message-Id: <20211102043024.449478-1-linux@roeck-us.net> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When building m68k:allmodconfig, recent versions of gcc generate the following error if the length of UTS_RELEASE is less than 8 bytes. In function 'memcpy_and_pad', inlined from 'nvmet_execute_disc_identify' at drivers/nvme/target/discovery.c:268:2: arch/m68k/include/asm/string.h:72:25: error: '__builtin_memcpy' reading 8 bytes from a region of size 7 Discussions around the problem suggest that this only happens if an architecture does not provide strlen(), if -ffreestanding is provided as compiler option, and if CONFIG_FORTIFY_SOURCE=n. All of this is the case for m68k. The exact reasons are unknown, but seem to be related to the ability of the compiler to evaluate the return value of strlen() and the resulting execution flow in memcpy_and_pad(). It would be possible to work around the problem by using sizeof(UTS_RELEASE) instead of strlen(UTS_RELEASE), but that would only postpone the problem until the function is called in a similar way. Uninline memcpy_and_pad() instead to solve the problem for good. Suggested-by: Linus Torvalds Cc: Geert Uytterhoeven Signed-off-by: Guenter Roeck --- include/linux/string.h | 19 ++----------------- lib/string.c | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/include/linux/string.h b/include/linux/string.h index 5e96d656be7a..d68097b4f600 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -262,23 +262,8 @@ void __write_overflow(void) __compiletime_error("detected write beyond size of o #include #endif -/** - * memcpy_and_pad - Copy one buffer to another with padding - * @dest: Where to copy to - * @dest_len: The destination buffer size - * @src: Where to copy from - * @count: The number of bytes to copy - * @pad: Character to use for padding if space is left in destination. - */ -static inline void memcpy_and_pad(void *dest, size_t dest_len, - const void *src, size_t count, int pad) -{ - if (dest_len > count) { - memcpy(dest, src, count); - memset(dest + count, pad, dest_len - count); - } else - memcpy(dest, src, dest_len); -} +void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count, + int pad); /** * str_has_prefix - Test if a string has a given prefix diff --git a/lib/string.c b/lib/string.c index b2de45a581f4..ff13d6d77a05 100644 --- a/lib/string.c +++ b/lib/string.c @@ -1165,3 +1165,22 @@ void fortify_panic(const char *name) BUG(); } EXPORT_SYMBOL(fortify_panic); + +/** + * memcpy_and_pad - Copy one buffer to another with padding + * @dest: Where to copy to + * @dest_len: The destination buffer size + * @src: Where to copy from + * @count: The number of bytes to copy + * @pad: Character to use for padding if space is left in destination. + */ +void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count, + int pad) +{ + if (dest_len > count) { + memcpy(dest, src, count); + memset(dest + count, pad, dest_len - count); + } else + memcpy(dest, src, dest_len); +} +EXPORT_SYMBOL(memcpy_and_pad); -- 2.33.0