Re: [PATCH 04/33] gpiolib-of: use match_string() helper

2018-05-21 Thread Andy Shevchenko
On Mon, May 21, 2018 at 2:57 PM, Yisheng Xie  wrote:
> match_string() returns the index of an array for a matching string,
> which can be used intead of open coded variant.
>

https://patchwork.ozlabs.org/patch/908134/

> Cc: Linus Walleij 
> Cc: linux-g...@vger.kernel.org
> Signed-off-by: Yisheng Xie 


-- 
With Best Regards,
Andy Shevchenko


Re: [v4 09/11] drivers/hwmon: Add PECI cputemp driver

2018-05-21 Thread Jae Hyun Yoo

On 5/21/2018 2:06 PM, Guenter Roeck wrote:

On Mon, May 21, 2018 at 11:01:58PM +0200, Andrew Lunn wrote:

+#if IS_ENABLED(CONFIG_X86)
+#include 
+#else
+#define INTEL_FAM6_HASWELL_X   0x3F
+#define INTEL_FAM6_BROADWELL_X 0x4F
+#define INTEL_FAM6_SKYLAKE_X   0x55
+#endif


The entire code is very Intel specific. Why this #if instead of
making the driver dependent on X86 ?



This code will be running on ARM kernel at this moment with a purpose
of monitoring remote x86 CPUs through PECI connection


How about moving what you need from asm/intel-family.h into a header
file in include/linux/. Or move the entire header?


That might be another option. The one non-option is the #if IS_ENABLED()
in the driver.

Thanks,
Guenter



Looks like only this module includes the header file like this way. So
I'm going to keep this code for now after adding a comment like Guenter
suggested. We could consider moving the header file location later if
other module also needs the header in the same way. Thanks Andrew and
Guenter!

-Jae


[PATCH 4.16 079/110] proc: Provide details on speculation flaw mitigations

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Kees Cook 

commit fae1fa0fc6cca8beee3ab8ed71d54f9a78fa3f64 upstream

As done with seccomp and no_new_privs, also show speculation flaw
mitigation state in /proc/$pid/status.

Signed-off-by: Kees Cook 
Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 fs/proc/array.c |   22 ++
 1 file changed, 22 insertions(+)

--- a/fs/proc/array.c
+++ b/fs/proc/array.c
@@ -85,6 +85,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -347,6 +348,27 @@ static inline void task_seccomp(struct s
 #ifdef CONFIG_SECCOMP
seq_put_decimal_ull(m, "\nSeccomp:\t", p->seccomp.mode);
 #endif
+   seq_printf(m, "\nSpeculation Store Bypass:\t");
+   switch (arch_prctl_spec_ctrl_get(p, PR_SPEC_STORE_BYPASS)) {
+   case -EINVAL:
+   seq_printf(m, "unknown");
+   break;
+   case PR_SPEC_NOT_AFFECTED:
+   seq_printf(m, "not vulnerable");
+   break;
+   case PR_SPEC_PRCTL | PR_SPEC_DISABLE:
+   seq_printf(m, "thread mitigated");
+   break;
+   case PR_SPEC_PRCTL | PR_SPEC_ENABLE:
+   seq_printf(m, "thread vulnerable");
+   break;
+   case PR_SPEC_DISABLE:
+   seq_printf(m, "globally mitigated");
+   break;
+   default:
+   seq_printf(m, "vulnerable");
+   break;
+   }
seq_putc(m, '\n');
 }
 




[PATCH 4.16 081/110] x86/bugs: Make boot modes __ro_after_init

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Kees Cook 

commit f9544b2b076ca90d887c5ae5d74fab4c21bb7c13 upstream

There's no reason for these to be changed after boot.

Signed-off-by: Kees Cook 
Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/kernel/cpu/bugs.c |5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -129,7 +129,8 @@ static const char *spectre_v2_strings[]
 #undef pr_fmt
 #define pr_fmt(fmt) "Spectre V2 : " fmt
 
-static enum spectre_v2_mitigation spectre_v2_enabled = SPECTRE_V2_NONE;
+static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
+   SPECTRE_V2_NONE;
 
 void x86_spec_ctrl_set(u64 val)
 {
@@ -407,7 +408,7 @@ retpoline_auto:
 #undef pr_fmt
 #define pr_fmt(fmt)"Speculative Store Bypass: " fmt
 
-static enum ssb_mitigation ssb_mode = SPEC_STORE_BYPASS_NONE;
+static enum ssb_mitigation ssb_mode __ro_after_init = SPEC_STORE_BYPASS_NONE;
 
 /* The kernel command line selection */
 enum ssb_mitigation_cmd {




[PATCH 4.16 082/110] prctl: Add force disable speculation

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit 356e4bfff2c5489e016fdb925adbf12a1e3950ee upstream

For certain use cases it is desired to enforce mitigations so they cannot
be undone afterwards. That's important for loader stubs which want to
prevent a child from disabling the mitigation again. Will also be used for
seccomp(). The extra state preserving of the prctl state for SSB is a
preparatory step for EBPF dymanic speculation control.

Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 Documentation/userspace-api/spec_ctrl.rst |   34 ++---
 arch/x86/kernel/cpu/bugs.c|   35 +-
 fs/proc/array.c   |3 ++
 include/linux/sched.h |   10 +++-
 include/uapi/linux/prctl.h|1 
 5 files changed, 59 insertions(+), 24 deletions(-)

--- a/Documentation/userspace-api/spec_ctrl.rst
+++ b/Documentation/userspace-api/spec_ctrl.rst
@@ -25,19 +25,21 @@ PR_GET_SPECULATION_CTRL
 ---
 
 PR_GET_SPECULATION_CTRL returns the state of the speculation misfeature
-which is selected with arg2 of prctl(2). The return value uses bits 0-2 with
+which is selected with arg2 of prctl(2). The return value uses bits 0-3 with
 the following meaning:
 
-  ===
-Bit  Define   Description
-  ===
-0PR_SPEC_PRCTLMitigation can be controlled per task by
-  PR_SET_SPECULATION_CTRL
-1PR_SPEC_ENABLE   The speculation feature is enabled, mitigation is
-  disabled
-2PR_SPEC_DISABLE  The speculation feature is disabled, mitigation is
-  enabled
-  ===
+ = ===
+Bit  DefineDescription
+ = ===
+0PR_SPEC_PRCTL Mitigation can be controlled per task by
+   PR_SET_SPECULATION_CTRL
+1PR_SPEC_ENABLEThe speculation feature is enabled, mitigation is
+   disabled
+2PR_SPEC_DISABLE   The speculation feature is disabled, mitigation is
+   enabled
+3PR_SPEC_FORCE_DISABLE Same as PR_SPEC_DISABLE, but cannot be undone. A
+   subsequent prctl(..., PR_SPEC_ENABLE) will fail.
+ = ===
 
 If all bits are 0 the CPU is not affected by the speculation misfeature.
 
@@ -47,9 +49,11 @@ misfeature will fail.
 
 PR_SET_SPECULATION_CTRL
 ---
+
 PR_SET_SPECULATION_CTRL allows to control the speculation misfeature, which
 is selected by arg2 of :manpage:`prctl(2)` per task. arg3 is used to hand
-in the control value, i.e. either PR_SPEC_ENABLE or PR_SPEC_DISABLE.
+in the control value, i.e. either PR_SPEC_ENABLE or PR_SPEC_DISABLE or
+PR_SPEC_FORCE_DISABLE.
 
 Common error codes
 --
@@ -70,10 +74,13 @@ Value   Meaning
 0   Success
 
 ERANGE  arg3 is incorrect, i.e. it's neither PR_SPEC_ENABLE nor
-PR_SPEC_DISABLE
+PR_SPEC_DISABLE nor PR_SPEC_FORCE_DISABLE
 
 ENXIO   Control of the selected speculation misfeature is not possible.
 See PR_GET_SPECULATION_CTRL.
+
+EPERM   Speculation was disabled with PR_SPEC_FORCE_DISABLE and caller
+tried to enable it again.
 === =
 
 Speculation misfeature controls
@@ -84,3 +91,4 @@ Speculation misfeature controls
* prctl(PR_GET_SPECULATION_CTRL, PR_SPEC_STORE_BYPASS, 0, 0, 0);
* prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_STORE_BYPASS, PR_SPEC_ENABLE, 0, 
0);
* prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_STORE_BYPASS, PR_SPEC_DISABLE, 0, 
0);
+   * prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_STORE_BYPASS, 
PR_SPEC_FORCE_DISABLE, 0, 0);
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -533,21 +533,37 @@ static void ssb_select_mitigation()
 
 static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
 {
-   bool rds = !!test_tsk_thread_flag(task, TIF_RDS);
+   bool update;
 
if (ssb_mode != SPEC_STORE_BYPASS_PRCTL)
return -ENXIO;
 
-   if (ctrl == PR_SPEC_ENABLE)
-   clear_tsk_thread_flag(task, TIF_RDS);
-   else
-   set_tsk_thread_flag(task, TIF_RDS);
+   switch (ctrl) {
+   case PR_SPEC_ENABLE:
+   /* If speculation is force disabled, enable is not allowed */
+   if (task_spec_ssb_force_disable(task))
+   return -EPERM;
+   task_clear_spec_ssb_disable(task);
+ 

Re: [PATCH 03/33] Staging: gdm724x: use match_string() helper

2018-05-21 Thread Andy Shevchenko
On Mon, May 21, 2018 at 2:57 PM, Yisheng Xie  wrote:
> match_string() returns the index of an array for a matching string,
> which can be used intead of open coded variant.


> +   ret = match_string((const char **)DRIVER_STRING,
> +  TTY_MAX_COUNT, tty->driver->driver_name);

It looks like DRIVER_STRING should be converted to const * const.

-- 
With Best Regards,
Andy Shevchenko


[PATCH 4.16 084/110] seccomp: Add filter flag to opt-out of SSB mitigation

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Kees Cook 

commit 00a02d0c502a06d15e07b857f8ff921e3e402675 upstream

If a seccomp user is not interested in Speculative Store Bypass mitigation
by default, it can set the new SECCOMP_FILTER_FLAG_SPEC_ALLOW flag when
adding filters.

Signed-off-by: Kees Cook 
Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 include/linux/seccomp.h   |5 +++--
 include/uapi/linux/seccomp.h  |5 +++--
 kernel/seccomp.c  |   19 +++
 tools/testing/selftests/seccomp/seccomp_bpf.c |   22 +++---
 4 files changed, 36 insertions(+), 15 deletions(-)

--- a/include/linux/seccomp.h
+++ b/include/linux/seccomp.h
@@ -4,8 +4,9 @@
 
 #include 
 
-#define SECCOMP_FILTER_FLAG_MASK   (SECCOMP_FILTER_FLAG_TSYNC | \
-SECCOMP_FILTER_FLAG_LOG)
+#define SECCOMP_FILTER_FLAG_MASK   (SECCOMP_FILTER_FLAG_TSYNC  | \
+SECCOMP_FILTER_FLAG_LOG| \
+SECCOMP_FILTER_FLAG_SPEC_ALLOW)
 
 #ifdef CONFIG_SECCOMP
 
--- a/include/uapi/linux/seccomp.h
+++ b/include/uapi/linux/seccomp.h
@@ -17,8 +17,9 @@
 #define SECCOMP_GET_ACTION_AVAIL   2
 
 /* Valid flags for SECCOMP_SET_MODE_FILTER */
-#define SECCOMP_FILTER_FLAG_TSYNC  1
-#define SECCOMP_FILTER_FLAG_LOG2
+#define SECCOMP_FILTER_FLAG_TSYNC  (1UL << 0)
+#define SECCOMP_FILTER_FLAG_LOG(1UL << 1)
+#define SECCOMP_FILTER_FLAG_SPEC_ALLOW (1UL << 2)
 
 /*
  * All BPF programs must return a 32-bit value.
--- a/kernel/seccomp.c
+++ b/kernel/seccomp.c
@@ -243,7 +243,8 @@ static inline void spec_mitigate(struct
 }
 
 static inline void seccomp_assign_mode(struct task_struct *task,
-  unsigned long seccomp_mode)
+  unsigned long seccomp_mode,
+  unsigned long flags)
 {
assert_spin_locked(&task->sighand->siglock);
 
@@ -253,8 +254,9 @@ static inline void seccomp_assign_mode(s
 * filter) is set.
 */
smp_mb__before_atomic();
-   /* Assume seccomp processes want speculation flaw mitigation. */
-   spec_mitigate(task, PR_SPEC_STORE_BYPASS);
+   /* Assume default seccomp processes want spec flaw mitigation. */
+   if ((flags & SECCOMP_FILTER_FLAG_SPEC_ALLOW) == 0)
+   spec_mitigate(task, PR_SPEC_STORE_BYPASS);
set_tsk_thread_flag(task, TIF_SECCOMP);
 }
 
@@ -322,7 +324,7 @@ static inline pid_t seccomp_can_sync_thr
  * without dropping the locks.
  *
  */
-static inline void seccomp_sync_threads(void)
+static inline void seccomp_sync_threads(unsigned long flags)
 {
struct task_struct *thread, *caller;
 
@@ -363,7 +365,8 @@ static inline void seccomp_sync_threads(
 * allow one thread to transition the other.
 */
if (thread->seccomp.mode == SECCOMP_MODE_DISABLED)
-   seccomp_assign_mode(thread, SECCOMP_MODE_FILTER);
+   seccomp_assign_mode(thread, SECCOMP_MODE_FILTER,
+   flags);
}
 }
 
@@ -486,7 +489,7 @@ static long seccomp_attach_filter(unsign
 
/* Now that the new filter is in place, synchronize to all threads. */
if (flags & SECCOMP_FILTER_FLAG_TSYNC)
-   seccomp_sync_threads();
+   seccomp_sync_threads(flags);
 
return 0;
 }
