Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp568791rdg; Thu, 12 Oct 2023 14:06:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE+I3PPjZZsyOFfHeQnmUh5fw7XCeowZd69pPbITUbssOVzW3sSYsClwcGs1VwuooztNY/p X-Received: by 2002:a05:6a20:2445:b0:151:7d4c:899c with SMTP id t5-20020a056a20244500b001517d4c899cmr25470695pzc.25.1697144774763; Thu, 12 Oct 2023 14:06:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697144774; cv=none; d=google.com; s=arc-20160816; b=UXzhds9hDHv5fOyiKxov5defwT2gzj7cv7tFezsdFeBeN/kSCt0/VOd5cuIMWOWkkh 80yLyAFb639urPupcgmIMVKYPE9nO82LZ7zE1FMYu8ifkru2SCFh1omqffMSdPp4F0Dn PUlIu6czHueLIbecEZemGnZAvdgvssbVMu+vnvImc+yX2w+zNu1IAAjRYnSYxWAhoXXp a3E2BDrv4KTYYsk60YiSh+T9eqY9UIam1xjp8zNpe4N1lADviE7bLEx5KFtiTdHJyBcE DkpEhVtmoR1VGqhaY8jKaEw/eCN7gtTMRApX6ZR9LBq6+hX0vCXAsWlhVUqcnDf3st/d BPwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=DmYbDLi462mhz/h94tn+FuQJl/K+QQJOfQDRUT1QidM=; fh=Mc58lTbdd3u+1tiYI5h7HSCkYgR2c78h7l9oIAhqBY8=; b=TXGTAAmS70TuSKcAWyy3G6Kvl/R8e+OAXp8NWgLoqVSwV6KWo9A09G3uYB9vZyqHKG dZ8W9mep2xNVecFzqcQ7vzN5A2Pbm8tK4Wl5ksAhEHdIfFhIWo5oy6OjYxPauWg+rJfn /EXBNhjpDqrfctNAWFAskSD5smJmDQHpAEy53FohbJQizcuUOJTh/o+x4t9DvvJhnFmV OHY/TzA283aDMsr3PLlTuCTzFvk9soLZk4AsbOvpzcYRR05v6Xt5lTOWayH/XRseWYJ4 XHnOwEXlvpijWu/UzX9tyRzkyGNucWkkH2OF6zDpxiKMooxJsl5MJjsGbu/QiBXZUOH4 MXLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=OVUXOr0F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id ba7-20020a170902720700b001c5da8b630fsi2921830plb.421.2023.10.12.14.06.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Oct 2023 14:06:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=OVUXOr0F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 4EFDB81A4FE7; Thu, 12 Oct 2023 14:06:12 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442825AbjJLVF4 (ORCPT + 99 others); Thu, 12 Oct 2023 17:05:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1442824AbjJLVFz (ORCPT ); Thu, 12 Oct 2023 17:05:55 -0400 Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2B051D9 for ; Thu, 12 Oct 2023 14:05:52 -0700 (PDT) Received: by mail-ed1-x534.google.com with SMTP id 4fb4d7f45d1cf-5333fb34be3so2447799a12.1 for ; Thu, 12 Oct 2023 14:05:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697144750; x=1697749550; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=DmYbDLi462mhz/h94tn+FuQJl/K+QQJOfQDRUT1QidM=; b=OVUXOr0F2uGMn9oksBEf2FT9+qY/TU8TbF46gD/7Ofpyx0Q++TKLen2zGXl7gypCzQ KZcYHuSY3EMNtHD6p9KwfggxyP9laui5lF0WQGvXDZGY/cFhbIyUHg3uMUK++VzaSk/I J1ni/jC12dH5IBBhZFNgeN6lBK2B0LW4ei+rhkhqd7yoFBT1mmLgwtfTeoxKjf2IEmxz jeJRw2o4Q7kNiauHS68qzA0vxFgrTVK+B3X7DmrxP9HnPrOooonv9USLfyk/3xYdUMEA wNEykMTjAHFoma/Ne5XEW25z5leU3mZRP9PhLuNC5cE6z1gmwW22MrraDkGOyLnXFrPf qQYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697144750; x=1697749550; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DmYbDLi462mhz/h94tn+FuQJl/K+QQJOfQDRUT1QidM=; b=HPA79VTsa3/DiTvIxsO5GCAjkEnt0mQa4sNfEgL/P9VUuYkqhT+fOu0L04s7m6mhZ7 GP6ps4PLeAumR+tRTjAiGJ59D20j1mddo5QF+SwSwmVQQG+IEEMAerzYCvM48AnDvmAQ +MmWkivtQZmEC7Wh0X6hA74ZzD11EqtN49bkrOMn9dn4EyCtPPLFlrSmha2HbaBCJque MK5/9UKT8xeFiZew1Sd5yh9sv/Q7ampNKXcJ/t30YyJxffI5rcrGV7uRpYa/Yg4/fb10 SPtld+qF7QTAwrNwgFnHybxUnrCItPp+TByd8X8Ep1E2dN3UoOkLDhBsx0NLnNnwNcdt /qXw== X-Gm-Message-State: AOJu0Yw3BzxOdJFL1nIxyt2ApBnbsEcFvU+E0pqSHCLYy0e/V60Wbsmf jaSk/LTig8VuGkwgzTmRpeAAHvYr0W6sGOC+zV0= X-Received: by 2002:a05:6402:2314:b0:53e:1741:d76b with SMTP id l20-20020a056402231400b0053e1741d76bmr1943019eda.39.1697144750412; Thu, 12 Oct 2023 14:05:50 -0700 (PDT) MIME-Version: 1.0 References: <20231012161237.114733-1-ubizjak@gmail.com> <20231012161237.114733-2-ubizjak@gmail.com> In-Reply-To: From: Uros Bizjak Date: Thu, 12 Oct 2023 23:05:39 +0200 Message-ID: Subject: Re: [PATCH 1/4] x86/percpu: Use explicit segment registers in lib/cmpxchg{8,16}b_emu.S To: "H. Peter Anvin" Cc: x86@kernel.org, xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Peter Zijlstra Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Thu, 12 Oct 2023 14:06:12 -0700 (PDT) On Thu, Oct 12, 2023 at 11:02=E2=80=AFPM H. Peter Anvin wro= te: > > On October 12, 2023 9:10:36 AM PDT, Uros Bizjak wrote= : > >PER_CPU_VAR macro is intended to be applied to a symbol, it is not > >intended to be used as a selector between %fs and %gs segment > >registers for general operands. > > > >The address to these emulation functions is passed in a register, so > >use explicit segment registers to access percpu variable instead. > > > >Also add a missing function comment to this_cpu_cmpxchg8b_emu. > > > >No functional changes intended. > > > >Cc: Thomas Gleixner > >Cc: Ingo Molnar > >Cc: Borislav Petkov > >Cc: Dave Hansen > >Cc: "H. Peter Anvin" > >Cc: Peter Zijlstra > >Signed-off-by: Uros Bizjak > >--- > > arch/x86/lib/cmpxchg16b_emu.S | 12 ++++++------ > > arch/x86/lib/cmpxchg8b_emu.S | 30 +++++++++++++++++++++--------- > > 2 files changed, 27 insertions(+), 15 deletions(-) > > > >diff --git a/arch/x86/lib/cmpxchg16b_emu.S b/arch/x86/lib/cmpxchg16b_emu= .S > >index 6962df315793..2bd8b89bce75 100644 > >--- a/arch/x86/lib/cmpxchg16b_emu.S > >+++ b/arch/x86/lib/cmpxchg16b_emu.S > >@@ -23,14 +23,14 @@ SYM_FUNC_START(this_cpu_cmpxchg16b_emu) > > cli > > > > /* if (*ptr =3D=3D old) */ > >- cmpq PER_CPU_VAR(0(%rsi)), %rax > >+ cmpq %gs:(%rsi), %rax > > jne .Lnot_same > >- cmpq PER_CPU_VAR(8(%rsi)), %rdx > >+ cmpq %gs:8(%rsi), %rdx > > jne .Lnot_same > > > > /* *ptr =3D new */ > >- movq %rbx, PER_CPU_VAR(0(%rsi)) > >- movq %rcx, PER_CPU_VAR(8(%rsi)) > >+ movq %rbx, %gs:(%rsi) > >+ movq %rcx, %gs:8(%rsi) > > > > /* set ZF in EFLAGS to indicate success */ > > orl $X86_EFLAGS_ZF, (%rsp) > >@@ -42,8 +42,8 @@ SYM_FUNC_START(this_cpu_cmpxchg16b_emu) > > /* *ptr !=3D old */ > > > > /* old =3D *ptr */ > >- movq PER_CPU_VAR(0(%rsi)), %rax > >- movq PER_CPU_VAR(8(%rsi)), %rdx > >+ movq %gs:(%rsi), %rax > >+ movq %gs:8(%rsi), %rdx > > > > /* clear ZF in EFLAGS to indicate failure */ > > andl $(~X86_EFLAGS_ZF), (%rsp) > >diff --git a/arch/x86/lib/cmpxchg8b_emu.S b/arch/x86/lib/cmpxchg8b_emu.S > >index 49805257b125..b7d68d5e2d31 100644 > >--- a/arch/x86/lib/cmpxchg8b_emu.S > >+++ b/arch/x86/lib/cmpxchg8b_emu.S > >@@ -24,12 +24,12 @@ SYM_FUNC_START(cmpxchg8b_emu) > > pushfl > > cli > > > >- cmpl 0(%esi), %eax > >+ cmpl (%esi), %eax > > jne .Lnot_same > > cmpl 4(%esi), %edx > > jne .Lnot_same > > > >- movl %ebx, 0(%esi) > >+ movl %ebx, (%esi) > > movl %ecx, 4(%esi) > > > > orl $X86_EFLAGS_ZF, (%esp) > >@@ -38,7 +38,7 @@ SYM_FUNC_START(cmpxchg8b_emu) > > RET > > > > .Lnot_same: > >- movl 0(%esi), %eax > >+ movl (%esi), %eax > > movl 4(%esi), %edx > > > > andl $(~X86_EFLAGS_ZF), (%esp) > >@@ -53,18 +53,30 @@ EXPORT_SYMBOL(cmpxchg8b_emu) > > > > #ifndef CONFIG_UML > > > >+/* > >+ * Emulate 'cmpxchg8b %fs:(%esi)' > >+ * > >+ * Inputs: > >+ * %esi : memory location to compare > >+ * %eax : low 32 bits of old value > >+ * %edx : high 32 bits of old value > >+ * %ebx : low 32 bits of new value > >+ * %ecx : high 32 bits of new value > >+ * > >+ * Notably this is not LOCK prefixed and is not safe against NMIs > >+ */ > > SYM_FUNC_START(this_cpu_cmpxchg8b_emu) > > > > pushfl > > cli > > > >- cmpl PER_CPU_VAR(0(%esi)), %eax > >+ cmpl %fs:(%esi), %eax > > jne .Lnot_same2 > >- cmpl PER_CPU_VAR(4(%esi)), %edx > >+ cmpl %fs:4(%esi), %edx > > jne .Lnot_same2 > > > >- movl %ebx, PER_CPU_VAR(0(%esi)) > >- movl %ecx, PER_CPU_VAR(4(%esi)) > >+ movl %ebx, %fs:(%esi) > >+ movl %ecx, %fs:4(%esi) > > > > orl $X86_EFLAGS_ZF, (%esp) > > > >@@ -72,8 +84,8 @@ SYM_FUNC_START(this_cpu_cmpxchg8b_emu) > > RET > > > > .Lnot_same2: > >- movl PER_CPU_VAR(0(%esi)), %eax > >- movl PER_CPU_VAR(4(%esi)), %edx > >+ movl %fs:(%esi), %eax > >+ movl %fs:4(%esi), %edx > > > > andl $(~X86_EFLAGS_ZF), (%esp) > > > > %fs?? Yes, 32-bit targets default to %fs. Please also note a new version (v2) of the patch that reimplements this part. Uros.