From mboxrd@z Thu Jan 1 00:00:00 1970 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0E9F0C83030 for ; Thu, 3 Jul 2025 10:40:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8475F6B0147; Thu, 3 Jul 2025 06:40:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 81F066B0148; Thu, 3 Jul 2025 06:40:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 735EA6B0149; Thu, 3 Jul 2025 06:40:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 6400B6B0147 for ; Thu, 3 Jul 2025 06:40:16 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 3438F80142 for ; Thu, 3 Jul 2025 10:40:16 +0000 (UTC) X-FDA: 83622608832.11.E2ABA43 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.10]) by imf09.hostedemail.com (Postfix) with ESMTP id 71F6F140004 for ; Thu, 3 Jul 2025 10:40:13 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=gyAklBF7; spf=none (imf09.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 198.175.65.10) smtp.mailfrom=kirill.shutemov@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1751539214; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/O6ZkMMepuBvmqT28Q+Nb4KYxlDF6t8Z1CysuzWduko=; b=0GKkns/wNEaTfT7EpAc7KPL8bubNGjQOgHXuJBqYkVazW6lfyAfRZhYgfIN3Yz084MTSjb 6nmKfJX4ugDwZx0tLWie9EozgyosGr8vXBdDUBdMr6B/QbPIzCIhH4mWujb7EcH397AWBN /kQMMnZsHbFq/H4wL2tP/YiQwXbcZvo= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=gyAklBF7; spf=none (imf09.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 198.175.65.10) smtp.mailfrom=kirill.shutemov@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1751539214; a=rsa-sha256; cv=none; b=Zi22aQl0sujvJsePKQepBd3S/AARB27ls4VPUgNhL0HEYNbD/T0zgEZ2jLnmnu/tKqg9iN mpTU/BaE9+boGJVhM/K38JB8pvZJEFcDduiJj50TsOXv7HFpWt9xWcc2ZXYxRJz6nUOTM8 6FInoNUrvIQq5W4kxsksl3PFLsB/DKo= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1751539214; x=1783075214; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=/QFzSYlDk96wJWtvoJqxVFlqu43W4FEzZE0XzT6dz6U=; b=gyAklBF7df1E7YZck4pCcUB6GH255Jof6GTJ8pXG95nWooqNZ+5NaAPr CkddsethjAzwVdbixS5zikx8wlCpYO/7Sn7WYU1nAoxDT72ZFogm1KVqo JJiRMDfspXNah31NWDaX1eqzZxZMOv7nwgm/PtHL56O32OhBxMk0H7f5x 8XR1fYavcXIFvxUG2amaTdctWVbjRUFM/4jdlMR84m5Vcbd72QvfkHqyo 9kAImQnglfGcsu3JG3z11CGzX4+zXmLVfSHKXlOvNm9dvxWjSf7e7dUzI QFUqn/Poaf2n8xnJ0tKlGl1Jy+ffhBNbbwynWl3MuNHFZREQQzi8d8EaM g==; X-CSE-ConnectionGUID: 5b7y4gNiRhG0RTr6YoZq1w== X-CSE-MsgGUID: GX8y6OGWSBm8/yzvQaTbvQ== X-IronPort-AV: E=McAfee;i="6800,10657,11482"; a="71296942" X-IronPort-AV: E=Sophos;i="6.16,284,1744095600"; d="scan'208";a="71296942" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jul 2025 03:40:11 -0700 X-CSE-ConnectionGUID: JMsPgBN7SpybGEfPT/WDlA== X-CSE-MsgGUID: SDEzVWdjTVKsq90XD7Ugbg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.16,284,1744095600"; d="scan'208";a="160030340" Received: from black.fi.intel.com ([10.237.72.28]) by orviesa005.jf.intel.com with ESMTP; 03 Jul 2025 03:39:59 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 0D7D41FE; Thu, 03 Jul 2025 13:39:58 +0300 (EEST) Date: Thu, 3 Jul 2025 13:39:57 +0300 From: "Kirill A. Shutemov" To: David Laight Cc: Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Ard Biesheuvel , "Paul E. McKenney" , Josh Poimboeuf , Xiongwei Song , Xin Li , "Mike Rapoport (IBM)" , Brijesh Singh , Michael Roth , Tony Luck , Alexey Kardashevskiy , Alexander Shishkin , Jonathan Corbet , Sohil Mehta , Ingo Molnar , Pawan Gupta , Daniel Sneddon , Kai Huang , Sandipan Das , Breno Leitao , Rick Edgecombe , Alexei Starovoitov , Hou Tao , Juergen Gross , Vegard Nossum , Kees Cook , Eric Biggers , Jason Gunthorpe , "Masami Hiramatsu (Google)" , Andrew Morton , Luis Chamberlain , Yuntao Wang , Rasmus Villemoes , Christophe Leroy , Tejun Heo , Changbin Du , Huang Shijie , Geert Uytterhoeven , Namhyung Kim , Arnaldo Carvalho de Melo , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCHv8 02/17] x86/asm: Introduce inline memcpy and memset Message-ID: References: <20250701095849.2360685-1-kirill.shutemov@linux.intel.com> <20250701095849.2360685-3-kirill.shutemov@linux.intel.com> <20250703094417.165e5893@pumpkin> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250703094417.165e5893@pumpkin> X-Stat-Signature: 9idnnjcpx9jn4dfgyfjj4ufdguowc7rz X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 71F6F140004 X-Rspam-User: X-HE-Tag: 1751539213-150952 X-HE-Meta: U2FsdGVkX1/iPIe+KvtrDsmt1YVAomwWs9cFtqvD7RmgOnqgaFbNWBb3eDwCLd8zBaw1fQeNqfk1Emigjs73/uW1dLllJfnjSqLAdWhnz93zYkWlZcEJVIHkWkaRYej0Ekgb7OsHfXQkufRg57ScYN1dh81h8E7IlgpH7WdFikRUHEi+6u/B24UDt837Toyeo+J9B2yZHyNwOrDU/E/jdYMwKfXmymhALJ0S1Ar6i92DDSWeOPXZPSu6mTrbB0VDLtgMJRwYDEO7fvF1twVMAq9XNoTSg6yH7tYCyM1EJPEnh3f168eKEBK1Wgo6ZgpiPv6h4zlk5paEkdYlq3YEtiJbSjJPqI1benVzRz1RtLQeKJk8nC4I4/4fn/cRF0hAyXQ0mqLyyh9qu/TxmENi0tShXm0v6/+uDDEmapgyXCJE4dLFJSUqkVBJwRVwWW/7V3HhEUxBDrpaUvdoWHnw8gWltlpu1lD2oAtxjYK25izKPXGMVVKvOFHdKhequgkahDa8cOnnowcxcXri2pv+1c5JWdp7F+Ker1yYHuxh2fsxSBjJS4R757w8b3ZfVwK3VhwxBwlmJM2Zz0v+5YBPpWmnI+/i8oUZd54hLVWXoJRp0iF2vW79ObRQYbKBrqloxDXx6RqGPuN5ICZXDdQo4YW4s8+a6KAsepPlgb8DxrYL3pOUZLxz0yD0rUsTQGC78kd+s9u71nMdNlBJytuKimhyNCiPort2ctpVORDrR9/1wShqQo4+plgwrKNhyU+u1PgoSS9M32MdFxJT/dVU8F5atI278vlaHgfsdbuh8RNWJQtdNT2N61MrUjFOSc+Gn6m/BhN+p+KJr1v3DTQFw8/8CQor0bxdaNOWLIUKcSl5EnJIgm3EhFrd18XzJ0NoojvB9jVPy2D+3k/h7U+wFHypL7lzMSv2UAOs1ArmfQBmOhu6qncYIUrolt3cGgE0sdn7aJQ/iDKgRCRiR6v iaLBhlOQ a7nSzhjLU9TV+DgqDxrznXSPwBVK/JpxdVOoPOu0x7UTLbZo8rUd0+A6igZHiT1o6XBoO7LTmhvBwOFE+WrcmexFngGkDNQVphb313RnhYuBNm3mKI2DTeRAidFpQcZBY1ZCSBiVu4TPdc2JSuzL9vyBP+VGjvXH+VVgPlHURtx6jPDvKORZo491Ih6QGhDzEAgXOmR20aOJcRlpQG77lmI8KXZj1O15d9wYx7GlzwrXD8y/jQ7j38AlnKGFMGs9Y1iDC4z3q7Z7kUfA1Ugh4PzR1t/3oRmdTt9mn7LuFuz6n3O9RwiIksjh2PXtUS3ragydg8Dc0kx2sLvSyft8rQT9Ij/NU20WaXz7/xK2MAKhsW7TcCDPZ+LwCU3VTzDCo8CpNYuJRpsthcJteGqu6kGSOetIpfxx8cJj/ahJLkoD0oH8= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Thu, Jul 03, 2025 at 09:44:17AM +0100, David Laight wrote: > On Tue, 1 Jul 2025 12:58:31 +0300 > "Kirill A. Shutemov" wrote: > > > Extract memcpy and memset functions from copy_user_generic() and > > __clear_user(). > > > > They can be used as inline memcpy and memset instead of the GCC builtins > > whenever necessary. LASS requires them to handle text_poke. > > Except they contain the fault handlers so aren't generic calls. That's true. I will add a comment to clarify it. > > Originally-by: Peter Zijlstra > > Link: https://lore.kernel.org/all/20241029184840.GJ14555@noisy.programming.kicks-ass.net/ > > Signed-off-by: Kirill A. Shutemov > > --- > > arch/x86/include/asm/string.h | 46 +++++++++++++++++++++++++++++++ > > arch/x86/include/asm/uaccess_64.h | 38 +++++++------------------ > > arch/x86/lib/clear_page_64.S | 13 +++++++-- > > 3 files changed, 67 insertions(+), 30 deletions(-) > > > > diff --git a/arch/x86/include/asm/string.h b/arch/x86/include/asm/string.h > > index c3c2c1914d65..17f6b5bfa8c1 100644 > > --- a/arch/x86/include/asm/string.h > > +++ b/arch/x86/include/asm/string.h > > @@ -1,6 +1,52 @@ > > /* SPDX-License-Identifier: GPL-2.0 */ > > +#ifndef _ASM_X86_STRING_H > > +#define _ASM_X86_STRING_H > > + > > +#include > > +#include > > +#include > > + > > #ifdef CONFIG_X86_32 > > # include > > #else > > # include > > #endif > > + > > +#ifdef CONFIG_X86_64 > > +#define ALT_64(orig, alt, feat) ALTERNATIVE(orig, alt, feat) > > +#else > > +#define ALT_64(orig, alt, feat) orig "\n" > > +#endif > > + > > +static __always_inline void *__inline_memcpy(void *to, const void *from, size_t len) > > +{ > > + void *ret = to; > > + > > + asm volatile("1:\n\t" > > + ALT_64("rep movsb", > > + "call rep_movs_alternative", ALT_NOT(X86_FEATURE_FSRM)) > > + "2:\n\t" > > + _ASM_EXTABLE_UA(1b, 2b) > > + : "+c" (len), "+D" (to), "+S" (from), ASM_CALL_CONSTRAINT > > + : : "memory", _ASM_AX); > > + > > + return ret + len; > > +} > > + > > +static __always_inline void *__inline_memset(void *addr, int v, size_t len) > > +{ > > + void *ret = addr; > > + > > + asm volatile("1:\n\t" > > + ALT_64("rep stosb", > > + "call rep_stos_alternative", ALT_NOT(X86_FEATURE_FSRM)) > > + "2:\n\t" > > + _ASM_EXTABLE_UA(1b, 2b) > > + : "+c" (len), "+D" (addr), ASM_CALL_CONSTRAINT > > + : "a" ((uint8_t)v) > > You shouldn't need the (uint8_t) cast (should that be (u8) anyway). > At best it doesn't matter, at worst it will add code to mask with 0xff. Right, will drop. > > + : "memory", _ASM_SI, _ASM_DX); > > + > > + return ret + len; > > +} > > + > > +#endif /* _ASM_X86_STRING_H */ > > diff --git a/arch/x86/include/asm/uaccess_64.h b/arch/x86/include/asm/uaccess_64.h > > index c8a5ae35c871..eb531e13e659 100644 > > --- a/arch/x86/include/asm/uaccess_64.h > > +++ b/arch/x86/include/asm/uaccess_64.h > > @@ -13,6 +13,7 @@ > > #include > > #include > > #include > > +#include > > > > /* > > * Virtual variable: there's no actual backing store for this, > > @@ -118,21 +119,12 @@ rep_movs_alternative(void *to, const void *from, unsigned len); > > static __always_inline __must_check unsigned long > > copy_user_generic(void *to, const void *from, unsigned long len) > > { > > + void *ret; > > + > > stac(); > > - /* > > - * If CPU has FSRM feature, use 'rep movs'. > > - * Otherwise, use rep_movs_alternative. > > - */ > > - asm volatile( > > - "1:\n\t" > > - ALTERNATIVE("rep movsb", > > - "call rep_movs_alternative", ALT_NOT(X86_FEATURE_FSRM)) > > - "2:\n" > > - _ASM_EXTABLE_UA(1b, 2b) > > - :"+c" (len), "+D" (to), "+S" (from), ASM_CALL_CONSTRAINT > > - : : "memory", "rax"); > > + ret = __inline_memcpy(to, from, len); > > clac(); > > - return len; > > + return ret - to; > > } > > > > static __always_inline __must_check unsigned long > > @@ -178,25 +170,15 @@ rep_stos_alternative(void __user *addr, unsigned long len); > > > > static __always_inline __must_check unsigned long __clear_user(void __user *addr, unsigned long size) > > { > > + void *ptr = (__force void *)addr; > > + void *ret; > > + > > might_fault(); > > stac(); > > - > > - /* > > - * No memory constraint because it doesn't change any memory gcc > > - * knows about. > > - */ > > - asm volatile( > > - "1:\n\t" > > - ALTERNATIVE("rep stosb", > > - "call rep_stos_alternative", ALT_NOT(X86_FEATURE_FSRS)) > > - "2:\n" > > - _ASM_EXTABLE_UA(1b, 2b) > > - : "+c" (size), "+D" (addr), ASM_CALL_CONSTRAINT > > - : "a" (0)); > > - > > + ret = __inline_memset(ptr, 0, size); > > clac(); > > > > - return size; > > + return ret - ptr; > > } > > > > static __always_inline unsigned long clear_user(void __user *to, unsigned long n) > > diff --git a/arch/x86/lib/clear_page_64.S b/arch/x86/lib/clear_page_64.S > > index a508e4a8c66a..47b613690f84 100644 > > --- a/arch/x86/lib/clear_page_64.S > > +++ b/arch/x86/lib/clear_page_64.S > > @@ -55,17 +55,26 @@ SYM_FUNC_END(clear_page_erms) > > EXPORT_SYMBOL_GPL(clear_page_erms) > > > > /* > > - * Default clear user-space. > > + * Default memset. > > * Input: > > * rdi destination > > + * rsi scratch > > * rcx count > > - * rax is zero > > + * al is value > > * > > * Output: > > * rcx: uncleared bytes or 0 if successful. > > + * rdx: clobbered > > */ > > SYM_FUNC_START(rep_stos_alternative) > > ANNOTATE_NOENDBR > > + > > + movzbq %al, %rsi > > + movabs $0x0101010101010101, %rax > > + > > + /* RDX:RAX = RAX * RSI */ > > + mulq %rsi > > NAK - you can't do that here. > Neither %rsi nor %rdx can be trashed. > The function has a very explicit calling convention. What calling convention? We change the only caller to confirm to this. > It is also almost certainly a waste of time. > Pretty much all the calls will be for a constant 0x00. > Rename it all memzero() ... text_poke_memset() is not limited to zeroing. -- Kiryl Shutsemau / Kirill A. Shutemov