@@ -835,7 +838,7 @@ static long seccomp_set_mode_strict(void
 #ifdef TIF_NOTSC
disable_TSC();
 #endif
-   seccomp_assign_mode(current, seccomp_mode);
+   seccomp_assign_mode(current, seccomp_mode, 0);
ret = 0;
 
 out:
@@ -893,7 +896,7 @@ static long seccomp_set_mode_filter(unsi
/* Do not free the successfully attached filter. */
prepared = NULL;
 
-   seccomp_assign_mode(current, seccomp_mode);
+   seccomp_assign_mode(current, seccomp_mode, flags);
 out:
spin_unlock_irq(¤t->sighand->siglock);
if (flags & SECCOMP_FILTER_FLAG_TSYNC)
--- a/tools/testing/selftests/seccomp/seccomp_bpf.c
+++ b/tools/testing/selftests/seccomp/seccomp_bpf.c
@@ -134,11 +134,15 @@ struct seccomp_data {
 #endif
 
 #ifndef SECCOMP_FILTER_FLAG_TSYNC
-#define SECCOMP_FILTER_FLAG_TSYNC 1
+#define SECCOMP_FILTER_FLAG_TSYNC (1UL << 0)
 #endif
 
 #ifndef SECCOMP_FILTER_FLAG_LOG
-#define SECCOMP_FILTER_FLAG_LOG 2
+#define SECCOMP_FILTER_FLAG_LOG (1UL << 1)
+#endif
+
+#ifndef SECCOMP_FILTER_FLAG_SPEC_ALLOW
+#define SECCOMP_FILTER_FLAG_SPEC_ALLOW (1UL << 2)
 #endif
 
 #ifndef PTRACE_SECCOMP_GET_METADATA
@@ -2072,14 +2076,26 @@ TEST(seccomp_syscall_mode_lock)
 TEST(detect_seccomp_filter_flags)
 {
unsigned int flags[] = { SECCOMP_FILTER_FLAG_TSYNC,
-SECCOMP_FILTER_FLAG_LOG };

[PATCH 4.16 083/110] seccomp: Use PR_SPEC_FORCE_DISABLE

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit b849a812f7eb92e96d1c8239b06581b2cfd8b275 upstream

Use PR_SPEC_FORCE_DISABLE in seccomp() because seccomp does not allow to
widen restrictions.

Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 kernel/seccomp.c |2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

--- a/kernel/seccomp.c
+++ b/kernel/seccomp.c
@@ -239,7 +239,7 @@ static inline void spec_mitigate(struct
int state = arch_prctl_spec_ctrl_get(task, which);
 
if (state > 0 && (state & PR_SPEC_PRCTL))
-   arch_prctl_spec_ctrl_set(task, which, PR_SPEC_DISABLE);
+   arch_prctl_spec_ctrl_set(task, which, PR_SPEC_FORCE_DISABLE);
 }
 
 static inline void seccomp_assign_mode(struct task_struct *task,




[PATCH 4.16 088/110] proc: Use underscores for SSBD in status

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Konrad Rzeszutek Wilk 

commit e96f46ee8587607a828f783daa6eb5b44d25004d upstream

The style for the 'status' file is CamelCase or this. _.

Fixes: fae1fa0fc ("proc: Provide details on speculation flaw mitigations")
Signed-off-by: Konrad Rzeszutek Wilk 
Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 fs/proc/array.c |2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

--- a/fs/proc/array.c
+++ b/fs/proc/array.c
@@ -348,7 +348,7 @@ static inline void task_seccomp(struct s
 #ifdef CONFIG_SECCOMP
seq_put_decimal_ull(m, "\nSeccomp:\t", p->seccomp.mode);
 #endif
-   seq_printf(m, "\nSpeculation Store Bypass:\t");
+   seq_printf(m, "\nSpeculation_Store_Bypass:\t");
switch (arch_prctl_spec_ctrl_get(p, PR_SPEC_STORE_BYPASS)) {
case -EINVAL:
seq_printf(m, "unknown");




[PATCH 4.16 085/110] seccomp: Move speculation migitation control to arch code

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit 8bf37d8c067bb7eb8e7c381bdadf9bd89182b6bc upstream

The migitation control is simpler to implement in architecture code as it
avoids the extra function call to check the mode. Aside of that having an
explicit seccomp enabled mode in the architecture mitigations would require
even more workarounds.

Move it into architecture code and provide a weak function in the seccomp
code. Remove the 'which' argument as this allows the architecture to decide
which mitigations are relevant for seccomp.

Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/kernel/cpu/bugs.c |   29 ++---
 include/linux/nospec.h |2 ++
 kernel/seccomp.c   |   15 ++-
 3 files changed, 22 insertions(+), 24 deletions(-)

--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -569,6 +569,24 @@ static int ssb_prctl_set(struct task_str
return 0;
 }
 
+int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
+unsigned long ctrl)
+{
+   switch (which) {
+   case PR_SPEC_STORE_BYPASS:
+   return ssb_prctl_set(task, ctrl);
+   default:
+   return -ENODEV;
+   }
+}
+
+#ifdef CONFIG_SECCOMP
+void arch_seccomp_spec_mitigate(struct task_struct *task)
+{
+   ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
+}
+#endif
+
 static int ssb_prctl_get(struct task_struct *task)
 {
switch (ssb_mode) {
@@ -587,17 +605,6 @@ static int ssb_prctl_get(struct task_str
}
 }
 
-int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
-unsigned long ctrl)
-{
-   switch (which) {
-   case PR_SPEC_STORE_BYPASS:
-   return ssb_prctl_set(task, ctrl);
-   default:
-   return -ENODEV;
-   }
-}
-
 int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
 {
switch (which) {
--- a/include/linux/nospec.h
+++ b/include/linux/nospec.h
@@ -62,5 +62,7 @@ static inline unsigned long array_index_
 int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which);
 int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
 unsigned long ctrl);
+/* Speculation control for seccomp enforced mitigation */
+void arch_seccomp_spec_mitigate(struct task_struct *task);
 
 #endif /* _LINUX_NOSPEC_H */
--- a/kernel/seccomp.c
+++ b/kernel/seccomp.c
@@ -229,18 +229,7 @@ static inline bool seccomp_may_assign_mo
return true;
 }
 
-/*
- * If a given speculation mitigation is opt-in (prctl()-controlled),
- * select it, by disabling speculation (enabling mitigation).
- */
-static inline void spec_mitigate(struct task_struct *task,
-unsigned long which)
-{
-   int state = arch_prctl_spec_ctrl_get(task, which);
-
-   if (state > 0 && (state & PR_SPEC_PRCTL))
-   arch_prctl_spec_ctrl_set(task, which, PR_SPEC_FORCE_DISABLE);
-}
+void __weak arch_seccomp_spec_mitigate(struct task_struct *task) { }
 
 static inline void seccomp_assign_mode(struct task_struct *task,
   unsigned long seccomp_mode,
@@ -256,7 +245,7 @@ static inline void seccomp_assign_mode(s
smp_mb__before_atomic();
/* Assume default seccomp processes want spec flaw mitigation. */
if ((flags & SECCOMP_FILTER_FLAG_SPEC_ALLOW) == 0)
-   spec_mitigate(task, PR_SPEC_STORE_BYPASS);
+   arch_seccomp_spec_mitigate(task);
set_tsk_thread_flag(task, TIF_SECCOMP);
 }
 




[PATCH 4.16 087/110] x86/bugs: Rename _RDS to _SSBD

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Konrad Rzeszutek Wilk 

commit 9f65fb29374ee37856dbad847b4e121aab72b510 upstream

Intel collateral will reference the SSB mitigation bit in IA32_SPEC_CTL[2]
as SSBD (Speculative Store Bypass Disable).

Hence changing it.

It is unclear yet what the MSR_IA32_ARCH_CAPABILITIES (0x10a) Bit(4) name
is going to be. Following the rename it would be SSBD_NO but that rolls out
to Speculative Store Bypass Disable No.

Also fixed the missing space in X86_FEATURE_AMD_SSBD.

[ tglx: Fixup x86_amd_rds_enable() and rds_tif_to_amd_ls_cfg() as well ]

Signed-off-by: Konrad Rzeszutek Wilk 
Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/cpufeatures.h |4 ++--
 arch/x86/include/asm/msr-index.h   |   10 +-
 arch/x86/include/asm/spec-ctrl.h   |   12 ++--
 arch/x86/include/asm/thread_info.h |6 +++---
 arch/x86/kernel/cpu/amd.c  |   14 +++---
 arch/x86/kernel/cpu/bugs.c |   36 ++--
 arch/x86/kernel/cpu/common.c   |2 +-
 arch/x86/kernel/cpu/intel.c|2 +-
 arch/x86/kernel/process.c  |8 
 arch/x86/kvm/cpuid.c   |2 +-
 arch/x86/kvm/vmx.c |6 +++---
 11 files changed, 51 insertions(+), 51 deletions(-)

--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -215,7 +215,7 @@
 #define X86_FEATURE_USE_IBPB   ( 7*32+21) /* "" Indirect Branch 
Prediction Barrier enabled */
 #define X86_FEATURE_USE_IBRS_FW( 7*32+22) /* "" Use IBRS 
during runtime firmware calls */
 #define X86_FEATURE_SPEC_STORE_BYPASS_DISABLE  ( 7*32+23) /* "" Disable 
Speculative Store Bypass. */
-#define X86_FEATURE_AMD_RDS(7*32+24)  /* "" AMD RDS implementation 
*/
+#define X86_FEATURE_AMD_SSBD   ( 7*32+24)  /* "" AMD SSBD 
implementation */
 
 /* Virtualization flags: Linux defined, word 8 */
 #define X86_FEATURE_TPR_SHADOW ( 8*32+ 0) /* Intel TPR Shadow */
@@ -335,7 +335,7 @@
 #define X86_FEATURE_SPEC_CTRL  (18*32+26) /* "" Speculation Control 
(IBRS + IBPB) */
 #define X86_FEATURE_INTEL_STIBP(18*32+27) /* "" Single Thread 
Indirect Branch Predictors */
 #define X86_FEATURE_ARCH_CAPABILITIES  (18*32+29) /* IA32_ARCH_CAPABILITIES 
MSR (Intel) */
-#define X86_FEATURE_RDS(18*32+31) /* Reduced Data 
Speculation */
+#define X86_FEATURE_SSBD   (18*32+31) /* Speculative Store Bypass 
Disable */
 
 /*
  * BUG word(s)
--- a/arch/x86/include/asm/msr-index.h
+++ b/arch/x86/include/asm/msr-index.h
@@ -42,8 +42,8 @@
 #define MSR_IA32_SPEC_CTRL 0x0048 /* Speculation Control */
 #define SPEC_CTRL_IBRS (1 << 0)   /* Indirect Branch 
Restricted Speculation */
 #define SPEC_CTRL_STIBP(1 << 1)   /* Single Thread 
Indirect Branch Predictors */
-#define SPEC_CTRL_RDS_SHIFT2  /* Reduced Data Speculation 
bit */
-#define SPEC_CTRL_RDS  (1 << SPEC_CTRL_RDS_SHIFT)   /* Reduced 
Data Speculation */
+#define SPEC_CTRL_SSBD_SHIFT   2  /* Speculative Store Bypass 
Disable bit */
+#define SPEC_CTRL_SSBD (1 << SPEC_CTRL_SSBD_SHIFT)   /* 
Speculative Store Bypass Disable */
 
 #define MSR_IA32_PRED_CMD  0x0049 /* Prediction Command */
 #define PRED_CMD_IBPB  (1 << 0)   /* Indirect Branch 
Prediction Barrier */
@@ -70,10 +70,10 @@
 #define MSR_IA32_ARCH_CAPABILITIES 0x010a
 #define ARCH_CAP_RDCL_NO   (1 << 0)   /* Not susceptible to 
Meltdown */
 #define ARCH_CAP_IBRS_ALL  (1 << 1)   /* Enhanced IBRS support */
-#define ARCH_CAP_RDS_NO(1 << 4)   /*
+#define ARCH_CAP_SSBD_NO   (1 << 4)   /*
* Not susceptible to 
Speculative Store Bypass
-   * attack, so no Reduced 
Data Speculation control
-   * required.
+   * attack, so no Speculative 
Store Bypass
+   * control required.
*/
 
 #define MSR_IA32_BBL_CR_CTL0x0119
--- a/arch/x86/include/asm/spec-ctrl.h
+++ b/arch/x86/include/asm/spec-ctrl.h
@@ -17,20 +17,20 @@ extern void x86_spec_ctrl_restore_host(u
 
 /* AMD specific Speculative Store Bypass MSR data */
 extern u64 x86_amd_ls_cfg_base;
-extern u64 x86_amd_ls_cfg_rds_mask;
+extern u64 x86_amd_ls_cfg_ssbd_mask;
 
 /* The Intel SPEC CTRL MSR base value cache */
 extern u64 x86_spec_ctrl_base;
 
-static inline u64 rds_tif_to_spec_ctrl(u64 tifn)
+static inline u64 ssbd_tif_to_spec_ctrl(u64 tifn)
 {
-   BUILD_BUG_ON(TIF_RDS < SPEC_CTRL_RDS_

[PATCH] bpf: prevent memory disambiguation attack

2018-05-21 Thread Alexei Starovoitov
Detect code patterns where malicious 'speculative store bypass' can be used
and sanitize such patterns.

 39: (bf) r3 = r10
 40: (07) r3 += -216
 41: (79) r8 = *(u64 *)(r7 +0)   // slow read
 42: (7a) *(u64 *)(r10 -72) = 0  // verifier inserts this instruction
 43: (7b) *(u64 *)(r8 +0) = r3   // this store becomes slow due to r8
 44: (79) r1 = *(u64 *)(r6 +0)   // cpu speculatively executes this load
 45: (71) r2 = *(u8 *)(r1 +0)// speculatively arbitrary 'load byte'
 // is now sanitized

Above code after x86 JIT becomes:
 e5: mov%rbp,%rdx
 e8: add$0xff28,%rdx
 ef: mov0x0(%r13),%r14
 f3: movq   $0x0,-0x48(%rbp)
 fb: mov%rdx,0x0(%r14)
 ff: mov0x0(%rbx),%rdi
103: movzbq 0x0(%rdi),%rsi

Signed-off-by: Alexei Starovoitov 
---
 include/linux/bpf_verifier.h |  1 +
 kernel/bpf/verifier.c| 59 +---
 2 files changed, 57 insertions(+), 3 deletions(-)

diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 7e61c395fddf..65cfc2f59db9 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -146,6 +146,7 @@ struct bpf_insn_aux_data {
s32 call_imm;   /* saved imm field of call insn 
*/
};
int ctx_field_size; /* the ctx field size for load insn, maybe 0 */
+   int sanitize_stack_off; /* stack slot to be cleared */
bool seen; /* this insn was processed by the verifier */
 };
 
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 5dd1dcb902bf..2ce967a63ede 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -978,7 +978,7 @@ static bool register_is_null(struct bpf_reg_state *reg)
  */
 static int check_stack_write(struct bpf_verifier_env *env,
 struct bpf_func_state *state, /* func where 
register points to */
-int off, int size, int value_regno)
+int off, int size, int value_regno, int insn_idx)
 {
struct bpf_func_state *cur; /* state of the current function */
int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err;
@@ -1017,8 +1017,33 @@ static int check_stack_write(struct bpf_verifier_env 
*env,
state->stack[spi].spilled_ptr = cur->regs[value_regno];
state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
 
-   for (i = 0; i < BPF_REG_SIZE; i++)
+   for (i = 0; i < BPF_REG_SIZE; i++) {
+   if (state->stack[spi].slot_type[i] == STACK_MISC &&
+   !env->allow_ptr_leaks) {
+   int *poff = 
&env->insn_aux_data[insn_idx].sanitize_stack_off;
+   int soff = (-spi - 1) * BPF_REG_SIZE;
+
+   /* detected reuse of integer stack slot with a 
pointer
+* which means either llvm is reusing stack 
slot or
+* an attacker is trying to exploit 
CVE-2018-3639
+* (speculative store bypass)
+* Have to sanitize that slot with preemptive
+* store of zero.
+*/
+   if (*poff && *poff != soff) {
+   /* disallow programs where single insn 
stores
+* into two different stack slots, 
since verifier
+* cannot sanitize them
+*/
+   verbose(env,
+   "insn %d cannot access two 
stack slots fp%d and fp%d",
+   insn_idx, *poff, soff);
+   return -EINVAL;
+   }
+   *poff = soff;
+   }
state->stack[spi].slot_type[i] = STACK_SPILL;
+   }
} else {
u8 type = STACK_MISC;
 
@@ -1694,7 +1719,7 @@ static int check_mem_access(struct bpf_verifier_env *env, 
int insn_idx, u32 regn
 
if (t == BPF_WRITE)
err = check_stack_write(env, state, off, size,
-   value_regno);
+   value_regno, insn_idx);
else
err = check_stack_read(env, state, off, size,
   value_regno);
@@ -5169,6 +5194,34 @@ static int convert_ctx_accesses(struct bpf_verifier_env 
*env)
else
continue;
 
+   if (type == BPF_WRITE &&
+   env->insn_aux_data[i + delta].sanitize_stack_off) {
+   struct bpf_insn patch[] = {
+ 

[PATCH 4.16 092/110] x86/bugs: Fix the parameters alignment and missing void

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Konrad Rzeszutek Wilk 

commit ffed645e3be0e32f8e9ab068d257aee8d0fe8eec upstream

Fixes: 7bb4d366c ("x86/bugs: Make cpu_show_common() static")
Fixes: 24f7fc83b ("x86/bugs: Provide boot parameters for the 
spec_store_bypass_disable mitigation")
Signed-off-by: Konrad Rzeszutek Wilk 
Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/kernel/cpu/bugs.c |4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -531,7 +531,7 @@ static enum ssb_mitigation __init __ssb_
return mode;
 }
 
-static void ssb_select_mitigation()
+static void ssb_select_mitigation(void)
 {
ssb_mode = __ssb_select_mitigation();
 
@@ -641,7 +641,7 @@ void x86_spec_ctrl_setup_ap(void)
 #ifdef CONFIG_SYSFS
 
 static ssize_t cpu_show_common(struct device *dev, struct device_attribute 
*attr,
-   char *buf, unsigned int bug)
+  char *buf, unsigned int bug)
 {
if (!boot_cpu_has_bug(bug))
return sprintf(buf, "Not affected\n");




Re: [Xen-devel][RFC 3/3] xen/gntdev: Add support for Linux dma buffers

2018-05-21 Thread Dongwon Kim
Still need more time to review the whole code changes but I noticed one thing.

We've been using the term "hyper_dmabuf" for hypervisor-agnostic linux dmabuf
solution and we are planning to call any of our future solution for other
hypervisors the same name. So having same name for this xen-specific structure
or functions you implemented is confusing. Would you change it to something
else like... "xen_"? 

On Thu, May 17, 2018 at 11:26:04AM +0300, Oleksandr Andrushchenko wrote:
> From: Oleksandr Andrushchenko 
> 
> Signed-off-by: Oleksandr Andrushchenko 
> ---
>  drivers/xen/gntdev.c  | 954 +-
>  include/uapi/xen/gntdev.h | 101 
>  include/xen/gntdev_exp.h  |  23 +
>  3 files changed, 1066 insertions(+), 12 deletions(-)
>  create mode 100644 include/xen/gntdev_exp.h
> 
> diff --git a/drivers/xen/gntdev.c b/drivers/xen/gntdev.c
> index 9510f228efe9..0ee88e193362 100644
> --- a/drivers/xen/gntdev.c
> +++ b/drivers/xen/gntdev.c
> @@ -4,6 +4,8 @@
>   * Device for accessing (in user-space) pages that have been granted by other
>   * domains.
>   *
> + * DMA buffer implementation is based on drivers/gpu/drm/drm_prime.c.
> + *
>   * Copyright (c) 2006-2007, D G Murray.
>   *   (c) 2009 Gerd Hoffmann 
>   *
> @@ -37,6 +39,9 @@
>  #include 
>  #include 
>  
> +#include 
> +#include 
> +
>  #include 
>  #include 
>  #include 
> @@ -61,16 +66,39 @@ static atomic_t pages_mapped = ATOMIC_INIT(0);
>  static int use_ptemod;
>  #define populate_freeable_maps use_ptemod
>  
> +#ifndef GRANT_INVALID_REF
> +/*
> + * Note on usage of grant reference 0 as invalid grant reference:
> + * grant reference 0 is valid, but never exposed to a driver,
> + * because of the fact it is already in use/reserved by the PV console.
> + */
> +#define GRANT_INVALID_REF0
> +#endif
> +
>  struct gntdev_priv {
>   /* maps with visible offsets in the file descriptor */
>   struct list_head maps;
>   /* maps that are not visible; will be freed on munmap.
>* Only populated if populate_freeable_maps == 1 */
>   struct list_head freeable_maps;
> + /* List of dma-bufs. */
> + struct list_head dma_bufs;
>   /* lock protects maps and freeable_maps */
>   struct mutex lock;
>   struct mm_struct *mm;
>   struct mmu_notifier mn;
> +
> + /* Private data of the hyper DMA buffers. */
> +
> + struct device *dev;
> + /* List of exported DMA buffers. */
> + struct list_head dmabuf_exp_list;
> + /* List of wait objects. */
> + struct list_head dmabuf_exp_wait_list;
> + /* List of imported DMA buffers. */
> + struct list_head dmabuf_imp_list;
> + /* This is the lock which protects dma_buf_xxx lists. */
> + struct mutex dmabuf_lock;
>  };
>  
>  struct unmap_notify {
> @@ -95,10 +123,65 @@ struct grant_map {
>   struct gnttab_unmap_grant_ref *kunmap_ops;
>   struct page **pages;
>   unsigned long pages_vm_start;
> +
> + /*
> +  * All the fields starting with dmabuf_ are only valid if this
> +  * mapping is used for exporting a DMA buffer.
> +  * If dmabuf_vaddr is not NULL then this mapping is backed by DMA
> +  * capable memory.
> +  */
> +
> + /* Flags used to create this DMA buffer: GNTDEV_DMABUF_FLAG_XXX. */
> + bool dmabuf_flags;
> + /* Virtual/CPU address of the DMA buffer. */
> + void *dmabuf_vaddr;
> + /* Bus address of the DMA buffer. */
> + dma_addr_t dmabuf_bus_addr;
> +};
> +
> +struct hyper_dmabuf {
> + struct gntdev_priv *priv;
> + struct dma_buf *dmabuf;
> + struct list_head next;
> + int fd;
> +
> + union {
> + struct {
> + /* Exported buffers are reference counted. */
> + struct kref refcount;
> + struct grant_map *map;
> + } exp;
> + struct {
> + /* Granted references of the imported buffer. */
> + grant_ref_t *refs;
> + /* Scatter-gather table of the imported buffer. */
> + struct sg_table *sgt;
> + /* dma-buf attachment of the imported buffer. */
> + struct dma_buf_attachment *attach;
> + } imp;
> + } u;
> +
> + /* Number of pages this buffer has. */
> + int nr_pages;
> + /* Pages of this buffer. */
> + struct page **pages;
> +};
> +
> +struct hyper_dmabuf_wait_obj {
> + struct list_head next;
> + struct hyper_dmabuf *hyper_dmabuf;
> + struct completion completion;
> +};
> +
> +struct hyper_dambuf_attachment {
minor typo: dam->dma (same thing in other places as well.)

> + struct sg_table *sgt;
> + enum dma_data_direction dir;
>  };
>  
>  static int unmap_grant_pages(struct grant_map *map, int offset, int pages);
>  
> +static struct miscdevice gntdev_miscdev;
> +
>  /* -- */
>  
>  static void g

[PATCH 4.16 091/110] x86/bugs: Make cpu_show_common() static

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Jiri Kosina 

commit 7bb4d366cba992904bffa4820d24e70a3de93e76 upstream

cpu_show_common() is not used outside of arch/x86/kernel/cpu/bugs.c, so
make it static.

Signed-off-by: Jiri Kosina 
Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/kernel/cpu/bugs.c |2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -640,7 +640,7 @@ void x86_spec_ctrl_setup_ap(void)
 
 #ifdef CONFIG_SYSFS
 
-ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
+static ssize_t cpu_show_common(struct device *dev, struct device_attribute 
*attr,
char *buf, unsigned int bug)
 {
if (!boot_cpu_has_bug(bug))




[PATCH 4.16 066/110] x86/bugs, KVM: Support the combination of guest and host IBRS

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Konrad Rzeszutek Wilk 

commit 5cf687548705412da47c9cec342fd952d71ed3d5 upstream

A guest may modify the SPEC_CTRL MSR from the value used by the
kernel. Since the kernel doesn't use IBRS, this means a value of zero is
what is needed in the host.

But the 336996-Speculative-Execution-Side-Channel-Mitigations.pdf refers to
the other bits as reserved so the kernel should respect the boot time
SPEC_CTRL value and use that.

This allows to deal with future extensions to the SPEC_CTRL interface if
any at all.

Note: This uses wrmsrl() instead of native_wrmsl(). I does not make any
difference as paravirt will over-write the callq *0xfff.. with the wrmsrl
assembler code.

Signed-off-by: Konrad Rzeszutek Wilk 
Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Ingo Molnar 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/nospec-branch.h |   10 ++
 arch/x86/kernel/cpu/bugs.c   |   18 ++
 arch/x86/kvm/svm.c   |6 ++
 arch/x86/kvm/vmx.c   |6 ++
 4 files changed, 32 insertions(+), 8 deletions(-)

--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -228,6 +228,16 @@ enum spectre_v2_mitigation {
 extern void x86_spec_ctrl_set(u64);
 extern u64 x86_spec_ctrl_get_default(void);
 
+/*
+ * On VMENTER we must preserve whatever view of the SPEC_CTRL MSR
+ * the guest has, while on VMEXIT we restore the host view. This
+ * would be easier if SPEC_CTRL were architecturally maskable or
+ * shadowable for guests but this is not (currently) the case.
+ * Takes the guest view of SPEC_CTRL MSR as a parameter.
+ */
+extern void x86_spec_ctrl_set_guest(u64);
+extern void x86_spec_ctrl_restore_host(u64);
+
 extern char __indirect_thunk_start[];
 extern char __indirect_thunk_end[];
 
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -123,6 +123,24 @@ u64 x86_spec_ctrl_get_default(void)
 }
 EXPORT_SYMBOL_GPL(x86_spec_ctrl_get_default);
 
+void x86_spec_ctrl_set_guest(u64 guest_spec_ctrl)
+{
+   if (!boot_cpu_has(X86_FEATURE_IBRS))
+   return;
+   if (x86_spec_ctrl_base != guest_spec_ctrl)
+   wrmsrl(MSR_IA32_SPEC_CTRL, guest_spec_ctrl);
+}
+EXPORT_SYMBOL_GPL(x86_spec_ctrl_set_guest);
+
+void x86_spec_ctrl_restore_host(u64 guest_spec_ctrl)
+{
+   if (!boot_cpu_has(X86_FEATURE_IBRS))
+   return;
+   if (x86_spec_ctrl_base != guest_spec_ctrl)
+   wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
+}
+EXPORT_SYMBOL_GPL(x86_spec_ctrl_restore_host);
+
 #ifdef RETPOLINE
 static bool spectre_v2_bad_module;
 
--- a/arch/x86/kvm/svm.c
+++ b/arch/x86/kvm/svm.c
@@ -5401,8 +5401,7 @@ static void svm_vcpu_run(struct kvm_vcpu
 * is no need to worry about the conditional branch over the wrmsr
 * being speculatively taken.
 */
-   if (svm->spec_ctrl)
-   native_wrmsrl(MSR_IA32_SPEC_CTRL, svm->spec_ctrl);
+   x86_spec_ctrl_set_guest(svm->spec_ctrl);
 
asm volatile (
"push %%" _ASM_BP "; \n\t"
@@ -5514,8 +5513,7 @@ static void svm_vcpu_run(struct kvm_vcpu
if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
 
-   if (svm->spec_ctrl)
-   native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
+   x86_spec_ctrl_restore_host(svm->spec_ctrl);
 
/* Eliminate branch target predictions from guest mode */
vmexit_fill_RSB();
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -9468,8 +9468,7 @@ static void __noclone vmx_vcpu_run(struc
 * is no need to worry about the conditional branch over the wrmsr
 * being speculatively taken.
 */
-   if (vmx->spec_ctrl)
-   native_wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
+   x86_spec_ctrl_set_guest(vmx->spec_ctrl);
 
vmx->__launched = vmx->loaded_vmcs->launched;
asm(
@@ -9607,8 +9606,7 @@ static void __noclone vmx_vcpu_run(struc
if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
 
-   if (vmx->spec_ctrl)
-   native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
+   x86_spec_ctrl_restore_host(vmx->spec_ctrl);
 
/* Eliminate branch target predictions from guest mode */
vmexit_fill_RSB();




[PATCH 4.16 093/110] x86/cpu: Make alternative_msr_write work for 32-bit code

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Jim Mattson 

commit 5f2b745f5e1304f438f9b2cd03ebc8120b6e0d3b upstream

Cast val and (val >> 32) to (u32), so that they fit in a
general-purpose register in both 32-bit and 64-bit code.

[ tglx: Made it u32 instead of uintptr_t ]

Fixes: c65732e4f721 ("x86/cpu: Restore CPUID_8000_0008_EBX reload")
Signed-off-by: Jim Mattson 
Signed-off-by: Konrad Rzeszutek Wilk 
Signed-off-by: Thomas Gleixner 
Acked-by: Linus Torvalds 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/nospec-branch.h |4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -265,8 +265,8 @@ void alternative_msr_write(unsigned int
 {
asm volatile(ALTERNATIVE("", "wrmsr", %c[feature])
: : "c" (msr),
-   "a" (val),
-   "d" (val >> 32),
+   "a" ((u32)val),
+   "d" ((u32)(val >> 32)),
[feature] "i" (feature)
: "memory");
 }




Re: [PATCH v7 2/2] leds: lm3601x: Introduce the lm3601x LED driver

2018-05-21 Thread Dan Murphy
Andy

Thanks for the review.  I was pretty sure I missed somethings.

On 05/21/2018 03:44 PM, Andy Shevchenko wrote:
> On Mon, May 21, 2018 at 9:09 PM, Dan Murphy  wrote:
>> Introduce the family of LED devices that can
>> drive a torch, strobe or IR LED.
>>
>> The LED driver can be configured with a strobe
>> timer to execute a strobe flash.  The IR LED
>> brightness is controlled via the torch brightness
>> register.
>>
>> The data sheet for each the LM36010 and LM36011
>> LED drivers can be found here:
>> http://www.ti.com/product/LM36010
>> http://www.ti.com/product/LM36011
> 
> Thanks for an update. My comments below.
> 
>> +config LEDS_LM3601X
>> +   tristate "LED support for LM3601x Chips"
>> +   depends on LEDS_CLASS && I2C && OF
> 
> Now OF here is superfluous.

This is the first one I missed.

ACK

> 
>> +   depends on LEDS_CLASS_FLASH
>> +   select REGMAP_I2C
>> +   help
>> + This option enables support for the TI LM3601x family
>> + of flash, torch and indicator classes.
> 
>> +#define LM3601X_TIMEOUT_MASK   0x1e
>> +#define LM3601X_ENABLE_MASK0x03
> 
> I dunno if GENMASK() will be better here.

I could probably make it more readable as well.

I am thinking

#define LM3601X_ENABLE_MASK (LM3601X_MODE_TORCH | LM3601X_MODE_IR_DRV)

> 
>> +#define LM3601X_LOWER_STEP_US  4
>> +#define LM3601X_UPPER_STEP_US  20
>> +#define LM3601X_MIN_TIMEOUT_US 4
>> +#define LM3601X_MAX_TIMEOUT_US 160
> 
>> +#define LM3601X_TIMEOUT_XOVER  40
> 
> Missed unit?

Ack

> 
>> +   ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG,
>> +   LM3601X_MODE_TORCH | LM3601X_MODE_IR_DRV,
>> +   led_mode_val);
> 
>> +   ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG,
>> +   LM3601X_MODE_TORCH | 
>> LM3601X_MODE_IR_DRV,
>> +   LM3601X_MODE_STROBE);
> 
> Perhaps
> #define ..._MODE_TORCH_WITH_IR   (..._TOCRH | ..._IR_DRV)
> ?
> 

This was defined already I just did not use the #define but I will probably
replace this with the LM3601X_ENABLE_MASK #define

>> +static int lm3601x_probe(struct i2c_client *client,
>> +   const struct i2c_device_id *id)
>> +{
>> +   struct lm3601x_led *led;
>> +   int err;
> 
>> +   err = lm3601x_parse_node(led, client->dev.of_node);
>> +   if (err < 0)
> 
>> +   return -ENODEV;
> 
> Shouldn't be
> 
> return err;

Ack.  But I might just get rid of err altogether when I make the change below to
return the status of the register.

> 
> ?
> 
>> +   err = lm3601x_register_leds(led);
>> +
>> +   return err;
> 
> I will leave this to maintainers since you seems have strong
> objections against more or less standard pattern, i.e.
> 
> return lm3601x_register_leds();

ACk.  I was going to change that but I was concerned about loosing err.
But I can change it.

> 
>> +}
> 


-- 
--
Dan Murphy


[PATCH 4.16 067/110] x86/bugs: Expose /sys/../spec_store_bypass

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Konrad Rzeszutek Wilk 

commit c456442cd3a59eeb1d60293c26cbe2ff2c4e42cf upstream

Add the sysfs file for the new vulerability. It does not do much except
show the words 'Vulnerable' for recent x86 cores.

Intel cores prior to family 6 are known not to be vulnerable, and so are
some Atoms and some Xeon Phi.

It assumes that older Cyrix, Centaur, etc. cores are immune.

Signed-off-by: Konrad Rzeszutek Wilk 
Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Ingo Molnar 
Signed-off-by: Greg Kroah-Hartman 
---
 Documentation/ABI/testing/sysfs-devices-system-cpu |1 
 arch/x86/include/asm/cpufeatures.h |1 
 arch/x86/kernel/cpu/bugs.c |5 
 arch/x86/kernel/cpu/common.c   |   23 +
 drivers/base/cpu.c |8 +++
 include/linux/cpu.h|2 +
 6 files changed, 40 insertions(+)

--- a/Documentation/ABI/testing/sysfs-devices-system-cpu
+++ b/Documentation/ABI/testing/sysfs-devices-system-cpu
@@ -453,6 +453,7 @@ What:   /sys/devices/system/cpu/vulnerabi
/sys/devices/system/cpu/vulnerabilities/meltdown
/sys/devices/system/cpu/vulnerabilities/spectre_v1
/sys/devices/system/cpu/vulnerabilities/spectre_v2
+   /sys/devices/system/cpu/vulnerabilities/spec_store_bypass
 Date:  January 2018
 Contact:   Linux kernel mailing list 
 Description:   Information about CPU vulnerabilities
--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -362,5 +362,6 @@
 #define X86_BUG_CPU_MELTDOWN   X86_BUG(14) /* CPU is affected by 
meltdown attack and needs kernel page table isolation */
 #define X86_BUG_SPECTRE_V1 X86_BUG(15) /* CPU is affected by 
Spectre variant 1 attack with conditional branches */
 #define X86_BUG_SPECTRE_V2 X86_BUG(16) /* CPU is affected by 
Spectre variant 2 attack with indirect branches */
+#define X86_BUG_SPEC_STORE_BYPASS  X86_BUG(17) /* CPU is affected by 
speculative store bypass attack */
 
 #endif /* _ASM_X86_CPUFEATURES_H */
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -404,4 +404,9 @@ ssize_t cpu_show_spectre_v2(struct devic
 {
return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
 }
+
+ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute 
*attr, char *buf)
+{
+   return cpu_show_common(dev, attr, buf, X86_BUG_SPEC_STORE_BYPASS);
+}
 #endif
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -918,10 +918,33 @@ static const __initconst struct x86_cpu_
{}
 };
 
+static const __initconst struct x86_cpu_id cpu_no_spec_store_bypass[] = {
+   { X86_VENDOR_INTEL, 6,  INTEL_FAM6_ATOM_PINEVIEW},
+   { X86_VENDOR_INTEL, 6,  INTEL_FAM6_ATOM_LINCROFT},
+   { X86_VENDOR_INTEL, 6,  INTEL_FAM6_ATOM_PENWELL },
+   { X86_VENDOR_INTEL, 6,  INTEL_FAM6_ATOM_CLOVERVIEW  },
+   { X86_VENDOR_INTEL, 6,  INTEL_FAM6_ATOM_CEDARVIEW   },
+   { X86_VENDOR_INTEL, 6,  INTEL_FAM6_ATOM_SILVERMONT1 },
+   { X86_VENDOR_INTEL, 6,  INTEL_FAM6_ATOM_AIRMONT },
+   { X86_VENDOR_INTEL, 6,  INTEL_FAM6_ATOM_SILVERMONT2 },
+   { X86_VENDOR_INTEL, 6,  INTEL_FAM6_ATOM_MERRIFIELD  },
+   { X86_VENDOR_INTEL, 6,  INTEL_FAM6_CORE_YONAH   },
+   { X86_VENDOR_INTEL, 6,  INTEL_FAM6_XEON_PHI_KNL },
+   { X86_VENDOR_INTEL, 6,  INTEL_FAM6_XEON_PHI_KNM },
+   { X86_VENDOR_CENTAUR,   5,  },
+   { X86_VENDOR_INTEL, 5,  },
+   { X86_VENDOR_NSC,   5,  },
+   { X86_VENDOR_ANY,   4,  },
+   {}
+};
+
 static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
 {
u64 ia32_cap = 0;
 
+   if (!x86_match_cpu(cpu_no_spec_store_bypass))
+   setup_force_cpu_bug(X86_BUG_SPEC_STORE_BYPASS);
+
if (x86_match_cpu(cpu_no_speculation))
return;
 
--- a/drivers/base/cpu.c
+++ b/drivers/base/cpu.c
@@ -532,14 +532,22 @@ ssize_t __weak cpu_show_spectre_v2(struc
return sprintf(buf, "Not affected\n");
 }
 
+ssize_t __weak cpu_show_spec_store_bypass(struct device *dev,
+ struct device_attribute *attr, char 
*buf)
+{
+   return sprintf(buf, "Not affected\n");
+}
+
 static DEVICE_ATTR(meltdown, 0444, cpu_show_meltdown, NULL);
 static DEVICE_ATTR(spectre_v1, 0444, cpu_show_spectre_v1, NULL);
 static DEVICE_ATTR(spectre_v2, 0444, cpu_show_spectre_v2, NULL);
+static DEVICE_ATTR(spec_s

[PATCH 4.16 094/110] KVM: SVM: Move spec control call after restore of GS

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit 15e6c22fd8e5a42c5ed6d487b7c9fe44c2517765 upstream

svm_vcpu_run() invokes x86_spec_ctrl_restore_host() after VMEXIT, but
before the host GS is restored. x86_spec_ctrl_restore_host() uses 'current'
to determine the host SSBD state of the thread. 'current' is GS based, but
host GS is not yet restored and the access causes a triple fault.

Move the call after the host GS restore.

Fixes: 885f82bfbc6f x86/process: Allow runtime control of Speculative Store 
Bypass
Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Konrad Rzeszutek Wilk 
Acked-by: Paolo Bonzini 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/kvm/svm.c |   24 
 1 file changed, 12 insertions(+), 12 deletions(-)

--- a/arch/x86/kvm/svm.c
+++ b/arch/x86/kvm/svm.c
@@ -5495,6 +5495,18 @@ static void svm_vcpu_run(struct kvm_vcpu
 #endif
);
 
+   /* Eliminate branch target predictions from guest mode */
+   vmexit_fill_RSB();
+
+#ifdef CONFIG_X86_64
+   wrmsrl(MSR_GS_BASE, svm->host.gs_base);
+#else
+   loadsegment(fs, svm->host.fs);
+#ifndef CONFIG_X86_32_LAZY_GS
+   loadsegment(gs, svm->host.gs);
+#endif
+#endif
+
/*
 * We do not use IBRS in the kernel. If this vCPU has used the
 * SPEC_CTRL MSR it may have left it on; save the value and
@@ -5515,18 +5527,6 @@ static void svm_vcpu_run(struct kvm_vcpu
 
x86_spec_ctrl_restore_host(svm->spec_ctrl);
 
-   /* Eliminate branch target predictions from guest mode */
-   vmexit_fill_RSB();
-
-#ifdef CONFIG_X86_64
-   wrmsrl(MSR_GS_BASE, svm->host.gs_base);
-#else
-   loadsegment(fs, svm->host.fs);
-#ifndef CONFIG_X86_32_LAZY_GS
-   loadsegment(gs, svm->host.gs);
-#endif
-#endif
-
reload_tss(vcpu);
 
local_irq_disable();




[PATCH 4.16 096/110] x86/cpufeatures: Disentangle MSR_SPEC_CTRL enumeration from IBRS

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit 7eb8956a7fec3c1f0abc2a5517dada99ccc8a961 upstream

The availability of the SPEC_CTRL MSR is enumerated by a CPUID bit on
Intel and implied by IBRS or STIBP support on AMD. That's just confusing
and in case an AMD CPU has IBRS not supported because the underlying
problem has been fixed but has another bit valid in the SPEC_CTRL MSR,
the thing falls apart.

Add a synthetic feature bit X86_FEATURE_MSR_SPEC_CTRL to denote the
availability on both Intel and AMD.

While at it replace the boot_cpu_has() checks with static_cpu_has() where
possible. This prevents late microcode loading from exposing SPEC_CTRL, but
late loading is already very limited as it does not reevaluate the
mitigation options and other bits and pieces. Having static_cpu_has() is
the simplest and least fragile solution.

Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Konrad Rzeszutek Wilk 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/cpufeatures.h |1 +
 arch/x86/kernel/cpu/bugs.c |   18 +++---
 arch/x86/kernel/cpu/common.c   |9 +++--
 arch/x86/kernel/cpu/intel.c|1 +
 4 files changed, 20 insertions(+), 9 deletions(-)

--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -206,6 +206,7 @@
 #define X86_FEATURE_RETPOLINE_AMD  ( 7*32+13) /* "" AMD Retpoline 
mitigation for Spectre variant 2 */
 #define X86_FEATURE_INTEL_PPIN ( 7*32+14) /* Intel Processor Inventory 
Number */
 #define X86_FEATURE_CDP_L2 ( 7*32+15) /* Code and Data 
Prioritization L2 */
+#define X86_FEATURE_MSR_SPEC_CTRL  ( 7*32+16) /* "" MSR SPEC_CTRL is 
implemented */
 
 #define X86_FEATURE_MBA( 7*32+18) /* Memory Bandwidth 
Allocation */
 #define X86_FEATURE_RSB_CTXSW  ( 7*32+19) /* "" Fill RSB on context 
switches */
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -64,7 +64,7 @@ void __init check_bugs(void)
 * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
 * init code as it is not enumerated and depends on the family.
 */
-   if (boot_cpu_has(X86_FEATURE_IBRS))
+   if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
 
/* Select the proper spectre mitigation before patching alternatives */
@@ -145,7 +145,7 @@ u64 x86_spec_ctrl_get_default(void)
 {
u64 msrval = x86_spec_ctrl_base;
 
-   if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
+   if (static_cpu_has(X86_FEATURE_SPEC_CTRL))
msrval |= ssbd_tif_to_spec_ctrl(current_thread_info()->flags);
return msrval;
 }
@@ -155,10 +155,12 @@ void x86_spec_ctrl_set_guest(u64 guest_s
 {
u64 host = x86_spec_ctrl_base;
 
-   if (!boot_cpu_has(X86_FEATURE_IBRS))
+   /* Is MSR_SPEC_CTRL implemented ? */
+   if (!static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
return;
 
-   if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
+   /* Intel controls SSB in MSR_SPEC_CTRL */
+   if (static_cpu_has(X86_FEATURE_SPEC_CTRL))
host |= ssbd_tif_to_spec_ctrl(current_thread_info()->flags);
 
if (host != guest_spec_ctrl)
@@ -170,10 +172,12 @@ void x86_spec_ctrl_restore_host(u64 gues
 {
u64 host = x86_spec_ctrl_base;
 
-   if (!boot_cpu_has(X86_FEATURE_IBRS))
+   /* Is MSR_SPEC_CTRL implemented ? */
+   if (!static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
return;
 
-   if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
+   /* Intel controls SSB in MSR_SPEC_CTRL */
+   if (static_cpu_has(X86_FEATURE_SPEC_CTRL))
host |= ssbd_tif_to_spec_ctrl(current_thread_info()->flags);
 
if (host != guest_spec_ctrl)
@@ -631,7 +635,7 @@ int arch_prctl_spec_ctrl_get(struct task
 
 void x86_spec_ctrl_setup_ap(void)
 {
-   if (boot_cpu_has(X86_FEATURE_IBRS))
+   if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
x86_spec_ctrl_set(x86_spec_ctrl_base & ~x86_spec_ctrl_mask);
 
if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -761,19 +761,24 @@ static void init_speculation_control(str
if (cpu_has(c, X86_FEATURE_SPEC_CTRL)) {
set_cpu_cap(c, X86_FEATURE_IBRS);
set_cpu_cap(c, X86_FEATURE_IBPB);
+   set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
}
 
if (cpu_has(c, X86_FEATURE_INTEL_STIBP))
set_cpu_cap(c, X86_FEATURE_STIBP);
 
-   if (cpu_has(c, X86_FEATURE_AMD_IBRS))
+   if (cpu_has(c, X86_FEATURE_AMD_IBRS)) {
set_cpu_cap(c, X86_FEATURE_IBRS);
+   set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
+   }
 
if (cpu_has(c, X86_FEATURE_AMD_IBPB))
set_cpu_cap(c,

[PATCH 4.16 069/110] x86/bugs: Provide boot parameters for the spec_store_bypass_disable mitigation

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Konrad Rzeszutek Wilk 

commit 24f7fc83b9204d20f878c57cb77d261ae825e033 upstream

Contemporary high performance processors use a common industry-wide
optimization known as "Speculative Store Bypass" in which loads from
addresses to which a recent store has occurred may (speculatively) see an
older value. Intel refers to this feature as "Memory Disambiguation" which
is part of their "Smart Memory Access" capability.

Memory Disambiguation can expose a cache side-channel attack against such
speculatively read values. An attacker can create exploit code that allows
them to read memory outside of a sandbox environment (for example,
malicious JavaScript in a web page), or to perform more complex attacks
against code running within the same privilege level, e.g. via the stack.

As a first step to mitigate against such attacks, provide two boot command
line control knobs:

 nospec_store_bypass_disable
 spec_store_bypass_disable=[off,auto,on]

By default affected x86 processors will power on with Speculative
Store Bypass enabled. Hence the provided kernel parameters are written
from the point of view of whether to enable a mitigation or not.
The parameters are as follows:

 - auto - Kernel detects whether your CPU model contains an implementation
  of Speculative Store Bypass and picks the most appropriate
  mitigation.

 - on   - disable Speculative Store Bypass
 - off  - enable Speculative Store Bypass

[ tglx: Reordered the checks so that the whole evaluation is not done
when the CPU does not support RDS ]

Signed-off-by: Konrad Rzeszutek Wilk 
Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Ingo Molnar 
Signed-off-by: Greg Kroah-Hartman 
---
 Documentation/admin-guide/kernel-parameters.txt |   33 +++
 arch/x86/include/asm/cpufeatures.h  |1 
 arch/x86/include/asm/nospec-branch.h|6 +
 arch/x86/kernel/cpu/bugs.c  |  103 
 4 files changed, 143 insertions(+)

--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -2647,6 +2647,9 @@
allow data leaks with this option, which is equivalent
to spectre_v2=off.
 
+   nospec_store_bypass_disable
+   [HW] Disable all mitigations for the Speculative Store 
Bypass vulnerability
+
noxsave [BUGS=X86] Disables x86 extended register state save
and restore using xsave. The kernel will fallback to
enabling legacy floating-point and sse state.
@@ -3997,6 +4000,36 @@
Not specifying this option is equivalent to
spectre_v2=auto.
 
+   spec_store_bypass_disable=
+   [HW] Control Speculative Store Bypass (SSB) Disable 
mitigation
+   (Speculative Store Bypass vulnerability)
+
+   Certain CPUs are vulnerable to an exploit against a
+   a common industry wide performance optimization known
+   as "Speculative Store Bypass" in which recent stores
+   to the same memory location may not be observed by
+   later loads during speculative execution. The idea
+   is that such stores are unlikely and that they can
+   be detected prior to instruction retirement at the
+   end of a particular speculation execution window.
+
+   In vulnerable processors, the speculatively forwarded
+   store can be used in a cache side channel attack, for
+   example to read memory to which the attacker does not
+   directly have access (e.g. inside sandboxed code).
+
+   This parameter controls whether the Speculative Store
+   Bypass optimization is used.
+
+   on - Unconditionally disable Speculative Store 
Bypass
+   off- Unconditionally enable Speculative Store Bypass
+   auto   - Kernel detects whether the CPU model contains 
an
+implementation of Speculative Store Bypass and
+picks the most appropriate mitigation
+
+   Not specifying this option is equivalent to
+   spec_store_bypass_disable=auto.
+
spia_io_base=   [HW,MTD]
spia_fio_base=
spia_pedr=
--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -214,6 +214,7 @@
 
 #define X86_FEATURE_USE_IBPB   ( 7*32+21) /* "" Indirect Branch 
Prediction Barrier enabled */
 #define X86_FEATURE_USE_IBRS_FW( 7*32+22) /* "" Use IBRS 
durin

[PATCH 4.16 063/110] x86/bugs: Concentrate bug detection into a separate function

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Konrad Rzeszutek Wilk 

commit 4a28bfe3267b68e22c663ac26185aa16c9b879ef upstream

Combine the various logic which goes through all those
x86_cpu_id matching structures in one function.

Suggested-by: Borislav Petkov 
Signed-off-by: Konrad Rzeszutek Wilk 
Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Ingo Molnar 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/kernel/cpu/common.c |   21 +++--
 1 file changed, 11 insertions(+), 10 deletions(-)

--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -918,21 +918,27 @@ static const __initconst struct x86_cpu_
{}
 };
 
-static bool __init cpu_vulnerable_to_meltdown(struct cpuinfo_x86 *c)
+static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
 {
u64 ia32_cap = 0;
 
+   if (x86_match_cpu(cpu_no_speculation))
+   return;
+
+   setup_force_cpu_bug(X86_BUG_SPECTRE_V1);
+   setup_force_cpu_bug(X86_BUG_SPECTRE_V2);
+
if (x86_match_cpu(cpu_no_meltdown))
-   return false;
+   return;
 
if (cpu_has(c, X86_FEATURE_ARCH_CAPABILITIES))
rdmsrl(MSR_IA32_ARCH_CAPABILITIES, ia32_cap);
 
/* Rogue Data Cache Load? No! */
if (ia32_cap & ARCH_CAP_RDCL_NO)
-   return false;
+   return;
 
-   return true;
+   setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN);
 }
 
 /*
@@ -982,12 +988,7 @@ static void __init early_identify_cpu(st
 
setup_force_cpu_cap(X86_FEATURE_ALWAYS);
 
-   if (!x86_match_cpu(cpu_no_speculation)) {
-   if (cpu_vulnerable_to_meltdown(c))
-   setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN);
-   setup_force_cpu_bug(X86_BUG_SPECTRE_V1);
-   setup_force_cpu_bug(X86_BUG_SPECTRE_V2);
-   }
+   cpu_set_bug_bits(c);
 
fpu__init_system(c);
 




[PATCH 4.16 070/110] x86/bugs/intel: Set proper CPU features and setup RDS

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Konrad Rzeszutek Wilk 

commit 772439717dbf703b39990be58d8d4e3e4ad0598a upstream

Intel CPUs expose methods to:

 - Detect whether RDS capability is available via CPUID.7.0.EDX[31],

 - The SPEC_CTRL MSR(0x48), bit 2 set to enable RDS.

 - MSR_IA32_ARCH_CAPABILITIES, Bit(4) no need to enable RRS.

With that in mind if spec_store_bypass_disable=[auto,on] is selected set at
boot-time the SPEC_CTRL MSR to enable RDS if the platform requires it.

Note that this does not fix the KVM case where the SPEC_CTRL is exposed to
guests which can muck with it, see patch titled :
 KVM/SVM/VMX/x86/spectre_v2: Support the combination of guest and host IBRS.

And for the firmware (IBRS to be set), see patch titled:
 x86/spectre_v2: Read SPEC_CTRL MSR during boot and re-use reserved bits

[ tglx: Distangled it from the intel implementation and kept the call order ]

Signed-off-by: Konrad Rzeszutek Wilk 
Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Ingo Molnar 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/msr-index.h |6 ++
 arch/x86/kernel/cpu/bugs.c   |   30 --
 arch/x86/kernel/cpu/common.c |   10 ++
 arch/x86/kernel/cpu/cpu.h|2 ++
 arch/x86/kernel/cpu/intel.c  |1 +
 5 files changed, 43 insertions(+), 6 deletions(-)

--- a/arch/x86/include/asm/msr-index.h
+++ b/arch/x86/include/asm/msr-index.h
@@ -42,6 +42,7 @@
 #define MSR_IA32_SPEC_CTRL 0x0048 /* Speculation Control */
 #define SPEC_CTRL_IBRS (1 << 0)   /* Indirect Branch 
Restricted Speculation */
 #define SPEC_CTRL_STIBP(1 << 1)   /* Single Thread 
Indirect Branch Predictors */
+#define SPEC_CTRL_RDS  (1 << 2)   /* Reduced Data Speculation 
*/
 
 #define MSR_IA32_PRED_CMD  0x0049 /* Prediction Command */
 #define PRED_CMD_IBPB  (1 << 0)   /* Indirect Branch 
Prediction Barrier */
@@ -68,6 +69,11 @@
 #define MSR_IA32_ARCH_CAPABILITIES 0x010a
 #define ARCH_CAP_RDCL_NO   (1 << 0)   /* Not susceptible to 
Meltdown */
 #define ARCH_CAP_IBRS_ALL  (1 << 1)   /* Enhanced IBRS support */
+#define ARCH_CAP_RDS_NO(1 << 4)   /*
+   * Not susceptible to 
Speculative Store Bypass
+   * attack, so no Reduced 
Data Speculation control
+   * required.
+   */
 
 #define MSR_IA32_BBL_CR_CTL0x0119
 #define MSR_IA32_BBL_CR_CTL3   0x011e
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -117,7 +117,7 @@ static enum spectre_v2_mitigation spectr
 
 void x86_spec_ctrl_set(u64 val)
 {
-   if (val & ~SPEC_CTRL_IBRS)
+   if (val & ~(SPEC_CTRL_IBRS | SPEC_CTRL_RDS))
WARN_ONCE(1, "SPEC_CTRL MSR value 0x%16llx is unknown.\n", val);
else
wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base | val);
@@ -444,8 +444,28 @@ static enum ssb_mitigation_cmd __init __
break;
}
 
-   if (mode != SPEC_STORE_BYPASS_NONE)
+   /*
+* We have three CPU feature flags that are in play here:
+*  - X86_BUG_SPEC_STORE_BYPASS - CPU is susceptible.
+*  - X86_FEATURE_RDS - CPU is able to turn off speculative store bypass
+*  - X86_FEATURE_SPEC_STORE_BYPASS_DISABLE - engage the mitigation
+*/
+   if (mode != SPEC_STORE_BYPASS_NONE) {
setup_force_cpu_cap(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE);
+   /*
+* Intel uses the SPEC CTRL MSR Bit(2) for this, while AMD uses
+* a completely different MSR and bit dependent on family.
+*/
+   switch (boot_cpu_data.x86_vendor) {
+   case X86_VENDOR_INTEL:
+   x86_spec_ctrl_base |= SPEC_CTRL_RDS;
+   x86_spec_ctrl_set(SPEC_CTRL_RDS);
+   break;
+   case X86_VENDOR_AMD:
+   break;
+   }
+   }
+
return mode;
 }
 
@@ -459,6 +479,12 @@ static void ssb_select_mitigation()
 
 #undef pr_fmt
 
+void x86_spec_ctrl_setup_ap(void)
+{
+   if (boot_cpu_has(X86_FEATURE_IBRS))
+   x86_spec_ctrl_set(x86_spec_ctrl_base & (SPEC_CTRL_IBRS | 
SPEC_CTRL_RDS));
+}
+
 #ifdef CONFIG_SYSFS
 
 ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -942,7 +942,11 @@ static void __init cpu_set_bug_bits(stru
 {
u64 ia32_cap = 0;
 
-   if (!x86_match_cpu(cpu_no_spec_store_bypass))
+   if (cpu_has(c, X86_FEATURE_ARCH_CAPABILITIES))
+   rdmsrl(MSR_IA32_ARC

[PATCH 4.16 098/110] x86/cpufeatures: Add FEATURE_ZEN

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit d1035d971829dcf80e8686ccde26f94b0a069472 upstream

Add a ZEN feature bit so family-dependent static_cpu_has() optimizations
can be built for ZEN.

Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Konrad Rzeszutek Wilk 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/cpufeatures.h |1 +
 arch/x86/kernel/cpu/amd.c  |1 +
 2 files changed, 2 insertions(+)

--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -218,6 +218,7 @@
 #define X86_FEATURE_IBRS   ( 7*32+25) /* Indirect Branch 
Restricted Speculation */
 #define X86_FEATURE_IBPB   ( 7*32+26) /* Indirect Branch 
Prediction Barrier */
 #define X86_FEATURE_STIBP  ( 7*32+27) /* Single Thread Indirect 
Branch Predictors */
+#define X86_FEATURE_ZEN( 7*32+28) /* "" CPU is AMD 
family 0x17 (Zen) */
 
 /* Virtualization flags: Linux defined, word 8 */
 #define X86_FEATURE_TPR_SHADOW ( 8*32+ 0) /* Intel TPR Shadow */
--- a/arch/x86/kernel/cpu/amd.c
+++ b/arch/x86/kernel/cpu/amd.c
@@ -812,6 +812,7 @@ static void init_amd_bd(struct cpuinfo_x
 
 static void init_amd_zn(struct cpuinfo_x86 *c)
 {
+   set_cpu_cap(c, X86_FEATURE_ZEN);
/*
 * Fix erratum 1076: CPB feature bit not being set in CPUID. It affects
 * all up to and including B1.




[PATCH 4.16 071/110] x86/bugs: Whitelist allowed SPEC_CTRL MSR values

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Konrad Rzeszutek Wilk 

commit 1115a859f33276fe8afb31c60cf9d8e657872558 upstream

Intel and AMD SPEC_CTRL (0x48) MSR semantics may differ in the
future (or in fact use different MSRs for the same functionality).

As such a run-time mechanism is required to whitelist the appropriate MSR
values.

[ tglx: Made the variable __ro_after_init ]

Signed-off-by: Konrad Rzeszutek Wilk 
Signed-off-by: Thomas Gleixner 
Reviewed-by: Ingo Molnar 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/kernel/cpu/bugs.c |   11 +--
 1 file changed, 9 insertions(+), 2 deletions(-)

--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -35,6 +35,12 @@ static void __init ssb_select_mitigation
  */
 static u64 __ro_after_init x86_spec_ctrl_base;
 
+/*
+ * The vendor and possibly platform specific bits which can be modified in
+ * x86_spec_ctrl_base.
+ */
+static u64 __ro_after_init x86_spec_ctrl_mask = ~SPEC_CTRL_IBRS;
+
 void __init check_bugs(void)
 {
identify_boot_cpu();
@@ -117,7 +123,7 @@ static enum spectre_v2_mitigation spectr
 
 void x86_spec_ctrl_set(u64 val)
 {
-   if (val & ~(SPEC_CTRL_IBRS | SPEC_CTRL_RDS))
+   if (val & x86_spec_ctrl_mask)
WARN_ONCE(1, "SPEC_CTRL MSR value 0x%16llx is unknown.\n", val);
else
wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base | val);
@@ -459,6 +465,7 @@ static enum ssb_mitigation_cmd __init __
switch (boot_cpu_data.x86_vendor) {
case X86_VENDOR_INTEL:
x86_spec_ctrl_base |= SPEC_CTRL_RDS;
+   x86_spec_ctrl_mask &= ~SPEC_CTRL_RDS;
x86_spec_ctrl_set(SPEC_CTRL_RDS);
break;
case X86_VENDOR_AMD:
@@ -482,7 +489,7 @@ static void ssb_select_mitigation()
 void x86_spec_ctrl_setup_ap(void)
 {
if (boot_cpu_has(X86_FEATURE_IBRS))
-   x86_spec_ctrl_set(x86_spec_ctrl_base & (SPEC_CTRL_IBRS | 
SPEC_CTRL_RDS));
+   x86_spec_ctrl_set(x86_spec_ctrl_base & ~x86_spec_ctrl_mask);
 }
 
 #ifdef CONFIG_SYSFS




[PATCH 4.16 109/110] x86/bugs: Rename SSBD_NO to SSB_NO

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Konrad Rzeszutek Wilk 

commit 240da953fcc6a9008c92fae5b1f727ee5ed167ab upstream

The "336996 Speculative Execution Side Channel Mitigations" from
May defines this as SSB_NO, hence lets sync-up.

Signed-off-by: Konrad Rzeszutek Wilk 
Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/msr-index.h |2 +-
 arch/x86/kernel/cpu/common.c |2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

--- a/arch/x86/include/asm/msr-index.h
+++ b/arch/x86/include/asm/msr-index.h
@@ -70,7 +70,7 @@
 #define MSR_IA32_ARCH_CAPABILITIES 0x010a
 #define ARCH_CAP_RDCL_NO   (1 << 0)   /* Not susceptible to 
Meltdown */
 #define ARCH_CAP_IBRS_ALL  (1 << 1)   /* Enhanced IBRS support */
-#define ARCH_CAP_SSBD_NO   (1 << 4)   /*
+#define ARCH_CAP_SSB_NO(1 << 4)   /*
* Not susceptible to 
Speculative Store Bypass
* attack, so no Speculative 
Store Bypass
* control required.
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -965,7 +965,7 @@ static void __init cpu_set_bug_bits(stru
rdmsrl(MSR_IA32_ARCH_CAPABILITIES, ia32_cap);
 
if (!x86_match_cpu(cpu_no_spec_store_bypass) &&
-  !(ia32_cap & ARCH_CAP_SSBD_NO))
+  !(ia32_cap & ARCH_CAP_SSB_NO))
setup_force_cpu_bug(X86_BUG_SPEC_STORE_BYPASS);
 
if (x86_match_cpu(cpu_no_speculation))




[PATCH 4.16 107/110] x86/speculation, KVM: Implement support for VIRT_SPEC_CTRL/LS_CFG

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit 47c61b3955cf712cadfc25635bf9bc174af030ea upstream

Add the necessary logic for supporting the emulated VIRT_SPEC_CTRL MSR to
x86_virt_spec_ctrl().  If either X86_FEATURE_LS_CFG_SSBD or
X86_FEATURE_VIRT_SPEC_CTRL is set then use the new guest_virt_spec_ctrl
argument to check whether the state must be modified on the host. The
update reuses speculative_store_bypass_update() so the ZEN-specific sibling
coordination can be reused.

Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/spec-ctrl.h |6 ++
 arch/x86/kernel/cpu/bugs.c   |   30 ++
 2 files changed, 36 insertions(+)

--- a/arch/x86/include/asm/spec-ctrl.h
+++ b/arch/x86/include/asm/spec-ctrl.h
@@ -53,6 +53,12 @@ static inline u64 ssbd_tif_to_spec_ctrl(
return (tifn & _TIF_SSBD) >> (TIF_SSBD - SPEC_CTRL_SSBD_SHIFT);
 }
 
+static inline unsigned long ssbd_spec_ctrl_to_tif(u64 spec_ctrl)
+{
+   BUILD_BUG_ON(TIF_SSBD < SPEC_CTRL_SSBD_SHIFT);
+   return (spec_ctrl & SPEC_CTRL_SSBD) << (TIF_SSBD - 
SPEC_CTRL_SSBD_SHIFT);
+}
+
 static inline u64 ssbd_tif_to_amd_ls_cfg(u64 tifn)
 {
return (tifn & _TIF_SSBD) ? x86_amd_ls_cfg_ssbd_mask : 0ULL;
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -162,6 +162,36 @@ x86_virt_spec_ctrl(u64 guest_spec_ctrl,
wrmsrl(MSR_IA32_SPEC_CTRL, msrval);
}
}
+
+   /*
+* If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
+* MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
+*/
+   if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
+   !static_cpu_has(X86_FEATURE_VIRT_SSBD))
+   return;
+
+   /*
+* If the host has SSBD mitigation enabled, force it in the host's
+* virtual MSR value. If its not permanently enabled, evaluate
+* current's TIF_SSBD thread flag.
+*/
+   if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE))
+   hostval = SPEC_CTRL_SSBD;
+   else
+   hostval = ssbd_tif_to_spec_ctrl(ti->flags);
+
+   /* Sanitize the guest value */
+   guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD;
+
+   if (hostval != guestval) {
+   unsigned long tif;
+
+   tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) :
+ssbd_spec_ctrl_to_tif(hostval);
+
+   speculative_store_bypass_update(tif);
+   }
 }
 EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl);
 




[PATCH 4.16 099/110] x86/speculation: Handle HT correctly on AMD

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit 1f50ddb4f4189243c05926b842dc1a0332195f31 upstream

The AMD64_LS_CFG MSR is a per core MSR on Family 17H CPUs. That means when
hyperthreading is enabled the SSBD bit toggle needs to take both cores into
account. Otherwise the following situation can happen:

CPU0CPU1

disable SSB
disable SSB
enable  SSB <- Enables it for the Core, i.e. for CPU0 as well

So after the SSB enable on CPU1 the task on CPU0 runs with SSB enabled
again.

On Intel the SSBD control is per core as well, but the synchronization
logic is implemented behind the per thread SPEC_CTRL MSR. It works like
this:

  CORE_SPEC_CTRL = THREAD0_SPEC_CTRL | THREAD1_SPEC_CTRL

i.e. if one of the threads enables a mitigation then this affects both and
the mitigation is only disabled in the core when both threads disabled it.

Add the necessary synchronization logic for AMD family 17H. Unfortunately
that requires a spinlock to serialize the access to the MSR, but the locks
are only shared between siblings.

Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Konrad Rzeszutek Wilk 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/spec-ctrl.h |6 +
 arch/x86/kernel/process.c|  125 +--
 arch/x86/kernel/smpboot.c|5 +
 3 files changed, 130 insertions(+), 6 deletions(-)

--- a/arch/x86/include/asm/spec-ctrl.h
+++ b/arch/x86/include/asm/spec-ctrl.h
@@ -33,6 +33,12 @@ static inline u64 ssbd_tif_to_amd_ls_cfg
return (tifn & _TIF_SSBD) ? x86_amd_ls_cfg_ssbd_mask : 0ULL;
 }
 
+#ifdef CONFIG_SMP
+extern void speculative_store_bypass_ht_init(void);
+#else
+static inline void speculative_store_bypass_ht_init(void) { }
+#endif
+
 extern void speculative_store_bypass_update(void);
 
 #endif
--- a/arch/x86/kernel/process.c
+++ b/arch/x86/kernel/process.c
@@ -279,22 +279,135 @@ static inline void switch_to_bitmap(stru
}
 }
 
-static __always_inline void __speculative_store_bypass_update(unsigned long 
tifn)
+#ifdef CONFIG_SMP
+
+struct ssb_state {
+   struct ssb_state*shared_state;
+   raw_spinlock_t  lock;
+   unsigned intdisable_state;
+   unsigned long   local_state;
+};
+
+#define LSTATE_SSB 0
+
+static DEFINE_PER_CPU(struct ssb_state, ssb_state);
+
+void speculative_store_bypass_ht_init(void)
+{
+   struct ssb_state *st = this_cpu_ptr(&ssb_state);
+   unsigned int this_cpu = smp_processor_id();
+   unsigned int cpu;
+
+   st->local_state = 0;
+
+   /*
+* Shared state setup happens once on the first bringup
+* of the CPU. It's not destroyed on CPU hotunplug.
+*/
+   if (st->shared_state)
+   return;
+
+   raw_spin_lock_init(&st->lock);
+
+   /*
+* Go over HT siblings and check whether one of them has set up the
+* shared state pointer already.
+*/
+   for_each_cpu(cpu, topology_sibling_cpumask(this_cpu)) {
+   if (cpu == this_cpu)
+   continue;
+
+   if (!per_cpu(ssb_state, cpu).shared_state)
+   continue;
+
+   /* Link it to the state of the sibling: */
+   st->shared_state = per_cpu(ssb_state, cpu).shared_state;
+   return;
+   }
+
+   /*
+* First HT sibling to come up on the core.  Link shared state of
+* the first HT sibling to itself. The siblings on the same core
+* which come up later will see the shared state pointer and link
+* themself to the state of this CPU.
+*/
+   st->shared_state = st;
+}
+
+/*
+ * Logic is: First HT sibling enables SSBD for both siblings in the core
+ * and last sibling to disable it, disables it for the whole core. This how
+ * MSR_SPEC_CTRL works in "hardware":
+ *
+ *  CORE_SPEC_CTRL = THREAD0_SPEC_CTRL | THREAD1_SPEC_CTRL
+ */
+static __always_inline void amd_set_core_ssb_state(unsigned long tifn)
 {
-   u64 msr;
+   struct ssb_state *st = this_cpu_ptr(&ssb_state);
+   u64 msr = x86_amd_ls_cfg_base;
 
-   if (static_cpu_has(X86_FEATURE_LS_CFG_SSBD)) {
-   msr = x86_amd_ls_cfg_base | ssbd_tif_to_amd_ls_cfg(tifn);
+   if (!static_cpu_has(X86_FEATURE_ZEN)) {
+   msr |= ssbd_tif_to_amd_ls_cfg(tifn);
wrmsrl(MSR_AMD64_LS_CFG, msr);
+   return;
+   }
+
+   if (tifn & _TIF_SSBD) {
+   /*
+* Since this can race with prctl(), block reentry on the
+* same CPU.
+*/
+   if (__test_and_set_bit(LSTATE_SSB, &st->local_state))
+   return;
+
+   msr |= x86_amd_ls_cfg_ssbd_mask;
+
+   raw_spin_lock(&st->shared_state->lock);
+   /* First sibling enables SSBD: */
+

[PATCH 4.16 103/110] x86/bugs: Unify x86_spec_ctrl_{set_guest,restore_host}

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Borislav Petkov 

commit cc69b34989210f067b2c51d5539b5f96ebcc3a01 upstream

Function bodies are very similar and are going to grow more almost
identical code. Add a bool arg to determine whether SPEC_CTRL is being set
for the guest or restored to the host.

No functional changes.

Signed-off-by: Borislav Petkov 
Signed-off-by: Thomas Gleixner 
Reviewed-by: Konrad Rzeszutek Wilk 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/spec-ctrl.h |   33 ++---
 arch/x86/kernel/cpu/bugs.c   |   60 +--
 2 files changed, 44 insertions(+), 49 deletions(-)

--- a/arch/x86/include/asm/spec-ctrl.h
+++ b/arch/x86/include/asm/spec-ctrl.h
@@ -13,10 +13,35 @@
  * Takes the guest view of SPEC_CTRL MSR as a parameter and also
  * the guest's version of VIRT_SPEC_CTRL, if emulated.
  */
-extern void x86_spec_ctrl_set_guest(u64 guest_spec_ctrl,
-   u64 guest_virt_spec_ctrl);
-extern void x86_spec_ctrl_restore_host(u64 guest_spec_ctrl,
-  u64 guest_virt_spec_ctrl);
+extern void x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, 
bool guest);
+
+/**
+ * x86_spec_ctrl_set_guest - Set speculation control registers for the guest
+ * @guest_spec_ctrl:   The guest content of MSR_SPEC_CTRL
+ * @guest_virt_spec_ctrl:  The guest controlled bits of MSR_VIRT_SPEC_CTRL
+ * (may get translated to MSR_AMD64_LS_CFG bits)
+ *
+ * Avoids writing to the MSR if the content/bits are the same
+ */
+static inline
+void x86_spec_ctrl_set_guest(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl)
+{
+   x86_virt_spec_ctrl(guest_spec_ctrl, guest_virt_spec_ctrl, true);
+}
+
+/**
+ * x86_spec_ctrl_restore_host - Restore host speculation control registers
+ * @guest_spec_ctrl:   The guest content of MSR_SPEC_CTRL
+ * @guest_virt_spec_ctrl:  The guest controlled bits of MSR_VIRT_SPEC_CTRL
+ * (may get translated to MSR_AMD64_LS_CFG bits)
+ *
+ * Avoids writing to the MSR if the content/bits are the same
+ */
+static inline
+void x86_spec_ctrl_restore_host(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl)
+{
+   x86_virt_spec_ctrl(guest_spec_ctrl, guest_virt_spec_ctrl, false);
+}
 
 /* AMD specific Speculative Store Bypass MSR data */
 extern u64 x86_amd_ls_cfg_base;
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -151,55 +151,25 @@ u64 x86_spec_ctrl_get_default(void)
 }
 EXPORT_SYMBOL_GPL(x86_spec_ctrl_get_default);
 
-/**
- * x86_spec_ctrl_set_guest - Set speculation control registers for the guest
- * @guest_spec_ctrl:   The guest content of MSR_SPEC_CTRL
- * @guest_virt_spec_ctrl:  The guest controlled bits of MSR_VIRT_SPEC_CTRL
- * (may get translated to MSR_AMD64_LS_CFG bits)
- *
- * Avoids writing to the MSR if the content/bits are the same
- */
-void x86_spec_ctrl_set_guest(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl)
+void
+x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool 
setguest)
 {
-   u64 host = x86_spec_ctrl_base;
+   struct thread_info *ti = current_thread_info();
+   u64 msr, host = x86_spec_ctrl_base;
 
/* Is MSR_SPEC_CTRL implemented ? */
-   if (!static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
-   return;
-
-   /* SSBD controlled in MSR_SPEC_CTRL */
-   if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
-   host |= ssbd_tif_to_spec_ctrl(current_thread_info()->flags);
-
-   if (host != guest_spec_ctrl)
-   wrmsrl(MSR_IA32_SPEC_CTRL, guest_spec_ctrl);
-}
-EXPORT_SYMBOL_GPL(x86_spec_ctrl_set_guest);
-
-/**
- * x86_spec_ctrl_restore_host - Restore host speculation control registers
- * @guest_spec_ctrl:   The guest content of MSR_SPEC_CTRL
- * @guest_virt_spec_ctrl:  The guest controlled bits of MSR_VIRT_SPEC_CTRL
- * (may get translated to MSR_AMD64_LS_CFG bits)
- *
- * Avoids writing to the MSR if the content/bits are the same
- */
-void x86_spec_ctrl_restore_host(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl)
-{
-   u64 host = x86_spec_ctrl_base;
-
-   /* Is MSR_SPEC_CTRL implemented ? */
-   if (!static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
-   return;
-
-   /* SSBD controlled in MSR_SPEC_CTRL */
-   if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
-   host |= ssbd_tif_to_spec_ctrl(current_thread_info()->flags);
-
-   if (host != guest_spec_ctrl)
-   wrmsrl(MSR_IA32_SPEC_CTRL, host);
+   if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
+   /* SSBD controlled in MSR_SPEC_CTRL */
+   if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
+   host |= ssbd_tif_to_spec_ctrl(ti->flags);
+
+   if (host != guest_spec_ctrl) {
+ 

[PATCH 4.16 100/110] x86/bugs, KVM: Extend speculation control for VIRT_SPEC_CTRL

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit ccbcd2674472a978b48c91c1fbfb66c0ff959f24 upstream

AMD is proposing a VIRT_SPEC_CTRL MSR to handle the Speculative Store
Bypass Disable via MSR_AMD64_LS_CFG so that guests do not have to care
about the bit position of the SSBD bit and thus facilitate migration.
Also, the sibling coordination on Family 17H CPUs can only be done on
the host.

Extend x86_spec_ctrl_set_guest() and x86_spec_ctrl_restore_host() with an
extra argument for the VIRT_SPEC_CTRL MSR.

Hand in 0 from VMX and in SVM add a new virt_spec_ctrl member to the CPU
data structure which is going to be used in later patches for the actual
implementation.

Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Konrad Rzeszutek Wilk 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/spec-ctrl.h |9 ++---
 arch/x86/kernel/cpu/bugs.c   |   20 ++--
 arch/x86/kvm/svm.c   |   11 +--
 arch/x86/kvm/vmx.c   |5 +++--
 4 files changed, 36 insertions(+), 9 deletions(-)

--- a/arch/x86/include/asm/spec-ctrl.h
+++ b/arch/x86/include/asm/spec-ctrl.h
@@ -10,10 +10,13 @@
  * the guest has, while on VMEXIT we restore the host view. This
  * would be easier if SPEC_CTRL were architecturally maskable or
  * shadowable for guests but this is not (currently) the case.
- * Takes the guest view of SPEC_CTRL MSR as a parameter.
+ * Takes the guest view of SPEC_CTRL MSR as a parameter and also
+ * the guest's version of VIRT_SPEC_CTRL, if emulated.
  */
-extern void x86_spec_ctrl_set_guest(u64);
-extern void x86_spec_ctrl_restore_host(u64);
+extern void x86_spec_ctrl_set_guest(u64 guest_spec_ctrl,
+   u64 guest_virt_spec_ctrl);
+extern void x86_spec_ctrl_restore_host(u64 guest_spec_ctrl,
+  u64 guest_virt_spec_ctrl);
 
 /* AMD specific Speculative Store Bypass MSR data */
 extern u64 x86_amd_ls_cfg_base;
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -151,7 +151,15 @@ u64 x86_spec_ctrl_get_default(void)
 }
 EXPORT_SYMBOL_GPL(x86_spec_ctrl_get_default);
 
-void x86_spec_ctrl_set_guest(u64 guest_spec_ctrl)
+/**
+ * x86_spec_ctrl_set_guest - Set speculation control registers for the guest
+ * @guest_spec_ctrl:   The guest content of MSR_SPEC_CTRL
+ * @guest_virt_spec_ctrl:  The guest controlled bits of MSR_VIRT_SPEC_CTRL
+ * (may get translated to MSR_AMD64_LS_CFG bits)
+ *
+ * Avoids writing to the MSR if the content/bits are the same
+ */
+void x86_spec_ctrl_set_guest(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl)
 {
u64 host = x86_spec_ctrl_base;
 
@@ -168,7 +176,15 @@ void x86_spec_ctrl_set_guest(u64 guest_s
 }
 EXPORT_SYMBOL_GPL(x86_spec_ctrl_set_guest);
 
-void x86_spec_ctrl_restore_host(u64 guest_spec_ctrl)
+/**
+ * x86_spec_ctrl_restore_host - Restore host speculation control registers
+ * @guest_spec_ctrl:   The guest content of MSR_SPEC_CTRL
+ * @guest_virt_spec_ctrl:  The guest controlled bits of MSR_VIRT_SPEC_CTRL
+ * (may get translated to MSR_AMD64_LS_CFG bits)
+ *
+ * Avoids writing to the MSR if the content/bits are the same
+ */
+void x86_spec_ctrl_restore_host(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl)
 {
u64 host = x86_spec_ctrl_base;
 
--- a/arch/x86/kvm/svm.c
+++ b/arch/x86/kvm/svm.c
@@ -192,6 +192,12 @@ struct vcpu_svm {
} host;
 
u64 spec_ctrl;
+   /*
+* Contains guest-controlled bits of VIRT_SPEC_CTRL, which will be
+* translated into the appropriate L2_CFG bits on the host to
+* perform speculative control.
+*/
+   u64 virt_spec_ctrl;
 
u32 *msrpm;
 
@@ -1910,6 +1916,7 @@ static void svm_vcpu_reset(struct kvm_vc
 
vcpu->arch.microcode_version = 0x0165;
svm->spec_ctrl = 0;
+   svm->virt_spec_ctrl = 0;
 
if (!init_event) {
svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
@@ -5401,7 +5408,7 @@ static void svm_vcpu_run(struct kvm_vcpu
 * is no need to worry about the conditional branch over the wrmsr
 * being speculatively taken.
 */
-   x86_spec_ctrl_set_guest(svm->spec_ctrl);
+   x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
 
asm volatile (
"push %%" _ASM_BP "; \n\t"
@@ -5525,7 +5532,7 @@ static void svm_vcpu_run(struct kvm_vcpu
if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
 
-   x86_spec_ctrl_restore_host(svm->spec_ctrl);
+   x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl);
 
reload_tss(vcpu);
 
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -9465,9 +9465,10 @@ static void __noclone vmx_vcpu_run(struc
 * is no need to worr

[PATCH 4.16 102/110] x86/speculation: Rework speculative_store_bypass_update()

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit 0270be3e34efb05a88bc4c422572ece038ef3608 upstream

The upcoming support for the virtual SPEC_CTRL MSR on AMD needs to reuse
speculative_store_bypass_update() to avoid code duplication. Add an
argument for supplying a thread info (TIF) value and create a wrapper
speculative_store_bypass_update_current() which is used at the existing
call site.

Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Konrad Rzeszutek Wilk 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/spec-ctrl.h |7 ++-
 arch/x86/kernel/cpu/bugs.c   |2 +-
 arch/x86/kernel/process.c|4 ++--
 3 files changed, 9 insertions(+), 4 deletions(-)

--- a/arch/x86/include/asm/spec-ctrl.h
+++ b/arch/x86/include/asm/spec-ctrl.h
@@ -42,6 +42,11 @@ extern void speculative_store_bypass_ht_
 static inline void speculative_store_bypass_ht_init(void) { }
 #endif
 
-extern void speculative_store_bypass_update(void);
+extern void speculative_store_bypass_update(unsigned long tif);
+
+static inline void speculative_store_bypass_update_current(void)
+{
+   speculative_store_bypass_update(current_thread_info()->flags);
+}
 
 #endif
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -598,7 +598,7 @@ static int ssb_prctl_set(struct task_str
 * mitigation until it is next scheduled.
 */
if (task == current && update)
-   speculative_store_bypass_update();
+   speculative_store_bypass_update_current();
 
return 0;
 }
--- a/arch/x86/kernel/process.c
+++ b/arch/x86/kernel/process.c
@@ -414,10 +414,10 @@ static __always_inline void __speculativ
intel_set_ssb_state(tifn);
 }
 
-void speculative_store_bypass_update(void)
+void speculative_store_bypass_update(unsigned long tif)
 {
preempt_disable();
-   __speculative_store_bypass_update(current_thread_info()->flags);
+   __speculative_store_bypass_update(tif);
preempt_enable();
 }
 




[PATCH 4.16 106/110] x86/bugs: Rework spec_ctrl base and mask logic

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit be6fcb5478e95bb1c91f489121238deb3abca46a upstream

x86_spec_ctrL_mask is intended to mask out bits from a MSR_SPEC_CTRL value
which are not to be modified. However the implementation is not really used
and the bitmask was inverted to make a check easier, which was removed in
"x86/bugs: Remove x86_spec_ctrl_set()"

Aside of that it is missing the STIBP bit if it is supported by the
platform, so if the mask would be used in x86_virt_spec_ctrl() then it
would prevent a guest from setting STIBP.

Add the STIBP bit if supported and use the mask in x86_virt_spec_ctrl() to
sanitize the value which is supplied by the guest.

Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/kernel/cpu/bugs.c |   26 +++---
 1 file changed, 19 insertions(+), 7 deletions(-)

--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -42,7 +42,7 @@ EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
  * The vendor and possibly platform specific bits which can be modified in
  * x86_spec_ctrl_base.
  */
-static u64 __ro_after_init x86_spec_ctrl_mask = ~SPEC_CTRL_IBRS;
+static u64 __ro_after_init x86_spec_ctrl_mask = SPEC_CTRL_IBRS;
 
 /*
  * AMD specific MSR info for Speculative Store Bypass control.
@@ -68,6 +68,10 @@ void __init check_bugs(void)
if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
 
+   /* Allow STIBP in MSR_SPEC_CTRL if supported */
+   if (boot_cpu_has(X86_FEATURE_STIBP))
+   x86_spec_ctrl_mask |= SPEC_CTRL_STIBP;
+
/* Select the proper spectre mitigation before patching alternatives */
spectre_v2_select_mitigation();
 
@@ -136,18 +140,26 @@ static enum spectre_v2_mitigation spectr
 void
 x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool 
setguest)
 {
+   u64 msrval, guestval, hostval = x86_spec_ctrl_base;
struct thread_info *ti = current_thread_info();
-   u64 msr, host = x86_spec_ctrl_base;
 
/* Is MSR_SPEC_CTRL implemented ? */
if (static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL)) {
+   /*
+* Restrict guest_spec_ctrl to supported values. Clear the
+* modifiable bits in the host base value and or the
+* modifiable bits from the guest value.
+*/
+   guestval = hostval & ~x86_spec_ctrl_mask;
+   guestval |= guest_spec_ctrl & x86_spec_ctrl_mask;
+
/* SSBD controlled in MSR_SPEC_CTRL */
if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
-   host |= ssbd_tif_to_spec_ctrl(ti->flags);
+   hostval |= ssbd_tif_to_spec_ctrl(ti->flags);
 
-   if (host != guest_spec_ctrl) {
-   msr = setguest ? guest_spec_ctrl : host;
-   wrmsrl(MSR_IA32_SPEC_CTRL, msr);
+   if (hostval != guestval) {
+   msrval = setguest ? guestval : hostval;
+   wrmsrl(MSR_IA32_SPEC_CTRL, msrval);
}
}
 }
@@ -493,7 +505,7 @@ static enum ssb_mitigation __init __ssb_
switch (boot_cpu_data.x86_vendor) {
case X86_VENDOR_INTEL:
x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
-   x86_spec_ctrl_mask &= ~SPEC_CTRL_SSBD;
+   x86_spec_ctrl_mask |= SPEC_CTRL_SSBD;
wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
break;
case X86_VENDOR_AMD:




[PATCH 4.16 104/110] x86/bugs: Expose x86_spec_ctrl_base directly

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit fa8ac4988249c38476f6ad678a4848a736373403 upstream

x86_spec_ctrl_base is the system wide default value for the SPEC_CTRL MSR.
x86_spec_ctrl_get_default() returns x86_spec_ctrl_base and was intended to
prevent modification to that variable. Though the variable is read only
after init and globaly visible already.

Remove the function and export the variable instead.

Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Konrad Rzeszutek Wilk 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/nospec-branch.h |   16 +---
 arch/x86/include/asm/spec-ctrl.h |3 ---
 arch/x86/kernel/cpu/bugs.c   |   11 +--
 3 files changed, 6 insertions(+), 24 deletions(-)

--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -217,16 +217,7 @@ enum spectre_v2_mitigation {
SPECTRE_V2_IBRS,
 };
 
-/*
- * The Intel specification for the SPEC_CTRL MSR requires that we
- * preserve any already set reserved bits at boot time (e.g. for
- * future additions that this kernel is not currently aware of).
- * We then set any additional mitigation bits that we want
- * ourselves and always use this as the base for SPEC_CTRL.
- * We also use this when handling guest entry/exit as below.
- */
 extern void x86_spec_ctrl_set(u64);
-extern u64 x86_spec_ctrl_get_default(void);
 
 /* The Speculative Store Bypass disable variants */
 enum ssb_mitigation {
@@ -278,6 +269,9 @@ static inline void indirect_branch_predi
alternative_msr_write(MSR_IA32_PRED_CMD, val, X86_FEATURE_USE_IBPB);
 }
 
+/* The Intel SPEC CTRL MSR base value cache */
+extern u64 x86_spec_ctrl_base;
+
 /*
  * With retpoline, we must use IBRS to restrict branch prediction
  * before calling into firmware.
@@ -286,7 +280,7 @@ static inline void indirect_branch_predi
  */
 #define firmware_restrict_branch_speculation_start()   \
 do {   \
-   u64 val = x86_spec_ctrl_get_default() | SPEC_CTRL_IBRS; \
+   u64 val = x86_spec_ctrl_base | SPEC_CTRL_IBRS;  \
\
preempt_disable();  \
alternative_msr_write(MSR_IA32_SPEC_CTRL, val,  \
@@ -295,7 +289,7 @@ do {
\
 
 #define firmware_restrict_branch_speculation_end() \
 do {   \
-   u64 val = x86_spec_ctrl_get_default();  \
+   u64 val = x86_spec_ctrl_base;   \
\
alternative_msr_write(MSR_IA32_SPEC_CTRL, val,  \
  X86_FEATURE_USE_IBRS_FW); \
--- a/arch/x86/include/asm/spec-ctrl.h
+++ b/arch/x86/include/asm/spec-ctrl.h
@@ -47,9 +47,6 @@ void x86_spec_ctrl_restore_host(u64 gues
 extern u64 x86_amd_ls_cfg_base;
 extern u64 x86_amd_ls_cfg_ssbd_mask;
 
-/* The Intel SPEC CTRL MSR base value cache */
-extern u64 x86_spec_ctrl_base;
-
 static inline u64 ssbd_tif_to_spec_ctrl(u64 tifn)
 {
BUILD_BUG_ON(TIF_SSBD < SPEC_CTRL_SSBD_SHIFT);
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -36,6 +36,7 @@ static void __init ssb_select_mitigation
  * writes to SPEC_CTRL contain whatever reserved bits have been set.
  */
 u64 __ro_after_init x86_spec_ctrl_base;
+EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
 
 /*
  * The vendor and possibly platform specific bits which can be modified in
@@ -141,16 +142,6 @@ void x86_spec_ctrl_set(u64 val)
 }
 EXPORT_SYMBOL_GPL(x86_spec_ctrl_set);
 
-u64 x86_spec_ctrl_get_default(void)
-{
-   u64 msrval = x86_spec_ctrl_base;
-
-   if (static_cpu_has(X86_FEATURE_SPEC_CTRL))
-   msrval |= ssbd_tif_to_spec_ctrl(current_thread_info()->flags);
-   return msrval;
-}
-EXPORT_SYMBOL_GPL(x86_spec_ctrl_get_default);
-
 void
 x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool 
setguest)
 {




[PATCH 4.16 105/110] x86/bugs: Remove x86_spec_ctrl_set()

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit 4b59bdb569453a60b752b274ca61f009e37f4dae upstream

x86_spec_ctrl_set() is only used in bugs.c and the extra mask checks there
provide no real value as both call sites can just write x86_spec_ctrl_base
to MSR_SPEC_CTRL. x86_spec_ctrl_base is valid and does not need any extra
masking or checking.

Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Konrad Rzeszutek Wilk 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/nospec-branch.h |2 --
 arch/x86/kernel/cpu/bugs.c   |   13 ++---
 2 files changed, 2 insertions(+), 13 deletions(-)

--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -217,8 +217,6 @@ enum spectre_v2_mitigation {
SPECTRE_V2_IBRS,
 };
 
-extern void x86_spec_ctrl_set(u64);
-
 /* The Speculative Store Bypass disable variants */
 enum ssb_mitigation {
SPEC_STORE_BYPASS_NONE,
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -133,15 +133,6 @@ static const char *spectre_v2_strings[]
 static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
SPECTRE_V2_NONE;
 
-void x86_spec_ctrl_set(u64 val)
-{
-   if (val & x86_spec_ctrl_mask)
-   WARN_ONCE(1, "SPEC_CTRL MSR value 0x%16llx is unknown.\n", val);
-   else
-   wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base | val);
-}
-EXPORT_SYMBOL_GPL(x86_spec_ctrl_set);
-
 void
 x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool 
setguest)
 {
@@ -503,7 +494,7 @@ static enum ssb_mitigation __init __ssb_
case X86_VENDOR_INTEL:
x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
x86_spec_ctrl_mask &= ~SPEC_CTRL_SSBD;
-   x86_spec_ctrl_set(SPEC_CTRL_SSBD);
+   wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
break;
case X86_VENDOR_AMD:
x86_amd_ssb_disable();
@@ -615,7 +606,7 @@ int arch_prctl_spec_ctrl_get(struct task
 void x86_spec_ctrl_setup_ap(void)
 {
if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
-   x86_spec_ctrl_set(x86_spec_ctrl_base & ~x86_spec_ctrl_mask);
+   wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
 
if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
x86_amd_ssb_disable();




[PATCH 4.16 097/110] x86/cpufeatures: Disentangle SSBD enumeration

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit 52817587e706686fcdb27f14c1b000c92f266c96 upstream

The SSBD enumeration is similarly to the other bits magically shared
between Intel and AMD though the mechanisms are different.

Make X86_FEATURE_SSBD synthetic and set it depending on the vendor specific
features or family dependent setup.

Change the Intel bit to X86_FEATURE_SPEC_CTRL_SSBD to denote that SSBD is
controlled via MSR_SPEC_CTRL and fix up the usage sites.

Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Konrad Rzeszutek Wilk 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/cpufeatures.h |7 +++
 arch/x86/kernel/cpu/amd.c  |7 +--
 arch/x86/kernel/cpu/bugs.c |   10 +-
 arch/x86/kernel/cpu/common.c   |3 +++
 arch/x86/kernel/cpu/intel.c|1 +
 arch/x86/kernel/process.c  |2 +-
 6 files changed, 14 insertions(+), 16 deletions(-)

--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -207,15 +207,14 @@
 #define X86_FEATURE_INTEL_PPIN ( 7*32+14) /* Intel Processor Inventory 
Number */
 #define X86_FEATURE_CDP_L2 ( 7*32+15) /* Code and Data 
Prioritization L2 */
 #define X86_FEATURE_MSR_SPEC_CTRL  ( 7*32+16) /* "" MSR SPEC_CTRL is 
implemented */
-
+#define X86_FEATURE_SSBD   ( 7*32+17) /* Speculative Store Bypass 
Disable */
 #define X86_FEATURE_MBA( 7*32+18) /* Memory Bandwidth 
Allocation */
 #define X86_FEATURE_RSB_CTXSW  ( 7*32+19) /* "" Fill RSB on context 
switches */
 #define X86_FEATURE_SEV( 7*32+20) /* AMD Secure 
Encrypted Virtualization */
-
 #define X86_FEATURE_USE_IBPB   ( 7*32+21) /* "" Indirect Branch 
Prediction Barrier enabled */
 #define X86_FEATURE_USE_IBRS_FW( 7*32+22) /* "" Use IBRS 
during runtime firmware calls */
 #define X86_FEATURE_SPEC_STORE_BYPASS_DISABLE  ( 7*32+23) /* "" Disable 
Speculative Store Bypass. */
-#define X86_FEATURE_AMD_SSBD   ( 7*32+24)  /* "" AMD SSBD 
implementation */
+#define X86_FEATURE_LS_CFG_SSBD( 7*32+24)  /* "" AMD SSBD 
implementation via LS_CFG MSR */
 #define X86_FEATURE_IBRS   ( 7*32+25) /* Indirect Branch 
Restricted Speculation */
 #define X86_FEATURE_IBPB   ( 7*32+26) /* Indirect Branch 
Prediction Barrier */
 #define X86_FEATURE_STIBP  ( 7*32+27) /* Single Thread Indirect 
Branch Predictors */
@@ -338,7 +337,7 @@
 #define X86_FEATURE_SPEC_CTRL  (18*32+26) /* "" Speculation Control 
(IBRS + IBPB) */
 #define X86_FEATURE_INTEL_STIBP(18*32+27) /* "" Single Thread 
Indirect Branch Predictors */
 #define X86_FEATURE_ARCH_CAPABILITIES  (18*32+29) /* IA32_ARCH_CAPABILITIES 
MSR (Intel) */
-#define X86_FEATURE_SSBD   (18*32+31) /* Speculative Store Bypass 
Disable */
+#define X86_FEATURE_SPEC_CTRL_SSBD (18*32+31) /* "" Speculative Store 
Bypass Disable */
 
 /*
  * BUG word(s)
--- a/arch/x86/kernel/cpu/amd.c
+++ b/arch/x86/kernel/cpu/amd.c
@@ -570,8 +570,8 @@ static void bsp_init_amd(struct cpuinfo_
 * avoid RMW. If that faults, do not enable SSBD.
 */
if (!rdmsrl_safe(MSR_AMD64_LS_CFG, &x86_amd_ls_cfg_base)) {
+   setup_force_cpu_cap(X86_FEATURE_LS_CFG_SSBD);
setup_force_cpu_cap(X86_FEATURE_SSBD);
-   setup_force_cpu_cap(X86_FEATURE_AMD_SSBD);
x86_amd_ls_cfg_ssbd_mask = 1ULL << bit;
}
}
@@ -919,11 +919,6 @@ static void init_amd(struct cpuinfo_x86
/* AMD CPUs don't reset SS attributes on SYSRET, Xen does. */
if (!cpu_has(c, X86_FEATURE_XENPV))
set_cpu_bug(c, X86_BUG_SYSRET_SS_ATTRS);
-
-   if (boot_cpu_has(X86_FEATURE_AMD_SSBD)) {
-   set_cpu_cap(c, X86_FEATURE_SSBD);
-   set_cpu_cap(c, X86_FEATURE_AMD_SSBD);
-   }
 }
 
 #ifdef CONFIG_X86_32
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -159,8 +159,8 @@ void x86_spec_ctrl_set_guest(u64 guest_s
if (!static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
return;
 
-   /* Intel controls SSB in MSR_SPEC_CTRL */
-   if (static_cpu_has(X86_FEATURE_SPEC_CTRL))
+   /* SSBD controlled in MSR_SPEC_CTRL */
+   if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
host |= ssbd_tif_to_spec_ctrl(current_thread_info()->flags);
 
if (host != guest_spec_ctrl)
@@ -176,8 +176,8 @@ void x86_spec_ctrl_restore_host(u64 gues
if (!static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
return;
 
-   /* Intel controls SSB in MSR_SPEC_CTRL */
-   if (static_cpu_has(X86_FEATURE_SPEC_CTRL))
+   /* SSBD controlled in MSR_SPEC_CTRL */
+   if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
 

[PATCH 4.16 101/110] x86/speculation: Add virtualized speculative store bypass disable support

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Tom Lendacky 

commit 11fb0683493b2da112cd64c9dada221b52463bf7 upstream

Some AMD processors only support a non-architectural means of enabling
speculative store bypass disable (SSBD).  To allow a simplified view of
this to a guest, an architectural definition has been created through a new
CPUID bit, 0x8008_EBX[25], and a new MSR, 0xc001011f.  With this, a
hypervisor can virtualize the existence of this definition and provide an
architectural method for using SSBD to a guest.

Add the new CPUID feature, the new MSR and update the existing SSBD
support to use this MSR when present.

Signed-off-by: Tom Lendacky 
Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/cpufeatures.h |1 +
 arch/x86/include/asm/msr-index.h   |2 ++
 arch/x86/kernel/cpu/bugs.c |4 +++-
 arch/x86/kernel/process.c  |   13 -
 4 files changed, 18 insertions(+), 2 deletions(-)

--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -282,6 +282,7 @@
 #define X86_FEATURE_AMD_IBPB   (13*32+12) /* "" Indirect Branch 
Prediction Barrier */
 #define X86_FEATURE_AMD_IBRS   (13*32+14) /* "" Indirect Branch 
Restricted Speculation */
 #define X86_FEATURE_AMD_STIBP  (13*32+15) /* "" Single Thread Indirect 
Branch Predictors */
+#define X86_FEATURE_VIRT_SSBD  (13*32+25) /* Virtualized Speculative 
Store Bypass Disable */
 
 /* Thermal and Power Management Leaf, CPUID level 0x0006 (EAX), word 14 */
 #define X86_FEATURE_DTHERM (14*32+ 0) /* Digital Thermal Sensor */
--- a/arch/x86/include/asm/msr-index.h
+++ b/arch/x86/include/asm/msr-index.h
@@ -347,6 +347,8 @@
 #define MSR_AMD64_SEV_ENABLED_BIT  0
 #define MSR_AMD64_SEV_ENABLED  BIT_ULL(MSR_AMD64_SEV_ENABLED_BIT)
 
+#define MSR_AMD64_VIRT_SPEC_CTRL   0xc001011f
+
 /* Fam 17h MSRs */
 #define MSR_F17H_IRPERF0xc0e9
 
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -205,7 +205,9 @@ static void x86_amd_ssb_disable(void)
 {
u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_ssbd_mask;
 
-   if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
+   if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
+   wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, SPEC_CTRL_SSBD);
+   else if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
wrmsrl(MSR_AMD64_LS_CFG, msrval);
 }
 
--- a/arch/x86/kernel/process.c
+++ b/arch/x86/kernel/process.c
@@ -388,6 +388,15 @@ static __always_inline void amd_set_core
 }
 #endif
 
+static __always_inline void amd_set_ssb_virt_state(unsigned long tifn)
+{
+   /*
+* SSBD has the same definition in SPEC_CTRL and VIRT_SPEC_CTRL,
+* so ssbd_tif_to_spec_ctrl() just works.
+*/
+   wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, ssbd_tif_to_spec_ctrl(tifn));
+}
+
 static __always_inline void intel_set_ssb_state(unsigned long tifn)
 {
u64 msr = x86_spec_ctrl_base | ssbd_tif_to_spec_ctrl(tifn);
@@ -397,7 +406,9 @@ static __always_inline void intel_set_ss
 
 static __always_inline void __speculative_store_bypass_update(unsigned long 
tifn)
 {
-   if (static_cpu_has(X86_FEATURE_LS_CFG_SSBD))
+   if (static_cpu_has(X86_FEATURE_VIRT_SSBD))
+   amd_set_ssb_virt_state(tifn);
+   else if (static_cpu_has(X86_FEATURE_LS_CFG_SSBD))
amd_set_core_ssb_state(tifn);
else
intel_set_ssb_state(tifn);




[PATCH 4.16 108/110] KVM: SVM: Implement VIRT_SPEC_CTRL support for SSBD

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Tom Lendacky 

commit bc226f07dcd3c9ef0b7f6236fe356ea4a9cb4769 upstream

Expose the new virtualized architectural mechanism, VIRT_SSBD, for using
speculative store bypass disable (SSBD) under SVM.  This will allow guests
to use SSBD on hardware that uses non-architectural mechanisms for enabling
SSBD.

[ tglx: Folded the migration fixup from Paolo Bonzini ]

Signed-off-by: Tom Lendacky 
Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/kvm_host.h |2 +-
 arch/x86/kernel/cpu/common.c|3 ++-
 arch/x86/kvm/cpuid.c|   11 +--
 arch/x86/kvm/svm.c  |   21 +++--
 arch/x86/kvm/vmx.c  |   18 +++---
 arch/x86/kvm/x86.c  |   13 -
 6 files changed, 50 insertions(+), 18 deletions(-)

--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -933,7 +933,7 @@ struct kvm_x86_ops {
int (*hardware_setup)(void);   /* __init */
void (*hardware_unsetup)(void);/* __exit */
bool (*cpu_has_accelerated_tpr)(void);
-   bool (*cpu_has_high_real_mode_segbase)(void);
+   bool (*has_emulated_msr)(int index);
void (*cpuid_update)(struct kvm_vcpu *vcpu);
 
int (*vm_init)(struct kvm *kvm);
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -767,7 +767,8 @@ static void init_speculation_control(str
if (cpu_has(c, X86_FEATURE_INTEL_STIBP))
set_cpu_cap(c, X86_FEATURE_STIBP);
 
-   if (cpu_has(c, X86_FEATURE_SPEC_CTRL_SSBD))
+   if (cpu_has(c, X86_FEATURE_SPEC_CTRL_SSBD) ||
+   cpu_has(c, X86_FEATURE_VIRT_SSBD))
set_cpu_cap(c, X86_FEATURE_SSBD);
 
if (cpu_has(c, X86_FEATURE_AMD_IBRS)) {
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -374,7 +374,7 @@ static inline int __do_cpuid_ent(struct
 
/* cpuid 0x8008.ebx */
const u32 kvm_cpuid_8000_0008_ebx_x86_features =
-   F(AMD_IBPB) | F(AMD_IBRS);
+   F(AMD_IBPB) | F(AMD_IBRS) | F(VIRT_SSBD);
 
/* cpuid 0xC001.edx */
const u32 kvm_cpuid_C000_0001_edx_x86_features =
@@ -642,13 +642,20 @@ static inline int __do_cpuid_ent(struct
g_phys_as = phys_as;
entry->eax = g_phys_as | (virt_as << 8);
entry->edx = 0;
-   /* IBRS and IBPB aren't necessarily present in hardware cpuid */
+   /*
+* IBRS, IBPB and VIRT_SSBD aren't necessarily present in
+* hardware cpuid
+*/
if (boot_cpu_has(X86_FEATURE_AMD_IBPB))
entry->ebx |= F(AMD_IBPB);
if (boot_cpu_has(X86_FEATURE_AMD_IBRS))
entry->ebx |= F(AMD_IBRS);
+   if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
+   entry->ebx |= F(VIRT_SSBD);
entry->ebx &= kvm_cpuid_8000_0008_ebx_x86_features;
cpuid_mask(&entry->ebx, CPUID_8000_0008_EBX);
+   if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
+   entry->ebx |= F(VIRT_SSBD);
break;
}
case 0x8019:
--- a/arch/x86/kvm/svm.c
+++ b/arch/x86/kvm/svm.c
@@ -3971,6 +3971,13 @@ static int svm_get_msr(struct kvm_vcpu *
 
msr_info->data = svm->spec_ctrl;
break;
+   case MSR_AMD64_VIRT_SPEC_CTRL:
+   if (!msr_info->host_initiated &&
+   !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
+   return 1;
+
+   msr_info->data = svm->virt_spec_ctrl;
+   break;
case MSR_F15H_IC_CFG: {
 
int family, model;
@@ -4105,6 +4112,16 @@ static int svm_set_msr(struct kvm_vcpu *
break;
set_msr_interception(svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
break;
+   case MSR_AMD64_VIRT_SPEC_CTRL:
+   if (!msr->host_initiated &&
+   !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
+   return 1;
+
+   if (data & ~SPEC_CTRL_SSBD)
+   return 1;
+
+   svm->virt_spec_ctrl = data;
+   break;
case MSR_STAR:
svm->vmcb->save.star = data;
break;
@@ -5635,7 +5652,7 @@ static bool svm_cpu_has_accelerated_tpr(
return false;
 }
 
-static bool svm_has_high_real_mode_segbase(void)
+static bool svm_has_emulated_msr(int index)
 {
return true;
 }
@@ -6859,7 +6876,7 @@ static struct kvm_x86_ops svm_x86_ops __
.hardware_enable = svm_hardware_enable,
.hardware_disable = svm_hardware_disable,
.cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
-   .cpu_has_high_real_mode_segbase = svm_has_high_real_mode_s

[PATCH 4.16 110/110] bpf: Prevent memory disambiguation attack

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Alexei Starovoitov 

commit af86ca4e3088fe5eacf2f7e58c01fa68ca067672 upstream

Detect code patterns where malicious 'speculative store bypass' can be used
and sanitize such patterns.

 39: (bf) r3 = r10
 40: (07) r3 += -216
 41: (79) r8 = *(u64 *)(r7 +0)   // slow read
 42: (7a) *(u64 *)(r10 -72) = 0  // verifier inserts this instruction
 43: (7b) *(u64 *)(r8 +0) = r3   // this store becomes slow due to r8
 44: (79) r1 = *(u64 *)(r6 +0)   // cpu speculatively executes this load
 45: (71) r2 = *(u8 *)(r1 +0)// speculatively arbitrary 'load byte'
 // is now sanitized

Above code after x86 JIT becomes:
 e5: mov%rbp,%rdx
 e8: add$0xff28,%rdx
 ef: mov0x0(%r13),%r14
 f3: movq   $0x0,-0x48(%rbp)
 fb: mov%rdx,0x0(%r14)
 ff: mov0x0(%rbx),%rdi
103: movzbq 0x0(%rdi),%rsi

Signed-off-by: Alexei Starovoitov 
Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 include/linux/bpf_verifier.h |1 
 kernel/bpf/verifier.c|   59 ---
 2 files changed, 57 insertions(+), 3 deletions(-)

--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -146,6 +146,7 @@ struct bpf_insn_aux_data {
s32 call_imm;   /* saved imm field of call insn 
*/
};
int ctx_field_size; /* the ctx field size for load insn, maybe 0 */
+   int sanitize_stack_off; /* stack slot to be cleared */
bool seen; /* this insn was processed by the verifier */
 };
 
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -970,7 +970,7 @@ static bool register_is_null(struct bpf_
  */
 static int check_stack_write(struct bpf_verifier_env *env,
 struct bpf_func_state *state, /* func where 
register points to */
-int off, int size, int value_regno)
+int off, int size, int value_regno, int insn_idx)
 {
struct bpf_func_state *cur; /* state of the current function */
int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err;
@@ -1009,8 +1009,33 @@ static int check_stack_write(struct bpf_
state->stack[spi].spilled_ptr = cur->regs[value_regno];
state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
 
-   for (i = 0; i < BPF_REG_SIZE; i++)
+   for (i = 0; i < BPF_REG_SIZE; i++) {
+   if (state->stack[spi].slot_type[i] == STACK_MISC &&
+   !env->allow_ptr_leaks) {
+   int *poff = 
&env->insn_aux_data[insn_idx].sanitize_stack_off;
+   int soff = (-spi - 1) * BPF_REG_SIZE;
+
+   /* detected reuse of integer stack slot with a 
pointer
+* which means either llvm is reusing stack 
slot or
+* an attacker is trying to exploit 
CVE-2018-3639
+* (speculative store bypass)
+* Have to sanitize that slot with preemptive
+* store of zero.
+*/
+   if (*poff && *poff != soff) {
+   /* disallow programs where single insn 
stores
+* into two different stack slots, 
since verifier
+* cannot sanitize them
+*/
+   verbose(env,
+   "insn %d cannot access two 
stack slots fp%d and fp%d",
+   insn_idx, *poff, soff);
+   return -EINVAL;
+   }
+   *poff = soff;
+   }
state->stack[spi].slot_type[i] = STACK_SPILL;
+   }
} else {
u8 type = STACK_MISC;
 
@@ -1685,7 +1710,7 @@ static int check_mem_access(struct bpf_v
 
if (t == BPF_WRITE)
err = check_stack_write(env, state, off, size,
-   value_regno);
+   value_regno, insn_idx);
else
err = check_stack_read(env, state, off, size,
   value_regno);
@@ -5156,6 +5181,34 @@ static int convert_ctx_accesses(struct b
else
continue;
 
+   if (type == BPF_WRITE &&
+   env->insn_aux_data[i + delta].sanitize_stack_off) {
+   struct bpf_insn patch[] = {
+   /* Sanitize suspicious stack slot with zero

[PATCH 4.16 068/110] x86/cpufeatures: Add X86_FEATURE_RDS

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Konrad Rzeszutek Wilk 

commit 0cc5fa00b0a88dad140b4e5c2cead9951ad36822 upstream

Add the CPU feature bit CPUID.7.0.EDX[31] which indicates whether the CPU
supports Reduced Data Speculation.

[ tglx: Split it out from a later patch ]

Signed-off-by: Konrad Rzeszutek Wilk 
Signed-off-by: Thomas Gleixner 
Reviewed-by: Ingo Molnar 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/cpufeatures.h |1 +
 1 file changed, 1 insertion(+)

--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -333,6 +333,7 @@
 #define X86_FEATURE_SPEC_CTRL  (18*32+26) /* "" Speculation Control 
(IBRS + IBPB) */
 #define X86_FEATURE_INTEL_STIBP(18*32+27) /* "" Single Thread 
Indirect Branch Predictors */
 #define X86_FEATURE_ARCH_CAPABILITIES  (18*32+29) /* IA32_ARCH_CAPABILITIES 
MSR (Intel) */
+#define X86_FEATURE_RDS(18*32+31) /* Reduced Data 
Speculation */
 
 /*
  * BUG word(s)




[PATCH 4.16 072/110] x86/bugs/AMD: Add support to disable RDS on Fam[15,16,17]h if requested

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Konrad Rzeszutek Wilk 

commit 764f3c21588a059cd783c6ba0734d4db2d72822d upstream

AMD does not need the Speculative Store Bypass mitigation to be enabled.

The parameters for this are already available and can be done via MSR
C001_1020. Each family uses a different bit in that MSR for this.

[ tglx: Expose the bit mask via a variable and move the actual MSR fiddling
into the bugs code as that's the right thing to do and also required
to prepare for dynamic enable/disable ]

Suggested-by: Borislav Petkov 
Signed-off-by: Konrad Rzeszutek Wilk 
Signed-off-by: Thomas Gleixner 
Reviewed-by: Ingo Molnar 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/cpufeatures.h   |1 +
 arch/x86/include/asm/nospec-branch.h |4 
 arch/x86/kernel/cpu/amd.c|   26 ++
 arch/x86/kernel/cpu/bugs.c   |   27 ++-
 arch/x86/kernel/cpu/common.c |4 
 5 files changed, 61 insertions(+), 1 deletion(-)

--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -215,6 +215,7 @@
 #define X86_FEATURE_USE_IBPB   ( 7*32+21) /* "" Indirect Branch 
Prediction Barrier enabled */
 #define X86_FEATURE_USE_IBRS_FW( 7*32+22) /* "" Use IBRS 
during runtime firmware calls */
 #define X86_FEATURE_SPEC_STORE_BYPASS_DISABLE  ( 7*32+23) /* "" Disable 
Speculative Store Bypass. */
+#define X86_FEATURE_AMD_RDS(7*32+24)  /* "" AMD RDS implementation 
*/
 
 /* Virtualization flags: Linux defined, word 8 */
 #define X86_FEATURE_TPR_SHADOW ( 8*32+ 0) /* Intel TPR Shadow */
--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -244,6 +244,10 @@ enum ssb_mitigation {
SPEC_STORE_BYPASS_DISABLE,
 };
 
+/* AMD specific Speculative Store Bypass MSR data */
+extern u64 x86_amd_ls_cfg_base;
+extern u64 x86_amd_ls_cfg_rds_mask;
+
 extern char __indirect_thunk_start[];
 extern char __indirect_thunk_end[];
 
--- a/arch/x86/kernel/cpu/amd.c
+++ b/arch/x86/kernel/cpu/amd.c
@@ -10,6 +10,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -554,6 +555,26 @@ static void bsp_init_amd(struct cpuinfo_
rdmsrl(MSR_FAM10H_NODE_ID, value);
nodes_per_socket = ((value >> 3) & 7) + 1;
}
+
+   if (c->x86 >= 0x15 && c->x86 <= 0x17) {
+   unsigned int bit;
+
+   switch (c->x86) {
+   case 0x15: bit = 54; break;
+   case 0x16: bit = 33; break;
+   case 0x17: bit = 10; break;
+   default: return;
+   }
+   /*
+* Try to cache the base value so further operations can
+* avoid RMW. If that faults, do not enable RDS.
+*/
+   if (!rdmsrl_safe(MSR_AMD64_LS_CFG, &x86_amd_ls_cfg_base)) {
+   setup_force_cpu_cap(X86_FEATURE_RDS);
+   setup_force_cpu_cap(X86_FEATURE_AMD_RDS);
+   x86_amd_ls_cfg_rds_mask = 1ULL << bit;
+   }
+   }
 }
 
 static void early_detect_mem_encrypt(struct cpuinfo_x86 *c)
@@ -898,6 +919,11 @@ static void init_amd(struct cpuinfo_x86
/* AMD CPUs don't reset SS attributes on SYSRET, Xen does. */
if (!cpu_has(c, X86_FEATURE_XENPV))
set_cpu_bug(c, X86_BUG_SYSRET_SS_ATTRS);
+
+   if (boot_cpu_has(X86_FEATURE_AMD_RDS)) {
+   set_cpu_cap(c, X86_FEATURE_RDS);
+   set_cpu_cap(c, X86_FEATURE_AMD_RDS);
+   }
 }
 
 #ifdef CONFIG_X86_32
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -41,6 +41,13 @@ static u64 __ro_after_init x86_spec_ctrl
  */
 static u64 __ro_after_init x86_spec_ctrl_mask = ~SPEC_CTRL_IBRS;
 
+/*
+ * AMD specific MSR info for Speculative Store Bypass control.
+ * x86_amd_ls_cfg_rds_mask is initialized in identify_boot_cpu().
+ */
+u64 __ro_after_init x86_amd_ls_cfg_base;
+u64 __ro_after_init x86_amd_ls_cfg_rds_mask;
+
 void __init check_bugs(void)
 {
identify_boot_cpu();
@@ -52,7 +59,8 @@ void __init check_bugs(void)
 
/*
 * Read the SPEC_CTRL MSR to account for reserved bits which may
-* have unknown values.
+* have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
+* init code as it is not enumerated and depends on the family.
 */
if (boot_cpu_has(X86_FEATURE_IBRS))
rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
@@ -154,6 +162,14 @@ void x86_spec_ctrl_restore_host(u64 gues
 }
 EXPORT_SYMBOL_GPL(x86_spec_ctrl_restore_host);
 
+static void x86_amd_rds_enable(void)
+{
+   u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_rds_mask;
+
+   if (boot_cpu_has(X86_FEATURE_AMD_RDS))
+   wrmsrl(MSR_AMD64_LS_CFG, msrval);
+}
+
 #ifdef RETPOLINE
 static bool sp

[PATCH 4.16 086/110] x86/speculation: Make "seccomp" the default mode for Speculative Store Bypass

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Kees Cook 

commit f21b53b20c754021935ea43364dbf53778eeba32 upstream

Unless explicitly opted out of, anything running under seccomp will have
SSB mitigations enabled. Choosing the "prctl" mode will disable this.

[ tglx: Adjusted it to the new arch_seccomp_spec_mitigate() mechanism ]

Signed-off-by: Kees Cook 
Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 Documentation/admin-guide/kernel-parameters.txt |   26 ---
 arch/x86/include/asm/nospec-branch.h|1 
 arch/x86/kernel/cpu/bugs.c  |   32 +---
 3 files changed, 41 insertions(+), 18 deletions(-)

--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -4021,19 +4021,27 @@
This parameter controls whether the Speculative Store
Bypass optimization is used.
 
-   on - Unconditionally disable Speculative Store 
Bypass
-   off- Unconditionally enable Speculative Store Bypass
-   auto   - Kernel detects whether the CPU model contains 
an
-implementation of Speculative Store Bypass and
-picks the most appropriate mitigation.
-   prctl  - Control Speculative Store Bypass per thread
-via prctl. Speculative Store Bypass is enabled
-for a process by default. The state of the 
control
-is inherited on fork.
+   on  - Unconditionally disable Speculative Store 
Bypass
+   off - Unconditionally enable Speculative Store 
Bypass
+   auto- Kernel detects whether the CPU model contains 
an
+ implementation of Speculative Store Bypass and
+ picks the most appropriate mitigation. If the
+ CPU is not vulnerable, "off" is selected. If 
the
+ CPU is vulnerable the default mitigation is
+ architecture and Kconfig dependent. See below.
+   prctl   - Control Speculative Store Bypass per thread
+ via prctl. Speculative Store Bypass is enabled
+ for a process by default. The state of the 
control
+ is inherited on fork.
+   seccomp - Same as "prctl" above, but all seccomp threads
+ will disable SSB unless they explicitly opt 
out.
 
Not specifying this option is equivalent to
spec_store_bypass_disable=auto.
 
+   Default mitigations:
+   X86:If CONFIG_SECCOMP=y "seccomp", otherwise "prctl"
+
spia_io_base=   [HW,MTD]
spia_fio_base=
spia_pedr=
--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -233,6 +233,7 @@ enum ssb_mitigation {
SPEC_STORE_BYPASS_NONE,
SPEC_STORE_BYPASS_DISABLE,
SPEC_STORE_BYPASS_PRCTL,
+   SPEC_STORE_BYPASS_SECCOMP,
 };
 
 extern char __indirect_thunk_start[];
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -416,22 +416,25 @@ enum ssb_mitigation_cmd {
SPEC_STORE_BYPASS_CMD_AUTO,
SPEC_STORE_BYPASS_CMD_ON,
SPEC_STORE_BYPASS_CMD_PRCTL,
+   SPEC_STORE_BYPASS_CMD_SECCOMP,
 };
 
 static const char *ssb_strings[] = {
[SPEC_STORE_BYPASS_NONE]= "Vulnerable",
[SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass 
disabled",
-   [SPEC_STORE_BYPASS_PRCTL]   = "Mitigation: Speculative Store Bypass 
disabled via prctl"
+   [SPEC_STORE_BYPASS_PRCTL]   = "Mitigation: Speculative Store Bypass 
disabled via prctl",
+   [SPEC_STORE_BYPASS_SECCOMP] = "Mitigation: Speculative Store Bypass 
disabled via prctl and seccomp",
 };
 
 static const struct {
const char *option;
enum ssb_mitigation_cmd cmd;
 } ssb_mitigation_options[] = {
-   { "auto",   SPEC_STORE_BYPASS_CMD_AUTO },  /* Platform decides */
-   { "on", SPEC_STORE_BYPASS_CMD_ON },/* Disable Speculative 
Store Bypass */
-   { "off",SPEC_STORE_BYPASS_CMD_NONE },  /* Don't touch 
Speculative Store Bypass */
-   { "prctl",  SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative 
Store Bypass via prctl */
+   { "auto",   SPEC_STORE_BYPASS_CMD_AUTO },/* Platform decides */
+   { "on", SPEC_STORE_BYPASS_CMD_ON },  /* Disable Speculative 
Store Bypass */
+   { "off",SPEC_STORE_BYPASS_CMD_NONE },/* Don't touch 
S

[PATCH 4.16 064/110] x86/bugs: Concentrate bug reporting into a separate function

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Konrad Rzeszutek Wilk 

commit d1059518b4789cabe34bb4b714d07e6089c82ca1 upstream

Those SysFS functions have a similar preamble, as such make common
code to handle them.

Suggested-by: Borislav Petkov 
Signed-off-by: Konrad Rzeszutek Wilk 
Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Ingo Molnar 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/kernel/cpu/bugs.c |   46 +++--
 1 file changed, 32 insertions(+), 14 deletions(-)

--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -314,30 +314,48 @@ retpoline_auto:
 #undef pr_fmt
 
 #ifdef CONFIG_SYSFS
-ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, 
char *buf)
+
+ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
+   char *buf, unsigned int bug)
 {
-   if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
+   if (!boot_cpu_has_bug(bug))
return sprintf(buf, "Not affected\n");
-   if (boot_cpu_has(X86_FEATURE_PTI))
-   return sprintf(buf, "Mitigation: PTI\n");
+
+   switch (bug) {
+   case X86_BUG_CPU_MELTDOWN:
+   if (boot_cpu_has(X86_FEATURE_PTI))
+   return sprintf(buf, "Mitigation: PTI\n");
+
+   break;
+
+   case X86_BUG_SPECTRE_V1:
+   return sprintf(buf, "Mitigation: __user pointer 
sanitization\n");
+
+   case X86_BUG_SPECTRE_V2:
+   return sprintf(buf, "%s%s%s%s\n", 
spectre_v2_strings[spectre_v2_enabled],
+  boot_cpu_has(X86_FEATURE_USE_IBPB) ? ", IBPB" : 
"",
+  boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", 
IBRS_FW" : "",
+  spectre_v2_module_string());
+
+   default:
+   break;
+   }
+
return sprintf(buf, "Vulnerable\n");
 }
 
+ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, 
char *buf)
+{
+   return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
+}
+
 ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, 
char *buf)
 {
-   if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1))
-   return sprintf(buf, "Not affected\n");
-   return sprintf(buf, "Mitigation: __user pointer sanitization\n");
+   return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
 }
 
 ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, 
char *buf)
 {
-   if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
-   return sprintf(buf, "Not affected\n");
-
-   return sprintf(buf, "%s%s%s%s\n", 
spectre_v2_strings[spectre_v2_enabled],
-  boot_cpu_has(X86_FEATURE_USE_IBPB) ? ", IBPB" : "",
-  boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
-  spectre_v2_module_string());
+   return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
 }
 #endif




[PATCH 4.16 089/110] Documentation/spec_ctrl: Do some minor cleanups

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Borislav Petkov 

commit dd0792699c4058e63c0715d9a7c2d40226fcdddc upstream

Fix some typos, improve formulations, end sentences with a fullstop.

Signed-off-by: Borislav Petkov 
Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 Documentation/userspace-api/spec_ctrl.rst |   24 
 1 file changed, 12 insertions(+), 12 deletions(-)

--- a/Documentation/userspace-api/spec_ctrl.rst
+++ b/Documentation/userspace-api/spec_ctrl.rst
@@ -2,13 +2,13 @@
 Speculation Control
 ===
 
-Quite some CPUs have speculation related misfeatures which are in fact
-vulnerabilites causing data leaks in various forms even accross privilege
-domains.
+Quite some CPUs have speculation-related misfeatures which are in
+fact vulnerabilities causing data leaks in various forms even across
+privilege domains.
 
 The kernel provides mitigation for such vulnerabilities in various
-forms. Some of these mitigations are compile time configurable and some on
-the kernel command line.
+forms. Some of these mitigations are compile-time configurable and some
+can be supplied on the kernel command line.
 
 There is also a class of mitigations which are very expensive, but they can
 be restricted to a certain set of processes or tasks in controlled
@@ -32,18 +32,18 @@ the following meaning:
 Bit  DefineDescription
  = ===
 0PR_SPEC_PRCTL Mitigation can be controlled per task by
-   PR_SET_SPECULATION_CTRL
+   PR_SET_SPECULATION_CTRL.
 1PR_SPEC_ENABLEThe speculation feature is enabled, mitigation is
-   disabled
+   disabled.
 2PR_SPEC_DISABLE   The speculation feature is disabled, mitigation is
-   enabled
+   enabled.
 3PR_SPEC_FORCE_DISABLE Same as PR_SPEC_DISABLE, but cannot be undone. A
subsequent prctl(..., PR_SPEC_ENABLE) will fail.
  = ===
 
 If all bits are 0 the CPU is not affected by the speculation misfeature.
 
-If PR_SPEC_PRCTL is set, then the per task control of the mitigation is
+If PR_SPEC_PRCTL is set, then the per-task control of the mitigation is
 available. If not set, prctl(PR_SET_SPECULATION_CTRL) for the speculation
 misfeature will fail.
 
@@ -61,9 +61,9 @@ Common error codes
 Value   Meaning
 === =
 EINVAL  The prctl is not implemented by the architecture or unused
-prctl(2) arguments are not 0
+prctl(2) arguments are not 0.
 
-ENODEV  arg2 is selecting a not supported speculation misfeature
+ENODEV  arg2 is selecting a not supported speculation misfeature.
 === =
 
 PR_SET_SPECULATION_CTRL error codes
@@ -74,7 +74,7 @@ Value   Meaning
 0   Success
 
 ERANGE  arg3 is incorrect, i.e. it's neither PR_SPEC_ENABLE nor
-PR_SPEC_DISABLE nor PR_SPEC_FORCE_DISABLE
+PR_SPEC_DISABLE nor PR_SPEC_FORCE_DISABLE.
 
 ENXIO   Control of the selected speculation misfeature is not possible.
 See PR_GET_SPECULATION_CTRL.




[PATCH 4.16 090/110] x86/bugs: Fix __ssb_select_mitigation() return type

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Jiri Kosina 

commit d66d8ff3d21667b41eddbe86b35ab411e40d8c5f upstream

__ssb_select_mitigation() returns one of the members of enum ssb_mitigation,
not ssb_mitigation_cmd; fix the prototype to reflect that.

Fixes: 24f7fc83b9204 ("x86/bugs: Provide boot parameters for the 
spec_store_bypass_disable mitigation")
Signed-off-by: Jiri Kosina 
Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/kernel/cpu/bugs.c |2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -468,7 +468,7 @@ static enum ssb_mitigation_cmd __init ss
return cmd;
 }
 
-static enum ssb_mitigation_cmd __init __ssb_select_mitigation(void)
+static enum ssb_mitigation __init __ssb_select_mitigation(void)
 {
enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
enum ssb_mitigation_cmd cmd;




[PATCH 4.16 078/110] nospec: Allow getting/setting on non-current task

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Kees Cook 

commit 7bbf1373e228840bb0295a2ca26d548ef37f448e upstream

Adjust arch_prctl_get/set_spec_ctrl() to operate on tasks other than
current.

This is needed both for /proc/$pid/status queries and for seccomp (since
thread-syncing can trigger seccomp in non-current threads).

Signed-off-by: Kees Cook 
Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/kernel/cpu/bugs.c |   27 ---
 include/linux/nospec.h |7 +--
 kernel/sys.c   |9 +
 3 files changed, 26 insertions(+), 17 deletions(-)

--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -530,31 +530,35 @@ static void ssb_select_mitigation()
 
 #undef pr_fmt
 
-static int ssb_prctl_set(unsigned long ctrl)
+static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
 {
-   bool rds = !!test_tsk_thread_flag(current, TIF_RDS);
+   bool rds = !!test_tsk_thread_flag(task, TIF_RDS);
 
if (ssb_mode != SPEC_STORE_BYPASS_PRCTL)
return -ENXIO;
 
if (ctrl == PR_SPEC_ENABLE)
-   clear_tsk_thread_flag(current, TIF_RDS);
+   clear_tsk_thread_flag(task, TIF_RDS);
else
-   set_tsk_thread_flag(current, TIF_RDS);
+   set_tsk_thread_flag(task, TIF_RDS);
 
-   if (rds != !!test_tsk_thread_flag(current, TIF_RDS))
+   /*
+* If being set on non-current task, delay setting the CPU
+* mitigation until it is next scheduled.
+*/
+   if (task == current && rds != !!test_tsk_thread_flag(task, TIF_RDS))
speculative_store_bypass_update();
 
return 0;
 }
 
-static int ssb_prctl_get(void)
+static int ssb_prctl_get(struct task_struct *task)
 {
switch (ssb_mode) {
case SPEC_STORE_BYPASS_DISABLE:
return PR_SPEC_DISABLE;
case SPEC_STORE_BYPASS_PRCTL:
-   if (test_tsk_thread_flag(current, TIF_RDS))
+   if (test_tsk_thread_flag(task, TIF_RDS))
return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
default:
@@ -564,24 +568,25 @@ static int ssb_prctl_get(void)
}
 }
 
-int arch_prctl_spec_ctrl_set(unsigned long which, unsigned long ctrl)
+int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
+unsigned long ctrl)
 {
if (ctrl != PR_SPEC_ENABLE && ctrl != PR_SPEC_DISABLE)
return -ERANGE;
 
switch (which) {
case PR_SPEC_STORE_BYPASS:
-   return ssb_prctl_set(ctrl);
+   return ssb_prctl_set(task, ctrl);
default:
return -ENODEV;
}
 }
 
-int arch_prctl_spec_ctrl_get(unsigned long which)
+int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
 {
switch (which) {
case PR_SPEC_STORE_BYPASS:
-   return ssb_prctl_get();
+   return ssb_prctl_get(task);
default:
return -ENODEV;
}
--- a/include/linux/nospec.h
+++ b/include/linux/nospec.h
@@ -7,6 +7,8 @@
 #define _LINUX_NOSPEC_H
 #include 
 
+struct task_struct;
+
 /**
  * array_index_mask_nospec() - generate a ~0 mask when index < size, 0 
otherwise
  * @index: array element index
@@ -57,7 +59,8 @@ static inline unsigned long array_index_
 })
 
 /* Speculation control prctl */
-int arch_prctl_spec_ctrl_get(unsigned long which);
-int arch_prctl_spec_ctrl_set(unsigned long which, unsigned long ctrl);
+int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which);
+int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
+unsigned long ctrl);
 
 #endif /* _LINUX_NOSPEC_H */
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -2192,12 +2192,13 @@ static int propagate_has_child_subreaper
return 1;
 }
 
-int __weak arch_prctl_spec_ctrl_get(unsigned long which)
+int __weak arch_prctl_spec_ctrl_get(struct task_struct *t, unsigned long which)
 {
return -EINVAL;
 }
 
-int __weak arch_prctl_spec_ctrl_set(unsigned long which, unsigned long ctrl)
+int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which,
+   unsigned long ctrl)
 {
return -EINVAL;
 }
@@ -2413,12 +2414,12 @@ SYSCALL_DEFINE5(prctl, int, option, unsi
case PR_GET_SPECULATION_CTRL:
if (arg3 || arg4 || arg5)
return -EINVAL;
-   error = arch_prctl_spec_ctrl_get(arg2);
+   error = arch_prctl_spec_ctrl_get(me, arg2);
break;
case PR_SET_SPECULATION_CTRL:
if (arg4 || arg5)
return -EINVAL;
-   error = arch_prctl_spec_ctrl_set(arg2, arg3);
+   error = arch_prctl_spec_ctrl_set(me, arg2, arg3)

[PATCH 4.16 065/110] x86/bugs: Read SPEC_CTRL MSR during boot and re-use reserved bits

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Konrad Rzeszutek Wilk 

commit 1b86883ccb8d5d9506529d42dbe1a5257cb30b18 upstream

The 336996-Speculative-Execution-Side-Channel-Mitigations.pdf refers to all
the other bits as reserved. The Intel SDM glossary defines reserved as
implementation specific - aka unknown.

As such at bootup this must be taken it into account and proper masking for
the bits in use applied.

A copy of this document is available at
https://bugzilla.kernel.org/show_bug.cgi?id=199511

[ tglx: Made x86_spec_ctrl_base __ro_after_init ]

Suggested-by: Jon Masters 
Signed-off-by: Konrad Rzeszutek Wilk 
Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Ingo Molnar 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/nospec-branch.h |   24 
 arch/x86/kernel/cpu/bugs.c   |   28 
 2 files changed, 48 insertions(+), 4 deletions(-)

--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -217,6 +217,17 @@ enum spectre_v2_mitigation {
SPECTRE_V2_IBRS,
 };
 
+/*
+ * The Intel specification for the SPEC_CTRL MSR requires that we
+ * preserve any already set reserved bits at boot time (e.g. for
+ * future additions that this kernel is not currently aware of).
+ * We then set any additional mitigation bits that we want
+ * ourselves and always use this as the base for SPEC_CTRL.
+ * We also use this when handling guest entry/exit as below.
+ */
+extern void x86_spec_ctrl_set(u64);
+extern u64 x86_spec_ctrl_get_default(void);
+
 extern char __indirect_thunk_start[];
 extern char __indirect_thunk_end[];
 
@@ -254,8 +265,9 @@ void alternative_msr_write(unsigned int
 
 static inline void indirect_branch_prediction_barrier(void)
 {
-   alternative_msr_write(MSR_IA32_PRED_CMD, PRED_CMD_IBPB,
- X86_FEATURE_USE_IBPB);
+   u64 val = PRED_CMD_IBPB;
+
+   alternative_msr_write(MSR_IA32_PRED_CMD, val, X86_FEATURE_USE_IBPB);
 }
 
 /*
@@ -266,14 +278,18 @@ static inline void indirect_branch_predi
  */
 #define firmware_restrict_branch_speculation_start()   \
 do {   \
+   u64 val = x86_spec_ctrl_get_default() | SPEC_CTRL_IBRS; \
+   \
preempt_disable();  \
-   alternative_msr_write(MSR_IA32_SPEC_CTRL, SPEC_CTRL_IBRS,   \
+   alternative_msr_write(MSR_IA32_SPEC_CTRL, val,  \
  X86_FEATURE_USE_IBRS_FW); \
 } while (0)
 
 #define firmware_restrict_branch_speculation_end() \
 do {   \
-   alternative_msr_write(MSR_IA32_SPEC_CTRL, 0,\
+   u64 val = x86_spec_ctrl_get_default();  \
+   \
+   alternative_msr_write(MSR_IA32_SPEC_CTRL, val,  \
  X86_FEATURE_USE_IBRS_FW); \
preempt_enable();   \
 } while (0)
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -28,6 +28,12 @@
 
 static void __init spectre_v2_select_mitigation(void);
 
+/*
+ * Our boot-time value of the SPEC_CTRL MSR. We read it once so that any
+ * writes to SPEC_CTRL contain whatever reserved bits have been set.
+ */
+static u64 __ro_after_init x86_spec_ctrl_base;
+
 void __init check_bugs(void)
 {
identify_boot_cpu();
@@ -37,6 +43,13 @@ void __init check_bugs(void)
print_cpu_info(&boot_cpu_data);
}
 
+   /*
+* Read the SPEC_CTRL MSR to account for reserved bits which may
+* have unknown values.
+*/
+   if (boot_cpu_has(X86_FEATURE_IBRS))
+   rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
+
/* Select the proper spectre mitigation before patching alternatives */
spectre_v2_select_mitigation();
 
@@ -95,6 +108,21 @@ static const char *spectre_v2_strings[]
 
 static enum spectre_v2_mitigation spectre_v2_enabled = SPECTRE_V2_NONE;
 
+void x86_spec_ctrl_set(u64 val)
+{
+   if (val & ~SPEC_CTRL_IBRS)
+   WARN_ONCE(1, "SPEC_CTRL MSR value 0x%16llx is unknown.\n", val);
+   else
+   wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base | val);
+}
+EXPORT_SYMBOL_GPL(x86_spec_ctrl_set);
+
+u64 x86_spec_ctrl_get_default(void)
+{
+   return x86_spec_ctrl_base;
+}
+EXPORT_SYMBOL_GPL(x86_spec_ctrl_get_default);
+
 #ifdef RETPOLINE
 static bool spectre_v2_bad_module;
 




[PATCH 4.16 055/110] Btrfs: fix xattr loss after power failure

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Filipe Manana 

commit 9a8fca62aacc1599fea8e813d01e1955513e4fad upstream.

If a file has xattrs, we fsync it, to ensure we clear the flags
BTRFS_INODE_NEEDS_FULL_SYNC and BTRFS_INODE_COPY_EVERYTHING from its
inode, the current transaction commits and then we fsync it (without
either of those bits being set in its inode), we end up not logging
all its xattrs. This results in deleting all xattrs when replying the
log after a power failure.

Trivial reproducer

  $ mkfs.btrfs -f /dev/sdb
  $ mount /dev/sdb /mnt

  $ touch /mnt/foobar
  $ setfattr -n user.xa -v qwerty /mnt/foobar
  $ xfs_io -c "fsync" /mnt/foobar

  $ sync

  $ xfs_io -c "pwrite -S 0xab 0 64K" /mnt/foobar
  $ xfs_io -c "fsync" /mnt/foobar
  

  $ mount /dev/sdb /mnt
  $ getfattr --absolute-names --dump /mnt/foobar
  
  $

So fix this by making sure all xattrs are logged if we log a file's inode
item and neither the flags BTRFS_INODE_NEEDS_FULL_SYNC nor
BTRFS_INODE_COPY_EVERYTHING were set in the inode.

Fixes: 36283bf777d9 ("Btrfs: fix fsync xattr loss in the fast fsync path")
Cc:  # 4.2+
Signed-off-by: Filipe Manana 
Signed-off-by: David Sterba 
Signed-off-by: Greg Kroah-Hartman 

---
 fs/btrfs/tree-log.c |7 +++
 1 file changed, 7 insertions(+)

--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -4749,6 +4749,7 @@ static int btrfs_log_inode(struct btrfs_
struct extent_map_tree *em_tree = &inode->extent_tree;
u64 logged_isize = 0;
bool need_log_inode_item = true;
+   bool xattrs_logged = false;
 
path = btrfs_alloc_path();
if (!path)
@@ -5050,6 +5051,7 @@ next_key:
err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
if (err)
goto out_unlock;
+   xattrs_logged = true;
if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
btrfs_release_path(path);
btrfs_release_path(dst_path);
@@ -5062,6 +5064,11 @@ log_extents:
btrfs_release_path(dst_path);
if (need_log_inode_item) {
err = log_inode_item(trans, log, dst_path, inode);
+   if (!err && !xattrs_logged) {
+   err = btrfs_log_all_xattrs(trans, root, inode, path,
+  dst_path);
+   btrfs_release_path(path);
+   }
if (err)
goto out_unlock;
}




[PATCH 4.16 053/110] ARM: 8770/1: kprobes: Prohibit probing on optimized_callback

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Masami Hiramatsu 

commit 70948c05fdde0aac32f9667856a88725c192fa40 upstream.

Prohibit probing on optimized_callback() because
it is called from kprobes itself. If we put a kprobes
on it, that will cause a recursive call loop.
Mark it NOKPROBE_SYMBOL.

Fixes: 0dc016dbd820 ("ARM: kprobes: enable OPTPROBES for ARM 32")
Signed-off-by: Masami Hiramatsu 
Cc: sta...@vger.kernel.org
Signed-off-by: Russell King 
Signed-off-by: Greg Kroah-Hartman 

---
 arch/arm/probes/kprobes/opt-arm.c |1 +
 1 file changed, 1 insertion(+)

--- a/arch/arm/probes/kprobes/opt-arm.c
+++ b/arch/arm/probes/kprobes/opt-arm.c
@@ -192,6 +192,7 @@ optimized_callback(struct optimized_kpro
 
local_irq_restore(flags);
 }
+NOKPROBE_SYMBOL(optimized_callback)
 
 int arch_prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe 
*orig)
 {




[PATCH 4.16 057/110] btrfs: property: Set incompat flag if lzo/zstd compression is set

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Misono Tomohiro 

commit 1a63c198ddb810c790101d693c7071cca703b3c7 upstream.

Incompat flag of LZO/ZSTD compression should be set at:

 1. mount time (-o compress/compress-force)
 2. when defrag is done
 3. when property is set

Currently 3. is missing and this commit adds this.

This could lead to a filesystem that uses ZSTD but is not marked as
such. If a kernel without a ZSTD support encounteres a ZSTD compressed
extent, it will handle that but this could be confusing to the user.

Typically the filesystem is mounted with the ZSTD option, but the
discrepancy can arise when a filesystem is never mounted with ZSTD and
then the property on some file is set (and some new extents are
written). A simple mount with -o compress=zstd will fix that up on an
unpatched kernel.

Same goes for LZO, but this has been around for a very long time
(2.6.37) so it's unlikely that a pre-LZO kernel would be used.

Fixes: 5c1aab1dd544 ("btrfs: Add zstd support")
CC: sta...@vger.kernel.org # 4.14+
Signed-off-by: Tomohiro Misono 
Reviewed-by: Anand Jain 
Reviewed-by: David Sterba 
[ add user visible impact ]
Signed-off-by: David Sterba 
Signed-off-by: Greg Kroah-Hartman 

---
 fs/btrfs/props.c |   12 
 1 file changed, 8 insertions(+), 4 deletions(-)

--- a/fs/btrfs/props.c
+++ b/fs/btrfs/props.c
@@ -393,6 +393,7 @@ static int prop_compression_apply(struct
  const char *value,
  size_t len)
 {
+   struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
int type;
 
if (len == 0) {
@@ -403,14 +404,17 @@ static int prop_compression_apply(struct
return 0;
}
 
-   if (!strncmp("lzo", value, 3))
+   if (!strncmp("lzo", value, 3)) {
type = BTRFS_COMPRESS_LZO;
-   else if (!strncmp("zlib", value, 4))
+   btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
+   } else if (!strncmp("zlib", value, 4)) {
type = BTRFS_COMPRESS_ZLIB;
-   else if (!strncmp("zstd", value, len))
+   } else if (!strncmp("zstd", value, len)) {
type = BTRFS_COMPRESS_ZSTD;
-   else
+   btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
+   } else {
return -EINVAL;
+   }
 
BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;




[PATCH 4.16 035/110] cpufreq: armada-37xx: driver relies on cpufreq-dt

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Miquel Raynal 

commit 0cf442c6bcf572e04f5690340d5b8e62afcee2ca upstream.

Armada-37xx driver registers a cpufreq-dt driver. Not having
CONFIG_CPUFREQ_DT selected leads to a silent abort during the probe.
Prevent that situation by having the former depending on the latter.

Fixes: 92ce45fb875d7 (cpufreq: Add DVFS support for Armada 37xx)
Cc: 4.16+  # 4.16+
Signed-off-by: Miquel Raynal 
Acked-by: Viresh Kumar 
Signed-off-by: Rafael J. Wysocki 
Signed-off-by: Greg Kroah-Hartman 

---
 drivers/cpufreq/Kconfig.arm |2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

--- a/drivers/cpufreq/Kconfig.arm
+++ b/drivers/cpufreq/Kconfig.arm
@@ -20,7 +20,7 @@ config ACPI_CPPC_CPUFREQ
 
 config ARM_ARMADA_37XX_CPUFREQ
tristate "Armada 37xx CPUFreq support"
-   depends on ARCH_MVEBU
+   depends on ARCH_MVEBU && CPUFREQ_DT
help
  This adds the CPUFreq driver support for Marvell Armada 37xx SoCs.
  The Armada 37xx PMU supports 4 frequency and VDD levels.




[PATCH 4.16 059/110] btrfs: Split btrfs_del_delalloc_inode into 2 functions

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Nikolay Borisov 

commit 2b8773313494ede83a26fb372466e634564002ed upstream.

This is in preparation of fixing delalloc inodes leakage on transaction
abort. Also export the new function.

Signed-off-by: Nikolay Borisov 
Reviewed-by: David Sterba 
Reviewed-by: Anand Jain 
Signed-off-by: David Sterba 
Signed-off-by: Greg Kroah-Hartman 

---
 fs/btrfs/ctree.h |2 ++
 fs/btrfs/inode.c |   13 ++---
 2 files changed, 12 insertions(+), 3 deletions(-)

--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -3153,6 +3153,8 @@ noinline int can_nocow_extent(struct ino
  u64 *orig_start, u64 *orig_block_len,
  u64 *ram_bytes);
 
+void __btrfs_del_delalloc_inode(struct btrfs_root *root,
+   struct btrfs_inode *inode);
 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry);
 int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index);
 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -1762,12 +1762,12 @@ static void btrfs_add_delalloc_inodes(st
spin_unlock(&root->delalloc_lock);
 }
 
-static void btrfs_del_delalloc_inode(struct btrfs_root *root,
-struct btrfs_inode *inode)
+
+void __btrfs_del_delalloc_inode(struct btrfs_root *root,
+   struct btrfs_inode *inode)
 {
struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
 
-   spin_lock(&root->delalloc_lock);
if (!list_empty(&inode->delalloc_inodes)) {
list_del_init(&inode->delalloc_inodes);
clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
@@ -1780,6 +1780,13 @@ static void btrfs_del_delalloc_inode(str
spin_unlock(&fs_info->delalloc_root_lock);
}
}
+}
+
+static void btrfs_del_delalloc_inode(struct btrfs_root *root,
+struct btrfs_inode *inode)
+{
+   spin_lock(&root->delalloc_lock);
+   __btrfs_del_delalloc_inode(root, inode);
spin_unlock(&root->delalloc_lock);
 }
 




[PATCH 4.16 051/110] tick/broadcast: Use for_each_cpu() specially on UP kernels

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Dexuan Cui 

commit 5596fe34495cf0f645f417eb928ef224df3e3cb4 upstream.

for_each_cpu() unintuitively reports CPU0 as set independent of the actual
cpumask content on UP kernels. This causes an unexpected PIT interrupt
storm on a UP kernel running in an SMP virtual machine on Hyper-V, and as
a result, the virtual machine can suffer from a strange random delay of 1~20
minutes during boot-up, and sometimes it can hang forever.

Protect if by checking whether the cpumask is empty before entering the
for_each_cpu() loop.

[ tglx: Use !IS_ENABLED(CONFIG_SMP) instead of #ifdeffery ]

Signed-off-by: Dexuan Cui 
Signed-off-by: Thomas Gleixner 
Cc: Josh Poulson 
Cc: "Michael Kelley (EOSG)" 
Cc: Peter Zijlstra 
Cc: Frederic Weisbecker 
Cc: sta...@vger.kernel.org
Cc: Rakib Mullick 
Cc: Jork Loeser 
Cc: Greg Kroah-Hartman 
Cc: Andrew Morton 
Cc: KY Srinivasan 
Cc: Linus Torvalds 
Cc: Alexey Dobriyan 
Cc: Dmitry Vyukov 
Link: 
https://lkml.kernel.org/r/kl1p15301mb000678289fe55ba365b3279abf...@kl1p15301mb0006.apcp153.prod.outlook.com
Link: 
https://lkml.kernel.org/r/kl1p15301mb0006fa63bc22beb64902eaa0bf...@kl1p15301mb0006.apcp153.prod.outlook.com
Signed-off-by: Greg Kroah-Hartman 

---
 kernel/time/tick-broadcast.c |8 
 1 file changed, 8 insertions(+)

--- a/kernel/time/tick-broadcast.c
+++ b/kernel/time/tick-broadcast.c
@@ -612,6 +612,14 @@ static void tick_handle_oneshot_broadcas
now = ktime_get();
/* Find all expired events */
for_each_cpu(cpu, tick_broadcast_oneshot_mask) {
+   /*
+* Required for !SMP because for_each_cpu() reports
+* unconditionally CPU0 as set on UP kernels.
+*/
+   if (!IS_ENABLED(CONFIG_SMP) &&
+   cpumask_empty(tick_broadcast_oneshot_mask))
+   break;
+
td = &per_cpu(tick_cpu_device, cpu);
if (td->evtdev->next_event <= now) {
cpumask_set_cpu(cpu, tmpmask);




[PATCH 4.16 048/110] ARM: 8771/1: kprobes: Prohibit kprobes on do_undefinstr

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Masami Hiramatsu 

commit eb0146daefdde65665b7f076fbff7b49dade95b9 upstream.

Prohibit kprobes on do_undefinstr because kprobes on
arm is implemented by undefined instruction. This means
if we probe do_undefinstr(), it can cause infinit
recursive exception.

Fixes: 24ba613c9d6c ("ARM kprobes: core code")
Signed-off-by: Masami Hiramatsu 
Cc: sta...@vger.kernel.org
Signed-off-by: Russell King 
Signed-off-by: Greg Kroah-Hartman 

---
 arch/arm/kernel/traps.c |5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

--- a/arch/arm/kernel/traps.c
+++ b/arch/arm/kernel/traps.c
@@ -19,6 +19,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -417,7 +418,8 @@ void unregister_undef_hook(struct undef_
raw_spin_unlock_irqrestore(&undef_lock, flags);
 }
 
-static int call_undef_hook(struct pt_regs *regs, unsigned int instr)
+static nokprobe_inline
+int call_undef_hook(struct pt_regs *regs, unsigned int instr)
 {
struct undef_hook *hook;
unsigned long flags;
@@ -490,6 +492,7 @@ die_sig:
 
arm_notify_die("Oops - undefined instruction", regs, &info, 0, 6);
 }
+NOKPROBE_SYMBOL(do_undefinstr)
 
 /*
  * Handle FIQ similarly to NMI on x86 systems.




[PATCH 4.16 042/110] s390/qdio: dont release memory in qdio_setup_irq()

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Julian Wiedmann 

commit 2e68adcd2fb21b7188ba449f0fab3bee2910e500 upstream.

Calling qdio_release_memory() on error is just plain wrong. It frees
the main qdio_irq struct, when following code still uses it.

Also, no other error path in qdio_establish() does this. So trust
callers to clean up via qdio_free() if some step of the QDIO
initialization fails.

Fixes: 779e6e1c724d ("[S390] qdio: new qdio driver.")
Cc:  #v2.6.27+
Signed-off-by: Julian Wiedmann 
Signed-off-by: Martin Schwidefsky 
Signed-off-by: Greg Kroah-Hartman 

---
 drivers/s390/cio/qdio_setup.c |   10 ++
 1 file changed, 2 insertions(+), 8 deletions(-)

--- a/drivers/s390/cio/qdio_setup.c
+++ b/drivers/s390/cio/qdio_setup.c
@@ -456,7 +456,6 @@ int qdio_setup_irq(struct qdio_initializ
 {
struct ciw *ciw;
struct qdio_irq *irq_ptr = init_data->cdev->private->qdio_data;
-   int rc;
 
memset(&irq_ptr->qib, 0, sizeof(irq_ptr->qib));
memset(&irq_ptr->siga_flag, 0, sizeof(irq_ptr->siga_flag));
@@ -493,16 +492,14 @@ int qdio_setup_irq(struct qdio_initializ
ciw = ccw_device_get_ciw(init_data->cdev, CIW_TYPE_EQUEUE);
if (!ciw) {
DBF_ERROR("%4x NO EQ", irq_ptr->schid.sch_no);
-   rc = -EINVAL;
-   goto out_err;
+   return -EINVAL;
}
irq_ptr->equeue = *ciw;
 
ciw = ccw_device_get_ciw(init_data->cdev, CIW_TYPE_AQUEUE);
if (!ciw) {
DBF_ERROR("%4x NO AQ", irq_ptr->schid.sch_no);
-   rc = -EINVAL;
-   goto out_err;
+   return -EINVAL;
}
irq_ptr->aqueue = *ciw;
 
@@ -510,9 +507,6 @@ int qdio_setup_irq(struct qdio_initializ
irq_ptr->orig_handler = init_data->cdev->handler;
init_data->cdev->handler = qdio_int_handler;
return 0;
-out_err:
-   qdio_release_memory(irq_ptr);
-   return rc;
 }
 
 void qdio_print_subchannel_info(struct qdio_irq *irq_ptr,




[PATCH 4.16 041/110] s390/cpum_sf: ensure sample frequency of perf event attributes is non-zero

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Hendrik Brueckner 

commit 4bbaf2584b86b0772413edeac22ff448f36351b1 upstream.

Correct a trinity finding for the perf_event_open() system call with
a perf event attribute structure that uses a frequency but has the
sampling frequency set to zero.  This causes a FP divide exception during
the sample rate initialization for the hardware sampling facility.

Fixes: 8c069ff4bd606 ("s390/perf: add support for the CPU-Measurement Sampling 
Facility")
Cc: sta...@vger.kernel.org # 3.14+
Reviewed-by: Heiko Carstens 
Signed-off-by: Hendrik Brueckner 
Signed-off-by: Martin Schwidefsky 
Signed-off-by: Greg Kroah-Hartman 

---
 arch/s390/kernel/perf_cpum_sf.c |4 
 1 file changed, 4 insertions(+)

--- a/arch/s390/kernel/perf_cpum_sf.c
+++ b/arch/s390/kernel/perf_cpum_sf.c
@@ -753,6 +753,10 @@ static int __hw_perf_event_init(struct p
 */
rate = 0;
if (attr->freq) {
+   if (!attr->sample_freq) {
+   err = -EINVAL;
+   goto out;
+   }
rate = freq_to_sample_rate(&si, attr->sample_freq);
rate = hw_limit_rate(&si, rate);
attr->freq = 0;




[PATCH 4.16 023/110] vsprintf: Replace memory barrier with static_key for random_ptr_key update

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Steven Rostedt (VMware) 

commit 85f4f12d51397f1648e1f4350f77e24039b82d61 upstream.

Reviewing Tobin's patches for getting pointers out early before
entropy has been established, I noticed that there's a lone smp_mb() in
the code. As with most lone memory barriers, this one appears to be
incorrectly used.

We currently basically have this:

get_random_bytes(&ptr_key, sizeof(ptr_key));
/*
 * have_filled_random_ptr_key==true is dependent on get_random_bytes().
 * ptr_to_id() needs to see have_filled_random_ptr_key==true
 * after get_random_bytes() returns.
 */
smp_mb();
WRITE_ONCE(have_filled_random_ptr_key, true);

And later we have:

if (unlikely(!have_filled_random_ptr_key))
return string(buf, end, "(ptrval)", spec);

/* Missing memory barrier here. */

hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);

As the CPU can perform speculative loads, we could have a situation
with the following:

CPU0CPU1

   load ptr_key = 0
   store ptr_key = random
   smp_mb()
   store have_filled_random_ptr_key

   load have_filled_random_ptr_key = true

BAD BAD BAD! (you're so bad!)

Because nothing prevents CPU1 from loading ptr_key before loading
have_filled_random_ptr_key.

But this race is very unlikely, but we can't keep an incorrect smp_mb() in
place. Instead, replace the have_filled_random_ptr_key with a static_branch
not_filled_random_ptr_key, that is initialized to true and changed to false
when we get enough entropy. If the update happens in early boot, the
static_key is updated immediately, otherwise it will have to wait till
entropy is filled and this happens in an interrupt handler which can't
enable a static_key, as that requires a preemptible context. In that case, a
work_queue is used to enable it, as entropy already took too long to
establish in the first place waiting a little more shouldn't hurt anything.

The benefit of using the static key is that the unlikely branch in
vsprintf() now becomes a nop.

Link: http://lkml.kernel.org/r/20180515100558.21df5...@gandalf.local.home

Cc: sta...@vger.kernel.org
Fixes: ad67b74d2469d ("printk: hash addresses printed with %p")
Acked-by: Linus Torvalds 
Signed-off-by: Steven Rostedt (VMware) 
Signed-off-by: Greg Kroah-Hartman 

---
 lib/vsprintf.c |   26 +++---
 1 file changed, 15 insertions(+), 11 deletions(-)

--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1659,19 +1659,22 @@ char *pointer_string(char *buf, char *en
return number(buf, end, (unsigned long int)ptr, spec);
 }
 
-static bool have_filled_random_ptr_key __read_mostly;
+static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
 static siphash_key_t ptr_key __read_mostly;
 
-static void fill_random_ptr_key(struct random_ready_callback *unused)
+static void enable_ptr_key_workfn(struct work_struct *work)
 {
get_random_bytes(&ptr_key, sizeof(ptr_key));
-   /*
-* have_filled_random_ptr_key==true is dependent on get_random_bytes().
-* ptr_to_id() needs to see have_filled_random_ptr_key==true
-* after get_random_bytes() returns.
-*/
-   smp_mb();
-   WRITE_ONCE(have_filled_random_ptr_key, true);
+   /* Needs to run from preemptible context */
+   static_branch_disable(¬_filled_random_ptr_key);
+}
+
+static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
+
+static void fill_random_ptr_key(struct random_ready_callback *unused)
+{
+   /* This may be in an interrupt handler. */
+   queue_work(system_unbound_wq, &enable_ptr_key_work);
 }
 
 static struct random_ready_callback random_ready = {
@@ -1685,7 +1688,8 @@ static int __init initialize_ptr_random(
if (!ret) {
return 0;
} else if (ret == -EALREADY) {
-   fill_random_ptr_key(&random_ready);
+   /* This is in preemptible context */
+   enable_ptr_key_workfn(&enable_ptr_key_work);
return 0;
}
 
@@ -1699,7 +1703,7 @@ static char *ptr_to_id(char *buf, char *
unsigned long hashval;
const int default_width = 2 * sizeof(ptr);
 
-   if (unlikely(!have_filled_random_ptr_key)) {
+   if (static_branch_unlikely(¬_filled_random_ptr_key)) {
spec.field_width = default_width;
/* string length must be less than default_width */
return string(buf, end, "(ptrval)", spec);




[PATCH 4.16 040/110] s390/qdio: fix access to uninitialized qdio_q fields

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Julian Wiedmann 

commit e521813468f786271a87e78e8644243bead48fad upstream.

Ever since CQ/QAOB support was added, calling qdio_free() straight after
qdio_alloc() results in qdio_release_memory() accessing uninitialized
memory (ie. q->u.out.use_cq and q->u.out.aobs). Followed by a
kmem_cache_free() on the random AOB addresses.

For older kernels that don't have 6e30c549f6ca, the same applies if
qdio_establish() fails in the DEV_STATE_ONLINE check.

While initializing q->u.out.use_cq would be enough to fix this
particular bug, the more future-proof change is to just zero-alloc the
whole struct.

Fixes: 104ea556ee7f ("qdio: support asynchronous delivery of storage blocks")
Cc:  #v3.2+
Signed-off-by: Julian Wiedmann 
Signed-off-by: Martin Schwidefsky 
Signed-off-by: Greg Kroah-Hartman 

---
 drivers/s390/cio/qdio_setup.c |2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

--- a/drivers/s390/cio/qdio_setup.c
+++ b/drivers/s390/cio/qdio_setup.c
@@ -141,7 +141,7 @@ static int __qdio_allocate_qs(struct qdi
int i;
 
for (i = 0; i < nr_queues; i++) {
-   q = kmem_cache_alloc(qdio_q_cache, GFP_KERNEL);
+   q = kmem_cache_zalloc(qdio_q_cache, GFP_KERNEL);
if (!q)
return -ENOMEM;
 




[PATCH 4.16 005/110] usbip: usbip_host: fix NULL-ptr deref and use-after-free errors

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Shuah Khan (Samsung OSG) 

commit 22076557b07c12086eeb16b8ce2b0b735f7a27e7 upstream.

usbip_host updates device status without holding lock from stub probe,
disconnect and rebind code paths. When multiple requests to import a
device are received, these unprotected code paths step all over each
other and drive fails with NULL-ptr deref and use-after-free errors.

The driver uses a table lock to protect the busid array for adding and
deleting busids to the table. However, the probe, disconnect and rebind
paths get the busid table entry and update the status without holding
the busid table lock. Add a new finer grain lock to protect the busid
entry. This new lock will be held to search and update the busid entry
fields from get_busid_idx(), add_match_busid() and del_match_busid().

match_busid_show() does the same to access the busid entry fields.

get_busid_priv() changed to return the pointer to the busid entry holding
the busid lock. stub_probe(), stub_disconnect() and stub_device_rebind()
call put_busid_priv() to release the busid lock before returning. This
changes fixes the unprotected code paths eliminating the race conditions
in updating the busid entries.

Reported-by: Jakub Jirasek
Signed-off-by: Shuah Khan (Samsung OSG) 
Cc: stable 
Signed-off-by: Greg Kroah-Hartman 

---
 drivers/usb/usbip/stub.h  |2 ++
 drivers/usb/usbip/stub_dev.c  |   33 +++--
 drivers/usb/usbip/stub_main.c |   40 +++-
 3 files changed, 60 insertions(+), 15 deletions(-)

--- a/drivers/usb/usbip/stub.h
+++ b/drivers/usb/usbip/stub.h
@@ -73,6 +73,7 @@ struct bus_id_priv {
struct stub_device *sdev;
struct usb_device *udev;
char shutdown_busid;
+   spinlock_t busid_lock;
 };
 
 /* stub_priv is allocated from stub_priv_cache */
@@ -83,6 +84,7 @@ extern struct usb_device_driver stub_dri
 
 /* stub_main.c */
 struct bus_id_priv *get_busid_priv(const char *busid);
+void put_busid_priv(struct bus_id_priv *bid);
 int del_match_busid(char *busid);
 void stub_device_cleanup_urbs(struct stub_device *sdev);
 
--- a/drivers/usb/usbip/stub_dev.c
+++ b/drivers/usb/usbip/stub_dev.c
@@ -300,7 +300,7 @@ static int stub_probe(struct usb_device
struct stub_device *sdev = NULL;
const char *udev_busid = dev_name(&udev->dev);
struct bus_id_priv *busid_priv;
-   int rc;
+   int rc = 0;
 
dev_dbg(&udev->dev, "Enter probe\n");
 
@@ -317,13 +317,15 @@ static int stub_probe(struct usb_device
 * other matched drivers by the driver core.
 * See driver_probe_device() in driver/base/dd.c
 */
-   return -ENODEV;
+   rc = -ENODEV;
+   goto call_put_busid_priv;
}
 
if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
 udev_busid);
-   return -ENODEV;
+   rc = -ENODEV;
+   goto call_put_busid_priv;
}
 
if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
@@ -331,13 +333,16 @@ static int stub_probe(struct usb_device
"%s is attached on vhci_hcd... skip!\n",
udev_busid);
 
-   return -ENODEV;
+   rc = -ENODEV;
+   goto call_put_busid_priv;
}
 
/* ok, this is my device */
sdev = stub_device_alloc(udev);
-   if (!sdev)
-   return -ENOMEM;
+   if (!sdev) {
+   rc = -ENOMEM;
+   goto call_put_busid_priv;
+   }
 
dev_info(&udev->dev,
"usbip-host: register new device (bus %u dev %u)\n",
@@ -369,7 +374,9 @@ static int stub_probe(struct usb_device
}
busid_priv->status = STUB_BUSID_ALLOC;
 
-   return 0;
+   rc = 0;
+   goto call_put_busid_priv;
+
 err_files:
usb_hub_release_port(udev->parent, udev->portnum,
 (struct usb_dev_state *) udev);
@@ -379,6 +386,9 @@ err_port:
 
busid_priv->sdev = NULL;
stub_device_free(sdev);
+
+call_put_busid_priv:
+   put_busid_priv(busid_priv);
return rc;
 }
 
@@ -417,7 +427,7 @@ static void stub_disconnect(struct usb_d
/* get stub_device */
if (!sdev) {
dev_err(&udev->dev, "could not get device");
-   return;
+   goto call_put_busid_priv;
}
 
dev_set_drvdata(&udev->dev, NULL);
@@ -432,12 +442,12 @@ static void stub_disconnect(struct usb_d
  (struct usb_dev_state *) udev);
if (rc) {
dev_dbg(&udev->dev, "unable to release port\n");
-   return;
+   goto call_put_busid_priv;
}
 
/* If usb reset is called from event handler */
if (

[PATCH 4.16 003/110] usbip: usbip_host: delete device from busid_table after rebind

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Shuah Khan (Samsung OSG) 

commit 1e180f167d4e413afccbbb4a421b48b2de832549 upstream.

Device is left in the busid_table after unbind and rebind. Rebind
initiates usb bus scan and the original driver claims the device.
After rescan the device should be deleted from the busid_table as
it no longer belongs to usbip_host.

Fix it to delete the device after device_attach() succeeds.

Signed-off-by: Shuah Khan (Samsung OSG) 
Cc: stable 
Signed-off-by: Greg Kroah-Hartman 

---
 drivers/usb/usbip/stub_main.c |6 ++
 1 file changed, 6 insertions(+)

--- a/drivers/usb/usbip/stub_main.c
+++ b/drivers/usb/usbip/stub_main.c
@@ -186,6 +186,9 @@ static ssize_t rebind_store(struct devic
if (!bid)
return -ENODEV;
 
+   /* mark the device for deletion so probe ignores it during rescan */
+   bid->status = STUB_BUSID_OTHER;
+
/* device_attach() callers should hold parent lock for USB */
if (bid->udev->dev.parent)
device_lock(bid->udev->dev.parent);
@@ -197,6 +200,9 @@ static ssize_t rebind_store(struct devic
return ret;
}
 
+   /* delete device from busid_table */
+   del_match_busid((char *) buf);
+
return count;
 }
 




[PATCH 4.16 009/110] ALSA: hda: Add Lenovo C50 All in one to the power_save blacklist

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Hans de Goede 

commit c8beccc19b92f5172994c0732db689c08f4f98e5 upstream.

Power-saving is causing loud plops on the Lenovo C50 All in one, add it
to the blacklist.

BugLink: https://bugzilla.redhat.com/show_bug.cgi?id=1572975
Signed-off-by: Hans de Goede 
Cc: 
Signed-off-by: Takashi Iwai 
Signed-off-by: Greg Kroah-Hartman 

---
 sound/pci/hda/hda_intel.c |2 ++
 1 file changed, 2 insertions(+)

--- a/sound/pci/hda/hda_intel.c
+++ b/sound/pci/hda/hda_intel.c
@@ -2208,6 +2208,8 @@ static struct snd_pci_quirk power_save_b
SND_PCI_QUIRK(0x1849, 0x0c0c, "Asrock B85M-ITX", 0),
/* https://bugzilla.redhat.com/show_bug.cgi?id=1525104 */
SND_PCI_QUIRK(0x1043, 0x8733, "Asus Prime X370-Pro", 0),
+   /* https://bugzilla.redhat.com/show_bug.cgi?id=1572975 */
+   SND_PCI_QUIRK(0x17aa, 0x36a7, "Lenovo C50 All in one", 0),
/* https://bugzilla.kernel.org/show_bug.cgi?id=198611 */
SND_PCI_QUIRK(0x17aa, 0x2227, "Lenovo X1 Carbon 3rd Gen", 0),
{}




[PATCH 4.16 018/110] KVM: arm/arm64: VGIC/ITS: protect kvm_read_guest() calls with SRCU lock

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Andre Przywara 

commit bf308242ab98b5d1648c3663e753556bef9bec01 upstream.

kvm_read_guest() will eventually look up in kvm_memslots(), which requires
either to hold the kvm->slots_lock or to be inside a kvm->srcu critical
section.
In contrast to x86 and s390 we don't take the SRCU lock on every guest
exit, so we have to do it individually for each kvm_read_guest() call.

Provide a wrapper which does that and use that everywhere.

Note that ending the SRCU critical section before returning from the
kvm_read_guest() wrapper is safe, because the data has been *copied*, so
we don't need to rely on valid references to the memslot anymore.

Cc: Stable  # 4.8+
Reported-by: Jan Glauber 
Signed-off-by: Andre Przywara 
Acked-by: Christoffer Dall 
Signed-off-by: Paolo Bonzini 
Signed-off-by: Greg Kroah-Hartman 

---
 arch/arm/include/asm/kvm_mmu.h   |   16 
 arch/arm64/include/asm/kvm_mmu.h |   16 
 virt/kvm/arm/vgic/vgic-its.c |   15 ---
 3 files changed, 40 insertions(+), 7 deletions(-)

--- a/arch/arm/include/asm/kvm_mmu.h
+++ b/arch/arm/include/asm/kvm_mmu.h
@@ -295,6 +295,22 @@ static inline unsigned int kvm_get_vmid_
return 8;
 }
 
+/*
+ * We are not in the kvm->srcu critical section most of the time, so we take
+ * the SRCU read lock here. Since we copy the data from the user page, we
+ * can immediately drop the lock again.
+ */
+static inline int kvm_read_guest_lock(struct kvm *kvm,
+ gpa_t gpa, void *data, unsigned long len)
+{
+   int srcu_idx = srcu_read_lock(&kvm->srcu);
+   int ret = kvm_read_guest(kvm, gpa, data, len);
+
+   srcu_read_unlock(&kvm->srcu, srcu_idx);
+
+   return ret;
+}
+
 static inline void *kvm_get_hyp_vector(void)
 {
return kvm_ksym_ref(__kvm_hyp_vector);
--- a/arch/arm64/include/asm/kvm_mmu.h
+++ b/arch/arm64/include/asm/kvm_mmu.h
@@ -348,6 +348,22 @@ static inline unsigned int kvm_get_vmid_
return (cpuid_feature_extract_unsigned_field(reg, 
ID_AA64MMFR1_VMIDBITS_SHIFT) == 2) ? 16 : 8;
 }
 
+/*
+ * We are not in the kvm->srcu critical section most of the time, so we take
+ * the SRCU read lock here. Since we copy the data from the user page, we
+ * can immediately drop the lock again.
+ */
+static inline int kvm_read_guest_lock(struct kvm *kvm,
+ gpa_t gpa, void *data, unsigned long len)
+{
+   int srcu_idx = srcu_read_lock(&kvm->srcu);
+   int ret = kvm_read_guest(kvm, gpa, data, len);
+
+   srcu_read_unlock(&kvm->srcu, srcu_idx);
+
+   return ret;
+}
+
 #ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
 #include 
 
--- a/virt/kvm/arm/vgic/vgic-its.c
+++ b/virt/kvm/arm/vgic/vgic-its.c
@@ -281,8 +281,8 @@ static int update_lpi_config(struct kvm
int ret;
unsigned long flags;
 
-   ret = kvm_read_guest(kvm, propbase + irq->intid - GIC_LPI_OFFSET,
-&prop, 1);
+   ret = kvm_read_guest_lock(kvm, propbase + irq->intid - GIC_LPI_OFFSET,
+ &prop, 1);
 
if (ret)
return ret;
@@ -444,8 +444,9 @@ static int its_sync_lpi_pending_table(st
 * this very same byte in the last iteration. Reuse that.
 */
if (byte_offset != last_byte_offset) {
-   ret = kvm_read_guest(vcpu->kvm, pendbase + byte_offset,
-&pendmask, 1);
+   ret = kvm_read_guest_lock(vcpu->kvm,
+ pendbase + byte_offset,
+ &pendmask, 1);
if (ret) {
kfree(intids);
return ret;
@@ -789,7 +790,7 @@ static bool vgic_its_check_id(struct vgi
return false;
 
/* Each 1st level entry is represented by a 64-bit value. */
-   if (kvm_read_guest(its->dev->kvm,
+   if (kvm_read_guest_lock(its->dev->kvm,
   BASER_ADDRESS(baser) + index * sizeof(indirect_ptr),
   &indirect_ptr, sizeof(indirect_ptr)))
return false;
@@ -1370,8 +1371,8 @@ static void vgic_its_process_commands(st
cbaser = CBASER_ADDRESS(its->cbaser);
 
while (its->cwriter != its->creadr) {
-   int ret = kvm_read_guest(kvm, cbaser + its->creadr,
-cmd_buf, ITS_CMD_SIZE);
+   int ret = kvm_read_guest_lock(kvm, cbaser + its->creadr,
+ cmd_buf, ITS_CMD_SIZE);
/*
 * If kvm_read_guest() fails, this could be due to the guest
 * programming a bogus value in CBASER or something else going




[PATCH 4.14 66/95] seccomp: Enable speculation flaw mitigations

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Kees Cook 

commit 5c3070890d06ff82eecb808d02d2ca39169533ef upstream

When speculation flaw mitigations are opt-in (via prctl), using seccomp
will automatically opt-in to these protections, since using seccomp
indicates at least some level of sandboxing is desired.

Signed-off-by: Kees Cook 
Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 kernel/seccomp.c |   17 +
 1 file changed, 17 insertions(+)

--- a/kernel/seccomp.c
+++ b/kernel/seccomp.c
@@ -19,6 +19,8 @@
 #include 
 #include 
 #include 
+#include 
+#include 
 #include 
 #include 
 #include 
@@ -227,6 +229,19 @@ static inline bool seccomp_may_assign_mo
return true;
 }
 
+/*
+ * If a given speculation mitigation is opt-in (prctl()-controlled),
+ * select it, by disabling speculation (enabling mitigation).
+ */
+static inline void spec_mitigate(struct task_struct *task,
+unsigned long which)
+{
+   int state = arch_prctl_spec_ctrl_get(task, which);
+
+   if (state > 0 && (state & PR_SPEC_PRCTL))
+   arch_prctl_spec_ctrl_set(task, which, PR_SPEC_DISABLE);
+}
+
 static inline void seccomp_assign_mode(struct task_struct *task,
   unsigned long seccomp_mode)
 {
@@ -238,6 +253,8 @@ static inline void seccomp_assign_mode(s
 * filter) is set.
 */
smp_mb__before_atomic();
+   /* Assume seccomp processes want speculation flaw mitigation. */
+   spec_mitigate(task, PR_SPEC_STORE_BYPASS);
set_tsk_thread_flag(task, TIF_SECCOMP);
 }
 




[PATCH 4.16 013/110] spi: bcm-qspi: Always read and set BSPI_MAST_N_BOOT_CTRL

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Kamal Dasu 

commit 602805fb618b018b7a41fbb3f93c1992b078b1ae upstream.

Always confirm the BSPI_MAST_N_BOOT_CTRL bit when enabling
or disabling BSPI transfers.

Fixes: 4e3b2d236fe00 ("spi: bcm-qspi: Add BSPI spi-nor flash controller driver")
Signed-off-by: Kamal Dasu 
Signed-off-by: Mark Brown 
Cc: sta...@vger.kernel.org
Signed-off-by: Greg Kroah-Hartman 

---
 drivers/spi/spi-bcm-qspi.c |4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

--- a/drivers/spi/spi-bcm-qspi.c
+++ b/drivers/spi/spi-bcm-qspi.c
@@ -490,7 +490,7 @@ static int bcm_qspi_bspi_set_mode(struct
 
 static void bcm_qspi_enable_bspi(struct bcm_qspi *qspi)
 {
-   if (!has_bspi(qspi) || (qspi->bspi_enabled))
+   if (!has_bspi(qspi))
return;
 
qspi->bspi_enabled = 1;
@@ -505,7 +505,7 @@ static void bcm_qspi_enable_bspi(struct
 
 static void bcm_qspi_disable_bspi(struct bcm_qspi *qspi)
 {
-   if (!has_bspi(qspi) || (!qspi->bspi_enabled))
+   if (!has_bspi(qspi))
return;
 
qspi->bspi_enabled = 0;




[PATCH 4.16 012/110] spi: bcm-qspi: Avoid setting MSPI_CDRAM_PCS for spi-nor master

2018-05-21 Thread Greg Kroah-Hartman
4.16-stable review patch.  If anyone has any objections, please let me know.

--

From: Kamal Dasu 

commit 5eb9a07a4ae1008b67d8bcd47bddb3dae97456b7 upstream.

Added fix for probing of spi-nor device non-zero chip selects. Set
MSPI_CDRAM_PCS (peripheral chip select) with spi master for MSPI
controller and not for MSPI/BSPI spi-nor master controller. Ensure
setting of cs bit in chip select register on chip select change.

Fixes: fa236a7ef24048 ("spi: bcm-qspi: Add Broadcom MSPI driver")
Signed-off-by: Kamal Dasu 
Signed-off-by: Mark Brown 
Cc: sta...@vger.kernel.org
Signed-off-by: Greg Kroah-Hartman 

---
 drivers/spi/spi-bcm-qspi.c |   24 
 1 file changed, 16 insertions(+), 8 deletions(-)

--- a/drivers/spi/spi-bcm-qspi.c
+++ b/drivers/spi/spi-bcm-qspi.c
@@ -519,16 +519,19 @@ static void bcm_qspi_disable_bspi(struct
 
 static void bcm_qspi_chip_select(struct bcm_qspi *qspi, int cs)
 {
-   u32 data = 0;
+   u32 rd = 0;
+   u32 wr = 0;
 
-   if (qspi->curr_cs == cs)
-   return;
if (qspi->base[CHIP_SELECT]) {
-   data = bcm_qspi_read(qspi, CHIP_SELECT, 0);
-   data = (data & ~0xff) | (1 << cs);
-   bcm_qspi_write(qspi, CHIP_SELECT, 0, data);
+   rd = bcm_qspi_read(qspi, CHIP_SELECT, 0);
+   wr = (rd & ~0xff) | (1 << cs);
+   if (rd == wr)
+   return;
+   bcm_qspi_write(qspi, CHIP_SELECT, 0, wr);
usleep_range(10, 20);
}
+
+   dev_dbg(&qspi->pdev->dev, "using cs:%d\n", cs);
qspi->curr_cs = cs;
 }
 
@@ -755,8 +758,13 @@ static int write_to_hw(struct bcm_qspi *
dev_dbg(&qspi->pdev->dev, "WR %04x\n", val);
}
mspi_cdram = MSPI_CDRAM_CONT_BIT;
-   mspi_cdram |= (~(1 << spi->chip_select) &
-  MSPI_CDRAM_PCS);
+
+   if (has_bspi(qspi))
+   mspi_cdram &= ~1;
+   else
+   mspi_cdram |= (~(1 << spi->chip_select) &
+  MSPI_CDRAM_PCS);
+
mspi_cdram |= ((tp.trans->bits_per_word <= 8) ? 0 :
MSPI_CDRAM_BITSE_BIT);
 




[PATCH 4.14 71/95] seccomp: Move speculation migitation control to arch code

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit 8bf37d8c067bb7eb8e7c381bdadf9bd89182b6bc upstream

The migitation control is simpler to implement in architecture code as it
avoids the extra function call to check the mode. Aside of that having an
explicit seccomp enabled mode in the architecture mitigations would require
even more workarounds.

Move it into architecture code and provide a weak function in the seccomp
code. Remove the 'which' argument as this allows the architecture to decide
which mitigations are relevant for seccomp.

Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/kernel/cpu/bugs.c |   29 ++---
 include/linux/nospec.h |2 ++
 kernel/seccomp.c   |   15 ++-
 3 files changed, 22 insertions(+), 24 deletions(-)

--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -569,6 +569,24 @@ static int ssb_prctl_set(struct task_str
return 0;
 }
 
+int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
+unsigned long ctrl)
+{
+   switch (which) {
+   case PR_SPEC_STORE_BYPASS:
+   return ssb_prctl_set(task, ctrl);
+   default:
+   return -ENODEV;
+   }
+}
+
+#ifdef CONFIG_SECCOMP
+void arch_seccomp_spec_mitigate(struct task_struct *task)
+{
+   ssb_prctl_set(task, PR_SPEC_FORCE_DISABLE);
+}
+#endif
+
 static int ssb_prctl_get(struct task_struct *task)
 {
switch (ssb_mode) {
@@ -587,17 +605,6 @@ static int ssb_prctl_get(struct task_str
}
 }
 
-int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
-unsigned long ctrl)
-{
-   switch (which) {
-   case PR_SPEC_STORE_BYPASS:
-   return ssb_prctl_set(task, ctrl);
-   default:
-   return -ENODEV;
-   }
-}
-
 int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
 {
switch (which) {
--- a/include/linux/nospec.h
+++ b/include/linux/nospec.h
@@ -62,5 +62,7 @@ static inline unsigned long array_index_
 int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which);
 int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
 unsigned long ctrl);
+/* Speculation control for seccomp enforced mitigation */
+void arch_seccomp_spec_mitigate(struct task_struct *task);
 
 #endif /* _LINUX_NOSPEC_H */
--- a/kernel/seccomp.c
+++ b/kernel/seccomp.c
@@ -229,18 +229,7 @@ static inline bool seccomp_may_assign_mo
return true;
 }
 
-/*
- * If a given speculation mitigation is opt-in (prctl()-controlled),
- * select it, by disabling speculation (enabling mitigation).
- */
-static inline void spec_mitigate(struct task_struct *task,
-unsigned long which)
-{
-   int state = arch_prctl_spec_ctrl_get(task, which);
-
-   if (state > 0 && (state & PR_SPEC_PRCTL))
-   arch_prctl_spec_ctrl_set(task, which, PR_SPEC_FORCE_DISABLE);
-}
+void __weak arch_seccomp_spec_mitigate(struct task_struct *task) { }
 
 static inline void seccomp_assign_mode(struct task_struct *task,
   unsigned long seccomp_mode,
@@ -256,7 +245,7 @@ static inline void seccomp_assign_mode(s
smp_mb__before_atomic();
/* Assume default seccomp processes want spec flaw mitigation. */
if ((flags & SECCOMP_FILTER_FLAG_SPEC_ALLOW) == 0)
-   spec_mitigate(task, PR_SPEC_STORE_BYPASS);
+   arch_seccomp_spec_mitigate(task);
set_tsk_thread_flag(task, TIF_SECCOMP);
 }
 




[PATCH 4.14 73/95] x86/bugs: Rename _RDS to _SSBD

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Konrad Rzeszutek Wilk 

commit 9f65fb29374ee37856dbad847b4e121aab72b510 upstream

Intel collateral will reference the SSB mitigation bit in IA32_SPEC_CTL[2]
as SSBD (Speculative Store Bypass Disable).

Hence changing it.

It is unclear yet what the MSR_IA32_ARCH_CAPABILITIES (0x10a) Bit(4) name
is going to be. Following the rename it would be SSBD_NO but that rolls out
to Speculative Store Bypass Disable No.

Also fixed the missing space in X86_FEATURE_AMD_SSBD.

[ tglx: Fixup x86_amd_rds_enable() and rds_tif_to_amd_ls_cfg() as well ]

Signed-off-by: Konrad Rzeszutek Wilk 
Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/cpufeatures.h |4 ++--
 arch/x86/include/asm/msr-index.h   |   10 +-
 arch/x86/include/asm/spec-ctrl.h   |   12 ++--
 arch/x86/include/asm/thread_info.h |6 +++---
 arch/x86/kernel/cpu/amd.c  |   14 +++---
 arch/x86/kernel/cpu/bugs.c |   36 ++--
 arch/x86/kernel/cpu/common.c   |2 +-
 arch/x86/kernel/cpu/intel.c|2 +-
 arch/x86/kernel/process.c  |8 
 arch/x86/kvm/cpuid.c   |2 +-
 arch/x86/kvm/vmx.c |6 +++---
 11 files changed, 51 insertions(+), 51 deletions(-)

--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -213,7 +213,7 @@
 #define X86_FEATURE_USE_IBPB   ( 7*32+21) /* "" Indirect Branch 
Prediction Barrier enabled */
 #define X86_FEATURE_USE_IBRS_FW( 7*32+22) /* "" Use IBRS 
during runtime firmware calls */
 #define X86_FEATURE_SPEC_STORE_BYPASS_DISABLE  ( 7*32+23) /* "" Disable 
Speculative Store Bypass. */
-#define X86_FEATURE_AMD_RDS(7*32+24)  /* "" AMD RDS implementation 
*/
+#define X86_FEATURE_AMD_SSBD   ( 7*32+24)  /* "" AMD SSBD 
implementation */
 
 /* Virtualization flags: Linux defined, word 8 */
 #define X86_FEATURE_TPR_SHADOW ( 8*32+ 0) /* Intel TPR Shadow */
@@ -333,7 +333,7 @@
 #define X86_FEATURE_SPEC_CTRL  (18*32+26) /* "" Speculation Control 
(IBRS + IBPB) */
 #define X86_FEATURE_INTEL_STIBP(18*32+27) /* "" Single Thread 
Indirect Branch Predictors */
 #define X86_FEATURE_ARCH_CAPABILITIES  (18*32+29) /* IA32_ARCH_CAPABILITIES 
MSR (Intel) */
-#define X86_FEATURE_RDS(18*32+31) /* Reduced Data 
Speculation */
+#define X86_FEATURE_SSBD   (18*32+31) /* Speculative Store Bypass 
Disable */
 
 /*
  * BUG word(s)
--- a/arch/x86/include/asm/msr-index.h
+++ b/arch/x86/include/asm/msr-index.h
@@ -42,8 +42,8 @@
 #define MSR_IA32_SPEC_CTRL 0x0048 /* Speculation Control */
 #define SPEC_CTRL_IBRS (1 << 0)   /* Indirect Branch 
Restricted Speculation */
 #define SPEC_CTRL_STIBP(1 << 1)   /* Single Thread 
Indirect Branch Predictors */
-#define SPEC_CTRL_RDS_SHIFT2  /* Reduced Data Speculation 
bit */
-#define SPEC_CTRL_RDS  (1 << SPEC_CTRL_RDS_SHIFT)   /* Reduced 
Data Speculation */
+#define SPEC_CTRL_SSBD_SHIFT   2  /* Speculative Store Bypass 
Disable bit */
+#define SPEC_CTRL_SSBD (1 << SPEC_CTRL_SSBD_SHIFT)   /* 
Speculative Store Bypass Disable */
 
 #define MSR_IA32_PRED_CMD  0x0049 /* Prediction Command */
 #define PRED_CMD_IBPB  (1 << 0)   /* Indirect Branch 
Prediction Barrier */
@@ -70,10 +70,10 @@
 #define MSR_IA32_ARCH_CAPABILITIES 0x010a
 #define ARCH_CAP_RDCL_NO   (1 << 0)   /* Not susceptible to 
Meltdown */
 #define ARCH_CAP_IBRS_ALL  (1 << 1)   /* Enhanced IBRS support */
-#define ARCH_CAP_RDS_NO(1 << 4)   /*
+#define ARCH_CAP_SSBD_NO   (1 << 4)   /*
* Not susceptible to 
Speculative Store Bypass
-   * attack, so no Reduced 
Data Speculation control
-   * required.
+   * attack, so no Speculative 
Store Bypass
+   * control required.
*/
 
 #define MSR_IA32_BBL_CR_CTL0x0119
--- a/arch/x86/include/asm/spec-ctrl.h
+++ b/arch/x86/include/asm/spec-ctrl.h
@@ -17,20 +17,20 @@ extern void x86_spec_ctrl_restore_host(u
 
 /* AMD specific Speculative Store Bypass MSR data */
 extern u64 x86_amd_ls_cfg_base;
-extern u64 x86_amd_ls_cfg_rds_mask;
+extern u64 x86_amd_ls_cfg_ssbd_mask;
 
 /* The Intel SPEC CTRL MSR base value cache */
 extern u64 x86_spec_ctrl_base;
 
-static inline u64 rds_tif_to_spec_ctrl(u64 tifn)
+static inline u64 ssbd_tif_to_spec_ctrl(u64 tifn)
 {
-   BUILD_BUG_ON(TIF_RDS < SPEC_CTRL_RDS_

[PATCH 4.14 94/95] KVM: SVM: Implement VIRT_SPEC_CTRL support for SSBD

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Tom Lendacky 

commit bc226f07dcd3c9ef0b7f6236fe356ea4a9cb4769 upstream

Expose the new virtualized architectural mechanism, VIRT_SSBD, for using
speculative store bypass disable (SSBD) under SVM.  This will allow guests
to use SSBD on hardware that uses non-architectural mechanisms for enabling
SSBD.

[ tglx: Folded the migration fixup from Paolo Bonzini ]

Signed-off-by: Tom Lendacky 
Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/kvm_host.h |2 +-
 arch/x86/kernel/cpu/common.c|3 ++-
 arch/x86/kvm/cpuid.c|   11 +--
 arch/x86/kvm/svm.c  |   21 +++--
 arch/x86/kvm/vmx.c  |   18 +++---
 arch/x86/kvm/x86.c  |   13 -
 6 files changed, 50 insertions(+), 18 deletions(-)

--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -908,7 +908,7 @@ struct kvm_x86_ops {
int (*hardware_setup)(void);   /* __init */
void (*hardware_unsetup)(void);/* __exit */
bool (*cpu_has_accelerated_tpr)(void);
-   bool (*cpu_has_high_real_mode_segbase)(void);
+   bool (*has_emulated_msr)(int index);
void (*cpuid_update)(struct kvm_vcpu *vcpu);
 
int (*vm_init)(struct kvm *kvm);
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -743,7 +743,8 @@ static void init_speculation_control(str
if (cpu_has(c, X86_FEATURE_INTEL_STIBP))
set_cpu_cap(c, X86_FEATURE_STIBP);
 
-   if (cpu_has(c, X86_FEATURE_SPEC_CTRL_SSBD))
+   if (cpu_has(c, X86_FEATURE_SPEC_CTRL_SSBD) ||
+   cpu_has(c, X86_FEATURE_VIRT_SSBD))
set_cpu_cap(c, X86_FEATURE_SSBD);
 
if (cpu_has(c, X86_FEATURE_AMD_IBRS)) {
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -367,7 +367,7 @@ static inline int __do_cpuid_ent(struct
 
/* cpuid 0x8008.ebx */
const u32 kvm_cpuid_8000_0008_ebx_x86_features =
-   F(AMD_IBPB) | F(AMD_IBRS);
+   F(AMD_IBPB) | F(AMD_IBRS) | F(VIRT_SSBD);
 
/* cpuid 0xC001.edx */
const u32 kvm_cpuid_C000_0001_edx_x86_features =
@@ -632,13 +632,20 @@ static inline int __do_cpuid_ent(struct
g_phys_as = phys_as;
entry->eax = g_phys_as | (virt_as << 8);
entry->edx = 0;
-   /* IBRS and IBPB aren't necessarily present in hardware cpuid */
+   /*
+* IBRS, IBPB and VIRT_SSBD aren't necessarily present in
+* hardware cpuid
+*/
if (boot_cpu_has(X86_FEATURE_AMD_IBPB))
entry->ebx |= F(AMD_IBPB);
if (boot_cpu_has(X86_FEATURE_AMD_IBRS))
entry->ebx |= F(AMD_IBRS);
+   if (boot_cpu_has(X86_FEATURE_VIRT_SSBD))
+   entry->ebx |= F(VIRT_SSBD);
entry->ebx &= kvm_cpuid_8000_0008_ebx_x86_features;
cpuid_mask(&entry->ebx, CPUID_8000_0008_EBX);
+   if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD))
+   entry->ebx |= F(VIRT_SSBD);
break;
}
case 0x8019:
--- a/arch/x86/kvm/svm.c
+++ b/arch/x86/kvm/svm.c
@@ -3630,6 +3630,13 @@ static int svm_get_msr(struct kvm_vcpu *
 
msr_info->data = svm->spec_ctrl;
break;
+   case MSR_AMD64_VIRT_SPEC_CTRL:
+   if (!msr_info->host_initiated &&
+   !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
+   return 1;
+
+   msr_info->data = svm->virt_spec_ctrl;
+   break;
case MSR_IA32_UCODE_REV:
msr_info->data = 0x0165;
break;
@@ -3764,6 +3771,16 @@ static int svm_set_msr(struct kvm_vcpu *
break;
set_msr_interception(svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
break;
+   case MSR_AMD64_VIRT_SPEC_CTRL:
+   if (!msr->host_initiated &&
+   !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
+   return 1;
+
+   if (data & ~SPEC_CTRL_SSBD)
+   return 1;
+
+   svm->virt_spec_ctrl = data;
+   break;
case MSR_STAR:
svm->vmcb->save.star = data;
break;
@@ -5249,7 +5266,7 @@ static bool svm_cpu_has_accelerated_tpr(
return false;
 }
 
-static bool svm_has_high_real_mode_segbase(void)
+static bool svm_has_emulated_msr(int index)
 {
return true;
 }
@@ -5556,7 +5573,7 @@ static struct kvm_x86_ops svm_x86_ops __
.hardware_enable = svm_hardware_enable,
.hardware_disable = svm_hardware_disable,
.cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
-   .cpu_has_high_real_mode_se

[PATCH 4.14 91/95] x86/bugs: Remove x86_spec_ctrl_set()

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit 4b59bdb569453a60b752b274ca61f009e37f4dae upstream

x86_spec_ctrl_set() is only used in bugs.c and the extra mask checks there
provide no real value as both call sites can just write x86_spec_ctrl_base
to MSR_SPEC_CTRL. x86_spec_ctrl_base is valid and does not need any extra
masking or checking.

Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Konrad Rzeszutek Wilk 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/nospec-branch.h |2 --
 arch/x86/kernel/cpu/bugs.c   |   13 ++---
 2 files changed, 2 insertions(+), 13 deletions(-)

--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -217,8 +217,6 @@ enum spectre_v2_mitigation {
SPECTRE_V2_IBRS,
 };
 
-extern void x86_spec_ctrl_set(u64);
-
 /* The Speculative Store Bypass disable variants */
 enum ssb_mitigation {
SPEC_STORE_BYPASS_NONE,
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -133,15 +133,6 @@ static const char *spectre_v2_strings[]
 static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init =
SPECTRE_V2_NONE;
 
-void x86_spec_ctrl_set(u64 val)
-{
-   if (val & x86_spec_ctrl_mask)
-   WARN_ONCE(1, "SPEC_CTRL MSR value 0x%16llx is unknown.\n", val);
-   else
-   wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base | val);
-}
-EXPORT_SYMBOL_GPL(x86_spec_ctrl_set);
-
 void
 x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool 
setguest)
 {
@@ -503,7 +494,7 @@ static enum ssb_mitigation __init __ssb_
case X86_VENDOR_INTEL:
x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
x86_spec_ctrl_mask &= ~SPEC_CTRL_SSBD;
-   x86_spec_ctrl_set(SPEC_CTRL_SSBD);
+   wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
break;
case X86_VENDOR_AMD:
x86_amd_ssb_disable();
@@ -615,7 +606,7 @@ int arch_prctl_spec_ctrl_get(struct task
 void x86_spec_ctrl_setup_ap(void)
 {
if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
-   x86_spec_ctrl_set(x86_spec_ctrl_base & ~x86_spec_ctrl_mask);
+   wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
 
if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
x86_amd_ssb_disable();




[PATCH 4.14 68/95] prctl: Add force disable speculation

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit 356e4bfff2c5489e016fdb925adbf12a1e3950ee upstream

For certain use cases it is desired to enforce mitigations so they cannot
be undone afterwards. That's important for loader stubs which want to
prevent a child from disabling the mitigation again. Will also be used for
seccomp(). The extra state preserving of the prctl state for SSB is a
preparatory step for EBPF dymanic speculation control.

Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 Documentation/userspace-api/spec_ctrl.rst |   34 ++---
 arch/x86/kernel/cpu/bugs.c|   35 +-
 fs/proc/array.c   |3 ++
 include/linux/sched.h |   10 +++-
 include/uapi/linux/prctl.h|1 
 5 files changed, 59 insertions(+), 24 deletions(-)

--- a/Documentation/userspace-api/spec_ctrl.rst
+++ b/Documentation/userspace-api/spec_ctrl.rst
@@ -25,19 +25,21 @@ PR_GET_SPECULATION_CTRL
 ---
 
 PR_GET_SPECULATION_CTRL returns the state of the speculation misfeature
-which is selected with arg2 of prctl(2). The return value uses bits 0-2 with
+which is selected with arg2 of prctl(2). The return value uses bits 0-3 with
 the following meaning:
 
-  ===
-Bit  Define   Description
-  ===
-0PR_SPEC_PRCTLMitigation can be controlled per task by
-  PR_SET_SPECULATION_CTRL
-1PR_SPEC_ENABLE   The speculation feature is enabled, mitigation is
-  disabled
-2PR_SPEC_DISABLE  The speculation feature is disabled, mitigation is
-  enabled
-  ===
+ = ===
+Bit  DefineDescription
+ = ===
+0PR_SPEC_PRCTL Mitigation can be controlled per task by
+   PR_SET_SPECULATION_CTRL
+1PR_SPEC_ENABLEThe speculation feature is enabled, mitigation is
+   disabled
+2PR_SPEC_DISABLE   The speculation feature is disabled, mitigation is
+   enabled
+3PR_SPEC_FORCE_DISABLE Same as PR_SPEC_DISABLE, but cannot be undone. A
+   subsequent prctl(..., PR_SPEC_ENABLE) will fail.
+ = ===
 
 If all bits are 0 the CPU is not affected by the speculation misfeature.
 
@@ -47,9 +49,11 @@ misfeature will fail.
 
 PR_SET_SPECULATION_CTRL
 ---
+
 PR_SET_SPECULATION_CTRL allows to control the speculation misfeature, which
 is selected by arg2 of :manpage:`prctl(2)` per task. arg3 is used to hand
-in the control value, i.e. either PR_SPEC_ENABLE or PR_SPEC_DISABLE.
+in the control value, i.e. either PR_SPEC_ENABLE or PR_SPEC_DISABLE or
+PR_SPEC_FORCE_DISABLE.
 
 Common error codes
 --
@@ -70,10 +74,13 @@ Value   Meaning
 0   Success
 
 ERANGE  arg3 is incorrect, i.e. it's neither PR_SPEC_ENABLE nor
-PR_SPEC_DISABLE
+PR_SPEC_DISABLE nor PR_SPEC_FORCE_DISABLE
 
 ENXIO   Control of the selected speculation misfeature is not possible.
 See PR_GET_SPECULATION_CTRL.
+
+EPERM   Speculation was disabled with PR_SPEC_FORCE_DISABLE and caller
+tried to enable it again.
 === =
 
 Speculation misfeature controls
@@ -84,3 +91,4 @@ Speculation misfeature controls
* prctl(PR_GET_SPECULATION_CTRL, PR_SPEC_STORE_BYPASS, 0, 0, 0);
* prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_STORE_BYPASS, PR_SPEC_ENABLE, 0, 
0);
* prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_STORE_BYPASS, PR_SPEC_DISABLE, 0, 
0);
+   * prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_STORE_BYPASS, 
PR_SPEC_FORCE_DISABLE, 0, 0);
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -533,21 +533,37 @@ static void ssb_select_mitigation()
 
 static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
 {
-   bool rds = !!test_tsk_thread_flag(task, TIF_RDS);
+   bool update;
 
if (ssb_mode != SPEC_STORE_BYPASS_PRCTL)
return -ENXIO;
 
-   if (ctrl == PR_SPEC_ENABLE)
-   clear_tsk_thread_flag(task, TIF_RDS);
-   else
-   set_tsk_thread_flag(task, TIF_RDS);
+   switch (ctrl) {
+   case PR_SPEC_ENABLE:
+   /* If speculation is force disabled, enable is not allowed */
+   if (task_spec_ssb_force_disable(task))
+   return -EPERM;
+   task_clear_spec_ssb_disable(task);
+ 

[PATCH 4.14 82/95] x86/cpufeatures: Disentangle MSR_SPEC_CTRL enumeration from IBRS

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit 7eb8956a7fec3c1f0abc2a5517dada99ccc8a961 upstream

The availability of the SPEC_CTRL MSR is enumerated by a CPUID bit on
Intel and implied by IBRS or STIBP support on AMD. That's just confusing
and in case an AMD CPU has IBRS not supported because the underlying
problem has been fixed but has another bit valid in the SPEC_CTRL MSR,
the thing falls apart.

Add a synthetic feature bit X86_FEATURE_MSR_SPEC_CTRL to denote the
availability on both Intel and AMD.

While at it replace the boot_cpu_has() checks with static_cpu_has() where
possible. This prevents late microcode loading from exposing SPEC_CTRL, but
late loading is already very limited as it does not reevaluate the
mitigation options and other bits and pieces. Having static_cpu_has() is
the simplest and least fragile solution.

Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Konrad Rzeszutek Wilk 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/cpufeatures.h |2 ++
 arch/x86/kernel/cpu/bugs.c |   18 +++---
 arch/x86/kernel/cpu/common.c   |9 +++--
 arch/x86/kernel/cpu/intel.c|1 +
 4 files changed, 21 insertions(+), 9 deletions(-)

--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -206,6 +206,8 @@
 #define X86_FEATURE_RETPOLINE_AMD  ( 7*32+13) /* "" AMD Retpoline 
mitigation for Spectre variant 2 */
 #define X86_FEATURE_INTEL_PPIN ( 7*32+14) /* Intel Processor Inventory 
Number */
 
+#define X86_FEATURE_MSR_SPEC_CTRL  ( 7*32+16) /* "" MSR SPEC_CTRL is 
implemented */
+
 #define X86_FEATURE_MBA( 7*32+18) /* Memory Bandwidth 
Allocation */
 #define X86_FEATURE_RSB_CTXSW  ( 7*32+19) /* "" Fill RSB on context 
switches */
 
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -64,7 +64,7 @@ void __init check_bugs(void)
 * have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
 * init code as it is not enumerated and depends on the family.
 */
-   if (boot_cpu_has(X86_FEATURE_IBRS))
+   if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
 
/* Select the proper spectre mitigation before patching alternatives */
@@ -145,7 +145,7 @@ u64 x86_spec_ctrl_get_default(void)
 {
u64 msrval = x86_spec_ctrl_base;
 
-   if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
+   if (static_cpu_has(X86_FEATURE_SPEC_CTRL))
msrval |= ssbd_tif_to_spec_ctrl(current_thread_info()->flags);
return msrval;
 }
@@ -155,10 +155,12 @@ void x86_spec_ctrl_set_guest(u64 guest_s
 {
u64 host = x86_spec_ctrl_base;
 
-   if (!boot_cpu_has(X86_FEATURE_IBRS))
+   /* Is MSR_SPEC_CTRL implemented ? */
+   if (!static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
return;
 
-   if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
+   /* Intel controls SSB in MSR_SPEC_CTRL */
+   if (static_cpu_has(X86_FEATURE_SPEC_CTRL))
host |= ssbd_tif_to_spec_ctrl(current_thread_info()->flags);
 
if (host != guest_spec_ctrl)
@@ -170,10 +172,12 @@ void x86_spec_ctrl_restore_host(u64 gues
 {
u64 host = x86_spec_ctrl_base;
 
-   if (!boot_cpu_has(X86_FEATURE_IBRS))
+   /* Is MSR_SPEC_CTRL implemented ? */
+   if (!static_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
return;
 
-   if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
+   /* Intel controls SSB in MSR_SPEC_CTRL */
+   if (static_cpu_has(X86_FEATURE_SPEC_CTRL))
host |= ssbd_tif_to_spec_ctrl(current_thread_info()->flags);
 
if (host != guest_spec_ctrl)
@@ -631,7 +635,7 @@ int arch_prctl_spec_ctrl_get(struct task
 
 void x86_spec_ctrl_setup_ap(void)
 {
-   if (boot_cpu_has(X86_FEATURE_IBRS))
+   if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
x86_spec_ctrl_set(x86_spec_ctrl_base & ~x86_spec_ctrl_mask);
 
if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -737,19 +737,24 @@ static void init_speculation_control(str
if (cpu_has(c, X86_FEATURE_SPEC_CTRL)) {
set_cpu_cap(c, X86_FEATURE_IBRS);
set_cpu_cap(c, X86_FEATURE_IBPB);
+   set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
}
 
if (cpu_has(c, X86_FEATURE_INTEL_STIBP))
set_cpu_cap(c, X86_FEATURE_STIBP);
 
-   if (cpu_has(c, X86_FEATURE_AMD_IBRS))
+   if (cpu_has(c, X86_FEATURE_AMD_IBRS)) {
set_cpu_cap(c, X86_FEATURE_IBRS);
+   set_cpu_cap(c, X86_FEATURE_MSR_SPEC_CTRL);
+   }
 
if (cpu_has(c, X86_FEATURE_AMD_IBPB))
set_cpu_cap(c, X86_FEATURE_IBPB);
 
-   if (cpu_has(c, X86_FEATURE_AMD_STIBP))
+   if (cpu_

[PATCH 4.14 77/95] x86/bugs: Make cpu_show_common() static

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Jiri Kosina 

commit 7bb4d366cba992904bffa4820d24e70a3de93e76 upstream

cpu_show_common() is not used outside of arch/x86/kernel/cpu/bugs.c, so
make it static.

Signed-off-by: Jiri Kosina 
Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/kernel/cpu/bugs.c |2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -640,7 +640,7 @@ void x86_spec_ctrl_setup_ap(void)
 
 #ifdef CONFIG_SYSFS
 
-ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
+static ssize_t cpu_show_common(struct device *dev, struct device_attribute 
*attr,
char *buf, unsigned int bug)
 {
if (!boot_cpu_has_bug(bug))




[PATCH 4.14 76/95] x86/bugs: Fix __ssb_select_mitigation() return type

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Jiri Kosina 

commit d66d8ff3d21667b41eddbe86b35ab411e40d8c5f upstream

__ssb_select_mitigation() returns one of the members of enum ssb_mitigation,
not ssb_mitigation_cmd; fix the prototype to reflect that.

Fixes: 24f7fc83b9204 ("x86/bugs: Provide boot parameters for the 
spec_store_bypass_disable mitigation")
Signed-off-by: Jiri Kosina 
Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/kernel/cpu/bugs.c |2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -468,7 +468,7 @@ static enum ssb_mitigation_cmd __init ss
return cmd;
 }
 
-static enum ssb_mitigation_cmd __init __ssb_select_mitigation(void)
+static enum ssb_mitigation __init __ssb_select_mitigation(void)
 {
enum ssb_mitigation mode = SPEC_STORE_BYPASS_NONE;
enum ssb_mitigation_cmd cmd;




[PATCH 4.14 40/95] ARM: 8772/1: kprobes: Prohibit kprobes on get_user functions

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Masami Hiramatsu 

commit 0d73c3f8e7f6ee2aab1bb350f60c180f5ae21a2c upstream.

Since do_undefinstr() uses get_user to get the undefined
instruction, it can be called before kprobes processes
recursive check. This can cause an infinit recursive
exception.
Prohibit probing on get_user functions.

Fixes: 24ba613c9d6c ("ARM kprobes: core code")
Signed-off-by: Masami Hiramatsu 
Cc: sta...@vger.kernel.org
Signed-off-by: Russell King 
Signed-off-by: Greg Kroah-Hartman 

---
 arch/arm/include/asm/assembler.h |   10 ++
 arch/arm/lib/getuser.S   |   10 ++
 2 files changed, 20 insertions(+)

--- a/arch/arm/include/asm/assembler.h
+++ b/arch/arm/include/asm/assembler.h
@@ -536,4 +536,14 @@ THUMB( orr \reg , \reg , #PSR_T_BIT)
 #endif
.endm
 
+#ifdef CONFIG_KPROBES
+#define _ASM_NOKPROBE(entry)   \
+   .pushsection "_kprobe_blacklist", "aw" ;\
+   .balign 4 ; \
+   .long entry;\
+   .popsection
+#else
+#define _ASM_NOKPROBE(entry)
+#endif
+
 #endif /* __ASM_ASSEMBLER_H__ */
--- a/arch/arm/lib/getuser.S
+++ b/arch/arm/lib/getuser.S
@@ -38,6 +38,7 @@ ENTRY(__get_user_1)
mov r0, #0
ret lr
 ENDPROC(__get_user_1)
+_ASM_NOKPROBE(__get_user_1)
 
 ENTRY(__get_user_2)
check_uaccess r0, 2, r1, r2, __get_user_bad
@@ -58,6 +59,7 @@ rb.reqr0
mov r0, #0
ret lr
 ENDPROC(__get_user_2)
+_ASM_NOKPROBE(__get_user_2)
 
 ENTRY(__get_user_4)
check_uaccess r0, 4, r1, r2, __get_user_bad
@@ -65,6 +67,7 @@ ENTRY(__get_user_4)
mov r0, #0
ret lr
 ENDPROC(__get_user_4)
+_ASM_NOKPROBE(__get_user_4)
 
 ENTRY(__get_user_8)
check_uaccess r0, 8, r1, r2, __get_user_bad8
@@ -78,6 +81,7 @@ ENTRY(__get_user_8)
mov r0, #0
ret lr
 ENDPROC(__get_user_8)
+_ASM_NOKPROBE(__get_user_8)
 
 #ifdef __ARMEB__
 ENTRY(__get_user_32t_8)
@@ -91,6 +95,7 @@ ENTRY(__get_user_32t_8)
mov r0, #0
ret lr
 ENDPROC(__get_user_32t_8)
+_ASM_NOKPROBE(__get_user_32t_8)
 
 ENTRY(__get_user_64t_1)
check_uaccess r0, 1, r1, r2, __get_user_bad8
@@ -98,6 +103,7 @@ ENTRY(__get_user_64t_1)
mov r0, #0
ret lr
 ENDPROC(__get_user_64t_1)
+_ASM_NOKPROBE(__get_user_64t_1)
 
 ENTRY(__get_user_64t_2)
check_uaccess r0, 2, r1, r2, __get_user_bad8
@@ -114,6 +120,7 @@ rb  .reqr0
mov r0, #0
ret lr
 ENDPROC(__get_user_64t_2)
+_ASM_NOKPROBE(__get_user_64t_2)
 
 ENTRY(__get_user_64t_4)
check_uaccess r0, 4, r1, r2, __get_user_bad8
@@ -121,6 +128,7 @@ ENTRY(__get_user_64t_4)
mov r0, #0
ret lr
 ENDPROC(__get_user_64t_4)
+_ASM_NOKPROBE(__get_user_64t_4)
 #endif
 
 __get_user_bad8:
@@ -131,6 +139,8 @@ __get_user_bad:
ret lr
 ENDPROC(__get_user_bad)
 ENDPROC(__get_user_bad8)
+_ASM_NOKPROBE(__get_user_bad)
+_ASM_NOKPROBE(__get_user_bad8)
 
 .pushsection __ex_table, "a"
.long   1b, __get_user_bad




[PATCH 4.14 37/95] tick/broadcast: Use for_each_cpu() specially on UP kernels

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Dexuan Cui 

commit 5596fe34495cf0f645f417eb928ef224df3e3cb4 upstream.

for_each_cpu() unintuitively reports CPU0 as set independent of the actual
cpumask content on UP kernels. This causes an unexpected PIT interrupt
storm on a UP kernel running in an SMP virtual machine on Hyper-V, and as
a result, the virtual machine can suffer from a strange random delay of 1~20
minutes during boot-up, and sometimes it can hang forever.

Protect if by checking whether the cpumask is empty before entering the
for_each_cpu() loop.

[ tglx: Use !IS_ENABLED(CONFIG_SMP) instead of #ifdeffery ]

Signed-off-by: Dexuan Cui 
Signed-off-by: Thomas Gleixner 
Cc: Josh Poulson 
Cc: "Michael Kelley (EOSG)" 
Cc: Peter Zijlstra 
Cc: Frederic Weisbecker 
Cc: sta...@vger.kernel.org
Cc: Rakib Mullick 
Cc: Jork Loeser 
Cc: Greg Kroah-Hartman 
Cc: Andrew Morton 
Cc: KY Srinivasan 
Cc: Linus Torvalds 
Cc: Alexey Dobriyan 
Cc: Dmitry Vyukov 
Link: 
https://lkml.kernel.org/r/kl1p15301mb000678289fe55ba365b3279abf...@kl1p15301mb0006.apcp153.prod.outlook.com
Link: 
https://lkml.kernel.org/r/kl1p15301mb0006fa63bc22beb64902eaa0bf...@kl1p15301mb0006.apcp153.prod.outlook.com
Signed-off-by: Greg Kroah-Hartman 

---
 kernel/time/tick-broadcast.c |8 
 1 file changed, 8 insertions(+)

--- a/kernel/time/tick-broadcast.c
+++ b/kernel/time/tick-broadcast.c
@@ -612,6 +612,14 @@ static void tick_handle_oneshot_broadcas
now = ktime_get();
/* Find all expired events */
for_each_cpu(cpu, tick_broadcast_oneshot_mask) {
+   /*
+* Required for !SMP because for_each_cpu() reports
+* unconditionally CPU0 as set on UP kernels.
+*/
+   if (!IS_ENABLED(CONFIG_SMP) &&
+   cpumask_empty(tick_broadcast_oneshot_mask))
+   break;
+
td = &per_cpu(tick_cpu_device, cpu);
if (td->evtdev->next_event <= now) {
cpumask_set_cpu(cpu, tmpmask);




[PATCH 4.14 58/95] x86/bugs/AMD: Add support to disable RDS on Fam[15,16,17]h if requested

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Konrad Rzeszutek Wilk 

commit 764f3c21588a059cd783c6ba0734d4db2d72822d upstream

AMD does not need the Speculative Store Bypass mitigation to be enabled.

The parameters for this are already available and can be done via MSR
C001_1020. Each family uses a different bit in that MSR for this.

[ tglx: Expose the bit mask via a variable and move the actual MSR fiddling
into the bugs code as that's the right thing to do and also required
to prepare for dynamic enable/disable ]

Suggested-by: Borislav Petkov 
Signed-off-by: Konrad Rzeszutek Wilk 
Signed-off-by: Thomas Gleixner 
Reviewed-by: Ingo Molnar 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/cpufeatures.h   |1 +
 arch/x86/include/asm/nospec-branch.h |4 
 arch/x86/kernel/cpu/amd.c|   26 ++
 arch/x86/kernel/cpu/bugs.c   |   27 ++-
 arch/x86/kernel/cpu/common.c |4 
 5 files changed, 61 insertions(+), 1 deletion(-)

--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -213,6 +213,7 @@
 #define X86_FEATURE_USE_IBPB   ( 7*32+21) /* "" Indirect Branch 
Prediction Barrier enabled */
 #define X86_FEATURE_USE_IBRS_FW( 7*32+22) /* "" Use IBRS 
during runtime firmware calls */
 #define X86_FEATURE_SPEC_STORE_BYPASS_DISABLE  ( 7*32+23) /* "" Disable 
Speculative Store Bypass. */
+#define X86_FEATURE_AMD_RDS(7*32+24)  /* "" AMD RDS implementation 
*/
 
 /* Virtualization flags: Linux defined, word 8 */
 #define X86_FEATURE_TPR_SHADOW ( 8*32+ 0) /* Intel TPR Shadow */
--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -244,6 +244,10 @@ enum ssb_mitigation {
SPEC_STORE_BYPASS_DISABLE,
 };
 
+/* AMD specific Speculative Store Bypass MSR data */
+extern u64 x86_amd_ls_cfg_base;
+extern u64 x86_amd_ls_cfg_rds_mask;
+
 extern char __indirect_thunk_start[];
 extern char __indirect_thunk_end[];
 
--- a/arch/x86/kernel/cpu/amd.c
+++ b/arch/x86/kernel/cpu/amd.c
@@ -10,6 +10,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -554,6 +555,26 @@ static void bsp_init_amd(struct cpuinfo_
rdmsrl(MSR_FAM10H_NODE_ID, value);
nodes_per_socket = ((value >> 3) & 7) + 1;
}
+
+   if (c->x86 >= 0x15 && c->x86 <= 0x17) {
+   unsigned int bit;
+
+   switch (c->x86) {
+   case 0x15: bit = 54; break;
+   case 0x16: bit = 33; break;
+   case 0x17: bit = 10; break;
+   default: return;
+   }
+   /*
+* Try to cache the base value so further operations can
+* avoid RMW. If that faults, do not enable RDS.
+*/
+   if (!rdmsrl_safe(MSR_AMD64_LS_CFG, &x86_amd_ls_cfg_base)) {
+   setup_force_cpu_cap(X86_FEATURE_RDS);
+   setup_force_cpu_cap(X86_FEATURE_AMD_RDS);
+   x86_amd_ls_cfg_rds_mask = 1ULL << bit;
+   }
+   }
 }
 
 static void early_init_amd(struct cpuinfo_x86 *c)
@@ -872,6 +893,11 @@ static void init_amd(struct cpuinfo_x86
/* AMD CPUs don't reset SS attributes on SYSRET, Xen does. */
if (!cpu_has(c, X86_FEATURE_XENPV))
set_cpu_bug(c, X86_BUG_SYSRET_SS_ATTRS);
+
+   if (boot_cpu_has(X86_FEATURE_AMD_RDS)) {
+   set_cpu_cap(c, X86_FEATURE_RDS);
+   set_cpu_cap(c, X86_FEATURE_AMD_RDS);
+   }
 }
 
 #ifdef CONFIG_X86_32
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -41,6 +41,13 @@ static u64 __ro_after_init x86_spec_ctrl
  */
 static u64 __ro_after_init x86_spec_ctrl_mask = ~SPEC_CTRL_IBRS;
 
+/*
+ * AMD specific MSR info for Speculative Store Bypass control.
+ * x86_amd_ls_cfg_rds_mask is initialized in identify_boot_cpu().
+ */
+u64 __ro_after_init x86_amd_ls_cfg_base;
+u64 __ro_after_init x86_amd_ls_cfg_rds_mask;
+
 void __init check_bugs(void)
 {
identify_boot_cpu();
@@ -52,7 +59,8 @@ void __init check_bugs(void)
 
/*
 * Read the SPEC_CTRL MSR to account for reserved bits which may
-* have unknown values.
+* have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
+* init code as it is not enumerated and depends on the family.
 */
if (boot_cpu_has(X86_FEATURE_IBRS))
rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
@@ -154,6 +162,14 @@ void x86_spec_ctrl_restore_host(u64 gues
 }
 EXPORT_SYMBOL_GPL(x86_spec_ctrl_restore_host);
 
+static void x86_amd_rds_enable(void)
+{
+   u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_rds_mask;
+
+   if (boot_cpu_has(X86_FEATURE_AMD_RDS))
+   wrmsrl(MSR_AMD64_LS_CFG, msrval);
+}
+
 #ifdef RETPOLINE
 static bool spectre_v2_b

[PATCH 4.14 61/95] prctl: Add speculation control prctls

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit b617cfc858161140d69cc0b5cc211996b557a1c7 upstream

Add two new prctls to control aspects of speculation related vulnerabilites
and their mitigations to provide finer grained control over performance
impacting mitigations.

PR_GET_SPECULATION_CTRL returns the state of the speculation misfeature
which is selected with arg2 of prctl(2). The return value uses bit 0-2 with
the following meaning:

Bit  Define   Description
0PR_SPEC_PRCTLMitigation can be controlled per task by
  PR_SET_SPECULATION_CTRL
1PR_SPEC_ENABLE   The speculation feature is enabled, mitigation is
  disabled
2PR_SPEC_DISABLE  The speculation feature is disabled, mitigation is
  enabled

If all bits are 0 the CPU is not affected by the speculation misfeature.

If PR_SPEC_PRCTL is set, then the per task control of the mitigation is
available. If not set, prctl(PR_SET_SPECULATION_CTRL) for the speculation
misfeature will fail.

PR_SET_SPECULATION_CTRL allows to control the speculation misfeature, which
is selected by arg2 of prctl(2) per task. arg3 is used to hand in the
control value, i.e. either PR_SPEC_ENABLE or PR_SPEC_DISABLE.

The common return values are:

EINVAL  prctl is not implemented by the architecture or the unused prctl()
arguments are not 0
ENODEV  arg2 is selecting a not supported speculation misfeature

PR_SET_SPECULATION_CTRL has these additional return values:

ERANGE  arg3 is incorrect, i.e. it's not either PR_SPEC_ENABLE or 
PR_SPEC_DISABLE
ENXIO   prctl control of the selected speculation misfeature is disabled

The first supported controlable speculation misfeature is
PR_SPEC_STORE_BYPASS. Add the define so this can be shared between
architectures.

Based on an initial patch from Tim Chen and mostly rewritten.

Signed-off-by: Thomas Gleixner 
Reviewed-by: Ingo Molnar 
Reviewed-by: Konrad Rzeszutek Wilk 
Signed-off-by: Greg Kroah-Hartman 
---
 Documentation/userspace-api/index.rst |1 
 Documentation/userspace-api/spec_ctrl.rst |   86 ++
 include/linux/nospec.h|5 +
 include/uapi/linux/prctl.h|   11 +++
 kernel/sys.c  |   22 +++
 5 files changed, 125 insertions(+)
 create mode 100644 Documentation/userspace-api/spec_ctrl.rst

--- a/Documentation/userspace-api/index.rst
+++ b/Documentation/userspace-api/index.rst
@@ -19,6 +19,7 @@ place where this information is gathered
no_new_privs
seccomp_filter
unshare
+   spec_ctrl
 
 .. only::  subproject and html
 
--- /dev/null
+++ b/Documentation/userspace-api/spec_ctrl.rst
@@ -0,0 +1,86 @@
+===
+Speculation Control
+===
+
+Quite some CPUs have speculation related misfeatures which are in fact
+vulnerabilites causing data leaks in various forms even accross privilege
+domains.
+
+The kernel provides mitigation for such vulnerabilities in various
+forms. Some of these mitigations are compile time configurable and some on
+the kernel command line.
+
+There is also a class of mitigations which are very expensive, but they can
+be restricted to a certain set of processes or tasks in controlled
+environments. The mechanism to control these mitigations is via
+:manpage:`prctl(2)`.
+
+There are two prctl options which are related to this:
+
+ * PR_GET_SPECULATION_CTRL
+
+ * PR_SET_SPECULATION_CTRL
+
+PR_GET_SPECULATION_CTRL
+---
+
+PR_GET_SPECULATION_CTRL returns the state of the speculation misfeature
+which is selected with arg2 of prctl(2). The return value uses bits 0-2 with
+the following meaning:
+
+  ===
+Bit  Define   Description
+  ===
+0PR_SPEC_PRCTLMitigation can be controlled per task by
+  PR_SET_SPECULATION_CTRL
+1PR_SPEC_ENABLE   The speculation feature is enabled, mitigation is
+  disabled
+2PR_SPEC_DISABLE  The speculation feature is disabled, mitigation is
+  enabled
+  ===
+
+If all bits are 0 the CPU is not affected by the speculation misfeature.
+
+If PR_SPEC_PRCTL is set, then the per task control of the mitigation is
+available. If not set, prctl(PR_SET_SPECULATION_CTRL) for the speculation
+misfeature will fail.
+
+PR_SET_SPECULATION_CTRL
+---
+PR_SET_SPECULATION_CTRL allows to control the speculation misfeature, which
+is selected by arg2 of :manpage:`prctl(2)` per task. arg3 is used to hand
+in the control value, i.e. either PR_SPEC_ENABLE or PR_SPEC_DISABLE.
+
+Common error codes
+--
+=== =
+

[PATCH 4.14 65/95] proc: Provide details on speculation flaw mitigations

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Kees Cook 

commit fae1fa0fc6cca8beee3ab8ed71d54f9a78fa3f64 upstream

As done with seccomp and no_new_privs, also show speculation flaw
mitigation state in /proc/$pid/status.

Signed-off-by: Kees Cook 
Signed-off-by: Thomas Gleixner 
Signed-off-by: Greg Kroah-Hartman 
---
 fs/proc/array.c |   22 ++
 1 file changed, 22 insertions(+)

--- a/fs/proc/array.c
+++ b/fs/proc/array.c
@@ -85,6 +85,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -347,6 +348,27 @@ static inline void task_seccomp(struct s
 #ifdef CONFIG_SECCOMP
seq_put_decimal_ull(m, "\nSeccomp:\t", p->seccomp.mode);
 #endif
+   seq_printf(m, "\nSpeculation Store Bypass:\t");
+   switch (arch_prctl_spec_ctrl_get(p, PR_SPEC_STORE_BYPASS)) {
+   case -EINVAL:
+   seq_printf(m, "unknown");
+   break;
+   case PR_SPEC_NOT_AFFECTED:
+   seq_printf(m, "not vulnerable");
+   break;
+   case PR_SPEC_PRCTL | PR_SPEC_DISABLE:
+   seq_printf(m, "thread mitigated");
+   break;
+   case PR_SPEC_PRCTL | PR_SPEC_ENABLE:
+   seq_printf(m, "thread vulnerable");
+   break;
+   case PR_SPEC_DISABLE:
+   seq_printf(m, "globally mitigated");
+   break;
+   default:
+   seq_printf(m, "vulnerable");
+   break;
+   }
seq_putc(m, '\n');
 }
 




[PATCH 4.14 50/95] x86/bugs: Concentrate bug reporting into a separate function

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Konrad Rzeszutek Wilk 

commit d1059518b4789cabe34bb4b714d07e6089c82ca1 upstream

Those SysFS functions have a similar preamble, as such make common
code to handle them.

Suggested-by: Borislav Petkov 
Signed-off-by: Konrad Rzeszutek Wilk 
Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Ingo Molnar 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/kernel/cpu/bugs.c |   46 +++--
 1 file changed, 32 insertions(+), 14 deletions(-)

--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -314,30 +314,48 @@ retpoline_auto:
 #undef pr_fmt
 
 #ifdef CONFIG_SYSFS
-ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, 
char *buf)
+
+ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr,
+   char *buf, unsigned int bug)
 {
-   if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
+   if (!boot_cpu_has_bug(bug))
return sprintf(buf, "Not affected\n");
-   if (boot_cpu_has(X86_FEATURE_PTI))
-   return sprintf(buf, "Mitigation: PTI\n");
+
+   switch (bug) {
+   case X86_BUG_CPU_MELTDOWN:
+   if (boot_cpu_has(X86_FEATURE_PTI))
+   return sprintf(buf, "Mitigation: PTI\n");
+
+   break;
+
+   case X86_BUG_SPECTRE_V1:
+   return sprintf(buf, "Mitigation: __user pointer 
sanitization\n");
+
+   case X86_BUG_SPECTRE_V2:
+   return sprintf(buf, "%s%s%s%s\n", 
spectre_v2_strings[spectre_v2_enabled],
+  boot_cpu_has(X86_FEATURE_USE_IBPB) ? ", IBPB" : 
"",
+  boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", 
IBRS_FW" : "",
+  spectre_v2_module_string());
+
+   default:
+   break;
+   }
+
return sprintf(buf, "Vulnerable\n");
 }
 
+ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, 
char *buf)
+{
+   return cpu_show_common(dev, attr, buf, X86_BUG_CPU_MELTDOWN);
+}
+
 ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, 
char *buf)
 {
-   if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1))
-   return sprintf(buf, "Not affected\n");
-   return sprintf(buf, "Mitigation: __user pointer sanitization\n");
+   return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V1);
 }
 
 ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, 
char *buf)
 {
-   if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
-   return sprintf(buf, "Not affected\n");
-
-   return sprintf(buf, "%s%s%s%s\n", 
spectre_v2_strings[spectre_v2_enabled],
-  boot_cpu_has(X86_FEATURE_USE_IBPB) ? ", IBPB" : "",
-  boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "",
-  spectre_v2_module_string());
+   return cpu_show_common(dev, attr, buf, X86_BUG_SPECTRE_V2);
 }
 #endif




[PATCH 4.14 55/95] x86/bugs: Provide boot parameters for the spec_store_bypass_disable mitigation

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Konrad Rzeszutek Wilk 

commit 24f7fc83b9204d20f878c57cb77d261ae825e033 upstream

Contemporary high performance processors use a common industry-wide
optimization known as "Speculative Store Bypass" in which loads from
addresses to which a recent store has occurred may (speculatively) see an
older value. Intel refers to this feature as "Memory Disambiguation" which
is part of their "Smart Memory Access" capability.

Memory Disambiguation can expose a cache side-channel attack against such
speculatively read values. An attacker can create exploit code that allows
them to read memory outside of a sandbox environment (for example,
malicious JavaScript in a web page), or to perform more complex attacks
against code running within the same privilege level, e.g. via the stack.

As a first step to mitigate against such attacks, provide two boot command
line control knobs:

 nospec_store_bypass_disable
 spec_store_bypass_disable=[off,auto,on]

By default affected x86 processors will power on with Speculative
Store Bypass enabled. Hence the provided kernel parameters are written
from the point of view of whether to enable a mitigation or not.
The parameters are as follows:

 - auto - Kernel detects whether your CPU model contains an implementation
  of Speculative Store Bypass and picks the most appropriate
  mitigation.

 - on   - disable Speculative Store Bypass
 - off  - enable Speculative Store Bypass

[ tglx: Reordered the checks so that the whole evaluation is not done
when the CPU does not support RDS ]

Signed-off-by: Konrad Rzeszutek Wilk 
Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Ingo Molnar 
Signed-off-by: Greg Kroah-Hartman 
---
 Documentation/admin-guide/kernel-parameters.txt |   33 +++
 arch/x86/include/asm/cpufeatures.h  |1 
 arch/x86/include/asm/nospec-branch.h|6 +
 arch/x86/kernel/cpu/bugs.c  |  103 
 4 files changed, 143 insertions(+)

--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -2600,6 +2600,9 @@
allow data leaks with this option, which is equivalent
to spectre_v2=off.
 
+   nospec_store_bypass_disable
+   [HW] Disable all mitigations for the Speculative Store 
Bypass vulnerability
+
noxsave [BUGS=X86] Disables x86 extended register state save
and restore using xsave. The kernel will fallback to
enabling legacy floating-point and sse state.
@@ -3930,6 +3933,36 @@
Not specifying this option is equivalent to
spectre_v2=auto.
 
+   spec_store_bypass_disable=
+   [HW] Control Speculative Store Bypass (SSB) Disable 
mitigation
+   (Speculative Store Bypass vulnerability)
+
+   Certain CPUs are vulnerable to an exploit against a
+   a common industry wide performance optimization known
+   as "Speculative Store Bypass" in which recent stores
+   to the same memory location may not be observed by
+   later loads during speculative execution. The idea
+   is that such stores are unlikely and that they can
+   be detected prior to instruction retirement at the
+   end of a particular speculation execution window.
+
+   In vulnerable processors, the speculatively forwarded
+   store can be used in a cache side channel attack, for
+   example to read memory to which the attacker does not
+   directly have access (e.g. inside sandboxed code).
+
+   This parameter controls whether the Speculative Store
+   Bypass optimization is used.
+
+   on - Unconditionally disable Speculative Store 
Bypass
+   off- Unconditionally enable Speculative Store Bypass
+   auto   - Kernel detects whether the CPU model contains 
an
+implementation of Speculative Store Bypass and
+picks the most appropriate mitigation
+
+   Not specifying this option is equivalent to
+   spec_store_bypass_disable=auto.
+
spia_io_base=   [HW,MTD]
spia_fio_base=
spia_pedr=
--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -212,6 +212,7 @@
 
 #define X86_FEATURE_USE_IBPB   ( 7*32+21) /* "" Indirect Branch 
Prediction Barrier enabled */
 #define X86_FEATURE_USE_IBRS_FW( 7*32+22) /* "" Use IBRS 
durin

[PATCH 4.14 47/95] btrfs: fix reading stale metadata blocks after degraded raid1 mounts

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Liu Bo 

commit 02a3307aa9c20b4f6626255b028f07f6cfa16feb upstream.

If a btree block, aka. extent buffer, is not available in the extent
buffer cache, it'll be read out from the disk instead, i.e.

btrfs_search_slot()
  read_block_for_search()  # hold parent and its lock, go to read child
btrfs_release_path()
read_tree_block()  # read child

Unfortunately, the parent lock got released before reading child, so
commit 5bdd3536cbbe ("Btrfs: Fix block generation verification race") had
used 0 as parent transid to read the child block.  It forces
read_tree_block() not to check if parent transid is different with the
generation id of the child that it reads out from disk.

A simple PoC is included in btrfs/124,

0. A two-disk raid1 btrfs,

1. Right after mkfs.btrfs, block A is allocated to be device tree's root.

2. Mount this filesystem and put it in use, after a while, device tree's
   root got COW but block A hasn't been allocated/overwritten yet.

3. Umount it and reload the btrfs module to remove both disks from the
   global @fs_devices list.

4. mount -odegraded dev1 and write some data, so now block A is allocated
   to be a leaf in checksum tree.  Note that only dev1 has the latest
   metadata of this filesystem.

5. Umount it and mount it again normally (with both disks), since raid1
   can pick up one disk by the writer task's pid, if btrfs_search_slot()
   needs to read block A, dev2 which does NOT have the latest metadata
   might be read for block A, then we got a stale block A.

6. As parent transid is not checked, block A is marked as uptodate and
   put into the extent buffer cache, so the future search won't bother
   to read disk again, which means it'll make changes on this stale
   one and make it dirty and flush it onto disk.

To avoid the problem, parent transid needs to be passed to
read_tree_block().

In order to get a valid parent transid, we need to hold the parent's
lock until finishing reading child.

This patch needs to be slightly adapted for stable kernels, the
&first_key parameter added to read_tree_block() is from 4.16+
(581c1760415c4). The fix is to replace 0 by 'gen'.

Fixes: 5bdd3536cbbe ("Btrfs: Fix block generation verification race")
CC: sta...@vger.kernel.org # 4.4+
Signed-off-by: Liu Bo 
Reviewed-by: Filipe Manana 
Reviewed-by: Qu Wenruo 
[ update changelog ]
Signed-off-by: David Sterba 
Signed-off-by: Nikolay Borisov 
Signed-off-by: Greg Kroah-Hartman 

---
 fs/btrfs/ctree.c |6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -2497,10 +2497,8 @@ read_block_for_search(struct btrfs_root
if (p->reada != READA_NONE)
reada_for_search(fs_info, p, level, slot, key->objectid);
 
-   btrfs_release_path(p);
-
ret = -EAGAIN;
-   tmp = read_tree_block(fs_info, blocknr, 0);
+   tmp = read_tree_block(fs_info, blocknr, gen);
if (!IS_ERR(tmp)) {
/*
 * If the read above didn't mark this buffer up to date,
@@ -2514,6 +2512,8 @@ read_block_for_search(struct btrfs_root
} else {
ret = PTR_ERR(tmp);
}
+
+   btrfs_release_path(p);
return ret;
 }
 




[PATCH 4.14 08/95] ALSA: control: fix a redundant-copy issue

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Wenwen Wang 

commit 3f12888dfae2a48741c4caa9214885b3aaf350f9 upstream.

In snd_ctl_elem_add_compat(), the fields of the struct 'data' need to be
copied from the corresponding fields of the struct 'data32' in userspace.
This is achieved by invoking copy_from_user() and get_user() functions. The
problem here is that the 'type' field is copied twice. One is by
copy_from_user() and one is by get_user(). Given that the 'type' field is
not used between the two copies, the second copy is *completely* redundant
and should be removed for better performance and cleanup. Also, these two
copies can cause inconsistent data: as the struct 'data32' resides in
userspace and a malicious userspace process can race to change the 'type'
field between the two copies to cause inconsistent data. Depending on how
the data is used in the future, such an inconsistency may cause potential
security risks.

For above reasons, we should take out the second copy.

Signed-off-by: Wenwen Wang 
Cc: 
Signed-off-by: Takashi Iwai 
Signed-off-by: Greg Kroah-Hartman 

---
 sound/core/control_compat.c |3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

--- a/sound/core/control_compat.c
+++ b/sound/core/control_compat.c
@@ -396,8 +396,7 @@ static int snd_ctl_elem_add_compat(struc
if (copy_from_user(&data->id, &data32->id, sizeof(data->id)) ||
copy_from_user(&data->type, &data32->type, 3 * sizeof(u32)))
goto error;
-   if (get_user(data->owner, &data32->owner) ||
-   get_user(data->type, &data32->type))
+   if (get_user(data->owner, &data32->owner))
goto error;
switch (data->type) {
case SNDRV_CTL_ELEM_TYPE_BOOLEAN:




[PATCH 4.14 42/95] Btrfs: send, fix invalid access to commit roots due to concurrent snapshotting

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Robbie Ko 

commit 6f2f0b394b54e2b159ef969a0b5274e9bbf82ff2 upstream.

[BUG]
btrfs incremental send BUG happens when creating a snapshot of snapshot
that is being used by send.

[REASON]
The problem can happen if while we are doing a send one of the snapshots
used (parent or send) is snapshotted, because snapshoting implies COWing
the root of the source subvolume/snapshot.

1. When doing an incremental send, the send process will get the commit
   roots from the parent and send snapshots, and add references to them
   through extent_buffer_get().

2. When a snapshot/subvolume is snapshotted, its root node is COWed
   (transaction.c:create_pending_snapshot()).

3. COWing releases the space used by the node immediately, through:

   __btrfs_cow_block()
   --btrfs_free_tree_block()
   btrfs_add_free_space(bytenr of node)

4. Because send doesn't hold a transaction open, it's possible that
   the transaction used to create the snapshot commits, switches the
   commit root and the old space used by the previous root node gets
   assigned to some other node allocation. Allocation of a new node will
   use the existing extent buffer found in memory, which we previously
   got a reference through extent_buffer_get(), and allow the extent
   buffer's content (pages) to be modified:

   btrfs_alloc_tree_block
   --btrfs_reserve_extent
   find_free_extent (get bytenr of old node)
   --btrfs_init_new_buffer (use bytenr of old node)
   btrfs_find_create_tree_block
   --alloc_extent_buffer
   find_extent_buffer (get old node)

5. So send can access invalid memory content and have unpredictable
   behaviour.

[FIX]
So we fix the problem by copying the commit roots of the send and
parent snapshots and use those copies.

CallTrace looks like this:
 [ cut here ]
 kernel BUG at fs/btrfs/ctree.c:1861!
 invalid opcode:  [#1] SMP
 CPU: 6 PID: 24235 Comm: btrfs Tainted: P   O 3.10.105 #23721
 88046652d680 ti: 88041b72 task.ti: 88041b72
 RIP: 0010:[] read_node_slot+0x108/0x110 [btrfs]
 RSP: 0018:88041b723b68  EFLAGS: 00010246
 RAX: 88043ca6b000 RBX: 88041b723c50 RCX: 8800
 RDX: 004c RSI: 880314b133f8 RDI: 880458b24000
 RBP:  R08: 0001 R09: 88041b723c66
 R10: 0001 R11: 1000 R12: 8803f3e48890
 R13: 8803f3e48880 R14: 880466351800 R15: 0001
 FS:  7f8c321dc8c0() GS:88047fcc()
 CS:  0010 DS:  ES:  CR0: 80050033
 R2: 7efd1006d000 CR3: 000213a24000 CR4: 003407e0
 DR0:  DR1:  DR2: 
 DR3:  DR6: fffe0ff0 DR7: 0400
 Stack:
 88041b723c50 8803f3e48880 8803f3e48890 8803f3e48880
 880466351800 0001 a08dd9d7 88041b723c50
 8803f3e48880 88041b723c66 a08dde85 a9ff88042d2c4400
 Call Trace:
 [] ? tree_move_down.isra.33+0x27/0x50 [btrfs]
 [] ? tree_advance+0xb5/0xc0 [btrfs]
 [] ? btrfs_compare_trees+0x2d4/0x760 [btrfs]
 [] ? finish_inode_if_needed+0x870/0x870 [btrfs]
 [] ? btrfs_ioctl_send+0xeda/0x1050 [btrfs]
 [] ? btrfs_ioctl+0x1e3d/0x33f0 [btrfs]
 [] ? handle_pte_fault+0x373/0x990
 [] ? atomic_notifier_call_chain+0x16/0x20
 [] ? set_task_cpu+0xb6/0x1d0
 [] ? handle_mm_fault+0x143/0x2a0
 [] ? __do_page_fault+0x1d0/0x500
 [] ? check_preempt_curr+0x57/0x90
 [] ? do_vfs_ioctl+0x4aa/0x990
 [] ? do_fork+0x113/0x3b0
 [] ? trace_hardirqs_off_thunk+0x3a/0x6c
 [] ? SyS_ioctl+0x88/0xa0
 [] ? system_call_fastpath+0x16/0x1b
 ---[ end trace 29576629ee80b2e1 ]---

Fixes: 7069830a9e38 ("Btrfs: add btrfs_compare_trees function")
CC: sta...@vger.kernel.org # 3.6+
Signed-off-by: Robbie Ko 
Reviewed-by: Filipe Manana 
Signed-off-by: David Sterba 
Signed-off-by: Greg Kroah-Hartman 

---
 fs/btrfs/ctree.c |   16 ++--
 1 file changed, 14 insertions(+), 2 deletions(-)

--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -5454,12 +5454,24 @@ int btrfs_compare_trees(struct btrfs_roo
down_read(&fs_info->commit_root_sem);
left_level = btrfs_header_level(left_root->commit_root);
left_root_level = left_level;
-   left_path->nodes[left_level] = left_root->commit_root;
+   left_path->nodes[left_level] =
+   btrfs_clone_extent_buffer(left_root->commit_root);
+   if (!left_path->nodes[left_level]) {
+   up_read(&fs_info->commit_root_sem);
+   ret = -ENOMEM;
+   goto out;
+   }
extent_buffer_get(left_path->nodes[left_level]);
 
right_level = btrfs_header_level(right_root->commit_root);
right_root_level = right_level;
-   right_path->nodes[right_level] = right_root->commit_root;
+   right_path->nodes[right_level] =
+   btrfs_clone_extent

[PATCH 4.14 48/95] x86/nospec: Simplify alternative_msr_write()

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Linus Torvalds 

commit 1aa7a5735a41418d8e01fa7c9565eb2657e2ea3f upstream

The macro is not type safe and I did look for why that "g" constraint for
the asm doesn't work: it's because the asm is more fundamentally wrong.

It does

movl %[val], %%eax

but "val" isn't a 32-bit value, so then gcc will pass it in a register,
and generate code like

movl %rsi, %eax

and gas will complain about a nonsensical 'mov' instruction (it's moving a
64-bit register to a 32-bit one).

Passing it through memory will just hide the real bug - gcc still thinks
the memory location is 64-bit, but the "movl" will only load the first 32
bits and it all happens to work because x86 is little-endian.

Convert it to a type safe inline function with a little trick which hands
the feature into the ALTERNATIVE macro.

Signed-off-by: Linus Torvalds 
Signed-off-by: Thomas Gleixner 
Reviewed-by: Ingo Molnar 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/nospec-branch.h |   19 ++-
 1 file changed, 10 insertions(+), 9 deletions(-)

--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -241,15 +241,16 @@ static inline void vmexit_fill_RSB(void)
 #endif
 }
 
-#define alternative_msr_write(_msr, _val, _feature)\
-   asm volatile(ALTERNATIVE("",\
-"movl %[msr], %%ecx\n\t"   \
-"movl %[val], %%eax\n\t"   \
-"movl $0, %%edx\n\t"   \
-"wrmsr",   \
-_feature)  \
-: : [msr] "i" (_msr), [val] "i" (_val) \
-: "eax", "ecx", "edx", "memory")
+static __always_inline
+void alternative_msr_write(unsigned int msr, u64 val, unsigned int feature)
+{
+   asm volatile(ALTERNATIVE("", "wrmsr", %c[feature])
+   : : "c" (msr),
+   "a" (val),
+   "d" (val >> 32),
+   [feature] "i" (feature)
+   : "memory");
+}
 
 static inline void indirect_branch_prediction_barrier(void)
 {




[PATCH 4.14 44/95] btrfs: fix crash when trying to resume balance without the resume flag

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Anand Jain 

commit 02ee654d3a04563c67bfe658a05384548b9bb105 upstream.

We set the BTRFS_BALANCE_RESUME flag in the btrfs_recover_balance()
only, which isn't called during the remount. So when resuming from
the paused balance we hit the bug:

 kernel: kernel BUG at fs/btrfs/volumes.c:3890!
 ::
 kernel:  balance_kthread+0x51/0x60 [btrfs]
 kernel:  kthread+0x111/0x130
 ::
 kernel: RIP: btrfs_balance+0x12e1/0x1570 [btrfs] RSP: ba7d0090bde8

Reproducer:
  On a mounted filesystem:

  btrfs balance start --full-balance /btrfs
  btrfs balance pause /btrfs
  mount -o remount,ro /dev/sdb /btrfs
  mount -o remount,rw /dev/sdb /btrfs

To fix this set the BTRFS_BALANCE_RESUME flag in
btrfs_resume_balance_async().

CC: sta...@vger.kernel.org # 4.4+
Signed-off-by: Anand Jain 
Reviewed-by: David Sterba 
Signed-off-by: David Sterba 
Signed-off-by: Greg Kroah-Hartman 

---
 fs/btrfs/volumes.c |9 +
 1 file changed, 9 insertions(+)

--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -3964,6 +3964,15 @@ int btrfs_resume_balance_async(struct bt
return 0;
}
 
+   /*
+* A ro->rw remount sequence should continue with the paused balance
+* regardless of who pauses it, system or the user as of now, so set
+* the resume flag.
+*/
+   spin_lock(&fs_info->balance_lock);
+   fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
+   spin_unlock(&fs_info->balance_lock);
+
tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
return PTR_ERR_OR_ZERO(tsk);
 }




[PATCH 4.14 29/95] s390/cpum_sf: ensure sample frequency of perf event attributes is non-zero

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Hendrik Brueckner 

commit 4bbaf2584b86b0772413edeac22ff448f36351b1 upstream.

Correct a trinity finding for the perf_event_open() system call with
a perf event attribute structure that uses a frequency but has the
sampling frequency set to zero.  This causes a FP divide exception during
the sample rate initialization for the hardware sampling facility.

Fixes: 8c069ff4bd606 ("s390/perf: add support for the CPU-Measurement Sampling 
Facility")
Cc: sta...@vger.kernel.org # 3.14+
Reviewed-by: Heiko Carstens 
Signed-off-by: Hendrik Brueckner 
Signed-off-by: Martin Schwidefsky 
Signed-off-by: Greg Kroah-Hartman 

---
 arch/s390/kernel/perf_cpum_sf.c |4 
 1 file changed, 4 insertions(+)

--- a/arch/s390/kernel/perf_cpum_sf.c
+++ b/arch/s390/kernel/perf_cpum_sf.c
@@ -739,6 +739,10 @@ static int __hw_perf_event_init(struct p
 */
rate = 0;
if (attr->freq) {
+   if (!attr->sample_freq) {
+   err = -EINVAL;
+   goto out;
+   }
rate = freq_to_sample_rate(&si, attr->sample_freq);
rate = hw_limit_rate(&si, rate);
attr->freq = 0;




[PATCH 4.14 04/95] usbip: usbip_host: fix NULL-ptr deref and use-after-free errors

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Shuah Khan (Samsung OSG) 

commit 22076557b07c12086eeb16b8ce2b0b735f7a27e7 upstream.

usbip_host updates device status without holding lock from stub probe,
disconnect and rebind code paths. When multiple requests to import a
device are received, these unprotected code paths step all over each
other and drive fails with NULL-ptr deref and use-after-free errors.

The driver uses a table lock to protect the busid array for adding and
deleting busids to the table. However, the probe, disconnect and rebind
paths get the busid table entry and update the status without holding
the busid table lock. Add a new finer grain lock to protect the busid
entry. This new lock will be held to search and update the busid entry
fields from get_busid_idx(), add_match_busid() and del_match_busid().

match_busid_show() does the same to access the busid entry fields.

get_busid_priv() changed to return the pointer to the busid entry holding
the busid lock. stub_probe(), stub_disconnect() and stub_device_rebind()
call put_busid_priv() to release the busid lock before returning. This
changes fixes the unprotected code paths eliminating the race conditions
in updating the busid entries.

Reported-by: Jakub Jirasek
Signed-off-by: Shuah Khan (Samsung OSG) 
Cc: stable 
Signed-off-by: Greg Kroah-Hartman 

---
 drivers/usb/usbip/stub.h  |2 ++
 drivers/usb/usbip/stub_dev.c  |   33 +++--
 drivers/usb/usbip/stub_main.c |   40 +++-
 3 files changed, 60 insertions(+), 15 deletions(-)

--- a/drivers/usb/usbip/stub.h
+++ b/drivers/usb/usbip/stub.h
@@ -87,6 +87,7 @@ struct bus_id_priv {
struct stub_device *sdev;
struct usb_device *udev;
char shutdown_busid;
+   spinlock_t busid_lock;
 };
 
 /* stub_priv is allocated from stub_priv_cache */
@@ -97,6 +98,7 @@ extern struct usb_device_driver stub_dri
 
 /* stub_main.c */
 struct bus_id_priv *get_busid_priv(const char *busid);
+void put_busid_priv(struct bus_id_priv *bid);
 int del_match_busid(char *busid);
 void stub_device_cleanup_urbs(struct stub_device *sdev);
 
--- a/drivers/usb/usbip/stub_dev.c
+++ b/drivers/usb/usbip/stub_dev.c
@@ -314,7 +314,7 @@ static int stub_probe(struct usb_device
struct stub_device *sdev = NULL;
const char *udev_busid = dev_name(&udev->dev);
struct bus_id_priv *busid_priv;
-   int rc;
+   int rc = 0;
 
dev_dbg(&udev->dev, "Enter probe\n");
 
@@ -331,13 +331,15 @@ static int stub_probe(struct usb_device
 * other matched drivers by the driver core.
 * See driver_probe_device() in driver/base/dd.c
 */
-   return -ENODEV;
+   rc = -ENODEV;
+   goto call_put_busid_priv;
}
 
if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
 udev_busid);
-   return -ENODEV;
+   rc = -ENODEV;
+   goto call_put_busid_priv;
}
 
if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
@@ -345,13 +347,16 @@ static int stub_probe(struct usb_device
"%s is attached on vhci_hcd... skip!\n",
udev_busid);
 
-   return -ENODEV;
+   rc = -ENODEV;
+   goto call_put_busid_priv;
}
 
/* ok, this is my device */
sdev = stub_device_alloc(udev);
-   if (!sdev)
-   return -ENOMEM;
+   if (!sdev) {
+   rc = -ENOMEM;
+   goto call_put_busid_priv;
+   }
 
dev_info(&udev->dev,
"usbip-host: register new device (bus %u dev %u)\n",
@@ -383,7 +388,9 @@ static int stub_probe(struct usb_device
}
busid_priv->status = STUB_BUSID_ALLOC;
 
-   return 0;
+   rc = 0;
+   goto call_put_busid_priv;
+
 err_files:
usb_hub_release_port(udev->parent, udev->portnum,
 (struct usb_dev_state *) udev);
@@ -393,6 +400,9 @@ err_port:
 
busid_priv->sdev = NULL;
stub_device_free(sdev);
+
+call_put_busid_priv:
+   put_busid_priv(busid_priv);
return rc;
 }
 
@@ -431,7 +441,7 @@ static void stub_disconnect(struct usb_d
/* get stub_device */
if (!sdev) {
dev_err(&udev->dev, "could not get device");
-   return;
+   goto call_put_busid_priv;
}
 
dev_set_drvdata(&udev->dev, NULL);
@@ -446,12 +456,12 @@ static void stub_disconnect(struct usb_d
  (struct usb_dev_state *) udev);
if (rc) {
dev_dbg(&udev->dev, "unable to release port\n");
-   return;
+   goto call_put_busid_priv;
}
 
/* If usb reset is called from event handler */
if (

[PATCH 4.14 06/95] ALSA: usb: mixer: volume quirk for CM102-A+/102S+

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Federico Cuello 

commit 21493316a3c4598f308d5a9fa31cc74639c4caff upstream.

Currently it's not possible to set volume lower than 26% (it just mutes).

Also fixes this warning:

  Warning! Unlikely big volume range (=9472), cval->res is probably wrong.
  [13] FU [PCM Playback Volume] ch = 2, val = -9473/-1/1

, and volume works fine for full range.

Signed-off-by: Federico Cuello 
Cc: 
Signed-off-by: Takashi Iwai 
Signed-off-by: Greg Kroah-Hartman 

---
 sound/usb/mixer.c |8 
 1 file changed, 8 insertions(+)

--- a/sound/usb/mixer.c
+++ b/sound/usb/mixer.c
@@ -911,6 +911,14 @@ static void volume_control_quirks(struct
}
break;
 
+   case USB_ID(0x0d8c, 0x0103):
+   if (!strcmp(kctl->id.name, "PCM Playback Volume")) {
+   usb_audio_info(chip,
+"set volume quirk for CM102-A+/102S+\n");
+   cval->min = -256;
+   }
+   break;
+
case USB_ID(0x0471, 0x0101):
case USB_ID(0x0471, 0x0104):
case USB_ID(0x0471, 0x0105):




[PATCH 4.14 02/95] usbip: usbip_host: delete device from busid_table after rebind

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Shuah Khan (Samsung OSG) 

commit 1e180f167d4e413afccbbb4a421b48b2de832549 upstream.

Device is left in the busid_table after unbind and rebind. Rebind
initiates usb bus scan and the original driver claims the device.
After rescan the device should be deleted from the busid_table as
it no longer belongs to usbip_host.

Fix it to delete the device after device_attach() succeeds.

Signed-off-by: Shuah Khan (Samsung OSG) 
Cc: stable 
Signed-off-by: Greg Kroah-Hartman 

---
 drivers/usb/usbip/stub_main.c |6 ++
 1 file changed, 6 insertions(+)

--- a/drivers/usb/usbip/stub_main.c
+++ b/drivers/usb/usbip/stub_main.c
@@ -200,6 +200,9 @@ static ssize_t rebind_store(struct devic
if (!bid)
return -ENODEV;
 
+   /* mark the device for deletion so probe ignores it during rescan */
+   bid->status = STUB_BUSID_OTHER;
+
/* device_attach() callers should hold parent lock for USB */
if (bid->udev->dev.parent)
device_lock(bid->udev->dev.parent);
@@ -211,6 +214,9 @@ static ssize_t rebind_store(struct devic
return ret;
}
 
+   /* delete device from busid_table */
+   del_match_busid((char *) buf);
+
return count;
 }
 




[PATCH 4.14 20/95] netfilter: nf_socket: Fix out of bounds access in nf_sk_lookup_slow_v{4,6}

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Subash Abhinov Kasiviswanathan 

commit 32c1733f0dd4bd11d6e65512bf4dc337c0452c8e upstream.

skb_header_pointer will copy data into a buffer if data is non linear,
otherwise it will return a pointer in the linear section of the data.
nf_sk_lookup_slow_v{4,6} always copies data of size udphdr but later
accesses memory within the size of tcphdr (th->doff) in case of TCP
packets. This causes a crash when running with KASAN with the following
call stack -

BUG: KASAN: stack-out-of-bounds in xt_socket_lookup_slow_v4+0x524/0x718
net/netfilter/xt_socket.c:178
Read of size 2 at addr ffe3d417a87c by task syz-executor/28971
CPU: 2 PID: 28971 Comm: syz-executor Tainted: GB   W  O4.9.65+ #1
Call trace:
[] dump_backtrace+0x0/0x428 arch/arm64/kernel/traps.c:76
[] show_stack+0x28/0x38 arch/arm64/kernel/traps.c:226
[] __dump_stack lib/dump_stack.c:15 [inline]
[] dump_stack+0xd4/0x124 lib/dump_stack.c:51
[] print_address_description+0x68/0x258 mm/kasan/report.c:248
[] kasan_report_error mm/kasan/report.c:347 [inline]
[] kasan_report.part.2+0x228/0x2f0 mm/kasan/report.c:371
[] kasan_report+0x5c/0x70 mm/kasan/report.c:372
[] check_memory_region_inline mm/kasan/kasan.c:308 [inline]
[] __asan_load2+0x84/0x98 mm/kasan/kasan.c:739
[] __tcp_hdrlen include/linux/tcp.h:35 [inline]
[] xt_socket_lookup_slow_v4+0x524/0x718 
net/netfilter/xt_socket.c:178

Fix this by copying data into appropriate size headers based on protocol.

Fixes: a583636a83ea ("inet: refactor inet[6]_lookup functions to take skb")
Signed-off-by: Tejaswi Tanikella 
Signed-off-by: Subash Abhinov Kasiviswanathan 
Signed-off-by: Pablo Neira Ayuso 
Signed-off-by: Greg Kroah-Hartman 

---
 net/ipv4/netfilter/nf_socket_ipv4.c |6 --
 net/ipv6/netfilter/nf_socket_ipv6.c |6 --
 2 files changed, 8 insertions(+), 4 deletions(-)

--- a/net/ipv4/netfilter/nf_socket_ipv4.c
+++ b/net/ipv4/netfilter/nf_socket_ipv4.c
@@ -108,10 +108,12 @@ struct sock *nf_sk_lookup_slow_v4(struct
int doff = 0;
 
if (iph->protocol == IPPROTO_UDP || iph->protocol == IPPROTO_TCP) {
-   struct udphdr _hdr, *hp;
+   struct tcphdr _hdr;
+   struct udphdr *hp;
 
hp = skb_header_pointer(skb, ip_hdrlen(skb),
-   sizeof(_hdr), &_hdr);
+   iph->protocol == IPPROTO_UDP ?
+   sizeof(*hp) : sizeof(_hdr), &_hdr);
if (hp == NULL)
return NULL;
 
--- a/net/ipv6/netfilter/nf_socket_ipv6.c
+++ b/net/ipv6/netfilter/nf_socket_ipv6.c
@@ -116,9 +116,11 @@ struct sock *nf_sk_lookup_slow_v6(struct
}
 
if (tproto == IPPROTO_UDP || tproto == IPPROTO_TCP) {
-   struct udphdr _hdr, *hp;
+   struct tcphdr _hdr;
+   struct udphdr *hp;
 
-   hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
+   hp = skb_header_pointer(skb, thoff, tproto == IPPROTO_UDP ?
+   sizeof(*hp) : sizeof(_hdr), &_hdr);
if (hp == NULL)
return NULL;
 




[PATCH 4.14 24/95] lib/test_bitmap.c: fix bitmap optimisation tests to report errors correctly

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Matthew Wilcox 

commit 1e3054b98c5415d5cb5f8824fc33b548ae5644c3 upstream.

I had neglected to increment the error counter when the tests failed,
which made the tests noisy when they fail, but not actually return an
error code.

Link: http://lkml.kernel.org/r/20180509114328.9887-1-...@ellerman.id.au
Fixes: 3cc78125a081 ("lib/test_bitmap.c: add optimisation tests")
Signed-off-by: Matthew Wilcox 
Signed-off-by: Michael Ellerman 
Reported-by: Michael Ellerman 
Tested-by: Michael Ellerman 
Reviewed-by: Kees Cook 
Cc: Yury Norov 
Cc: Andy Shevchenko 
Cc: Geert Uytterhoeven 
Cc: [4.13+]
Signed-off-by: Andrew Morton 
Signed-off-by: Linus Torvalds 
Signed-off-by: Greg Kroah-Hartman 

---
 lib/test_bitmap.c |   21 +++--
 1 file changed, 15 insertions(+), 6 deletions(-)

--- a/lib/test_bitmap.c
+++ b/lib/test_bitmap.c
@@ -434,23 +434,32 @@ static void noinline __init test_mem_opt
unsigned int start, nbits;
 
for (start = 0; start < 1024; start += 8) {
-   memset(bmap1, 0x5a, sizeof(bmap1));
-   memset(bmap2, 0x5a, sizeof(bmap2));
for (nbits = 0; nbits < 1024 - start; nbits += 8) {
+   memset(bmap1, 0x5a, sizeof(bmap1));
+   memset(bmap2, 0x5a, sizeof(bmap2));
+
bitmap_set(bmap1, start, nbits);
__bitmap_set(bmap2, start, nbits);
-   if (!bitmap_equal(bmap1, bmap2, 1024))
+   if (!bitmap_equal(bmap1, bmap2, 1024)) {
printk("set not equal %d %d\n", start, nbits);
-   if (!__bitmap_equal(bmap1, bmap2, 1024))
+   failed_tests++;
+   }
+   if (!__bitmap_equal(bmap1, bmap2, 1024)) {
printk("set not __equal %d %d\n", start, nbits);
+   failed_tests++;
+   }
 
bitmap_clear(bmap1, start, nbits);
__bitmap_clear(bmap2, start, nbits);
-   if (!bitmap_equal(bmap1, bmap2, 1024))
+   if (!bitmap_equal(bmap1, bmap2, 1024)) {
printk("clear not equal %d %d\n", start, nbits);
-   if (!__bitmap_equal(bmap1, bmap2, 1024))
+   failed_tests++;
+   }
+   if (!__bitmap_equal(bmap1, bmap2, 1024)) {
printk("clear not __equal %d %d\n", start,
nbits);
+   failed_tests++;
+   }
}
}
 }




[PATCH 4.14 16/95] tracing/x86/xen: Remove zero data size trace events trace_xen_mmu_flush_tlb{_all}

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Steven Rostedt (VMware) 

commit 45dd9b0666a162f8e4be76096716670cf1741f0e upstream.

Doing an audit of trace events, I discovered two trace events in the xen
subsystem that use a hack to create zero data size trace events. This is not
what trace events are for. Trace events add memory footprint overhead, and
if all you need to do is see if a function is hit or not, simply make that
function noinline and use function tracer filtering.

Worse yet, the hack used was:

 __array(char, x, 0)

Which creates a static string of zero in length. There's assumptions about
such constructs in ftrace that this is a dynamic string that is nul
terminated. This is not the case with these tracepoints and can cause
problems in various parts of ftrace.

Nuke the trace events!

Link: http://lkml.kernel.org/r/20180509144605.5a220...@gandalf.local.home

Cc: sta...@vger.kernel.org
Fixes: 95a7d76897c1e ("xen/mmu: Use Xen specific TLB flush instead of the 
generic one.")
Reviewed-by: Juergen Gross 
Signed-off-by: Steven Rostedt (VMware) 
Signed-off-by: Greg Kroah-Hartman 

---
 arch/x86/xen/mmu.c |4 +---
 arch/x86/xen/mmu_pv.c  |4 +---
 include/trace/events/xen.h |   16 
 3 files changed, 2 insertions(+), 22 deletions(-)

--- a/arch/x86/xen/mmu.c
+++ b/arch/x86/xen/mmu.c
@@ -42,13 +42,11 @@ xmaddr_t arbitrary_virt_to_machine(void
 }
 EXPORT_SYMBOL_GPL(arbitrary_virt_to_machine);
 
-static void xen_flush_tlb_all(void)
+static noinline void xen_flush_tlb_all(void)
 {
struct mmuext_op *op;
struct multicall_space mcs;
 
-   trace_xen_mmu_flush_tlb_all(0);
-
preempt_disable();
 
mcs = xen_mc_entry(sizeof(*op));
--- a/arch/x86/xen/mmu_pv.c
+++ b/arch/x86/xen/mmu_pv.c
@@ -1280,13 +1280,11 @@ unsigned long xen_read_cr2_direct(void)
return this_cpu_read(xen_vcpu_info.arch.cr2);
 }
 
-static void xen_flush_tlb(void)
+static noinline void xen_flush_tlb(void)
 {
struct mmuext_op *op;
struct multicall_space mcs;
 
-   trace_xen_mmu_flush_tlb(0);
-
preempt_disable();
 
mcs = xen_mc_entry(sizeof(*op));
--- a/include/trace/events/xen.h
+++ b/include/trace/events/xen.h
@@ -349,22 +349,6 @@ DECLARE_EVENT_CLASS(xen_mmu_pgd,
 DEFINE_XEN_MMU_PGD_EVENT(xen_mmu_pgd_pin);
 DEFINE_XEN_MMU_PGD_EVENT(xen_mmu_pgd_unpin);
 
-TRACE_EVENT(xen_mmu_flush_tlb_all,
-   TP_PROTO(int x),
-   TP_ARGS(x),
-   TP_STRUCT__entry(__array(char, x, 0)),
-   TP_fast_assign((void)x),
-   TP_printk("%s", "")
-   );
-
-TRACE_EVENT(xen_mmu_flush_tlb,
-   TP_PROTO(int x),
-   TP_ARGS(x),
-   TP_STRUCT__entry(__array(char, x, 0)),
-   TP_fast_assign((void)x),
-   TP_printk("%s", "")
-   );
-
 TRACE_EVENT(xen_mmu_flush_tlb_one_user,
TP_PROTO(unsigned long addr),
TP_ARGS(addr),




[PATCH 4.14 14/95] powerpc: Dont preempt_disable() in show_cpuinfo()

2018-05-21 Thread Greg Kroah-Hartman
4.14-stable review patch.  If anyone has any objections, please let me know.

--

From: Benjamin Herrenschmidt 

commit 349524bc0da698ec77f2057cf4a4948eb6349265 upstream.

This causes warnings from cpufreq mutex code. This is also rather
unnecessary and ineffective. If we really want to prevent concurrent
unplug, we could take the unplug read lock but I don't see this being
critical.

Fixes: cd77b5ce208c ("powerpc/powernv/cpufreq: Fix the frequency read by 
/proc/cpuinfo")
Signed-off-by: Benjamin Herrenschmidt 
Signed-off-by: Michael Ellerman 
Acked-by: Michal Suchanek 
Signed-off-by: Greg Kroah-Hartman 

---
 arch/powerpc/kernel/setup-common.c |   11 ---
 1 file changed, 11 deletions(-)

--- a/arch/powerpc/kernel/setup-common.c
+++ b/arch/powerpc/kernel/setup-common.c
@@ -242,14 +242,6 @@ static int show_cpuinfo(struct seq_file
unsigned short maj;
unsigned short min;
 
-   /* We only show online cpus: disable preempt (overzealous, I
-* knew) to prevent cpu going down. */
-   preempt_disable();
-   if (!cpu_online(cpu_id)) {
-   preempt_enable();
-   return 0;
-   }
-
 #ifdef CONFIG_SMP
pvr = per_cpu(cpu_pvr, cpu_id);
 #else
@@ -358,9 +350,6 @@ static int show_cpuinfo(struct seq_file
 #ifdef CONFIG_SMP
seq_printf(m, "\n");
 #endif
-
-   preempt_enable();
-
/* If this is the last cpu, print the summary */
if (cpumask_next(cpu_id, cpu_online_mask) >= nr_cpu_ids)
show_cpuinfo_summary(m);




[PATCH 4.9 82/87] x86/bugs: Expose x86_spec_ctrl_base directly

2018-05-21 Thread Greg Kroah-Hartman
4.9-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit fa8ac4988249c38476f6ad678a4848a736373403 upstream

x86_spec_ctrl_base is the system wide default value for the SPEC_CTRL MSR.
x86_spec_ctrl_get_default() returns x86_spec_ctrl_base and was intended to
prevent modification to that variable. Though the variable is read only
after init and globaly visible already.

Remove the function and export the variable instead.

Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Konrad Rzeszutek Wilk 
Signed-off-by: David Woodhouse 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/nospec-branch.h |   16 +---
 arch/x86/include/asm/spec-ctrl.h |3 ---
 arch/x86/kernel/cpu/bugs.c   |   11 +--
 3 files changed, 6 insertions(+), 24 deletions(-)

--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -217,16 +217,7 @@ enum spectre_v2_mitigation {
SPECTRE_V2_IBRS,
 };
 
-/*
- * The Intel specification for the SPEC_CTRL MSR requires that we
- * preserve any already set reserved bits at boot time (e.g. for
- * future additions that this kernel is not currently aware of).
- * We then set any additional mitigation bits that we want
- * ourselves and always use this as the base for SPEC_CTRL.
- * We also use this when handling guest entry/exit as below.
- */
 extern void x86_spec_ctrl_set(u64);
-extern u64 x86_spec_ctrl_get_default(void);
 
 /* The Speculative Store Bypass disable variants */
 enum ssb_mitigation {
@@ -278,6 +269,9 @@ static inline void indirect_branch_predi
alternative_msr_write(MSR_IA32_PRED_CMD, val, X86_FEATURE_USE_IBPB);
 }
 
+/* The Intel SPEC CTRL MSR base value cache */
+extern u64 x86_spec_ctrl_base;
+
 /*
  * With retpoline, we must use IBRS to restrict branch prediction
  * before calling into firmware.
@@ -286,7 +280,7 @@ static inline void indirect_branch_predi
  */
 #define firmware_restrict_branch_speculation_start()   \
 do {   \
-   u64 val = x86_spec_ctrl_get_default() | SPEC_CTRL_IBRS; \
+   u64 val = x86_spec_ctrl_base | SPEC_CTRL_IBRS;  \
\
preempt_disable();  \
alternative_msr_write(MSR_IA32_SPEC_CTRL, val,  \
@@ -295,7 +289,7 @@ do {
\
 
 #define firmware_restrict_branch_speculation_end() \
 do {   \
-   u64 val = x86_spec_ctrl_get_default();  \
+   u64 val = x86_spec_ctrl_base;   \
\
alternative_msr_write(MSR_IA32_SPEC_CTRL, val,  \
  X86_FEATURE_USE_IBRS_FW); \
--- a/arch/x86/include/asm/spec-ctrl.h
+++ b/arch/x86/include/asm/spec-ctrl.h
@@ -47,9 +47,6 @@ void x86_spec_ctrl_restore_host(u64 gues
 extern u64 x86_amd_ls_cfg_base;
 extern u64 x86_amd_ls_cfg_ssbd_mask;
 
-/* The Intel SPEC CTRL MSR base value cache */
-extern u64 x86_spec_ctrl_base;
-
 static inline u64 ssbd_tif_to_spec_ctrl(u64 tifn)
 {
BUILD_BUG_ON(TIF_SSBD < SPEC_CTRL_SSBD_SHIFT);
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -35,6 +35,7 @@ static void __init ssb_select_mitigation
  * writes to SPEC_CTRL contain whatever reserved bits have been set.
  */
 u64 __ro_after_init x86_spec_ctrl_base;
+EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
 
 /*
  * The vendor and possibly platform specific bits which can be modified in
@@ -140,16 +141,6 @@ void x86_spec_ctrl_set(u64 val)
 }
 EXPORT_SYMBOL_GPL(x86_spec_ctrl_set);
 
-u64 x86_spec_ctrl_get_default(void)
-{
-   u64 msrval = x86_spec_ctrl_base;
-
-   if (static_cpu_has(X86_FEATURE_SPEC_CTRL))
-   msrval |= ssbd_tif_to_spec_ctrl(current_thread_info()->flags);
-   return msrval;
-}
-EXPORT_SYMBOL_GPL(x86_spec_ctrl_get_default);
-
 void
 x86_virt_spec_ctrl(u64 guest_spec_ctrl, u64 guest_virt_spec_ctrl, bool 
setguest)
 {




[PATCH 4.9 85/87] x86/speculation, KVM: Implement support for VIRT_SPEC_CTRL/LS_CFG

2018-05-21 Thread Greg Kroah-Hartman
4.9-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit 47c61b3955cf712cadfc25635bf9bc174af030ea upstream

Add the necessary logic for supporting the emulated VIRT_SPEC_CTRL MSR to
x86_virt_spec_ctrl().  If either X86_FEATURE_LS_CFG_SSBD or
X86_FEATURE_VIRT_SPEC_CTRL is set then use the new guest_virt_spec_ctrl
argument to check whether the state must be modified on the host. The
update reuses speculative_store_bypass_update() so the ZEN-specific sibling
coordination can be reused.

Signed-off-by: Thomas Gleixner 
Signed-off-by: David Woodhouse 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/spec-ctrl.h |6 ++
 arch/x86/kernel/cpu/bugs.c   |   30 ++
 2 files changed, 36 insertions(+)

--- a/arch/x86/include/asm/spec-ctrl.h
+++ b/arch/x86/include/asm/spec-ctrl.h
@@ -53,6 +53,12 @@ static inline u64 ssbd_tif_to_spec_ctrl(
return (tifn & _TIF_SSBD) >> (TIF_SSBD - SPEC_CTRL_SSBD_SHIFT);
 }
 
+static inline unsigned long ssbd_spec_ctrl_to_tif(u64 spec_ctrl)
+{
+   BUILD_BUG_ON(TIF_SSBD < SPEC_CTRL_SSBD_SHIFT);
+   return (spec_ctrl & SPEC_CTRL_SSBD) << (TIF_SSBD - 
SPEC_CTRL_SSBD_SHIFT);
+}
+
 static inline u64 ssbd_tif_to_amd_ls_cfg(u64 tifn)
 {
return (tifn & _TIF_SSBD) ? x86_amd_ls_cfg_ssbd_mask : 0ULL;
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -161,6 +161,36 @@ x86_virt_spec_ctrl(u64 guest_spec_ctrl,
wrmsrl(MSR_IA32_SPEC_CTRL, msrval);
}
}
+
+   /*
+* If SSBD is not handled in MSR_SPEC_CTRL on AMD, update
+* MSR_AMD64_L2_CFG or MSR_VIRT_SPEC_CTRL if supported.
+*/
+   if (!static_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
+   !static_cpu_has(X86_FEATURE_VIRT_SSBD))
+   return;
+
+   /*
+* If the host has SSBD mitigation enabled, force it in the host's
+* virtual MSR value. If its not permanently enabled, evaluate
+* current's TIF_SSBD thread flag.
+*/
+   if (static_cpu_has(X86_FEATURE_SPEC_STORE_BYPASS_DISABLE))
+   hostval = SPEC_CTRL_SSBD;
+   else
+   hostval = ssbd_tif_to_spec_ctrl(ti->flags);
+
+   /* Sanitize the guest value */
+   guestval = guest_virt_spec_ctrl & SPEC_CTRL_SSBD;
+
+   if (hostval != guestval) {
+   unsigned long tif;
+
+   tif = setguest ? ssbd_spec_ctrl_to_tif(guestval) :
+ssbd_spec_ctrl_to_tif(hostval);
+
+   speculative_store_bypass_update(tif);
+   }
 }
 EXPORT_SYMBOL_GPL(x86_virt_spec_ctrl);
 




[PATCH 4.9 77/87] x86/speculation: Handle HT correctly on AMD

2018-05-21 Thread Greg Kroah-Hartman
4.9-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit 1f50ddb4f4189243c05926b842dc1a0332195f31 upstream

The AMD64_LS_CFG MSR is a per core MSR on Family 17H CPUs. That means when
hyperthreading is enabled the SSBD bit toggle needs to take both cores into
account. Otherwise the following situation can happen:

CPU0CPU1

disable SSB
disable SSB
enable  SSB <- Enables it for the Core, i.e. for CPU0 as well

So after the SSB enable on CPU1 the task on CPU0 runs with SSB enabled
again.

On Intel the SSBD control is per core as well, but the synchronization
logic is implemented behind the per thread SPEC_CTRL MSR. It works like
this:

  CORE_SPEC_CTRL = THREAD0_SPEC_CTRL | THREAD1_SPEC_CTRL

i.e. if one of the threads enables a mitigation then this affects both and
the mitigation is only disabled in the core when both threads disabled it.

Add the necessary synchronization logic for AMD family 17H. Unfortunately
that requires a spinlock to serialize the access to the MSR, but the locks
are only shared between siblings.

Signed-off-by: Thomas Gleixner 
Reviewed-by: Borislav Petkov 
Reviewed-by: Konrad Rzeszutek Wilk 
Signed-off-by: David Woodhouse 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/spec-ctrl.h |6 +
 arch/x86/kernel/process.c|  125 +--
 arch/x86/kernel/smpboot.c|5 +
 3 files changed, 130 insertions(+), 6 deletions(-)

--- a/arch/x86/include/asm/spec-ctrl.h
+++ b/arch/x86/include/asm/spec-ctrl.h
@@ -33,6 +33,12 @@ static inline u64 ssbd_tif_to_amd_ls_cfg
return (tifn & _TIF_SSBD) ? x86_amd_ls_cfg_ssbd_mask : 0ULL;
 }
 
+#ifdef CONFIG_SMP
+extern void speculative_store_bypass_ht_init(void);
+#else
+static inline void speculative_store_bypass_ht_init(void) { }
+#endif
+
 extern void speculative_store_bypass_update(void);
 
 #endif
--- a/arch/x86/kernel/process.c
+++ b/arch/x86/kernel/process.c
@@ -203,22 +203,135 @@ static inline void switch_to_bitmap(stru
}
 }
 
-static __always_inline void __speculative_store_bypass_update(unsigned long 
tifn)
+#ifdef CONFIG_SMP
+
+struct ssb_state {
+   struct ssb_state*shared_state;
+   raw_spinlock_t  lock;
+   unsigned intdisable_state;
+   unsigned long   local_state;
+};
+
+#define LSTATE_SSB 0
+
+static DEFINE_PER_CPU(struct ssb_state, ssb_state);
+
+void speculative_store_bypass_ht_init(void)
+{
+   struct ssb_state *st = this_cpu_ptr(&ssb_state);
+   unsigned int this_cpu = smp_processor_id();
+   unsigned int cpu;
+
+   st->local_state = 0;
+
+   /*
+* Shared state setup happens once on the first bringup
+* of the CPU. It's not destroyed on CPU hotunplug.
+*/
+   if (st->shared_state)
+   return;
+
+   raw_spin_lock_init(&st->lock);
+
+   /*
+* Go over HT siblings and check whether one of them has set up the
+* shared state pointer already.
+*/
+   for_each_cpu(cpu, topology_sibling_cpumask(this_cpu)) {
+   if (cpu == this_cpu)
+   continue;
+
+   if (!per_cpu(ssb_state, cpu).shared_state)
+   continue;
+
+   /* Link it to the state of the sibling: */
+   st->shared_state = per_cpu(ssb_state, cpu).shared_state;
+   return;
+   }
+
+   /*
+* First HT sibling to come up on the core.  Link shared state of
+* the first HT sibling to itself. The siblings on the same core
+* which come up later will see the shared state pointer and link
+* themself to the state of this CPU.
+*/
+   st->shared_state = st;
+}
+
+/*
+ * Logic is: First HT sibling enables SSBD for both siblings in the core
+ * and last sibling to disable it, disables it for the whole core. This how
+ * MSR_SPEC_CTRL works in "hardware":
+ *
+ *  CORE_SPEC_CTRL = THREAD0_SPEC_CTRL | THREAD1_SPEC_CTRL
+ */
+static __always_inline void amd_set_core_ssb_state(unsigned long tifn)
 {
-   u64 msr;
+   struct ssb_state *st = this_cpu_ptr(&ssb_state);
+   u64 msr = x86_amd_ls_cfg_base;
 
-   if (static_cpu_has(X86_FEATURE_LS_CFG_SSBD)) {
-   msr = x86_amd_ls_cfg_base | ssbd_tif_to_amd_ls_cfg(tifn);
+   if (!static_cpu_has(X86_FEATURE_ZEN)) {
+   msr |= ssbd_tif_to_amd_ls_cfg(tifn);
wrmsrl(MSR_AMD64_LS_CFG, msr);
+   return;
+   }
+
+   if (tifn & _TIF_SSBD) {
+   /*
+* Since this can race with prctl(), block reentry on the
+* same CPU.
+*/
+   if (__test_and_set_bit(LSTATE_SSB, &st->local_state))
+   return;
+
+   msr |= x86_amd_ls_cfg_ssbd_mask;
+
+   raw_spin_lock(&st->shared_state->lock);
+   /* First

[PATCH 4.9 54/87] x86/speculation: Add prctl for Speculative Store Bypass mitigation

2018-05-21 Thread Greg Kroah-Hartman
4.9-stable review patch.  If anyone has any objections, please let me know.

--

From: Thomas Gleixner 

commit a73ec77ee17ec556fe7f165d00314cb7c047b1ac upstream

Add prctl based control for Speculative Store Bypass mitigation and make it
the default mitigation for Intel and AMD.

Andi Kleen provided the following rationale (slightly redacted):

 There are multiple levels of impact of Speculative Store Bypass:

 1) JITed sandbox.
It cannot invoke system calls, but can do PRIME+PROBE and may have call
interfaces to other code

 2) Native code process.
No protection inside the process at this level.

 3) Kernel.

 4) Between processes.

 The prctl tries to protect against case (1) doing attacks.

 If the untrusted code can do random system calls then control is already
 lost in a much worse way. So there needs to be system call protection in
 some way (using a JIT not allowing them or seccomp). Or rather if the
 process can subvert its environment somehow to do the prctl it can already
 execute arbitrary code, which is much worse than SSB.

 To put it differently, the point of the prctl is to not allow JITed code
 to read data it shouldn't read from its JITed sandbox. If it already has
 escaped its sandbox then it can already read everything it wants in its
 address space, and do much worse.

 The ability to control Speculative Store Bypass allows to enable the
 protection selectively without affecting overall system performance.

Based on an initial patch from Tim Chen. Completely rewritten.

Signed-off-by: Thomas Gleixner 
Reviewed-by: Konrad Rzeszutek Wilk 

Signed-off-by: David Woodhouse 
Signed-off-by: Greg Kroah-Hartman 
---
 Documentation/kernel-parameters.txt  |6 ++
 arch/x86/include/asm/nospec-branch.h |1 
 arch/x86/kernel/cpu/bugs.c   |   83 ++-
 3 files changed, 79 insertions(+), 11 deletions(-)

--- a/Documentation/kernel-parameters.txt
+++ b/Documentation/kernel-parameters.txt
@@ -4001,7 +4001,11 @@ bytes respectively. Such letter suffixes
off- Unconditionally enable Speculative Store Bypass
auto   - Kernel detects whether the CPU model contains 
an
 implementation of Speculative Store Bypass and
-picks the most appropriate mitigation
+picks the most appropriate mitigation.
+   prctl  - Control Speculative Store Bypass per thread
+via prctl. Speculative Store Bypass is enabled
+for a process by default. The state of the 
control
+is inherited on fork.
 
Not specifying this option is equivalent to
spec_store_bypass_disable=auto.
--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -232,6 +232,7 @@ extern u64 x86_spec_ctrl_get_default(voi
 enum ssb_mitigation {
SPEC_STORE_BYPASS_NONE,
SPEC_STORE_BYPASS_DISABLE,
+   SPEC_STORE_BYPASS_PRCTL,
 };
 
 extern char __indirect_thunk_start[];
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -11,6 +11,8 @@
 #include 
 #include 
 #include 
+#include 
+#include 
 
 #include 
 #include 
@@ -411,20 +413,23 @@ enum ssb_mitigation_cmd {
SPEC_STORE_BYPASS_CMD_NONE,
SPEC_STORE_BYPASS_CMD_AUTO,
SPEC_STORE_BYPASS_CMD_ON,
+   SPEC_STORE_BYPASS_CMD_PRCTL,
 };
 
 static const char *ssb_strings[] = {
[SPEC_STORE_BYPASS_NONE]= "Vulnerable",
-   [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass 
disabled"
+   [SPEC_STORE_BYPASS_DISABLE] = "Mitigation: Speculative Store Bypass 
disabled",
+   [SPEC_STORE_BYPASS_PRCTL]   = "Mitigation: Speculative Store Bypass 
disabled via prctl"
 };
 
 static const struct {
const char *option;
enum ssb_mitigation_cmd cmd;
 } ssb_mitigation_options[] = {
-   { "auto",   SPEC_STORE_BYPASS_CMD_AUTO }, /* Platform decides */
-   { "on", SPEC_STORE_BYPASS_CMD_ON },   /* Disable Speculative 
Store Bypass */
-   { "off",SPEC_STORE_BYPASS_CMD_NONE }, /* Don't touch 
Speculative Store Bypass */
+   { "auto",   SPEC_STORE_BYPASS_CMD_AUTO },  /* Platform decides */
+   { "on", SPEC_STORE_BYPASS_CMD_ON },/* Disable Speculative 
Store Bypass */
+   { "off",SPEC_STORE_BYPASS_CMD_NONE },  /* Don't touch 
Speculative Store Bypass */
+   { "prctl",  SPEC_STORE_BYPASS_CMD_PRCTL }, /* Disable Speculative 
Store Bypass via prctl */
 };
 
 static enum ssb_mitigation_cmd __init ssb_parse_cmdline(void)
@@ -474,14 +479,15 @@ static enum ssb_mitigation_cmd __init __
 
switch (cmd) {
case SPEC_STORE_BYPASS_CMD_AUTO:
-   /*
-* AMD platforms by default don't need SSB mitigation.
-  

[PATCH 4.9 75/87] x86/cpu/AMD: Fix erratum 1076 (CPB bit)

2018-05-21 Thread Greg Kroah-Hartman
4.9-stable review patch.  If anyone has any objections, please let me know.

--

From: Borislav Petkov 

commit f7f3dc00f61261cdc9ccd8b886f21bc4dffd6fd9 upstream

CPUID Fn8000_0007_EDX[CPB] is wrongly 0 on models up to B1. But they do
support CPB (AMD's Core Performance Boosting cpufreq CPU feature), so fix that.

Signed-off-by: Borislav Petkov 
Cc: Linus Torvalds 
Cc: Peter Zijlstra 
Cc: Sherry Hurwitz 
Cc: Thomas Gleixner 
Link: http://lkml.kernel.org/r/20170907170821.16021-1...@alien8.de
Signed-off-by: Ingo Molnar 
Signed-off-by: David Woodhouse 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/kernel/cpu/amd.c |   11 +++
 1 file changed, 11 insertions(+)

--- a/arch/x86/kernel/cpu/amd.c
+++ b/arch/x86/kernel/cpu/amd.c
@@ -749,6 +749,16 @@ static void init_amd_bd(struct cpuinfo_x
}
 }
 
+static void init_amd_zn(struct cpuinfo_x86 *c)
+{
+   /*
+* Fix erratum 1076: CPB feature bit not being set in CPUID. It affects
+* all up to and including B1.
+*/
+   if (c->x86_model <= 1 && c->x86_stepping <= 1)
+   set_cpu_cap(c, X86_FEATURE_CPB);
+}
+
 static void init_amd(struct cpuinfo_x86 *c)
 {
u32 dummy;
@@ -779,6 +789,7 @@ static void init_amd(struct cpuinfo_x86
case 0x10: init_amd_gh(c); break;
case 0x12: init_amd_ln(c); break;
case 0x15: init_amd_bd(c); break;
+   case 0x17: init_amd_zn(c); break;
}
 
/* Enable workaround for FXSAVE leak */




[PATCH 4.9 64/87] x86/bugs: Rename _RDS to _SSBD

2018-05-21 Thread Greg Kroah-Hartman
4.9-stable review patch.  If anyone has any objections, please let me know.

--

From: Konrad Rzeszutek Wilk 

commit 9f65fb29374ee37856dbad847b4e121aab72b510 upstream

Intel collateral will reference the SSB mitigation bit in IA32_SPEC_CTL[2]
as SSBD (Speculative Store Bypass Disable).

Hence changing it.

It is unclear yet what the MSR_IA32_ARCH_CAPABILITIES (0x10a) Bit(4) name
is going to be. Following the rename it would be SSBD_NO but that rolls out
to Speculative Store Bypass Disable No.

Also fixed the missing space in X86_FEATURE_AMD_SSBD.

[ tglx: Fixup x86_amd_rds_enable() and rds_tif_to_amd_ls_cfg() as well ]

Signed-off-by: Konrad Rzeszutek Wilk 
Signed-off-by: Thomas Gleixner 

Signed-off-by: David Woodhouse 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/include/asm/cpufeatures.h |4 ++--
 arch/x86/include/asm/msr-index.h   |   10 +-
 arch/x86/include/asm/spec-ctrl.h   |   12 ++--
 arch/x86/include/asm/thread_info.h |6 +++---
 arch/x86/kernel/cpu/amd.c  |   14 +++---
 arch/x86/kernel/cpu/bugs.c |   36 ++--
 arch/x86/kernel/cpu/common.c   |2 +-
 arch/x86/kernel/cpu/intel.c|2 +-
 arch/x86/kernel/process.c  |8 
 arch/x86/kvm/cpuid.c   |2 +-
 arch/x86/kvm/cpuid.h   |2 +-
 arch/x86/kvm/vmx.c |2 +-
 12 files changed, 50 insertions(+), 50 deletions(-)

--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -205,7 +205,7 @@
 #define X86_FEATURE_USE_IBPB   ( 7*32+21) /* "" Indirect Branch Prediction 
Barrier enabled */
 #define X86_FEATURE_USE_IBRS_FW( 7*32+22) /* "" Use IBRS during 
runtime firmware calls */
 #define X86_FEATURE_SPEC_STORE_BYPASS_DISABLE ( 7*32+23) /* "" Disable 
Speculative Store Bypass. */
-#define X86_FEATURE_AMD_RDS(7*32+24)  /* "" AMD RDS implementation */
+#define X86_FEATURE_AMD_SSBD   (7*32+24)  /* "" AMD SSBD implementation */
 
 /* Virtualization flags: Linux defined, word 8 */
 #define X86_FEATURE_TPR_SHADOW  ( 8*32+ 0) /* Intel TPR Shadow */
@@ -308,7 +308,7 @@
 #define X86_FEATURE_SPEC_CTRL  (18*32+26) /* "" Speculation Control 
(IBRS + IBPB) */
 #define X86_FEATURE_INTEL_STIBP(18*32+27) /* "" Single Thread 
Indirect Branch Predictors */
 #define X86_FEATURE_ARCH_CAPABILITIES  (18*32+29) /* IA32_ARCH_CAPABILITIES 
MSR (Intel) */
-#define X86_FEATURE_RDS(18*32+31) /* Reduced Data 
Speculation */
+#define X86_FEATURE_SSBD   (18*32+31) /* Speculative Store Bypass 
Disable */
 
 /*
  * BUG word(s)
--- a/arch/x86/include/asm/msr-index.h
+++ b/arch/x86/include/asm/msr-index.h
@@ -40,8 +40,8 @@
 #define MSR_IA32_SPEC_CTRL 0x0048 /* Speculation Control */
 #define SPEC_CTRL_IBRS (1 << 0)   /* Indirect Branch 
Restricted Speculation */
 #define SPEC_CTRL_STIBP(1 << 1)   /* Single Thread 
Indirect Branch Predictors */
-#define SPEC_CTRL_RDS_SHIFT2  /* Reduced Data Speculation 
bit */
-#define SPEC_CTRL_RDS  (1 << SPEC_CTRL_RDS_SHIFT)   /* Reduced 
Data Speculation */
+#define SPEC_CTRL_SSBD_SHIFT   2  /* Speculative Store Bypass 
Disable bit */
+#define SPEC_CTRL_SSBD (1 << SPEC_CTRL_SSBD_SHIFT)   /* 
Speculative Store Bypass Disable */
 
 #define MSR_IA32_PRED_CMD  0x0049 /* Prediction Command */
 #define PRED_CMD_IBPB  (1 << 0)   /* Indirect Branch 
Prediction Barrier */
@@ -63,10 +63,10 @@
 #define MSR_IA32_ARCH_CAPABILITIES 0x010a
 #define ARCH_CAP_RDCL_NO   (1 << 0)   /* Not susceptible to 
Meltdown */
 #define ARCH_CAP_IBRS_ALL  (1 << 1)   /* Enhanced IBRS support */
-#define ARCH_CAP_RDS_NO(1 << 4)   /*
+#define ARCH_CAP_SSBD_NO   (1 << 4)   /*
* Not susceptible to 
Speculative Store Bypass
-   * attack, so no Reduced 
Data Speculation control
-   * required.
+   * attack, so no Speculative 
Store Bypass
+   * control required.
*/
 
 #define MSR_IA32_BBL_CR_CTL0x0119
--- a/arch/x86/include/asm/spec-ctrl.h
+++ b/arch/x86/include/asm/spec-ctrl.h
@@ -17,20 +17,20 @@ extern void x86_spec_ctrl_restore_host(u
 
 /* AMD specific Speculative Store Bypass MSR data */
 extern u64 x86_amd_ls_cfg_base;
-extern u64 x86_amd_ls_cfg_rds_mask;
+extern u64 x86_amd_ls_cfg_ssbd_mask;
 
 /* The Intel SPEC CTRL MSR base value cache */
 extern u64 x86_spec_ctrl_base;
 
-static inline u64 rds_tif_to_spec_ctrl(u64 tifn)
+static inline u64 ssbd_tif_to_spec_ctrl(u64 tifn)
 {
-   BUILD_

[PATCH 4.9 69/87] x86/bugs: Fix the parameters alignment and missing void

2018-05-21 Thread Greg Kroah-Hartman
4.9-stable review patch.  If anyone has any objections, please let me know.

--

From: Konrad Rzeszutek Wilk 

commit ffed645e3be0e32f8e9ab068d257aee8d0fe8eec upstream

Fixes: 7bb4d366c ("x86/bugs: Make cpu_show_common() static")
Fixes: 24f7fc83b ("x86/bugs: Provide boot parameters for the 
spec_store_bypass_disable mitigation")
Signed-off-by: Konrad Rzeszutek Wilk 
Signed-off-by: Thomas Gleixner 
Signed-off-by: David Woodhouse 
Signed-off-by: Greg Kroah-Hartman 
---
 arch/x86/kernel/cpu/bugs.c |4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -530,7 +530,7 @@ static enum ssb_mitigation __init __ssb_
return mode;
 }
 
-static void ssb_select_mitigation()
+static void ssb_select_mitigation(void)
 {
ssb_mode = __ssb_select_mitigation();
 
@@ -640,7 +640,7 @@ void x86_spec_ctrl_setup_ap(void)
 #ifdef CONFIG_SYSFS
 
 static ssize_t cpu_show_common(struct device *dev, struct device_attribute 
*attr,
-   char *buf, unsigned int bug)
+  char *buf, unsigned int bug)
 {
if (!boot_cpu_has_bug(bug))
return sprintf(buf, "Not affected\n");




<    1   2   3   4   5   6   7   8   9   10   >