From: Jordy Zomer <jordyzomer@google.com>
To: linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org
Cc: corbet@lwn.net, jordyzomer@google.com,
dave.hansen@linux.intel.com, daniel@iogearbox.net,
tglx@linutronix.de, rdunlap@infradead.org,
pawan.kumar.gupta@linux.intel.com
Subject: [PATCH 1/1] nospec: Add documentation for array_index_nospec
Date: Fri, 16 Jun 2023 13:37:35 +0000 [thread overview]
Message-ID: <20230616133735.351479-2-jordyzomer@google.com> (raw)
In-Reply-To: <20230616133735.351479-1-jordyzomer@google.com>
array_index_nospec() should only be used if the upper boundary is a built
time constant. Otherwise the boundary could be speculated on as well.
While it might be more difficult to control two loads, it doesn't rule
out the problem. Adding this to the documentation so people won't mis-use
it instead of barrier_nospec().
Signed-off-by: Jordy Zomer <jordyzomer@google.com>
---
Documentation/staging/speculation.rst | 5 +++++
include/linux/nospec.h | 5 +++++
2 files changed, 10 insertions(+)
diff --git a/Documentation/staging/speculation.rst b/Documentation/staging/speculation.rst
index 8045d99bcf12..efc0ab32263b 100644
--- a/Documentation/staging/speculation.rst
+++ b/Documentation/staging/speculation.rst
@@ -79,6 +79,11 @@ A call to array_index_nospec(index, size) returns a sanitized index
value that is bounded to [0, size) even under cpu speculation
conditions.
+Please note that this function should only be used if the upper
+boundary is a built-time constant, otherwise this could be
+speculated on as well. If this is not the case please refer to
+barrier_nospec().
+
This can be used to protect the earlier load_array() example::
int load_array(int *array, unsigned int index)
diff --git a/include/linux/nospec.h b/include/linux/nospec.h
index 9f0af4f116d9..1d72c40595f4 100644
--- a/include/linux/nospec.h
+++ b/include/linux/nospec.h
@@ -51,6 +51,11 @@ static inline unsigned long array_index_mask_nospec(unsigned long index,
* ...if the CPU speculates past the bounds check then
* array_index_nospec() will clamp the index within the range of [0,
* size).
+ *
+ * Please note that this function should only be used if the upper
+ * boundary is a built-time constant, otherwise this could be
+ * speculated on as well. If this is not the case please refer to
+ * barrier_nospec().
*/
#define array_index_nospec(index, size) \
({ \
--
2.41.0.162.gfafddb0af9-goog
next prev parent reply other threads:[~2023-06-16 13:38 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-16 13:37 [PATCH 0/1] " Jordy Zomer
2023-06-16 13:37 ` Jordy Zomer [this message]
2023-06-16 16:18 ` [PATCH 1/1] " Dave Hansen
2023-06-16 18:05 ` Matthew Wilcox
2023-06-16 18:46 ` Jordy Zomer
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=20230616133735.351479-2-jordyzomer@google.com \
--to=jordyzomer@google.com \
--cc=corbet@lwn.net \
--cc=daniel@iogearbox.net \
--cc=dave.hansen@linux.intel.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pawan.kumar.gupta@linux.intel.com \
--cc=rdunlap@infradead.org \
--cc=tglx@linutronix.de \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®