From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Google-Smtp-Source: AH8x226Wlzn3ryVbtjbGv/bH9OlHIqYr+L4m1fDzVmF1xNNfXTE3N/hOjggYR1V3lI6Kxq7B2t7l ARC-Seal: i=1; a=rsa-sha256; t=1516476240; cv=none; d=google.com; s=arc-20160816; b=N37/Yg5tW+XeG1lX1XaxloNsO9O1biQ/v+yNTQB9gwsPC07SKmUV1fMtqQRoG0sHm4 hH3pNIYa0YaLXUD+dwNPMUc6idCE5fcaTrMQcTiIG445DH35cFVtG4vZzxiTnefkHBX6 G+idTtfQpfvJy/NJSrA1TfSaN81ymauLugnndkR5WpP0iMx/vd4chHs+nwe1ZlkIJMVO e7Vqk4ukT7TUaUoA5U9TTnVIt+JEK2Vyfl1XxD47e1aqg6YQfsWDDLACLlMPQkNMzZsU 5zIq2Hq7xMdunmycnEKR+/rFE707OPonMFbSsa1frHcO2BaggxqH9BYeMd1ODhsw26ZZ yLhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=AVEkq7KheIPviK2RPlqK/sovkN5xp5l9sCOdscAajnw=; b=DM+Fuy2agrdKyryH57H1wgYFv43Y9QnQRrJzj4S3k0M3zGuXGDy3KRoBKdwBMxRY3A AR5tD5MKr+4oj+h9t+aOhqxPokMV+tdxULesKHEJzaruM+ZH8rPqnGJBfbtBNgD3pUTa HzO8lVszPwVw6Cz9Oc7Y05x5EPMMq5vgrJevTMftbjdPFIlPd5w8eDUnLbUuWNZR1p/t G3AK/Lk/Pf92fz7tnSelCueuhiSY8iohoramHnS4UffA6KF8DkplO1Vqb1mPf78vlnX0 UFBXX48W7sz1Zo/wNdJynrXtTEASnMruIzgWG3wIzHztiFPGgpx1Az4hZ+qnrOWEfLNx sf7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.de header.s=amazon201209 header.b=ts9UuzAZ; spf=pass (google.com: domain of prvs=551b82ed1=karahmed@amazon.com designates 207.171.190.10 as permitted sender) smtp.mailfrom=prvs=551b82ed1=karahmed@amazon.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.de Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.de header.s=amazon201209 header.b=ts9UuzAZ; spf=pass (google.com: domain of prvs=551b82ed1=karahmed@amazon.com designates 207.171.190.10 as permitted sender) smtp.mailfrom=prvs=551b82ed1=karahmed@amazon.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.de X-IronPort-AV: E=Sophos;i="5.46,387,1511827200"; d="scan'208";a="716423200" From: KarimAllah Ahmed To: linux-kernel@vger.kernel.org Cc: KarimAllah Ahmed , Andi Kleen , Andrea Arcangeli , Andy Lutomirski , Arjan van de Ven , Ashok Raj , Asit Mallick , Borislav Petkov , Dan Williams , Dave Hansen , David Woodhouse , Greg Kroah-Hartman , "H . Peter Anvin" , Ingo Molnar , Janakarajan Natarajan , Joerg Roedel , Jun Nakajima , Laura Abbott , Linus Torvalds , Masami Hiramatsu , Paolo Bonzini , Peter Zijlstra , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Thomas Gleixner , Tim Chen , Tom Lendacky , kvm@vger.kernel.org, x86@kernel.org Subject: [RFC 08/10] x86/idle: Control Indirect Branch Speculation in idle Date: Sat, 20 Jan 2018 20:22:59 +0100 Message-Id: <1516476182-5153-9-git-send-email-karahmed@amazon.de> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1516476182-5153-1-git-send-email-karahmed@amazon.de> References: <1516476182-5153-1-git-send-email-karahmed@amazon.de> X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: =?utf-8?q?1590140589859243942?= X-GMAIL-MSGID: =?utf-8?q?1590140589859243942?= X-Mailing-List: linux-kernel@vger.kernel.org List-ID: From: Thomas Gleixner Indirect Branch Speculation (IBS) is controlled per physical core. If one thread disables it then it's disabled for the core. If a thread enters idle it makes sense to reenable IBS so the sibling thread can run with full speculation enabled in user space. This makes only sense in mwait_idle_with_hints() because mwait_idle() can serve an interrupt immediately before speculation can be stopped again. SKL which requires IBRS should use mwait_idle_with_hints() so this is a non issue and in the worst case a missed optimization. Originally-by: Tim Chen Signed-off-by: Thomas Gleixner --- arch/x86/include/asm/mwait.h | 14 ++++++++++++++ arch/x86/kernel/process.c | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/arch/x86/include/asm/mwait.h b/arch/x86/include/asm/mwait.h index 39a2fb2..f173072 100644 --- a/arch/x86/include/asm/mwait.h +++ b/arch/x86/include/asm/mwait.h @@ -6,6 +6,7 @@ #include #include +#include #define MWAIT_SUBSTATE_MASK 0xf #define MWAIT_CSTATE_MASK 0xf @@ -106,7 +107,20 @@ static inline void mwait_idle_with_hints(unsigned long eax, unsigned long ecx) mb(); } + /* + * Indirect Branch Speculation (IBS) is controlled per + * physical core. If one thread disables it, then it's + * disabled on all threads of the core. The kernel disables + * it on entry from user space. Reenable it on the thread + * which goes idle so the other thread has a chance to run + * with full speculation enabled in userspace. + */ + unrestrict_branch_speculation(); __monitor((void *)¤t_thread_info()->flags, 0, 0); + /* + * Restrict IBS again to protect kernel execution. + */ + restrict_branch_speculation(); if (!need_resched()) __mwait(eax, ecx); } diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c index 3cb2486..f941c5d 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -461,6 +461,20 @@ static __cpuidle void mwait_idle(void) mb(); /* quirk */ } + /* + * Indirect Branch Speculation (IBS) is controlled per + * physical core. If one thread disables it, then it's + * disabled on all threads of the core. The kernel disables + * it on entry from user space. For __sti_mwait() it's + * wrong to reenable it because an interrupt can be served + * before speculation can be stopped again. + * + * To plug that hole the interrupt entry code would need to + * save current state and restore. Not worth the trouble as + * SKL should not use mwait_idle(). It should use + * mwait_idle_with_hints() which can do speculation control + * safely. + */ __monitor((void *)¤t_thread_info()->flags, 0, 0); if (!need_resched()) __sti_mwait(0, 0); -- 2.7.4