Received: by 2002:a05:7412:b101:b0:e2:908c:2ebd with SMTP id az1csp2866515rdb; Wed, 15 Nov 2023 12:59:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IGd3VF8/m9X9x8h/MNxBEFIvcEnfocyGLCfpBG6/36eKT1YpIytI5MgXo2aJ5MXojWGL0gR X-Received: by 2002:a05:6870:818c:b0:1ea:21cc:67f3 with SMTP id k12-20020a056870818c00b001ea21cc67f3mr16827971oae.4.1700081985853; Wed, 15 Nov 2023 12:59:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700081985; cv=none; d=google.com; s=arc-20160816; b=yMLbBZjcIoDWriFsI7PekBTibjJ85c2Zya8wYj2PnoZmhkhvuDrcAzLt0QJDcgvAn4 iYj+19jQeVDQc9vd4fzSVgm3Ja2GTlHg5Nm+jmPu6e3C+bn10CVXMPpT9CvVYq0JZKM5 b8FTMK4h0xu20NVMxP+8cxl2smRlJsBX0Itp0KOa//C/VdbmzpTMxe7ZwMveH2XonQRO y5ewkJ6k3lPWb5d32DTJDgaFgXDymVQdinKvV96i7kdAIZRklgKDqC0ic4Zwswjduyep mH+JO/TDmZ9XgL2FT4ANt7K6VwJuIgkQ1O6UP9YCgyO36qVuka/dmlw8iL2iZpCCItll cBzA== 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=/r4jyRcgvxW7jDaR+TEUfRnfk5XkJd3nDCtIC2+1lJY=; fh=GUUR1XbY6PwxXZvabP9QvnaC9E67yka0grXtYLxvBaY=; b=MR9SgzmrwCxNIYJ/ekQrVhdiUwyXbl3/O1hWcr/VBgNGIYy6C15fjVAyOPXMmorytD AuNGWbCc07VvjRAo4volMKYTJ5Gt1edlTj81Y0NwuDvLq7cnHZMY+ZK3poMCBWMSAE28 Y8EFhlBBasLlJGlTFYuLS9bE4wKYqt7cQHQ6v5HiIaq6d5E4FfUm0vg/Da+h9gSioT3Q 2Zj8Wkej2YzJCBdH1hl2OSCShgS/WXvfmL9AoFZyT7s1813gaIw4/7vS6zsug4pQGROq 2m3p3Ihp5ovlPk7Bh6QZPvtUhrTKHjchOjNrscRjRf5z/WAv5T2aYPWV6iaLh4z1pTFB 3hUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=lnTIHydB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id c1-20020a655a81000000b005c1b322b3c1si4656016pgt.681.2023.11.15.12.59.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Nov 2023 12:59:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=lnTIHydB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id 2D62D80DF274; Wed, 15 Nov 2023 12:59:43 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344910AbjKOU7j (ORCPT + 99 others); Wed, 15 Nov 2023 15:59:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344916AbjKOU7U (ORCPT ); Wed, 15 Nov 2023 15:59:20 -0500 Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25A9C2695; Wed, 15 Nov 2023 12:58:43 -0800 (PST) Received: by mail-ed1-x52b.google.com with SMTP id 4fb4d7f45d1cf-53de8fc1ad8so190980a12.0; Wed, 15 Nov 2023 12:58:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700081921; x=1700686721; 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=/r4jyRcgvxW7jDaR+TEUfRnfk5XkJd3nDCtIC2+1lJY=; b=lnTIHydBjIpQB0yRTHmqnNU3HpT0Z31fDGXbWyzbXG2IQMpiZZa4wnWMk7phFNRhGY 1ltcX43V3GLZHIyBeLNvqzansf6qxAP98UYm01ZfJGxzWCljLqpOwYboLujsOIzldoAU 3JVWwZwyQ4pbEqX1qy7nJXNhguVVkEwgv6jpM5w2omTOEmJivofuuvOrccqoiAgCT0Ko jVrv97Tz3+SwchhLQ+XIf/w3UmMfkbQknr+holL3tr1sEfq6Ph4emradcDBbg86m2vPf /Y57bGRbfdf2r4VmgUID8dw5SitGXnTgHxuBrTcT7cNQWV2TMDh7bUi6NZfK7E5p/aVv ymFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700081921; x=1700686721; 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=/r4jyRcgvxW7jDaR+TEUfRnfk5XkJd3nDCtIC2+1lJY=; b=I/k2XZKKavm4KLLa2P/AfPZif91cIiiQVAj10zs8MCS5zjGI0QPf163LEIZHiJ+03+ B8m+AlG/iRuacM99FJqEKquzweXrzeDJ2owV6zb2K3AbMNXx/sOhKja/nAkY1keZQALS AYISE9MUXlhnGJaM7vRWk0AlERFOdspfCQozY8k//4lk8bi30bhkvi7t/3Z24PcUw/c1 sJzCVhTWi1op58r3tK1wNOam5A4TjxGF9Chf1T3RZpsx1p9WxO+lA0Cw2qo4b54UIKaZ 7Zw0vAqLhWj11U27XyyAJKu3+rz35ZrR1LiHDt7iFMsTK5sgzIWCG3IS09IhlZeBsHu/ sj+Q== X-Gm-Message-State: AOJu0YyLNI7fgRBGw5gg4RrgUNi+ojkSMTfa+d5prNX5lfXPmGiad1LU g8oJ15L5u89Q4es7+jMDLGGKa1gCg8ZLvwAi8c1MgBQMgEI= X-Received: by 2002:a05:6402:1859:b0:53e:1f7d:10f2 with SMTP id v25-20020a056402185900b0053e1f7d10f2mr11582797edy.10.1700081921281; Wed, 15 Nov 2023 12:58:41 -0800 (PST) MIME-Version: 1.0 References: <20231114170038.381634-1-ubizjak@gmail.com> In-Reply-To: From: Uros Bizjak Date: Wed, 15 Nov 2023 21:58:29 +0100 Message-ID: Subject: Re: [PATCH] x86/hyperv: Use atomic_try_cmpxchg() to micro-optimize hv_nmi_unknown() To: Michael Kelley Cc: "linux-hyperv@vger.kernel.org" , "x86@kernel.org" , "linux-kernel@vger.kernel.org" , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , 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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Wed, 15 Nov 2023 12:59:43 -0800 (PST) On Wed, Nov 15, 2023 at 6:19=E2=80=AFPM Michael Kelley wrote: > > From: Uros Bizjak Sent: Tuesday, November 14, 2023 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 success in > > the ZF flag, so this change saves a compare after CMPXCHG. The generate= d > > 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 Thanks, Uros.