From patchwork Thu Feb 16 11:42:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 13142974 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 41037C636CC for ; Thu, 16 Feb 2023 11:42:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229984AbjBPLmH (ORCPT ); Thu, 16 Feb 2023 06:42:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229558AbjBPLmG (ORCPT ); Thu, 16 Feb 2023 06:42:06 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A8AF772BF; Thu, 16 Feb 2023 03:42:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676547725; x=1708083725; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=DZtMdMHG1rZUSnjA3M8Y0P2RY5raVZxMdIWzLiuWreo=; b=ghMQ64ki6Qsk2e+IPb+eDDR6i6L53vute/EnDq+/6AE6lin+yMSYsedx IBFLCsM3/qF29uId/xQfkv46Co3dZYOK5tH5L6KzkHlmrqDn97OIkn/D4 KaRKCQVSXIa40Y3qbgC6iMRyj21jWfkXGqMfFk3pmUjJXTfvLkcMrnMWq xCjroer8Cz3EBZSCN/IMiEaxX/oxRqPpfQNQgNGc9mK07apGDdTb7uxu8 ot0vIM6CCWnQzew8Wghf4Au3eTbY3JkHBwquiWN5VjV1PU0iHlDrex4XU G8vvm/dbUJh1l0SvwgveuL/ho0rqehB8pe/UYuD1bI5bIx3p7Vtcws7AT w==; X-IronPort-AV: E=McAfee;i="6500,9779,10622"; a="359124735" X-IronPort-AV: E=Sophos;i="5.97,302,1669104000"; d="scan'208";a="359124735" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Feb 2023 03:42:05 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10622"; a="999003906" X-IronPort-AV: E=Sophos;i="5.97,302,1669104000"; d="scan'208";a="999003906" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga005.fm.intel.com with ESMTP; 16 Feb 2023 03:42:00 -0800 Received: by black.fi.intel.com (Postfix, from userid 1003) id 780BE1A6; Thu, 16 Feb 2023 13:42:40 +0200 (EET) From: Andy Shevchenko To: Andy Shevchenko , Kees Cook , linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org, sparclinux@vger.kernel.org, netdev@vger.kernel.org Cc: Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , "David S. Miller" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andy Shevchenko , Eric Dumazet , Jakub Kicinski , Paolo Abeni Subject: [PATCH net-next v1 1/2] string: Make memscan() to take const Date: Thu, 16 Feb 2023 13:42:33 +0200 Message-Id: <20230216114234.36343-1-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org Make memscan() to take const so it will be easier replace some memchr() cases with it. Signed-off-by: Andy Shevchenko --- arch/s390/include/asm/string.h | 4 ++-- arch/s390/lib/string.c | 2 +- arch/sparc/include/asm/asm-prototypes.h | 4 ++-- arch/sparc/include/asm/string.h | 7 ++++--- arch/x86/include/asm/string_32.h | 2 +- arch/x86/lib/string_32.c | 2 +- include/linux/string.h | 2 +- lib/string.c | 2 +- 8 files changed, 13 insertions(+), 12 deletions(-) diff --git a/arch/s390/include/asm/string.h b/arch/s390/include/asm/string.h index 3fae93ddb322..0196fd466a39 100644 --- a/arch/s390/include/asm/string.h +++ b/arch/s390/include/asm/string.h @@ -120,7 +120,7 @@ static inline void *memchr(const void * s, int c, size_t n) #endif #ifdef __HAVE_ARCH_MEMSCAN -static inline void *memscan(void *s, int c, size_t n) +static inline void *memscan(const void *s, int c, size_t n) { const void *ret = s + n; @@ -205,7 +205,7 @@ static inline size_t strnlen(const char * s, size_t n) #endif #else /* IN_ARCH_STRING_C */ void *memchr(const void * s, int c, size_t n); -void *memscan(void *s, int c, size_t n); +void *memscan(const void *s, int c, size_t n); char *strcat(char *dst, const char *src); char *strcpy(char *dst, const char *src); size_t strlen(const char *s); diff --git a/arch/s390/lib/string.c b/arch/s390/lib/string.c index 7d8741818239..ec9786fde1dd 100644 --- a/arch/s390/lib/string.c +++ b/arch/s390/lib/string.c @@ -331,7 +331,7 @@ EXPORT_SYMBOL(memcmp); * the area if @c is not found */ #ifdef __HAVE_ARCH_MEMSCAN -void *memscan(void *s, int c, size_t n) +void *memscan(const void *s, int c, size_t n) { const void *ret = s + n; diff --git a/arch/sparc/include/asm/asm-prototypes.h b/arch/sparc/include/asm/asm-prototypes.h index 4987c735ff56..3a82a86a27a6 100644 --- a/arch/sparc/include/asm/asm-prototypes.h +++ b/arch/sparc/include/asm/asm-prototypes.h @@ -13,8 +13,8 @@ #include #include -void *__memscan_zero(void *, size_t); -void *__memscan_generic(void *, int, size_t); +void *__memscan_zero(const void *, size_t); +void *__memscan_generic(const void *, int, size_t); void *__bzero(void *, size_t); void VISenter(void); /* Dummy prototype to supress warning */ #undef memcpy diff --git a/arch/sparc/include/asm/string.h b/arch/sparc/include/asm/string.h index 001a17baf2d5..7761a037b377 100644 --- a/arch/sparc/include/asm/string.h +++ b/arch/sparc/include/asm/string.h @@ -21,10 +21,11 @@ void *memmove(void *, const void *, __kernel_size_t); #define memscan(__arg0, __char, __arg2) \ ({ \ - void *__memscan_zero(void *, size_t); \ - void *__memscan_generic(void *, int, size_t); \ - void *__retval, *__addr = (__arg0); \ + void *__memscan_zero(const void *, size_t); \ + void *__memscan_generic(const void *, int, size_t); \ + const void *__addr = (__arg0); \ size_t __size = (__arg2); \ + void *__retval; \ \ if(__builtin_constant_p(__char) && !(__char)) \ __retval = __memscan_zero(__addr, __size); \ diff --git a/arch/x86/include/asm/string_32.h b/arch/x86/include/asm/string_32.h index 32c0d981a82a..30245f7707e7 100644 --- a/arch/x86/include/asm/string_32.h +++ b/arch/x86/include/asm/string_32.h @@ -223,7 +223,7 @@ static inline void *memset32(uint32_t *s, uint32_t v, size_t n) * find the first occurrence of byte 'c', or 1 past the area if none */ #define __HAVE_ARCH_MEMSCAN -extern void *memscan(void *addr, int c, size_t size); +extern void *memscan(const void *addr, int c, size_t size); #endif /* __KERNEL__ */ diff --git a/arch/x86/lib/string_32.c b/arch/x86/lib/string_32.c index 53b3f202267c..4124d6678f72 100644 --- a/arch/x86/lib/string_32.c +++ b/arch/x86/lib/string_32.c @@ -198,7 +198,7 @@ EXPORT_SYMBOL(memchr); #endif #ifdef __HAVE_ARCH_MEMSCAN -void *memscan(void *addr, int c, size_t size) +void *memscan(const void *addr, int c, size_t size) { if (!size) return addr; diff --git a/include/linux/string.h b/include/linux/string.h index c062c581a98b..a7bff7ed3cb0 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -150,7 +150,7 @@ extern void * memcpy(void *,const void *,__kernel_size_t); extern void * memmove(void *,const void *,__kernel_size_t); #endif #ifndef __HAVE_ARCH_MEMSCAN -extern void * memscan(void *,int,__kernel_size_t); +extern void * memscan(const void *,int,__kernel_size_t); #endif #ifndef __HAVE_ARCH_MEMCMP extern int memcmp(const void *,const void *,__kernel_size_t); diff --git a/lib/string.c b/lib/string.c index 3d55ef890106..30a63048d4cc 100644 --- a/lib/string.c +++ b/lib/string.c @@ -725,7 +725,7 @@ EXPORT_SYMBOL(bcmp); * returns the address of the first occurrence of @c, or 1 byte past * the area if @c is not found */ -void *memscan(void *addr, int c, size_t size) +void *memscan(const void *addr, int c, size_t size) { unsigned char *p = addr; From patchwork Thu Feb 16 11:42:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 13142975 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8FD3BC61DA4 for ; Thu, 16 Feb 2023 11:42:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230227AbjBPLmK (ORCPT ); Thu, 16 Feb 2023 06:42:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59354 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230063AbjBPLmI (ORCPT ); Thu, 16 Feb 2023 06:42:08 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CA3701F488; Thu, 16 Feb 2023 03:42:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676547726; x=1708083726; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=FfCAalj2Nee1CcJLc+LGlUXcvI9FY2hBGzZJL2n+MEo=; b=mw3oNVc5RgBteAH7PI08t5I/AveePF5vQim6KoKMLITrr0hkCjsgV5sz bktGZBxTUDAjbZVBdScT/19Q8AZgtRQx2lbx+2avgvsoGXyLOcOisg5eV sNlvUikl2CGIyWKlhGCxu3oZicZFHaPxIkzrSUH/kIA2vaF7FWNUje1Ya /IaA+9ieNAvkyM8M0fzxniXvDsfzgWJ/RgvSRk7Mlt59eh5/j5P+obJBF eAxNAMemOZqr+Kg+6xjcz195vVP/oC8WShX7to8doECbydSAMRvtFw4sG 4FLSUQ/NWewh+cf78qKFhYFgsqjhHIGyMku5t1XbyI53ocXXjqh3napdt g==; X-IronPort-AV: E=McAfee;i="6500,9779,10622"; a="359124747" X-IronPort-AV: E=Sophos;i="5.97,302,1669104000"; d="scan'208";a="359124747" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Feb 2023 03:42:05 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10622"; a="999003914" X-IronPort-AV: E=Sophos;i="5.97,302,1669104000"; d="scan'208";a="999003914" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga005.fm.intel.com with ESMTP; 16 Feb 2023 03:42:00 -0800 Received: by black.fi.intel.com (Postfix, from userid 1003) id 199DC1C5; Thu, 16 Feb 2023 13:42:41 +0200 (EET) From: Andy Shevchenko To: Andy Shevchenko , Kees Cook , linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org, sparclinux@vger.kernel.org, netdev@vger.kernel.org Cc: Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , "David S. Miller" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andy Shevchenko , Eric Dumazet , Jakub Kicinski , Paolo Abeni Subject: [PATCH net-next v1 2/2] dns: use memscan() instead of open coded variant Date: Thu, 16 Feb 2023 13:42:34 +0200 Message-Id: <20230216114234.36343-2-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230216114234.36343-1-andriy.shevchenko@linux.intel.com> References: <20230216114234.36343-1-andriy.shevchenko@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org memscan() is a standard API to provide an equivalent to memchr(foo, $CHAR, end - foo) ?: end so use it. Memory footprint (x86_64): Function old new delta dns_resolver_preparse 1429 1393 -36 Total: Before=3229, After=3193, chg -1.11% Signed-off-by: Andy Shevchenko --- net/dns_resolver/dns_key.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/net/dns_resolver/dns_key.c b/net/dns_resolver/dns_key.c index 01e54b46ae0b..835be6e2dd83 100644 --- a/net/dns_resolver/dns_key.c +++ b/net/dns_resolver/dns_key.c @@ -134,8 +134,8 @@ dns_resolver_preparse(struct key_preparsed_payload *prep) /* deal with any options embedded in the data */ end = data + datalen; - opt = memchr(data, '#', datalen); - if (!opt) { + opt = memscan(data, '#', datalen); + if (opt == end) { /* no options: the entire data is the result */ kdebug("no options"); result_len = datalen; @@ -150,7 +150,7 @@ dns_resolver_preparse(struct key_preparsed_payload *prep) const char *eq; char optval[128]; - next_opt = memchr(opt, '#', end - opt) ?: end; + next_opt = memscan(opt, '#', end - opt); opt_len = next_opt - opt; if (opt_len <= 0 || opt_len > sizeof(optval)) { pr_warn_ratelimited("Invalid option length (%d) for dns_resolver key\n", @@ -158,16 +158,10 @@ dns_resolver_preparse(struct key_preparsed_payload *prep) return -EINVAL; } - eq = memchr(opt, '=', opt_len); - if (eq) { - opt_nlen = eq - opt; - eq++; - memcpy(optval, eq, next_opt - eq); - optval[next_opt - eq] = '\0'; - } else { - opt_nlen = opt_len; - optval[0] = '\0'; - } + eq = memscan(opt, '=', opt_len); + opt_nlen = eq - opt; + memcpy(optval, eq, next_opt - eq); + optval[next_opt - eq] = '\0'; kdebug("option '%*.*s' val '%s'", opt_nlen, opt_nlen, opt, optval);