string: Make memscan() to take const
Subsystems (7)
PATCH 1/2 string: Make memscan() to take const
Make memscan() to take const so it will be easier replace some memchr() cases with it.
--- 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(-)
arch/s390/include/asm/string.h +2 -2
@@ -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);
arch/s390/lib/string.c +1 -1
@@ -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;
arch/sparc/include/asm/asm-prototypes.h +2 -2
@@ -13,8 +13,8 @@#include <asm/oplib.h> #include <linux/atomic.h> -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
arch/sparc/include/asm/string.h +4 -3
@@ -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); \
arch/x86/include/asm/string_32.h +1 -1
@@ -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__ */
arch/x86/lib/string_32.c +1 -1
@@ -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;
include/linux/string.h +1 -1
@@ -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_MEMCMPextern int memcmp (const void *,const void *,__kernel_size_t );
lib/string.c +1 -1
@@ -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;
Signature · 1 line
2.39.1
PATCH 2/2 dns: use memscan() instead of open coded variant
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%
--- net/dns_resolver/dns_key.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-)
net/dns_resolver/dns_key.c +7 -13
@@ -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);
Signature · 1 line
2.39.1
Jakub Kicinski
maintainerQuoted 2 lines · On Thu, 16 Feb 2023 13:42:33 +0200 Andy Shevchenko wrote:
Make
memscan()to take const so it will be easier replace somememchr()cases with it.
Let's do this after the merge window.
Quoted 5 lines · Jakub Kicinski
Quoted 2 lines · On Thu, 16 Feb 2023 13:42:33 +0200 Andy Shevchenko wrote:
Make
memscan()to take const so it will be easier replace somememchr()cases with it.Let's do this after the merge window.
Sure, it needs to be revised anyway. I just noticed that one corner case might not work properly.
Signature · 2 lines
With Best Regards, Andy Shevchenko