Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp3121895imm; Thu, 17 May 2018 03:52:29 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoWMxcyWfh1GkkfiSseIkHAuyKT9Na+uQbg9NH5NG+miIMnQP3gZnKXQDl+eNpv+AMj6Z7U X-Received: by 2002:a63:9302:: with SMTP id b2-v6mr3766399pge.263.1526554349748; Thu, 17 May 2018 03:52:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526554349; cv=none; d=google.com; s=arc-20160816; b=y6xUnqfzW0vDrvCIlwJyB9pPyGww1d2bjZEeXThvkE0BE9664ePROMZiQOcfJm/3Bi q59VKFCSVNiOUV5F757aJ6ES/dNPPaB+VT45ecKx12WXfzENlSjVKspLbftqmZ+jZEsM tXndg2Xp8Ljy4ssb6rFKfGXsWvA+5lr1U3C9qmRIaYYpmERE9ahmUeXTvVSIBuvbTmYl yMypPqWyj41uznbKkCtniNQe8e+eT/Aae9BbnLxLMtKchu42IdEkb+zaBn6TUrovAfr4 yBXBtsIwTSU7zGltZrXnMarClxEPzmdac6ZDRLPOXnsugfWwbdSgr7QekiqdI/ciVkRo +e8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:date:cc:to:subject:from:references :in-reply-to:message-id:arc-authentication-results; bh=Uh+ThFL1g0hmTyOLpiulAbsKYpLBsDxAvU6l9PHDRHo=; b=JqUv1LX7RTGb5/t5Rrz+XZWKGVWxgf2Ue2TRomvUbu/Zx/nUh6bRiWaf27KURFWQdc 3Ot1qyAqIb+NKkOVNNjBddbjoYhE0AqeVi/8xJhgmy7Z+alyzpDtBW8hBD3J6FkQRIMj ZeGNH5W8wM/5k1mEEY7pKDoZYTPO+iZj2nyGlgxj3qdo5027ifLRErrRYdt6VJc0hrNh PQiQ48joq7Js2xkIylq6P7Lbr4dH9RlEHQTNKwjdG+YLLoVQkbtJ8UwD3KEsiix81Ckr McnkD9N4W9IIAi+vSHydjALtMwbOQtRWCPUYlnXIqw8VRmpuh7tyrQbmm1azp9s1cnUl YPSQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l36-v6si4917578plg.82.2018.05.17.03.52.15; Thu, 17 May 2018 03:52:29 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752136AbeEQKu0 (ORCPT + 99 others); Thu, 17 May 2018 06:50:26 -0400 Received: from pegase1.c-s.fr ([93.17.236.30]:27618 "EHLO pegase1.c-s.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752014AbeEQKt6 (ORCPT ); Thu, 17 May 2018 06:49:58 -0400 Received: from localhost (mailhub1-int [192.168.12.234]) by localhost (Postfix) with ESMTP id 40mp3R5zsrz9v0kQ; Thu, 17 May 2018 12:49:55 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at c-s.fr Received: from pegase1.c-s.fr ([192.168.12.234]) by localhost (pegase1.c-s.fr [192.168.12.234]) (amavisd-new, port 10024) with ESMTP id NYwoGFHKE1OB; Thu, 17 May 2018 12:49:55 +0200 (CEST) Received: from messagerie.si.c-s.fr (messagerie.si.c-s.fr [192.168.25.192]) by pegase1.c-s.fr (Postfix) with ESMTP id 40mp3R5X4mz9v0k0; Thu, 17 May 2018 12:49:55 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 32ADF8B987; Thu, 17 May 2018 12:49:57 +0200 (CEST) X-Virus-Scanned: amavisd-new at c-s.fr Received: from messagerie.si.c-s.fr ([127.0.0.1]) by localhost (messagerie.si.c-s.fr [127.0.0.1]) (amavisd-new, port 10023) with ESMTP id EajShVzcnMYr; Thu, 17 May 2018 12:49:57 +0200 (CEST) Received: from po14934vm.idsi0.si.c-s.fr (po15451.idsi0.si.c-s.fr [172.25.231.2]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 0CD0B8B97E; Thu, 17 May 2018 12:49:57 +0200 (CEST) Received: by po14934vm.idsi0.si.c-s.fr (Postfix, from userid 0) id EF0A56F937; Thu, 17 May 2018 12:49:56 +0200 (CEST) Message-Id: In-Reply-To: References: From: Christophe Leroy Subject: [PATCH v2 4/5] powerpc/lib: inline string functions NUL size verification To: Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman Cc: linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Date: Thu, 17 May 2018 12:49:56 +0200 (CEST) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Many calls to memcmp(), strncmp(), strncpy() and memchr() are done with constant size. This patch gives GCC a chance to optimise out the NUL size verification. This is only done when CONFIG_FORTIFY_SOURCE is not set, because when CONFIG_FORTIFY_SOURCE is set, other inline versions of the functions are defined in linux/string.h and conflict with ours. Signed-off-by: Christophe Leroy --- arch/powerpc/include/asm/string.h | 45 +++++++++++++++++++++++++++++++--- arch/powerpc/kernel/prom_init_check.sh | 2 +- arch/powerpc/lib/memcmp_64.S | 8 ++++++ arch/powerpc/lib/string.S | 14 +++++++++++ arch/powerpc/lib/string_32.S | 8 ++++++ 5 files changed, 73 insertions(+), 4 deletions(-) diff --git a/arch/powerpc/include/asm/string.h b/arch/powerpc/include/asm/string.h index 9b8cedf618f4..35f1aaad9b50 100644 --- a/arch/powerpc/include/asm/string.h +++ b/arch/powerpc/include/asm/string.h @@ -15,17 +15,56 @@ #define __HAVE_ARCH_MEMCPY_FLUSHCACHE extern char * strcpy(char *,const char *); -extern char * strncpy(char *,const char *, __kernel_size_t); extern __kernel_size_t strlen(const char *); extern int strcmp(const char *,const char *); -extern int strncmp(const char *, const char *, __kernel_size_t); extern char * strcat(char *, const char *); extern void * memset(void *,int,__kernel_size_t); extern void * memcpy(void *,const void *,__kernel_size_t); extern void * memmove(void *,const void *,__kernel_size_t); +extern void * memcpy_flushcache(void *,const void *,__kernel_size_t); + +#ifdef CONFIG_FORTIFY_SOURCE + +extern char * strncpy(char *,const char *, __kernel_size_t); +extern int strncmp(const char *, const char *, __kernel_size_t); extern int memcmp(const void *,const void *,__kernel_size_t); extern void * memchr(const void *,int,__kernel_size_t); -extern void * memcpy_flushcache(void *,const void *,__kernel_size_t); + +#else + +extern char *__strncpy(char *,const char *, __kernel_size_t); +extern int __strncmp(const char *, const char *, __kernel_size_t); +extern int __memcmp(const void *,const void *,__kernel_size_t); +extern void *__memchr(const void *,int,__kernel_size_t); + +static inline char *strncpy(char *p, const char *q, __kernel_size_t size) +{ + if (unlikely(!size)) + return p; + return __strncpy(p, q, size); +} + +static inline int strncmp(const char *p, const char *q, __kernel_size_t size) +{ + if (unlikely(!size)) + return 0; + return __strncmp(p, q, size); +} + +static inline int memcmp(const void *p,const void *q,__kernel_size_t size) +{ + if (unlikely(!size)) + return 0; + return __memcmp(p, q, size); +} + +static inline void *memchr(const void *p, int c, __kernel_size_t size) +{ + if (unlikely(!size)) + return NULL; + return __memchr(p, c, size); +} +#endif #ifdef CONFIG_PPC64 #define __HAVE_ARCH_MEMSET32 diff --git a/arch/powerpc/kernel/prom_init_check.sh b/arch/powerpc/kernel/prom_init_check.sh index acb6b9226352..2d87e5f9d87b 100644 --- a/arch/powerpc/kernel/prom_init_check.sh +++ b/arch/powerpc/kernel/prom_init_check.sh @@ -18,7 +18,7 @@ WHITELIST="add_reloc_offset __bss_start __bss_stop copy_and_flush _end enter_prom memcpy memset reloc_offset __secondary_hold -__secondary_hold_acknowledge __secondary_hold_spinloop __start +__secondary_hold_acknowledge __secondary_hold_spinloop __start __strncmp strcmp strcpy strlcpy strlen strncmp strstr kstrtobool logo_linux_clut224 reloc_got2 kernstart_addr memstart_addr linux_banner _stext __prom_init_toc_start __prom_init_toc_end btext_setup_display TOC." diff --git a/arch/powerpc/lib/memcmp_64.S b/arch/powerpc/lib/memcmp_64.S index d75d18b7bd55..9b28286b85cf 100644 --- a/arch/powerpc/lib/memcmp_64.S +++ b/arch/powerpc/lib/memcmp_64.S @@ -29,8 +29,14 @@ #define LD ldx #endif +#ifndef CONFIG_FORTIFY_SOURCE +#define memcmp __memcmp +#endif + _GLOBAL(memcmp) +#ifdef CONFIG_FORTIFY_SOURCE cmpdi cr1,r5,0 +#endif /* Use the short loop if both strings are not 8B aligned */ or r6,r3,r4 @@ -39,7 +45,9 @@ _GLOBAL(memcmp) /* Use the short loop if length is less than 32B */ cmpdi cr6,r5,31 +#ifdef CONFIG_FORTIFY_SOURCE beq cr1,.Lzero +#endif bne .Lshort bgt cr6,.Llong diff --git a/arch/powerpc/lib/string.S b/arch/powerpc/lib/string.S index 0ef189847337..2521c159e644 100644 --- a/arch/powerpc/lib/string.S +++ b/arch/powerpc/lib/string.S @@ -14,12 +14,20 @@ #include .text + +#ifndef CONFIG_FORTIFY_SOURCE +#define strncpy __strncpy +#define strncmp __strncmp +#define memchr __memchr +#endif /* This clears out any unused part of the destination buffer, just as the libc version does. -- paulus */ _GLOBAL(strncpy) +#ifdef CONFIG_FORTIFY_SOURCE PPC_LCMPI 0,r5,0 beqlr +#endif mtctr r5 addi r6,r3,-1 addi r4,r4,-1 @@ -40,8 +48,10 @@ _GLOBAL(strncpy) EXPORT_SYMBOL(strncpy) _GLOBAL(strncmp) +#ifdef CONFIG_FORTIFY_SOURCE PPC_LCMPI 0,r5,0 beq- 2f +#endif mtctr r5 addi r5,r3,-1 addi r4,r4,-1 @@ -55,13 +65,17 @@ _GLOBAL(strncmp) beqlr 1 bdnzt eq,1b blr +#ifdef CONFIG_FORTIFY_SOURCE 2: li r3,0 blr +#endif EXPORT_SYMBOL(strncmp) _GLOBAL(memchr) +#ifdef CONFIG_FORTIFY_SOURCE PPC_LCMPI 0,r5,0 beq- 2f +#endif mtctr r5 addi r3,r3,-1 #ifdef CONFIG_PPC64 diff --git a/arch/powerpc/lib/string_32.S b/arch/powerpc/lib/string_32.S index 5c0e77baa9c7..15f6fa175ec1 100644 --- a/arch/powerpc/lib/string_32.S +++ b/arch/powerpc/lib/string_32.S @@ -17,9 +17,15 @@ .text +#ifndef CONFIG_FORTIFY_SOURCE +#define memcmp __memcmp +#endif + _GLOBAL(memcmp) +#ifdef CONFIG_FORTIFY_SOURCE PPC_LCMPI 0,r5,0 beq- 2f +#endif srawi. r7, r5, 2 /* Divide len by 4 */ mr r6, r3 beq- 3f @@ -56,8 +62,10 @@ _GLOBAL(memcmp) lbzx r0, r4, r7 subf. r3, r0, r3 blr +#ifdef CONFIG_FORTIFY_SOURCE 2: li r3,0 blr +#endif EXPORT_SYMBOL(memcmp) CACHELINE_BYTES = L1_CACHE_BYTES -- 2.13.3