From patchwork Sat Sep 26 04:19:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Morton X-Patchwork-Id: 11801095 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3BBD0112E for ; Sat, 26 Sep 2020 04:19:22 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id F310F2080A for ; Sat, 26 Sep 2020 04:19:21 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="glp2xNOy" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org F310F2080A Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=linux-foundation.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 2543C6B0070; Sat, 26 Sep 2020 00:19:21 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 2057E6B0071; Sat, 26 Sep 2020 00:19:21 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 11A966B0072; Sat, 26 Sep 2020 00:19:21 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0252.hostedemail.com [216.40.44.252]) by kanga.kvack.org (Postfix) with ESMTP id F16D96B0070 for ; Sat, 26 Sep 2020 00:19:20 -0400 (EDT) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id BAAE3180AD801 for ; Sat, 26 Sep 2020 04:19:20 +0000 (UTC) X-FDA: 77303908080.11.power30_070fcce2716d Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin11.hostedemail.com (Postfix) with ESMTP id 82E7D180F8B80 for ; Sat, 26 Sep 2020 04:19:20 +0000 (UTC) X-Spam-Summary: 50,0,0,f66b1f6cffed7b68,d41d8cd98f00b204,akpm@linux-foundation.org,,RULES_HIT:2:41:334:355:368:369:379:800:960:966:967:968:973:988:989:1260:1345:1359:1381:1431:1437:1535:1605:1606:1730:1747:1777:1792:2194:2196:2198:2199:2200:2201:2393:2525:2553:2568:2628:2682:2685:2859:2902:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3865:3866:3867:3868:3870:3871:3872:3873:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4117:4250:4321:4385:4605:5007:6117:6119:6261:6653:6737:7514:7576:7875:7903:8957:9025:9545:10004:11026:11473:11658:11914:12043:12048:12050:12257:12291:12295:12297:12517:12519:12555:12663:12679:12698:12737:12986:13149:13161:13221:13229:13230:13846:21080:21094:21323:21366:21451:21627:21740:21939:21990:30054:30070:30079:30089:30090:30091,0,RBL:198.145.29.99:@linux-foundation.org:.lbl8.mailshell.net-62.2.0.100 64.100.201.201;04yfcdfo8354chuyszhgxhaj4btosockkhq59rbdyo1dy8mcbb15ey8y69r5uof.yt4ak6ryj6wy7kqse8r9z3k6rj7dx7yntgbyr9bp57rwcb km357ncy X-HE-Tag: power30_070fcce2716d X-Filterd-Recvd-Size: 6575 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf15.hostedemail.com (Postfix) with ESMTP for ; Sat, 26 Sep 2020 04:19:20 +0000 (UTC) Received: from localhost.localdomain (c-71-198-47-131.hsd1.ca.comcast.net [71.198.47.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id BEF012078B; Sat, 26 Sep 2020 04:19:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1601093959; bh=gqjbGqJ1joxLFctcB/Msx4e1CUN0FUkvjKq/zP4B4zM=; h=Date:From:To:Subject:In-Reply-To:From; b=glp2xNOyJhKoRYpO+EPEp1Wz4yZampURWRfsCERI3F0fWG1GW292568BAd9RwJFcE qyvUK+UV60w93FDkCeLJe5SdrdEOh5VJBT+02DORAVykWjps9YDpBjt/+Y+dEIkEkc IZ3qxC8QmFZMNKiM1HY9QXoz6HMjpb2Sr4BTzzZ0= Date: Fri, 25 Sep 2020 21:19:18 -0700 From: Andrew Morton To: akpm@linux-foundation.org, andy.lavr@gmail.com, joe@perches.com, keescook@chromium.org, linux-mm@kvack.org, linux@rasmusvillemoes.dk, masahiroy@kernel.org, mm-commits@vger.kernel.org, natechancellor@gmail.com, ndesaulniers@google.com, nivedita@alum.mit.edu, samitolvanen@google.com, stable@vger.kernel.org, torvalds@linux-foundation.org Subject: [patch 5/9] lib/string.c: implement stpcpy Message-ID: <20200926041918.OfR3GXvLc%akpm@linux-foundation.org> In-Reply-To: <20200925211725.0fea54be9e9715486efea21f@linux-foundation.org> User-Agent: s-nail v14.8.16 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Nick Desaulniers Subject: lib/string.c: implement stpcpy LLVM implemented a recent "libcall optimization" that lowers calls to `sprintf(dest, "%s", str)` where the return value is used to `stpcpy(dest, str) - dest`. This generally avoids the machinery involved in parsing format strings. `stpcpy` is just like `strcpy` except it returns the pointer to the new tail of `dest`. This optimization was introduced into clang-12. Implement this so that we don't observe linkage failures due to missing symbol definitions for `stpcpy`. Similar to last year's fire drill with: commit 5f074f3e192f ("lib/string.c: implement a basic bcmp") The kernel is somewhere between a "freestanding" environment (no full libc) and "hosted" environment (many symbols from libc exist with the same type, function signature, and semantics). As H. Peter Anvin notes, there's not really a great way to inform the compiler that you're targeting a freestanding environment but would like to opt-in to some libcall optimizations (see pr/47280 below), rather than opt-out. Arvind notes, -fno-builtin-* behaves slightly differently between GCC and Clang, and Clang is missing many __builtin_* definitions, which I consider a bug in Clang and am working on fixing. Masahiro summarizes the subtle distinction between compilers justly: To prevent transformation from foo() into bar(), there are two ways in Clang to do that; -fno-builtin-foo, and -fno-builtin-bar. There is only one in GCC; -fno-buitin-foo. (Any difference in that behavior in Clang is likely a bug from a missing __builtin_* definition.) Masahiro also notes: We want to disable optimization from foo() to bar(), but we may still benefit from the optimization from foo() into something else. If GCC implements the same transform, we would run into a problem because it is not -fno-builtin-bar, but -fno-builtin-foo that disables that optimization. In this regard, -fno-builtin-foo would be more future-proof than -fno-built-bar, but -fno-builtin-foo is still potentially overkill. We may want to prevent calls from foo() being optimized into calls to bar(), but we still may want other optimization on calls to foo(). It seems that compilers today don't quite provide the fine grain control over which libcall optimizations pseudo-freestanding environments would prefer. Finally, Kees notes that this interface is unsafe, so we should not encourage its use. As such, I've removed the declaration from any header, but it still needs to be exported to avoid linkage errors in modules. Link: https://lkml.kernel.org/r/20200914161643.938408-1-ndesaulniers@google.com Link: https://bugs.llvm.org/show_bug.cgi?id=47162 Link: https://bugs.llvm.org/show_bug.cgi?id=47280 Link: https://github.com/ClangBuiltLinux/linux/issues/1126 Link: https://man7.org/linux/man-pages/man3/stpcpy.3.html Link: https://pubs.opengroup.org/onlinepubs/9699919799/functions/stpcpy.html Link: https://reviews.llvm.org/D85963 Reported-by: Sami Tolvanen Suggested-by: Andy Lavr Suggested-by: Arvind Sankar Suggested-by: Joe Perches Suggested-by: Kees Cook Suggested-by: Masahiro Yamada Suggested-by: Rasmus Villemoes Signed-off-by: Nick Desaulniers Tested-by: Nathan Chancellor Cc: Signed-off-by: Andrew Morton --- lib/string.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) --- a/lib/string.c~lib-stringc-implement-stpcpy +++ a/lib/string.c @@ -272,6 +272,30 @@ ssize_t strscpy_pad(char *dest, const ch } EXPORT_SYMBOL(strscpy_pad); +/** + * stpcpy - copy a string from src to dest returning a pointer to the new end + * of dest, including src's %NUL-terminator. May overrun dest. + * @dest: pointer to end of string being copied into. Must be large enough + * to receive copy. + * @src: pointer to the beginning of string being copied from. Must not overlap + * dest. + * + * stpcpy differs from strcpy in a key way: the return value is a pointer + * to the new %NUL-terminating character in @dest. (For strcpy, the return + * value is a pointer to the start of @dest). This interface is considered + * unsafe as it doesn't perform bounds checking of the inputs. As such it's + * not recommended for usage. Instead, its definition is provided in case + * the compiler lowers other libcalls to stpcpy. + */ +char *stpcpy(char *__restrict__ dest, const char *__restrict__ src); +char *stpcpy(char *__restrict__ dest, const char *__restrict__ src) +{ + while ((*dest++ = *src++) != '\0') + /* nothing */; + return --dest; +} +EXPORT_SYMBOL(stpcpy); + #ifndef __HAVE_ARCH_STRCAT /** * strcat - Append one %NUL-terminated string to another