Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp951156rdb; Tue, 30 Jan 2024 03:48:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IFbWUo5NkRfINHY5JoEy1cQIsfUaPn41lAPLcwYwAhBZe6S+wMUMXIBBRaNHPhGZ9H1fkeJ X-Received: by 2002:aa7:c40e:0:b0:55f:82b:2da3 with SMTP id j14-20020aa7c40e000000b0055f082b2da3mr3056444edq.22.1706615320848; Tue, 30 Jan 2024 03:48:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706615320; cv=pass; d=google.com; s=arc-20160816; b=QVSsUbH+SUYg/DCW6qTU5joGqMFbMKzAvw5IU+j/EDYqn2MJU671bOe8K3Cxn9MdDd 232j/+R59doxiFx2zHV0GgZlDT5S9IfuK+yTMygMo47IVHIhVNT4YYyTD4nprwHfK7kj OsqYoTVB/dJn4DNc1QriyIeHUjQnCfUUcDLMrofFTSIkEJgc9WwKAIfnTNoKLMwMsUd+ Yl2C57o2SkHRSyoJpRgKVhp8p2Ofd23uSsEmmEqqhz392C1A9YGemeI3afXSg3XrRaaA YC0aSXm1teYVvTLgTRFBWEYyttLV83dZxYcTEfNKiZpasGYqKpoIRFT1Lbsp1r+yKbfp G7DQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=uuTGalwL0lMf8FVm2d/+MsxrKibTvp6PaUbmqxvEgjg=; fh=Q0jyzMYaZxglnXek0gZpEIJkRMghJnGdbaLxyeMKQdc=; b=uT6YvLqs3e1gULf6u3igy4Dg203DHLjkmIfAZgDxsH6BGvIHjF/ilbjvCyKa8J6dFm iNxQ1XXeOKxr8RN+EqtdrcjYuo3e27tgmobzaCrC7IFJwE2e9bD9a59Znhz5znnzqtbx 9SS/7Q2Ew6AkI2T2X62+nKwU+U/eAUZ7jkBWRK3Ur5VCEaw/h5tA0ikzN2jDBtQru1oJ vFlteL/pXgahqJVoxmx/+zlfLtMdvxPVWPuFtRSa3HBc/XpOmTpap7h/7PS/M4Q0qMp8 V+8Q77ShlgPU3NRkK6+SNDpK29NyrpGQIhRH9X9Y/2IH9gmqJ4GXExuzaQIQJTfYMkAo EiWw== ARC-Authentication-Results: i=2; mx.google.com; dkim=fail (test mode) header.i=@ics.forth.gr header.s=av header.b=bGzFyDI1; arc=pass (i=1 spf=pass spfdomain=ics.forth.gr dkim=pass dkdomain=ics.forth.gr dmarc=pass fromdomain=ics.forth.gr); spf=pass (google.com: domain of linux-kernel+bounces-44544-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44544-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ics.forth.gr Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id el11-20020a056402360b00b0055f4ac3af93si304956edb.606.2024.01.30.03.48.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 03:48:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-44544-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=fail (test mode) header.i=@ics.forth.gr header.s=av header.b=bGzFyDI1; arc=pass (i=1 spf=pass spfdomain=ics.forth.gr dkim=pass dkdomain=ics.forth.gr dmarc=pass fromdomain=ics.forth.gr); spf=pass (google.com: domain of linux-kernel+bounces-44544-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44544-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ics.forth.gr Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 8F1DE1F279B6 for ; Tue, 30 Jan 2024 11:48:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 75F05679F4; Tue, 30 Jan 2024 11:45:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=ics.forth.gr header.i=@ics.forth.gr header.b="bGzFyDI1" Received: from mailgate.ics.forth.gr (mailgate.ics.forth.gr [139.91.1.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 55E1866B5B for ; Tue, 30 Jan 2024 11:45:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=139.91.1.2 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706615109; cv=none; b=F3bdp7yYlXHYV60rGYHcEw0DcMgra4PalzrH/PT2/K648isvlNXWNg3UuMSaxxtri+8FpDqf7BLd3fyy6wIrOOLFamHtx+Gs07pdtwXuHg1KGDKO5tgZ+scKTzw1vnuiBFdFtps/fOxy78BG7DrkcLP6TDxX8N2eG6KR6gAz04w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706615109; c=relaxed/simple; bh=5S10mTZhZbQFsJIaNUVqS3Tw0Ln3OPHQISVKPpP1DH0=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=EYGnqMlZNDrLwh1pZtdxPhg5+f+/UvfEdHMFGzyMwrhPvuWlEl4J1KVr2rszC71jVgSIdKeBS4cBTQL6z041NfQeA1N685cYxIFTfWqWMf21XMbdCAz2/Z8PNP+DEswZEaNpFFVqsDsziXBFP9i73bYsr4vvi9UdqjYipQyU0pg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ics.forth.gr; spf=pass smtp.mailfrom=ics.forth.gr; dkim=pass (2048-bit key) header.d=ics.forth.gr header.i=@ics.forth.gr header.b=bGzFyDI1; arc=none smtp.client-ip=139.91.1.2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ics.forth.gr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ics.forth.gr Received: from av3.ics.forth.gr (av3in.ics.forth.gr [139.91.1.77]) by mailgate.ics.forth.gr (8.15.2/ICS-FORTH/V10-1.8-GATE) with ESMTP id 40UBdFrK003642 for ; Tue, 30 Jan 2024 13:39:20 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; d=ics.forth.gr; s=av; c=relaxed/simple; q=dns/txt; i=@ics.forth.gr; t=1706614760; x=1709206760; h=From:Sender:Reply-To:Subject:Date:Message-ID:To:Cc:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=5S10mTZhZbQFsJIaNUVqS3Tw0Ln3OPHQISVKPpP1DH0=; b=bGzFyDI1hgZjW/BNzyYMSkN6f7dy/0ICzqVsXoXa39feXN+oKyTNU65lm9rUh9E3 J8hcVp4VH9GZbUAonayFCSEG1wan8nT5WnsnYPPSqaWd5z342lTBpY5TQ/FAo0/o GH8olK3wGaB0VZ9wJrGWUuae46+ZAvgGbH7vxLaQ5OxAkhGUj9iwJ+PD1cbTxoU9 qct0kmC3fSfgkya6UU8BopS+oXr78gFwBQgxkwGZ5mZeZCpXcXU0qkCtPQNT1L9i hE8qaOFtNeF/2zAkE/Dz1awjXvtvoGe0qeT+mNZkFx6ztsDex69GcbnuB+RvR2m/ h9no2V6FOB21fo599aeV2A==; X-AuditID: 8b5b014d-a23ec70000002178-80-65b8dfe81b6b Received: from enigma.ics.forth.gr (webmail.ics.forth.gr [139.91.151.35]) by av3.ics.forth.gr (Symantec Messaging Gateway) with SMTP id 71.91.08568.8EFD8B56; Tue, 30 Jan 2024 13:39:20 +0200 (EET) X-ICS-AUTH-INFO: Authenticated user: mick at ics.forth.gr Message-ID: Date: Tue, 30 Jan 2024 13:39:10 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 2/3] riscv: optimized memmove To: Jisheng Zhang , Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Matteo Croce , kernel test robot References: <20240128111013.2450-1-jszhang@kernel.org> <20240128111013.2450-3-jszhang@kernel.org> Content-Language: el-GR From: Nick Kossifidis In-Reply-To: <20240128111013.2450-3-jszhang@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrCLMWRmVeSWpSXmKPExsXSHT1dWffF/R2pBru3KVps/T2L3aL52Ho2 i8u75rBZbPvcwmbxqvkRm8XfX/9ZLV5e7mG2aJvF78Dh8eblSxaPwx1f2D0W73nJ5LFpVSeb x+Yl9R6tO/6ye1xqvs7u8XmTXABHFJdNSmpOZllqkb5dAlfGrO6ygjPCFb/fZTQwLufvYuTg kBAwkXi726eLkYtDSOAoo8TR3g72LkZOoLilxOZPL1lAbF4Be4mWg9PZQGwWAVWJOWtPMUPE BSVOznwCViMqIC9x/9YMdpCZwgKGErtvV4LMFBHYyiix9dVbdhCHWWASo8Tkr59YQYqEBJIl Hn+3BellFhCXOHL+N9hMNgFNifmXDoLN5BQwl+ia8YAVosZMomtrFyOELS+x/e0c5gmMArOQ nDELyahZSFpmIWlZwMiyilEgscxYLzO5WC8tv6gkQy+9aBMjOCIYfXcw3t78Vu8QIxMH4yFG CQ5mJRHen5pbU4V4UxIrq1KL8uOLSnNSiw8xSnOwKInznrBdkCwkkJ5YkpqdmlqQWgSTZeLg lGpgiv3TdzOY44tz/7NOfz9mpx7OT7wXiy0Df+YkaJ5TX3k0dNFhzUU9tT+bfrF1bJzuvmGL xZuFjIKdxe9XblAWrTqwdNqWubLGgkJ7N6XzTKl32hKlLKJ1qObp5bctbkEP3zOnXti6+f5K 7ttLGtuetSUtUzzBEbbH6fZE3buHo+sffbRql9+TzVjSWZZ+e+HSj21rr7R6eWW6mP0OTFNy PpH14c/NdXHF99h+z46TaD+05JB1Pau4yPmbTndS2Qr+5fdd/8XF0NB+PJ13ym8JE5e6ojUP Vx73e3Bw4n6nb0ncvS/yXobPdrbqu+yhfM5znfrDbyH2l49cUlh5UP/JteOeVxmF1D78rDqi mP4zSYmlOCPRUIu5qDgRAOLlaxz3AgAA On 1/28/24 13:10, Jisheng Zhang wrote: > From: Matteo Croce > > When the destination buffer is before the source one, or when the > buffers doesn't overlap, it's safe to use memcpy() instead, which is > optimized to use a bigger data size possible. > > Signed-off-by: Matteo Croce > Reported-by: kernel test robot > Signed-off-by: Jisheng Zhang I'd expect to have memmove handle both fw/bw copying and then memcpy being an alias to memmove, to also take care when regions overlap and avoid undefined behavior. > --- a/arch/riscv/lib/string.c > +++ b/arch/riscv/lib/string.c > @@ -119,3 +119,28 @@ void *memcpy(void *dest, const void *src, size_t count) __weak __alias(__memcpy) > EXPORT_SYMBOL(memcpy); > void *__pi_memcpy(void *dest, const void *src, size_t count) __alias(__memcpy); > void *__pi___memcpy(void *dest, const void *src, size_t count) __alias(__memcpy); > + > +/* > + * Simply check if the buffer overlaps an call memcpy() in case, > + * otherwise do a simple one byte at time backward copy. > + */ > +void *__memmove(void *dest, const void *src, size_t count) > +{ > + if (dest < src || src + count <= dest) > + return __memcpy(dest, src, count); > + > + if (dest > src) { > + const char *s = src + count; > + char *tmp = dest + count; > + > + while (count--) > + *--tmp = *--s; > + } > + return dest; > +} > +EXPORT_SYMBOL(__memmove); > + Here is an approach for the backwards case to get things started... static void copy_bw(void *dst_ptr, const void *src_ptr, size_t len) { union const_data src = { .as_bytes = src_ptr + len }; union data dst = { .as_bytes = dst_ptr + len }; size_t remaining = len; size_t src_offt = 0; if (len < 2 * WORD_SIZE) goto trailing_bw; for(; dst.as_uptr & WORD_MASK; remaining--) *--dst.as_bytes = *--src.as_bytes; src_offt = src.as_uptr & WORD_MASK; if (!src_offt) { for (; remaining >= WORD_SIZE; remaining -= WORD_SIZE) *--dst.as_ulong = *--src.as_ulong; } else { unsigned long cur, prev; src.as_bytes -= src_offt; for (; remaining >= WORD_SIZE; remaining -= WORD_SIZE) { cur = *src.as_ulong; prev = *--src.as_ulong; *--dst.as_ulong = cur << ((WORD_SIZE - src_offt) * 8) | prev >> (src_offt * 8); } src.as_bytes += src_offt; } trailing_bw: while (remaining-- > 0) *--dst.as_bytes = *--src.as_bytes; } Regards, Nick