Received: by 2002:a05:7412:40d:b0:e2:908c:2ebd with SMTP id 13csp1054894rdf; Wed, 22 Nov 2023 04:38:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IEQolb4WWmH0QVDV6jSiYxJwnlyU471FDyUg9y6S29CPK32t7MjZhnkUOrHU3366qag/zVj X-Received: by 2002:a92:b04:0:b0:35b:4731:15f3 with SMTP id b4-20020a920b04000000b0035b473115f3mr2170908ilf.10.1700656731907; Wed, 22 Nov 2023 04:38:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700656731; cv=none; d=google.com; s=arc-20160816; b=rUAJz5Rf/OsNZeZhX64OSjFGdie60lr2xxSzU8a46K6d+OcynMs6IU1XQVfyVnWrPV KE6GpOSqMaRwTHMnEpwL+dWo76q/sUXgsqeUKHsKs4I/K6I+5jxJf42xVSRkZ/V86gZr G2a3QXDHEVHQOxRlW1zPr5L32LWpEdZpKdO1h6K3KLIIM1FG43IAZbWRNTBJiDChK4KS 7HP6m/G76/EgXeoJllyZKczu2ekS+PD+egpobQISuhkyfADsem9BbQ8mBI6C+RxCOZnE QGZTva4dfdTERItRZM9bqsltYQ3g2KUB+2SOuTqV8zTvbsN8P0eX50Np5tmS6hHrZ23C GWnA== 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=AKXgT3EZzq5PttAsnZQG8TZjwKzXmLY9K8JLPmoHaas=; fh=SXiSUrsfHE+gNmfEVfF8buIocuA4aTAjp2VLMIl0ff0=; b=l03wjLluDM/li60r3t4kGMhSq9L6QkG9s50jfOgMLjXcuG65mUEvmkQpZOZoDfT+e/ SAQMupMGaye5MIBKXZeB7KCey61SX2ERdhDiivAWDX4I9vtPPPPABkMn6xusfi8kYQRJ 5N7QuVhMc9LpVeh6deSHZMZM+GaTGYkhIA2ISddcboPPJ6Ps9/6/cjBaPcjRWObmSlxf oact2joxisx+Iy2QDihZ+fLlqj3eKrRGap2aePnF/dIXqUqAAPrTZSOrutWdo7sZsrpD CqgLbxYQv0upXl4IH3g4OjkWno6h4uNQTUwp5qTs9wbgbG5PhdRblb0BAsw6oMICN0Gm a+ng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=XPQpNq5q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id y20-20020a056a00191400b006c4b01d5f85si13114482pfi.16.2023.11.22.04.38.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 04:38:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=XPQpNq5q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id 04A8C807C748; Wed, 22 Nov 2023 04:38:49 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344080AbjKVMij (ORCPT + 99 others); Wed, 22 Nov 2023 07:38:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343747AbjKVMii (ORCPT ); Wed, 22 Nov 2023 07:38:38 -0500 Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11AF2E7; Wed, 22 Nov 2023 04:38:35 -0800 (PST) Received: by mail-ed1-x52f.google.com with SMTP id 4fb4d7f45d1cf-5409bc907edso9434742a12.0; Wed, 22 Nov 2023 04:38:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700656713; x=1701261513; 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=AKXgT3EZzq5PttAsnZQG8TZjwKzXmLY9K8JLPmoHaas=; b=XPQpNq5qgFdGFcSc1/Mj4ou4J8ngPyVA3ZdZSaUHEkN90lUIVj0+WCe5nEIlP1kpte PPIzWUQp4tz4gLWYHHrlIdVgBz+5lcZejSf2vSPRUThz7ApXfyAVAsrKZ206ntfUh5Wp 4l55C0L9IRjYf8VZq51mu8bsmR3QvTMUWzRq8x81VtfnMosagBkLAjh5uu0KgBovzgRZ HGqYpwgRfHsxp/1qdcz0iNhX+nLirOdDxwdZyjwMlWfrgiKQqBjEvEeSrEF1U3cEicq9 0KgRvLrghRasWi5jOwKvNGhWhv5SIXiMXpkinXa2ZPd2Ve5Oj+wTdiQWpYvOF5q/vtZq nvzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700656713; x=1701261513; 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=AKXgT3EZzq5PttAsnZQG8TZjwKzXmLY9K8JLPmoHaas=; b=gwbqc8SHAo/sz+W2cwzA4f4TwvewlSxLKJ5e0hwZUzENX7rYYeH9qeEOYpj2oiGpbv aaS9GbKK3rIE1qV4eeFPlKmlgwXmV2+SggM0VrcY65QPUQNEHieZi/skbGMR9636RUvT vhDFgrAqZm0ykFlLue5VPZ7pLczoi5un3eCKiW0KfmKGszWyyaG7goesa/P82XMay8uk kiVu0LHjZdpI5vmM3hWdSl6EwVr7XqgKWEqOVrxYBSGyH1If6WeF39M3GghxVARV6wOC twxnWThf/gyTgFvDA36kRGyUr7raflS/xnAknp1k0hU6pV2pdxMdCZtaoL0znKuFIuX5 R2mw== X-Gm-Message-State: AOJu0YzU8A4QWHa/gzJLcHYUeVS0z8DfE0BGemtvm6z7fOSo/cLp2RiU g3FN6GOzaCMMO0YNTP0ob41rwYZkYEoWnurnVF8= X-Received: by 2002:a05:6402:14c4:b0:53e:7881:6bdf with SMTP id f4-20020a05640214c400b0053e78816bdfmr199825edx.14.1700656713251; Wed, 22 Nov 2023 04:38:33 -0800 (PST) MIME-Version: 1.0 References: <20231114170038.381634-1-ubizjak@gmail.com> In-Reply-To: From: Uros Bizjak Date: Wed, 22 Nov 2023 13:38:22 +0100 Message-ID: Subject: Re: [PATCH] x86/hyperv: Use atomic_try_cmpxchg() to micro-optimize hv_nmi_unknown() To: Wei Liu Cc: Michael Kelley , "linux-hyperv@vger.kernel.org" , "x86@kernel.org" , "linux-kernel@vger.kernel.org" , "K. Y. Srinivasan" , Haiyang Zhang , Dexuan Cui , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" 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,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.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 (agentk.vger.email [0.0.0.0]); Wed, 22 Nov 2023 04:38:49 -0800 (PST) On Wed, Nov 22, 2023 at 1:31=E2=80=AFPM Uros Bizjak wro= te: > > On Wed, Nov 22, 2023 at 4:52=E2=80=AFAM Wei Liu wrot= e: > > > > On Wed, Nov 15, 2023 at 09:58:29PM +0100, Uros Bizjak wrote: > > > On Wed, Nov 15, 2023 at 6:19=E2=80=AFPM Michael Kelley wrote: > > > > > > > > From: Uros Bizjak Sent: Tuesday, November 14, 2= 023 8:59 AM > > > > > > > > > > Use atomic_try_cmpxchg() instead of atomic_cmpxchg(*ptr, old, new= ) =3D=3D old > > > > > in hv_nmi_unknown(). On x86 the CMPXCHG instruction returns succe= ss in > > > > > the ZF flag, so this change saves a compare after CMPXCHG. The ge= nerated > > > > > asm code improves from: > > > > > > > > > > 3e: 65 8b 15 00 00 00 00 mov %gs:0x0(%rip),%edx > > > > > 45: b8 ff ff ff ff mov $0xffffffff,%eax > > > > > 4a: f0 0f b1 15 00 00 00 lock cmpxchg %edx,0x0(%rip) > > > > > 51: 00 > > > > > 52: 83 f8 ff cmp $0xffffffff,%eax > > > > > 55: 0f 95 c0 setne %al > > > > > > > > > > to: > > > > > > > > > > 3e: 65 8b 15 00 00 00 00 mov %gs:0x0(%rip),%edx > > > > > 45: b8 ff ff ff ff mov $0xffffffff,%eax > > > > > 4a: f0 0f b1 15 00 00 00 lock cmpxchg %edx,0x0(%rip) > > > > > 51: 00 > > > > > 52: 0f 95 c0 setne %al > > > > > > > > > > No functional change intended. > > > > > > > > > > Cc: "K. Y. Srinivasan" > > > > > Cc: Haiyang Zhang > > > > > Cc: Wei Liu > > > > > Cc: Dexuan Cui > > > > > Cc: Thomas Gleixner > > > > > Cc: Ingo Molnar > > > > > Cc: Borislav Petkov > > > > > Cc: Dave Hansen > > > > > Cc: "H. Peter Anvin" > > > > > Signed-off-by: Uros Bizjak > > > > > --- > > > > > arch/x86/kernel/cpu/mshyperv.c | 5 ++++- > > > > > 1 file changed, 4 insertions(+), 1 deletion(-) > > > > > > > > > > diff --git a/arch/x86/kernel/cpu/mshyperv.c > > > > > b/arch/x86/kernel/cpu/mshyperv.c index e6bba12c759c..01fa06dd06b6 > > > > > 100644 > > > > > --- a/arch/x86/kernel/cpu/mshyperv.c > > > > > +++ b/arch/x86/kernel/cpu/mshyperv.c > > > > > @@ -262,11 +262,14 @@ static uint32_t __init ms_hyperv_platform(= void) > > > > > static int hv_nmi_unknown(unsigned int val, struct pt_regs *regs)= { > > > > > static atomic_t nmi_cpu =3D ATOMIC_INIT(-1); > > > > > + unsigned int old_cpu, this_cpu; > > > > > > > > > > if (!unknown_nmi_panic) > > > > > return NMI_DONE; > > > > > > > > > > - if (atomic_cmpxchg(&nmi_cpu, -1, raw_smp_processor_id()) != =3D -1) > > > > > + old_cpu =3D -1; > > > > > + this_cpu =3D raw_smp_processor_id(); > > > > > + if (!atomic_try_cmpxchg(&nmi_cpu, &old_cpu, this_cpu)) > > > > > return NMI_HANDLED; > > > > > > > > > > return NMI_DONE; > > > > > -- > > > > > 2.41.0 > > > > > > > > The change looks correct to me. But is there any motivation other > > > > than saving 3 bytes of generated code? This is not a performance > > > > sensitive path. And the change adds 3 lines of source code. So > > > > I wonder if the change is worth the churn. > > > > > > Yes, I was trying to make the function more easy to understand and > > > similar to nmi_panic() from kernel/panic.c. I had also the idea of > > > using CPU_INVALID #define instead of -1, but IMO, the above works as > > > well. > > > > > > > In any case, > > > > > > > > Reviewed-by: Michael Kelley > > > > Applied to hyperv-fixes. > > > > Uros, just so you know, DKIM verification failed when I used b4 to appl= y > > this patch. You may want to check your email setup. > > Strange, because I didn't touch the mailer and git config for > months... and recently I have sent many patches this way without > problems. This one [1] checks OK, so it looks like some transient issue with gmail. [1] https://lore.kernel.org/lkml/20231120153419.3045-1-ubizjak@gmail.com/ Thanks, Uros.