All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Xen-devel <xen-devel@lists.xenproject.org>
Cc: "Andrew Cooper" <andrew.cooper3@citrix.com>,
	"Jan Beulich" <JBeulich@suse.com>,
	"Roger Pau Monné" <roger.pau@citrix.com>, "Wei Liu" <wl@xen.org>
Subject: [PATCH 2/2] xen/nospec: Allow evaluate_nospec() to short circuit constant expressions
Date: Mon,  4 Mar 2024 16:10:41 +0000	[thread overview]
Message-ID: <20240304161041.3465897-3-andrew.cooper3@citrix.com> (raw)
In-Reply-To: <20240304161041.3465897-1-andrew.cooper3@citrix.com>

When the compiler can reduce the condition to a constant, it can elide the
conditional and one of the basic blocks.  However, arch_evaluate_nospec() will
still insert speculation protection, despite there being nothing to protect.

Allow the speculation protection to be skipped entirely when the compiler is
removing the condition entirely.

e.g. for x86, given:

  int foo(void)
  {
      if ( evaluate_nospec(1) )
          return 2;
      else
          return 42;
  }

then before, we get:

  <foo>:
      lfence
      mov    $0x2,%eax
      retq

and afterwards, we get:

  <foo>:
      mov    $0x2,%eax
      retq

which is correct.  With no conditional branch to protect, the lfence isn't
providing any relevant safety.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
CC: Jan Beulich <JBeulich@suse.com>
CC: Roger Pau Monné <roger.pau@citrix.com>
CC: Wei Liu <wl@xen.org>
---
 xen/include/xen/nospec.h | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/xen/include/xen/nospec.h b/xen/include/xen/nospec.h
index a4155af08770..56cf67a44176 100644
--- a/xen/include/xen/nospec.h
+++ b/xen/include/xen/nospec.h
@@ -18,6 +18,15 @@ static always_inline bool evaluate_nospec(bool cond)
 #ifndef arch_evaluate_nospec
 #define arch_evaluate_nospec(cond) cond
 #endif
+
+    /*
+     * If the compiler can reduce the condition to a constant, then it won't
+     * be emitting a conditional branch, and there's nothing needing
+     * protecting.
+     */
+    if ( __builtin_constant_p(cond) )
+        return cond;
+
     return arch_evaluate_nospec(cond);
 }
 
-- 
2.30.2



  parent reply	other threads:[~2024-03-04 16:11 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-04 16:10 [PATCH 0/2] xen/nospec: Improvements Andrew Cooper
2024-03-04 16:10 ` [PATCH 1/2] xen/*/nospec: Provide common versions of evaluate_nospec/block_speculation Andrew Cooper
2024-03-04 16:31   ` Julien Grall
2024-03-04 16:41     ` Jan Beulich
2024-03-04 16:46       ` Julien Grall
2024-03-04 16:55         ` Jan Beulich
2024-03-04 17:07           ` Andrew Cooper
2024-03-04 17:40             ` Julien Grall
2024-03-04 17:50               ` Andrew Cooper
2024-03-05 15:15                 ` Oleksii
2024-03-05 15:43                   ` Jan Beulich
2024-03-05 15:47                     ` Andrew Cooper
2024-03-05 18:56                     ` Oleksii
2024-03-05  7:10               ` Jan Beulich
2024-03-04 16:47       ` Andrew Cooper
2024-03-04 16:45   ` Jan Beulich
2024-03-04 16:46     ` Andrew Cooper
2024-03-05  7:15   ` Jan Beulich
2024-03-05  7:34   ` Jan Beulich
2024-03-04 16:10 ` Andrew Cooper [this message]
2024-03-05  7:30   ` [PATCH 2/2] xen/nospec: Allow evaluate_nospec() to short circuit constant expressions Jan Beulich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240304161041.3465897-3-andrew.cooper3@citrix.com \
    --to=andrew.cooper3@citrix.com \
    --cc=JBeulich@suse.com \
    --cc=roger.pau@citrix.com \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.