Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1640534pxy; Mon, 2 Aug 2021 06:53:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy+oV1xUnSjr3zqNa4/2cYSExmj3PCDxPDvfmOeV15XGN5giXc+Nh3QDg96vuuld47penV9 X-Received: by 2002:a5e:dc0c:: with SMTP id b12mr691959iok.141.1627912392517; Mon, 02 Aug 2021 06:53:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627912392; cv=none; d=google.com; s=arc-20160816; b=gdcjHMAVjyvmlx+Xy5zuB0NslsfeivMhbYQ1OrquGvpwtAncPJqr8e2pz5qGc6kFK+ ZLRr+2VfcXRKWsGSqs35fOnSlHXqPROShdxUWWVpG9YvLuA0syRwT3+dGiijyY1WcrUU FhXXB8JdGrrY7lc3HEe974UNPVDCbNsYKahetOn+iOpkMDBAEv2P5HWls7JvcltgCjA/ svPE3zbRIM6c7/5THlai25coo+sHBqhxXO7SL2szxw4wmHKNJqPrxmigjEUNBPqYgshO oqKkG8gcyqzp4MX5srKTblZ6z6WMY6/Ho9I2chXydtHvMrJn7rzapwEluKo/NZPksJdL ATHw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=8Fpw+/nmonaAMearuDj+sr25L26XEZfEjMPWv0dJy8Y=; b=Zg4C7toPbjbsBh3ma2WLjOWYS6z1iSa2TR31uAPmCIr22xb3Ew59u3XcVrNZJvjhxV Pu9rzGz6a/WTj239DVhosaA60NsbryCPW8AUNYXa1XHVOumoj/LcDIT+LIqTGajGE0Dq VSD2Bv2qBGtHpaug8t9TNV/EvuPT5933HwQGgDyF1r4qEQzV9maUjURXHqigU00OEjfY Ilkz0c5Vhm2FRCSWiqP63SSIdME8142udPbDZVNbl1ue9w+LbNeVlRyfhkiZrIG0792X fkjLEjN61opXlPT2H+/228vfAY91WhVxpfQ5q4FbY/CTVqr/98M6r/kiZs4rF+e7IHz1 Ecjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=MJ0Fc593; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f11si13174929ils.120.2021.08.02.06.53.00; Mon, 02 Aug 2021 06:53:12 -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=@linuxfoundation.org header.s=korg header.b=MJ0Fc593; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234410AbhHBNv2 (ORCPT + 99 others); Mon, 2 Aug 2021 09:51:28 -0400 Received: from mail.kernel.org ([198.145.29.99]:58326 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234359AbhHBNsI (ORCPT ); Mon, 2 Aug 2021 09:48:08 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 5585B60527; Mon, 2 Aug 2021 13:47:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1627912077; bh=IdsctHln3L3h4CE1F5Ksk5EmzdsNzY+kCctty/2yTnk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MJ0Fc593cI38Ie516lYgDUP2s3Z7KOejzH5hgzV/SY33gSaDnMKkisKr/bvBP6gmU enPayxBsWh/Mjd6nFMHT3gq71JAStq7WM0De2GCP/oApeUyeF558ftWZqpT9NbXtnC /tx2qfbn6HOxICIMkdZawEkeXo8spkplOqCqhnsw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Matthew Wilcox , "H. Peter Anvin" , "James E.J. Bottomley" , "Martin K. Petersen" , David Miller , Ingo Molnar , Ivan Kokshaysky , Matt Turner , Michael Ellerman , Minchan Kim , Ralf Baechle , Richard Henderson , Russell King , Sam Ravnborg , Sergey Senozhatsky , Thomas Gleixner , Andrew Morton , Linus Torvalds , "Nobuhiro Iwamatsu (CIP)" Subject: [PATCH 4.9 13/32] lib/string.c: add multibyte memset functions Date: Mon, 2 Aug 2021 15:44:33 +0200 Message-Id: <20210802134333.346882923@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210802134332.931915241@linuxfoundation.org> References: <20210802134332.931915241@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Matthew Wilcox commit 3b3c4babd898715926d24ae10aa64778ace33aae upstream. Patch series "Multibyte memset variations", v4. A relatively common idiom we're missing is a function to fill an area of memory with a pattern which is larger than a single byte. I first noticed this with a zram patch which wanted to fill a page with an 'unsigned long' value. There turn out to be quite a few places in the kernel which can benefit from using an optimised function rather than a loop; sometimes text size, sometimes speed, and sometimes both. The optimised PowerPC version (not included here) improves performance by about 30% on POWER8 on just the raw memset_l(). Most of the extra lines of code come from the three testcases I added. This patch (of 8): memset16(), memset32() and memset64() are like memset(), but allow the caller to fill the destination with a value larger than a single byte. memset_l() and memset_p() allow the caller to use unsigned long and pointer values respectively. Link: http://lkml.kernel.org/r/20170720184539.31609-2-willy@infradead.org Signed-off-by: Matthew Wilcox Cc: "H. Peter Anvin" Cc: "James E.J. Bottomley" Cc: "Martin K. Petersen" Cc: David Miller Cc: Ingo Molnar Cc: Ivan Kokshaysky Cc: Matt Turner Cc: Michael Ellerman Cc: Minchan Kim Cc: Ralf Baechle Cc: Richard Henderson Cc: Russell King Cc: Sam Ravnborg Cc: Sergey Senozhatsky Cc: Thomas Gleixner Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Nobuhiro Iwamatsu (CIP) Signed-off-by: Greg Kroah-Hartman --- include/linux/string.h | 30 ++++++++++++++++++++++ lib/string.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) --- a/include/linux/string.h +++ b/include/linux/string.h @@ -103,6 +103,36 @@ extern __kernel_size_t strcspn(const cha #ifndef __HAVE_ARCH_MEMSET extern void * memset(void *,int,__kernel_size_t); #endif + +#ifndef __HAVE_ARCH_MEMSET16 +extern void *memset16(uint16_t *, uint16_t, __kernel_size_t); +#endif + +#ifndef __HAVE_ARCH_MEMSET32 +extern void *memset32(uint32_t *, uint32_t, __kernel_size_t); +#endif + +#ifndef __HAVE_ARCH_MEMSET64 +extern void *memset64(uint64_t *, uint64_t, __kernel_size_t); +#endif + +static inline void *memset_l(unsigned long *p, unsigned long v, + __kernel_size_t n) +{ + if (BITS_PER_LONG == 32) + return memset32((uint32_t *)p, v, n); + else + return memset64((uint64_t *)p, v, n); +} + +static inline void *memset_p(void **p, void *v, __kernel_size_t n) +{ + if (BITS_PER_LONG == 32) + return memset32((uint32_t *)p, (uintptr_t)v, n); + else + return memset64((uint64_t *)p, (uintptr_t)v, n); +} + #ifndef __HAVE_ARCH_MEMCPY extern void * memcpy(void *,const void *,__kernel_size_t); #endif --- a/lib/string.c +++ b/lib/string.c @@ -754,6 +754,72 @@ void memzero_explicit(void *s, size_t co } EXPORT_SYMBOL(memzero_explicit); +#ifndef __HAVE_ARCH_MEMSET16 +/** + * memset16() - Fill a memory area with a uint16_t + * @s: Pointer to the start of the area. + * @v: The value to fill the area with + * @count: The number of values to store + * + * Differs from memset() in that it fills with a uint16_t instead + * of a byte. Remember that @count is the number of uint16_ts to + * store, not the number of bytes. + */ +void *memset16(uint16_t *s, uint16_t v, size_t count) +{ + uint16_t *xs = s; + + while (count--) + *xs++ = v; + return s; +} +EXPORT_SYMBOL(memset16); +#endif + +#ifndef __HAVE_ARCH_MEMSET32 +/** + * memset32() - Fill a memory area with a uint32_t + * @s: Pointer to the start of the area. + * @v: The value to fill the area with + * @count: The number of values to store + * + * Differs from memset() in that it fills with a uint32_t instead + * of a byte. Remember that @count is the number of uint32_ts to + * store, not the number of bytes. + */ +void *memset32(uint32_t *s, uint32_t v, size_t count) +{ + uint32_t *xs = s; + + while (count--) + *xs++ = v; + return s; +} +EXPORT_SYMBOL(memset32); +#endif + +#ifndef __HAVE_ARCH_MEMSET64 +/** + * memset64() - Fill a memory area with a uint64_t + * @s: Pointer to the start of the area. + * @v: The value to fill the area with + * @count: The number of values to store + * + * Differs from memset() in that it fills with a uint64_t instead + * of a byte. Remember that @count is the number of uint64_ts to + * store, not the number of bytes. + */ +void *memset64(uint64_t *s, uint64_t v, size_t count) +{ + uint64_t *xs = s; + + while (count--) + *xs++ = v; + return s; +} +EXPORT_SYMBOL(memset64); +#endif + #ifndef __HAVE_ARCH_MEMCPY /** * memcpy - Copy one area of memory to another