From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.zytor.com (terminus.zytor.com [198.137.202.136]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3CF463CF201 for ; Fri, 24 Jul 2026 23:58:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.136 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784937510; cv=none; b=uL4UaoSAIMspvdGf3bLQECvdAvjJFZRQ/CuccicAHQwU8hmjIaM0W4XO3XSox/ZreI2juP64d50ZOam3NnY8X052SXV4QMLWsMlMrUrGN6GtBv9NoXi9TXvkghkKYsnaex8O/r8+8P0WBj0nLy+BSC0E3TTcgpqt2UmyP/WRe98= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784937510; c=relaxed/simple; bh=wzdxwK7esvDXmFoH0RnwKvrLxGQiyylbQXK3d/Ov1W4=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=AIVThgwpWMwd2w+I7n1s/UgHDZIojMa1Hk8HvFX2kZShjW1ff+SmBhTpIM74fE1HjpS0/qiJa1DRy+NSVraoHSQ2Mri7vKLFJI06b8BdQ7Idqdy0JlrPU6jY6tEUXbqrUqq6j2ocgE8dJ0Y0cBYNOEpunU5vUiKusQnnk/H/Mz4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zytor.com; spf=pass smtp.mailfrom=zytor.com; dkim=pass (2048-bit key) header.d=zytor.com header.i=@zytor.com header.b=c0Q5iqr9; arc=none smtp.client-ip=198.137.202.136 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zytor.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zytor.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=zytor.com header.i=@zytor.com header.b="c0Q5iqr9" Received: from [172.27.2.41] (c-76-133-66-138.hsd1.ca.comcast.net [76.133.66.138]) (authenticated bits=0) by mail.zytor.com (8.18.1/8.17.1) with ESMTPSA id 66ONvodj2120226 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NO); Fri, 24 Jul 2026 16:57:51 -0700 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.zytor.com 66ONvodj2120226 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zytor.com; s=2026062701; t=1784937472; bh=W1+RFu9YGD1n2obMJ7+LCs5yJk3LovJgDqEgPVEr4is=; h=Date:Subject:From:To:Cc:References:In-Reply-To:From; b=c0Q5iqr9Ip5NuYLC4z2dvmAcThmWm+yxSrbnM+TxbD3nYxbZ+2f5g9YlNJ4TPCBnF 0Kk1Y7FhOnYG9UClrN0c/WH5lKuX7QQXx7c6nWlUtf1db1xMfXG95qeBPqei09Xra8 f6wqqxwTk1NAhO740TleI0VWFLT8PtiA4FrkRyKUo8Yja1mVTr7yA8Xz7QMnCRh3cr WEisX+fhru3qwUwV+TklGDSEc2RUdlhclFwCw3c5gL3cDh7QBC3hBmsEECL/3abThc Ft1kLzX9GZJPu8LeRTQvIo+IPaDey9WFyqo/HlsIFXP9A/IPTIwjbXRn68lt8op1Ph ZPfbHGdVqxzdQ== Message-ID: Date: Fri, 24 Jul 2026 16:57:45 -0700 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 v7 2/5] x86/asm: add volatile, clobbers and zero-length check in inline memcmp From: "H. Peter Anvin" To: Mauricio Faria de Oliveira , Borislav Petkov Cc: Jan Beulich , Thomas Gleixner , Ingo Molnar , Dave Hansen , x86@kernel.org, Juergen Gross , Alexey Dobriyan , Boris Ostrovsky , kernel-dev@igalia.com, linux-kernel@vger.kernel.org, xen-devel@lists.xenproject.org References: <20260721-pvh-kasan-inline-v7-0-38979a50cef0@igalia.com> <20260721-pvh-kasan-inline-v7-2-38979a50cef0@igalia.com> <20260722170334.GCamD35gwrCng30WH1@fat_crate.local> <0F3A1121-208F-4F71-8C88-BEAE7CB50E75@zytor.com> <4daed8ac-b533-436f-9f86-6d297b87abbb@suse.com> <5e19b195-0ca2-4510-81cb-497b40e4aaf5@zytor.com> <8df0340f0dc7f6c303c6a7da78fad7b8@igalia.com> <7148BD5B-2E06-4137-B361-18AAF329DDC5@zytor.com> <348133f5-8a78-4805-bf0a-b1b08cdc65f2@zytor.com> Content-Language: en-US, sv-SE In-Reply-To: <348133f5-8a78-4805-bf0a-b1b08cdc65f2@zytor.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 2026-07-24 16:35, H. Peter Anvin wrote: > On 2026-07-24 14:53, H. Peter Anvin wrote: >> >> It's pretty bloaty; I don't know exactly how much the rest of the code bloats. >> >> I would be fine with having mem[n]eq() though as an explicit API. For the out-of-line version used for compiler-generated memcmp() calls it doesn't matter. >> >> Here is a very compact way the out-of-line version can be written (showing the 64-bit version; formatting apologies, on a phone): >> >> xor %eax,%eax /* Sets ZF */ >> repe cmpsb >> jr z,1f >> movzbl -1(%rdi),%edi >> mov -1(%rsi),%al >> sub %edi,%eax >> 1: ret >> > With a C wrapper to work in any mode: > > int memcmp(const void *a, const void *b, size_t size) > { > int diff; > > asm volatile("xor %0,%0 ;" /* Sets ZF for the size = 0 case */ > "repe cmpsb ;" > "jz 1f ;" /* When size = 0 loading is unsafe */ > "movb -1(%2),%b0 ;" > "movzbl -1(%1),%k1 ;" > "sub %k1,%0 ;" > "1:" > : "=&q" (diff), "+D" (a), "+S" (b), "+c" (size) > : : "memory"); > return diff; > } > And of course I got the comparison backwards (negative means a < b, thus we want a - b not b - a). Here is a fixed version. int memcmp(const void *a, const void *b, size_t size) { int diff; asm volatile("xor %0,%0 ;" /* Sets ZF for the size = 0 case */ "repe cmpsb ;" "jz 1f ;" /* When size = 0 loading is unsafe */ "movb -1(%1),%b0 ;" "movzbl -1(%2),%k2 ;" "sub %k2,%0 ;" "1:" : "=&q" (diff), "+D" (a), "+S" (b), "+c" (size) : : "memory"); return diff; } -hpa