* [PATCH 0/1] nospec: Add documentation for array_index_nospec
@ 2023-06-16 13:37 Jordy Zomer
2023-06-16 13:37 ` [PATCH 1/1] " Jordy Zomer
0 siblings, 1 reply; 5+ messages in thread
From: Jordy Zomer @ 2023-06-16 13:37 UTC (permalink / raw)
To: linux-kernel, linux-doc
Cc: corbet, jordyzomer, dave.hansen, daniel, tglx, rdunlap,
pawan.kumar.gupta
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().
Jordy Zomer (1):
nospec: Add documentation for array_index_nospec
Documentation/staging/speculation.rst | 5 +++++
include/linux/nospec.h | 5 +++++
2 files changed, 10 insertions(+)
--
2.41.0.162.gfafddb0af9-goog
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/1] nospec: Add documentation for array_index_nospec
2023-06-16 13:37 [PATCH 0/1] nospec: Add documentation for array_index_nospec Jordy Zomer
@ 2023-06-16 13:37 ` Jordy Zomer
2023-06-16 16:18 ` Dave Hansen
2023-06-16 18:05 ` Matthew Wilcox
0 siblings, 2 replies; 5+ messages in thread
From: Jordy Zomer @ 2023-06-16 13:37 UTC (permalink / raw)
To: linux-kernel, linux-doc
Cc: corbet, jordyzomer, dave.hansen, daniel, tglx, rdunlap,
pawan.kumar.gupta
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/1] nospec: Add documentation for array_index_nospec
2023-06-16 13:37 ` [PATCH 1/1] " Jordy Zomer
@ 2023-06-16 16:18 ` Dave Hansen
2023-06-16 18:05 ` Matthew Wilcox
1 sibling, 0 replies; 5+ messages in thread
From: Dave Hansen @ 2023-06-16 16:18 UTC (permalink / raw)
To: Jordy Zomer, linux-kernel, linux-doc
Cc: corbet, dave.hansen, daniel, tglx, rdunlap, pawan.kumar.gupta
On 6/16/23 06:37, Jordy Zomer wrote:
> 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().
Then shouldn't we be using __builtin_constant_p() to enforce this?
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/1] nospec: Add documentation for array_index_nospec
2023-06-16 13:37 ` [PATCH 1/1] " Jordy Zomer
2023-06-16 16:18 ` Dave Hansen
@ 2023-06-16 18:05 ` Matthew Wilcox
2023-06-16 18:46 ` Jordy Zomer
1 sibling, 1 reply; 5+ messages in thread
From: Matthew Wilcox @ 2023-06-16 18:05 UTC (permalink / raw)
To: Jordy Zomer
Cc: linux-kernel, linux-doc, corbet, dave.hansen, daniel, tglx,
rdunlap, pawan.kumar.gupta
On Fri, Jun 16, 2023 at 01:37:35PM +0000, Jordy Zomer wrote:
> +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().
"build time", not "built time". Also, "Please note that" doesn't
really add any value. You can just write:
This function should only be used if the upper boundary is a build-time
constant, otherwise this could be speculated on as well. If it is not
a constant, use barrier_nospec() instead.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/1] nospec: Add documentation for array_index_nospec
2023-06-16 18:05 ` Matthew Wilcox
@ 2023-06-16 18:46 ` Jordy Zomer
0 siblings, 0 replies; 5+ messages in thread
From: Jordy Zomer @ 2023-06-16 18:46 UTC (permalink / raw)
To: Matthew Wilcox
Cc: linux-kernel, linux-doc, corbet, dave.hansen, daniel, tglx,
rdunlap, pawan.kumar.gupta
Thanks both, I was planning on doing some plumbing next week to fix the
already affected calls and then add a BUILD_BUG_ON() in combination with
__builtin_constant_p() to prevent misuse from happening in the future. In
addition I'll send a V2 next week to fix the spelling/wording issue.
Cheers,
Jordy
On Fri, Jun 16, 2023 at 8:05 PM Matthew Wilcox <willy@infradead.org> wrote:
>
> On Fri, Jun 16, 2023 at 01:37:35PM +0000, Jordy Zomer wrote:
> > +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().
>
> "build time", not "built time". Also, "Please note that" doesn't
> really add any value. You can just write:
>
> This function should only be used if the upper boundary is a build-time
> constant, otherwise this could be speculated on as well. If it is not
> a constant, use barrier_nospec() instead.
>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-06-16 18:47 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-16 13:37 [PATCH 0/1] nospec: Add documentation for array_index_nospec Jordy Zomer
2023-06-16 13:37 ` [PATCH 1/1] " Jordy Zomer
2023-06-16 16:18 ` Dave Hansen
2023-06-16 18:05 ` Matthew Wilcox
2023-06-16 18:46 ` Jordy Zomer
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®