License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 15:07:57 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
2020-05-05 15:36:12 -07:00
|
|
|
#ifndef _ASM_X86_RESCTRL_H
|
|
|
|
#define _ASM_X86_RESCTRL_H
|
2017-07-25 14:14:23 -07:00
|
|
|
|
2019-01-29 17:44:36 -05:00
|
|
|
#ifdef CONFIG_X86_CPU_RESCTRL
|
2017-07-25 14:14:23 -07:00
|
|
|
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/jump_label.h>
|
|
|
|
|
2024-02-13 18:44:19 +00:00
|
|
|
/*
|
|
|
|
* This value can never be a valid CLOSID, and is used when mapping a
|
|
|
|
* (closid, rmid) pair to an index and back. On x86 only the RMID is
|
|
|
|
* needed. The index is a software defined value.
|
|
|
|
*/
|
|
|
|
#define X86_RESCTRL_EMPTY_CLOSID ((u32)~0)
|
|
|
|
|
2017-07-25 14:14:23 -07:00
|
|
|
/**
|
2018-11-21 20:28:27 +00:00
|
|
|
* struct resctrl_pqr_state - State cache for the PQR MSR
|
2017-08-09 11:46:34 -07:00
|
|
|
* @cur_rmid: The cached Resource Monitoring ID
|
|
|
|
* @cur_closid: The cached Class Of Service ID
|
|
|
|
* @default_rmid: The user assigned Resource Monitoring ID
|
|
|
|
* @default_closid: The user assigned cached Class Of Service ID
|
2017-07-25 14:14:23 -07:00
|
|
|
*
|
2022-11-06 22:24:08 +01:00
|
|
|
* The upper 32 bits of MSR_IA32_PQR_ASSOC contain closid and the
|
|
|
|
* lower 10 bits rmid. The update to MSR_IA32_PQR_ASSOC always
|
2017-07-25 14:14:43 -07:00
|
|
|
* contains both parts, so we need to cache them. This also
|
|
|
|
* stores the user configured per cpu CLOSID and RMID.
|
2017-07-25 14:14:23 -07:00
|
|
|
*
|
|
|
|
* The cache also helps to avoid pointless updates if the value does
|
|
|
|
* not change.
|
|
|
|
*/
|
2018-11-21 20:28:27 +00:00
|
|
|
struct resctrl_pqr_state {
|
2017-08-09 11:46:34 -07:00
|
|
|
u32 cur_rmid;
|
|
|
|
u32 cur_closid;
|
|
|
|
u32 default_rmid;
|
|
|
|
u32 default_closid;
|
2017-07-25 14:14:23 -07:00
|
|
|
};
|
|
|
|
|
2018-11-21 20:28:27 +00:00
|
|
|
DECLARE_PER_CPU(struct resctrl_pqr_state, pqr_state);
|
2017-07-25 14:14:42 -07:00
|
|
|
|
2024-02-13 18:44:33 +00:00
|
|
|
extern bool rdt_alloc_capable;
|
|
|
|
extern bool rdt_mon_capable;
|
|
|
|
|
2017-07-25 14:14:42 -07:00
|
|
|
DECLARE_STATIC_KEY_FALSE(rdt_enable_key);
|
2017-07-25 14:14:25 -07:00
|
|
|
DECLARE_STATIC_KEY_FALSE(rdt_alloc_enable_key);
|
2017-07-25 14:14:43 -07:00
|
|
|
DECLARE_STATIC_KEY_FALSE(rdt_mon_enable_key);
|
2017-07-25 14:14:23 -07:00
|
|
|
|
2024-02-13 18:44:33 +00:00
|
|
|
static inline bool resctrl_arch_alloc_capable(void)
|
|
|
|
{
|
|
|
|
return rdt_alloc_capable;
|
|
|
|
}
|
|
|
|
|
2024-02-13 18:44:31 +00:00
|
|
|
static inline void resctrl_arch_enable_alloc(void)
|
|
|
|
{
|
|
|
|
static_branch_enable_cpuslocked(&rdt_alloc_enable_key);
|
2024-02-13 18:44:32 +00:00
|
|
|
static_branch_inc_cpuslocked(&rdt_enable_key);
|
2024-02-13 18:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void resctrl_arch_disable_alloc(void)
|
|
|
|
{
|
|
|
|
static_branch_disable_cpuslocked(&rdt_alloc_enable_key);
|
2024-02-13 18:44:32 +00:00
|
|
|
static_branch_dec_cpuslocked(&rdt_enable_key);
|
2024-02-13 18:44:31 +00:00
|
|
|
}
|
|
|
|
|
2024-02-13 18:44:33 +00:00
|
|
|
static inline bool resctrl_arch_mon_capable(void)
|
|
|
|
{
|
|
|
|
return rdt_mon_capable;
|
|
|
|
}
|
|
|
|
|
2024-02-13 18:44:31 +00:00
|
|
|
static inline void resctrl_arch_enable_mon(void)
|
|
|
|
{
|
|
|
|
static_branch_enable_cpuslocked(&rdt_mon_enable_key);
|
2024-02-13 18:44:32 +00:00
|
|
|
static_branch_inc_cpuslocked(&rdt_enable_key);
|
2024-02-13 18:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void resctrl_arch_disable_mon(void)
|
|
|
|
{
|
|
|
|
static_branch_disable_cpuslocked(&rdt_mon_enable_key);
|
2024-02-13 18:44:32 +00:00
|
|
|
static_branch_dec_cpuslocked(&rdt_enable_key);
|
2024-02-13 18:44:31 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 14:14:23 -07:00
|
|
|
/*
|
2018-11-21 20:28:27 +00:00
|
|
|
* __resctrl_sched_in() - Writes the task's CLOSid/RMID to IA32_PQR_MSR
|
2017-07-25 14:14:23 -07:00
|
|
|
*
|
|
|
|
* Following considerations are made so that this has minimal impact
|
|
|
|
* on scheduler hot path:
|
|
|
|
* - This will stay as no-op unless we are running on an Intel SKU
|
2017-07-25 14:14:43 -07:00
|
|
|
* which supports resource control or monitoring and we enable by
|
|
|
|
* mounting the resctrl file system.
|
|
|
|
* - Caches the per cpu CLOSid/RMID values and does the MSR write only
|
|
|
|
* when a task with a different CLOSid/RMID is scheduled in.
|
|
|
|
* - We allocate RMIDs/CLOSids globally in order to keep this as
|
|
|
|
* simple as possible.
|
2017-07-25 14:14:23 -07:00
|
|
|
* Must be called with preemption disabled.
|
|
|
|
*/
|
x86/resctl: fix scheduler confusion with 'current'
The implementation of 'current' on x86 is very intentionally special: it
is a very common thing to look up, and it uses 'this_cpu_read_stable()'
to get the current thread pointer efficiently from per-cpu storage.
And the keyword in there is 'stable': the current thread pointer never
changes as far as a single thread is concerned. Even if when a thread
is preempted, or moved to another CPU, or even across an explicit call
'schedule()' that thread will still have the same value for 'current'.
It is, after all, the kernel base pointer to thread-local storage.
That's why it's stable to begin with, but it's also why it's important
enough that we have that special 'this_cpu_read_stable()' access for it.
So this is all done very intentionally to allow the compiler to treat
'current' as a value that never visibly changes, so that the compiler
can do CSE and combine multiple different 'current' accesses into one.
However, there is obviously one very special situation when the
currently running thread does actually change: inside the scheduler
itself.
So the scheduler code paths are special, and do not have a 'current'
thread at all. Instead there are _two_ threads: the previous and the
next thread - typically called 'prev' and 'next' (or prev_p/next_p)
internally.
So this is all actually quite straightforward and simple, and not all
that complicated.
Except for when you then have special code that is run in scheduler
context, that code then has to be aware that 'current' isn't really a
valid thing. Did you mean 'prev'? Did you mean 'next'?
In fact, even if then look at the code, and you use 'current' after the
new value has been assigned to the percpu variable, we have explicitly
told the compiler that 'current' is magical and always stable. So the
compiler is quite free to use an older (or newer) value of 'current',
and the actual assignment to the percpu storage is not relevant even if
it might look that way.
Which is exactly what happened in the resctl code, that blithely used
'current' in '__resctrl_sched_in()' when it really wanted the new
process state (as implied by the name: we're scheduling 'into' that new
resctl state). And clang would end up just using the old thread pointer
value at least in some configurations.
This could have happened with gcc too, and purely depends on random
compiler details. Clang just seems to have been more aggressive about
moving the read of the per-cpu current_task pointer around.
The fix is trivial: just make the resctl code adhere to the scheduler
rules of using the prev/next thread pointer explicitly, instead of using
'current' in a situation where it just wasn't valid.
That same code is then also used outside of the scheduler context (when
a thread resctl state is explicitly changed), and then we will just pass
in 'current' as that pointer, of course. There is no ambiguity in that
case.
The fix may be trivial, but noticing and figuring out what went wrong
was not. The credit for that goes to Stephane Eranian.
Reported-by: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/lkml/20230303231133.1486085-1-eranian@google.com/
Link: https://lore.kernel.org/lkml/alpine.LFD.2.01.0908011214330.3304@localhost.localdomain/
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Tested-by: Tony Luck <tony.luck@intel.com>
Tested-by: Stephane Eranian <eranian@google.com>
Tested-by: Babu Moger <babu.moger@amd.com>
Cc: stable@kernel.org
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2023-03-07 13:06:29 -08:00
|
|
|
static inline void __resctrl_sched_in(struct task_struct *tsk)
|
2017-07-25 14:14:23 -07:00
|
|
|
{
|
2018-11-21 20:28:27 +00:00
|
|
|
struct resctrl_pqr_state *state = this_cpu_ptr(&pqr_state);
|
2017-08-09 11:46:34 -07:00
|
|
|
u32 closid = state->default_closid;
|
|
|
|
u32 rmid = state->default_rmid;
|
2020-12-17 14:31:21 -08:00
|
|
|
u32 tmp;
|
2017-07-25 14:14:43 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If this task has a closid/rmid assigned, use it.
|
|
|
|
* Else use the closid/rmid assigned to this cpu.
|
|
|
|
*/
|
2017-07-25 14:14:25 -07:00
|
|
|
if (static_branch_likely(&rdt_alloc_enable_key)) {
|
x86/resctl: fix scheduler confusion with 'current'
The implementation of 'current' on x86 is very intentionally special: it
is a very common thing to look up, and it uses 'this_cpu_read_stable()'
to get the current thread pointer efficiently from per-cpu storage.
And the keyword in there is 'stable': the current thread pointer never
changes as far as a single thread is concerned. Even if when a thread
is preempted, or moved to another CPU, or even across an explicit call
'schedule()' that thread will still have the same value for 'current'.
It is, after all, the kernel base pointer to thread-local storage.
That's why it's stable to begin with, but it's also why it's important
enough that we have that special 'this_cpu_read_stable()' access for it.
So this is all done very intentionally to allow the compiler to treat
'current' as a value that never visibly changes, so that the compiler
can do CSE and combine multiple different 'current' accesses into one.
However, there is obviously one very special situation when the
currently running thread does actually change: inside the scheduler
itself.
So the scheduler code paths are special, and do not have a 'current'
thread at all. Instead there are _two_ threads: the previous and the
next thread - typically called 'prev' and 'next' (or prev_p/next_p)
internally.
So this is all actually quite straightforward and simple, and not all
that complicated.
Except for when you then have special code that is run in scheduler
context, that code then has to be aware that 'current' isn't really a
valid thing. Did you mean 'prev'? Did you mean 'next'?
In fact, even if then look at the code, and you use 'current' after the
new value has been assigned to the percpu variable, we have explicitly
told the compiler that 'current' is magical and always stable. So the
compiler is quite free to use an older (or newer) value of 'current',
and the actual assignment to the percpu storage is not relevant even if
it might look that way.
Which is exactly what happened in the resctl code, that blithely used
'current' in '__resctrl_sched_in()' when it really wanted the new
process state (as implied by the name: we're scheduling 'into' that new
resctl state). And clang would end up just using the old thread pointer
value at least in some configurations.
This could have happened with gcc too, and purely depends on random
compiler details. Clang just seems to have been more aggressive about
moving the read of the per-cpu current_task pointer around.
The fix is trivial: just make the resctl code adhere to the scheduler
rules of using the prev/next thread pointer explicitly, instead of using
'current' in a situation where it just wasn't valid.
That same code is then also used outside of the scheduler context (when
a thread resctl state is explicitly changed), and then we will just pass
in 'current' as that pointer, of course. There is no ambiguity in that
case.
The fix may be trivial, but noticing and figuring out what went wrong
was not. The credit for that goes to Stephane Eranian.
Reported-by: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/lkml/20230303231133.1486085-1-eranian@google.com/
Link: https://lore.kernel.org/lkml/alpine.LFD.2.01.0908011214330.3304@localhost.localdomain/
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Tested-by: Tony Luck <tony.luck@intel.com>
Tested-by: Stephane Eranian <eranian@google.com>
Tested-by: Babu Moger <babu.moger@amd.com>
Cc: stable@kernel.org
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2023-03-07 13:06:29 -08:00
|
|
|
tmp = READ_ONCE(tsk->closid);
|
2020-12-17 14:31:21 -08:00
|
|
|
if (tmp)
|
|
|
|
closid = tmp;
|
2017-07-25 14:14:43 -07:00
|
|
|
}
|
2017-07-25 14:14:23 -07:00
|
|
|
|
2017-07-25 14:14:43 -07:00
|
|
|
if (static_branch_likely(&rdt_mon_enable_key)) {
|
x86/resctl: fix scheduler confusion with 'current'
The implementation of 'current' on x86 is very intentionally special: it
is a very common thing to look up, and it uses 'this_cpu_read_stable()'
to get the current thread pointer efficiently from per-cpu storage.
And the keyword in there is 'stable': the current thread pointer never
changes as far as a single thread is concerned. Even if when a thread
is preempted, or moved to another CPU, or even across an explicit call
'schedule()' that thread will still have the same value for 'current'.
It is, after all, the kernel base pointer to thread-local storage.
That's why it's stable to begin with, but it's also why it's important
enough that we have that special 'this_cpu_read_stable()' access for it.
So this is all done very intentionally to allow the compiler to treat
'current' as a value that never visibly changes, so that the compiler
can do CSE and combine multiple different 'current' accesses into one.
However, there is obviously one very special situation when the
currently running thread does actually change: inside the scheduler
itself.
So the scheduler code paths are special, and do not have a 'current'
thread at all. Instead there are _two_ threads: the previous and the
next thread - typically called 'prev' and 'next' (or prev_p/next_p)
internally.
So this is all actually quite straightforward and simple, and not all
that complicated.
Except for when you then have special code that is run in scheduler
context, that code then has to be aware that 'current' isn't really a
valid thing. Did you mean 'prev'? Did you mean 'next'?
In fact, even if then look at the code, and you use 'current' after the
new value has been assigned to the percpu variable, we have explicitly
told the compiler that 'current' is magical and always stable. So the
compiler is quite free to use an older (or newer) value of 'current',
and the actual assignment to the percpu storage is not relevant even if
it might look that way.
Which is exactly what happened in the resctl code, that blithely used
'current' in '__resctrl_sched_in()' when it really wanted the new
process state (as implied by the name: we're scheduling 'into' that new
resctl state). And clang would end up just using the old thread pointer
value at least in some configurations.
This could have happened with gcc too, and purely depends on random
compiler details. Clang just seems to have been more aggressive about
moving the read of the per-cpu current_task pointer around.
The fix is trivial: just make the resctl code adhere to the scheduler
rules of using the prev/next thread pointer explicitly, instead of using
'current' in a situation where it just wasn't valid.
That same code is then also used outside of the scheduler context (when
a thread resctl state is explicitly changed), and then we will just pass
in 'current' as that pointer, of course. There is no ambiguity in that
case.
The fix may be trivial, but noticing and figuring out what went wrong
was not. The credit for that goes to Stephane Eranian.
Reported-by: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/lkml/20230303231133.1486085-1-eranian@google.com/
Link: https://lore.kernel.org/lkml/alpine.LFD.2.01.0908011214330.3304@localhost.localdomain/
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Tested-by: Tony Luck <tony.luck@intel.com>
Tested-by: Stephane Eranian <eranian@google.com>
Tested-by: Babu Moger <babu.moger@amd.com>
Cc: stable@kernel.org
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2023-03-07 13:06:29 -08:00
|
|
|
tmp = READ_ONCE(tsk->rmid);
|
2020-12-17 14:31:21 -08:00
|
|
|
if (tmp)
|
|
|
|
rmid = tmp;
|
2017-07-25 14:14:43 -07:00
|
|
|
}
|
2017-07-25 14:14:23 -07:00
|
|
|
|
2017-08-09 11:46:34 -07:00
|
|
|
if (closid != state->cur_closid || rmid != state->cur_rmid) {
|
|
|
|
state->cur_closid = closid;
|
|
|
|
state->cur_rmid = rmid;
|
2022-11-06 22:24:08 +01:00
|
|
|
wrmsr(MSR_IA32_PQR_ASSOC, rmid, closid);
|
2017-07-25 14:14:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 15:48:27 +00:00
|
|
|
static inline unsigned int resctrl_arch_round_mon_val(unsigned int val)
|
|
|
|
{
|
|
|
|
unsigned int scale = boot_cpu_data.x86_cache_occ_scale;
|
|
|
|
|
|
|
|
/* h/w works in units of "boot_cpu_data.x86_cache_occ_scale" */
|
|
|
|
val /= scale;
|
|
|
|
return val * scale;
|
|
|
|
}
|
|
|
|
|
2024-02-13 18:44:25 +00:00
|
|
|
static inline void resctrl_arch_set_closid_rmid(struct task_struct *tsk,
|
|
|
|
u32 closid, u32 rmid)
|
|
|
|
{
|
|
|
|
WRITE_ONCE(tsk->closid, closid);
|
|
|
|
WRITE_ONCE(tsk->rmid, rmid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool resctrl_arch_match_closid(struct task_struct *tsk, u32 closid)
|
|
|
|
{
|
|
|
|
return READ_ONCE(tsk->closid) == closid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool resctrl_arch_match_rmid(struct task_struct *tsk, u32 ignored,
|
|
|
|
u32 rmid)
|
|
|
|
{
|
|
|
|
return READ_ONCE(tsk->rmid) == rmid;
|
|
|
|
}
|
|
|
|
|
x86/resctl: fix scheduler confusion with 'current'
The implementation of 'current' on x86 is very intentionally special: it
is a very common thing to look up, and it uses 'this_cpu_read_stable()'
to get the current thread pointer efficiently from per-cpu storage.
And the keyword in there is 'stable': the current thread pointer never
changes as far as a single thread is concerned. Even if when a thread
is preempted, or moved to another CPU, or even across an explicit call
'schedule()' that thread will still have the same value for 'current'.
It is, after all, the kernel base pointer to thread-local storage.
That's why it's stable to begin with, but it's also why it's important
enough that we have that special 'this_cpu_read_stable()' access for it.
So this is all done very intentionally to allow the compiler to treat
'current' as a value that never visibly changes, so that the compiler
can do CSE and combine multiple different 'current' accesses into one.
However, there is obviously one very special situation when the
currently running thread does actually change: inside the scheduler
itself.
So the scheduler code paths are special, and do not have a 'current'
thread at all. Instead there are _two_ threads: the previous and the
next thread - typically called 'prev' and 'next' (or prev_p/next_p)
internally.
So this is all actually quite straightforward and simple, and not all
that complicated.
Except for when you then have special code that is run in scheduler
context, that code then has to be aware that 'current' isn't really a
valid thing. Did you mean 'prev'? Did you mean 'next'?
In fact, even if then look at the code, and you use 'current' after the
new value has been assigned to the percpu variable, we have explicitly
told the compiler that 'current' is magical and always stable. So the
compiler is quite free to use an older (or newer) value of 'current',
and the actual assignment to the percpu storage is not relevant even if
it might look that way.
Which is exactly what happened in the resctl code, that blithely used
'current' in '__resctrl_sched_in()' when it really wanted the new
process state (as implied by the name: we're scheduling 'into' that new
resctl state). And clang would end up just using the old thread pointer
value at least in some configurations.
This could have happened with gcc too, and purely depends on random
compiler details. Clang just seems to have been more aggressive about
moving the read of the per-cpu current_task pointer around.
The fix is trivial: just make the resctl code adhere to the scheduler
rules of using the prev/next thread pointer explicitly, instead of using
'current' in a situation where it just wasn't valid.
That same code is then also used outside of the scheduler context (when
a thread resctl state is explicitly changed), and then we will just pass
in 'current' as that pointer, of course. There is no ambiguity in that
case.
The fix may be trivial, but noticing and figuring out what went wrong
was not. The credit for that goes to Stephane Eranian.
Reported-by: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/lkml/20230303231133.1486085-1-eranian@google.com/
Link: https://lore.kernel.org/lkml/alpine.LFD.2.01.0908011214330.3304@localhost.localdomain/
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Tested-by: Tony Luck <tony.luck@intel.com>
Tested-by: Stephane Eranian <eranian@google.com>
Tested-by: Babu Moger <babu.moger@amd.com>
Cc: stable@kernel.org
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2023-03-07 13:06:29 -08:00
|
|
|
static inline void resctrl_sched_in(struct task_struct *tsk)
|
2017-07-25 14:14:42 -07:00
|
|
|
{
|
|
|
|
if (static_branch_likely(&rdt_enable_key))
|
x86/resctl: fix scheduler confusion with 'current'
The implementation of 'current' on x86 is very intentionally special: it
is a very common thing to look up, and it uses 'this_cpu_read_stable()'
to get the current thread pointer efficiently from per-cpu storage.
And the keyword in there is 'stable': the current thread pointer never
changes as far as a single thread is concerned. Even if when a thread
is preempted, or moved to another CPU, or even across an explicit call
'schedule()' that thread will still have the same value for 'current'.
It is, after all, the kernel base pointer to thread-local storage.
That's why it's stable to begin with, but it's also why it's important
enough that we have that special 'this_cpu_read_stable()' access for it.
So this is all done very intentionally to allow the compiler to treat
'current' as a value that never visibly changes, so that the compiler
can do CSE and combine multiple different 'current' accesses into one.
However, there is obviously one very special situation when the
currently running thread does actually change: inside the scheduler
itself.
So the scheduler code paths are special, and do not have a 'current'
thread at all. Instead there are _two_ threads: the previous and the
next thread - typically called 'prev' and 'next' (or prev_p/next_p)
internally.
So this is all actually quite straightforward and simple, and not all
that complicated.
Except for when you then have special code that is run in scheduler
context, that code then has to be aware that 'current' isn't really a
valid thing. Did you mean 'prev'? Did you mean 'next'?
In fact, even if then look at the code, and you use 'current' after the
new value has been assigned to the percpu variable, we have explicitly
told the compiler that 'current' is magical and always stable. So the
compiler is quite free to use an older (or newer) value of 'current',
and the actual assignment to the percpu storage is not relevant even if
it might look that way.
Which is exactly what happened in the resctl code, that blithely used
'current' in '__resctrl_sched_in()' when it really wanted the new
process state (as implied by the name: we're scheduling 'into' that new
resctl state). And clang would end up just using the old thread pointer
value at least in some configurations.
This could have happened with gcc too, and purely depends on random
compiler details. Clang just seems to have been more aggressive about
moving the read of the per-cpu current_task pointer around.
The fix is trivial: just make the resctl code adhere to the scheduler
rules of using the prev/next thread pointer explicitly, instead of using
'current' in a situation where it just wasn't valid.
That same code is then also used outside of the scheduler context (when
a thread resctl state is explicitly changed), and then we will just pass
in 'current' as that pointer, of course. There is no ambiguity in that
case.
The fix may be trivial, but noticing and figuring out what went wrong
was not. The credit for that goes to Stephane Eranian.
Reported-by: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/lkml/20230303231133.1486085-1-eranian@google.com/
Link: https://lore.kernel.org/lkml/alpine.LFD.2.01.0908011214330.3304@localhost.localdomain/
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Tested-by: Tony Luck <tony.luck@intel.com>
Tested-by: Stephane Eranian <eranian@google.com>
Tested-by: Babu Moger <babu.moger@amd.com>
Cc: stable@kernel.org
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2023-03-07 13:06:29 -08:00
|
|
|
__resctrl_sched_in(tsk);
|
2017-07-25 14:14:42 -07:00
|
|
|
}
|
|
|
|
|
2024-02-13 18:44:20 +00:00
|
|
|
static inline u32 resctrl_arch_system_num_rmid_idx(void)
|
|
|
|
{
|
|
|
|
/* RMID are independent numbers for x86. num_rmid_idx == num_rmid */
|
|
|
|
return boot_cpu_data.x86_cache_max_rmid + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void resctrl_arch_rmid_idx_decode(u32 idx, u32 *closid, u32 *rmid)
|
|
|
|
{
|
|
|
|
*rmid = idx;
|
|
|
|
*closid = X86_RESCTRL_EMPTY_CLOSID;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 resctrl_arch_rmid_idx_encode(u32 ignored, u32 rmid)
|
|
|
|
{
|
|
|
|
return rmid;
|
|
|
|
}
|
|
|
|
|
2024-02-13 18:44:29 +00:00
|
|
|
/* x86 can always read an rmid, nothing needs allocating */
|
|
|
|
struct rdt_resource;
|
|
|
|
static inline void *resctrl_arch_mon_ctx_alloc(struct rdt_resource *r, int evtid)
|
|
|
|
{
|
|
|
|
might_sleep();
|
|
|
|
return NULL;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline void resctrl_arch_mon_ctx_free(struct rdt_resource *r, int evtid,
|
|
|
|
void *ctx) { };
|
|
|
|
|
2020-05-05 15:36:13 -07:00
|
|
|
void resctrl_cpu_detect(struct cpuinfo_x86 *c);
|
|
|
|
|
2017-07-25 14:14:23 -07:00
|
|
|
#else
|
|
|
|
|
x86/resctl: fix scheduler confusion with 'current'
The implementation of 'current' on x86 is very intentionally special: it
is a very common thing to look up, and it uses 'this_cpu_read_stable()'
to get the current thread pointer efficiently from per-cpu storage.
And the keyword in there is 'stable': the current thread pointer never
changes as far as a single thread is concerned. Even if when a thread
is preempted, or moved to another CPU, or even across an explicit call
'schedule()' that thread will still have the same value for 'current'.
It is, after all, the kernel base pointer to thread-local storage.
That's why it's stable to begin with, but it's also why it's important
enough that we have that special 'this_cpu_read_stable()' access for it.
So this is all done very intentionally to allow the compiler to treat
'current' as a value that never visibly changes, so that the compiler
can do CSE and combine multiple different 'current' accesses into one.
However, there is obviously one very special situation when the
currently running thread does actually change: inside the scheduler
itself.
So the scheduler code paths are special, and do not have a 'current'
thread at all. Instead there are _two_ threads: the previous and the
next thread - typically called 'prev' and 'next' (or prev_p/next_p)
internally.
So this is all actually quite straightforward and simple, and not all
that complicated.
Except for when you then have special code that is run in scheduler
context, that code then has to be aware that 'current' isn't really a
valid thing. Did you mean 'prev'? Did you mean 'next'?
In fact, even if then look at the code, and you use 'current' after the
new value has been assigned to the percpu variable, we have explicitly
told the compiler that 'current' is magical and always stable. So the
compiler is quite free to use an older (or newer) value of 'current',
and the actual assignment to the percpu storage is not relevant even if
it might look that way.
Which is exactly what happened in the resctl code, that blithely used
'current' in '__resctrl_sched_in()' when it really wanted the new
process state (as implied by the name: we're scheduling 'into' that new
resctl state). And clang would end up just using the old thread pointer
value at least in some configurations.
This could have happened with gcc too, and purely depends on random
compiler details. Clang just seems to have been more aggressive about
moving the read of the per-cpu current_task pointer around.
The fix is trivial: just make the resctl code adhere to the scheduler
rules of using the prev/next thread pointer explicitly, instead of using
'current' in a situation where it just wasn't valid.
That same code is then also used outside of the scheduler context (when
a thread resctl state is explicitly changed), and then we will just pass
in 'current' as that pointer, of course. There is no ambiguity in that
case.
The fix may be trivial, but noticing and figuring out what went wrong
was not. The credit for that goes to Stephane Eranian.
Reported-by: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/lkml/20230303231133.1486085-1-eranian@google.com/
Link: https://lore.kernel.org/lkml/alpine.LFD.2.01.0908011214330.3304@localhost.localdomain/
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Tested-by: Tony Luck <tony.luck@intel.com>
Tested-by: Stephane Eranian <eranian@google.com>
Tested-by: Babu Moger <babu.moger@amd.com>
Cc: stable@kernel.org
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2023-03-07 13:06:29 -08:00
|
|
|
static inline void resctrl_sched_in(struct task_struct *tsk) {}
|
2020-05-05 15:36:13 -07:00
|
|
|
static inline void resctrl_cpu_detect(struct cpuinfo_x86 *c) {}
|
2017-07-25 14:14:23 -07:00
|
|
|
|
2019-01-29 17:44:36 -05:00
|
|
|
#endif /* CONFIG_X86_CPU_RESCTRL */
|
2017-07-25 14:14:23 -07:00
|
|
|
|
2020-05-05 15:36:12 -07:00
|
|
|
#endif /* _ASM_X86_RESCTRL_H */
|