Received: by 10.223.176.5 with SMTP id f5csp2887061wra; Mon, 5 Feb 2018 11:37:23 -0800 (PST) X-Google-Smtp-Source: AH8x226Yf4Lisb3+IcIhb3BbxtkzwA9ur+ATMVPn/nMZCPTq26rsK6QSWV2mXbfUolE759Sf1t67 X-Received: by 2002:a17:902:42a5:: with SMTP id h34-v6mr43362182pld.265.1517859443506; Mon, 05 Feb 2018 11:37:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517859443; cv=none; d=google.com; s=arc-20160816; b=dY3s6XA3Xwf+VEZFt4WmSx4Q08DJ/8VGHchoKK0kUx7GN0yEyh1K50yufhrUY6ljQw YOZUCtazVtXXgLBDhb3kbWCIijG5I7pJBf35kiAXhXMR74OHbOCqB1XSvvbT5sPJEySa w6AS1x4UOWs2SbAOnIt7cv9kRHSUCAArJ0r4KywQ5ZXIylJSv3slwiv/Sxmb7H7U9WEv m2egB55K8FF1eHbgxk/OSbGdGd9UHXGoXROKB/48+Uw8nl5DxUVOq9gusnkn5uyASAcB HRtYTnVavxyW+oJxn8/zudMFNMa33hcZrBWNNRZ9Uwy1xbN8m4R1JTg0cgqTrINuIsac Bf4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:to:subject:arc-authentication-results; bh=9ZPTtGUhV2nSF+c5XTKcaHKwRinNga1tmJrDF2ibV+I=; b=J/7pBp0q0SdMv2IsfXc3OFKhWXko3NuH+uuF8OwwGF427iFgMQytyH+beqmhRqDSg6 sWUfixJvcRJsVibcOodNpVQWwTsF4KWATr7MC7sfVYPQhHwi7w3n064XRVe5NBK/RvDD 6Fh5FmzL9y9a6b/NeX4sAnLx6DQflbl2E26CBZN9ckIOpdl0ep/Ssa/Yyp7srpgJVoRM rujrR3o3d/OGnlTvnDwah795/XFS3cJne5ydneNPCyXM3PGlQlMIKqeTHcAXldKVmBt5 zw6we9p7z1GJ/i40acUPrPAZZ0rDmJa109L+pIuk/XuT4+uYmnO2uccM30BwkwwHu4zi zZfg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w19-v6si7131296plq.735.2018.02.05.11.37.08; Mon, 05 Feb 2018 11:37:23 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751589AbeBETfM (ORCPT + 99 others); Mon, 5 Feb 2018 14:35:12 -0500 Received: from mga17.intel.com ([192.55.52.151]:37256 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750827AbeBETfG (ORCPT ); Mon, 5 Feb 2018 14:35:06 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga107.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 05 Feb 2018 11:35:06 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,465,1511856000"; d="scan'208";a="25018920" Received: from schen9-desk3.jf.intel.com (HELO [10.54.74.42]) ([10.54.74.42]) by FMSMGA003.fm.intel.com with ESMTP; 05 Feb 2018 11:35:06 -0800 Subject: Re: [tip:x86/pti] x86/speculation: Use Indirect Branch Prediction Barrier in context switch To: David Woodhouse , mingo@kernel.org, hpa@zytor.com, linux-kernel@vger.kernel.org, tglx@linutronix.de, luto@kernel.org, Greg Kroah-Hartman , linux-mm References: <1517263487-3708-1-git-send-email-dwmw@amazon.co.uk> <1517840309.31953.153.camel@infradead.org> From: Tim Chen Message-ID: Date: Mon, 5 Feb 2018 11:35:05 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.1.0 MIME-Version: 1.0 In-Reply-To: <1517840309.31953.153.camel@infradead.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 02/05/2018 06:18 AM, David Woodhouse wrote: > On Tue, 2018-01-30 at 14:39 -0800, tip-bot for Tim Chen wrote: >> Thanks to the reviewers and Andy Lutomirski for the suggestion of >> using ctx_id which got rid of the problem of mm pointer recycling. > > That one doesn't backport well to 4.9. Suggestions welcome. > Will something like the following work for 4.9 using active_mm? This patch is not really tested, but just want to put it out here to see if this is a reasonable backport. Tim Signed-off-by: Tim Chen --- diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index a7655f6..4994db2 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -75,6 +76,9 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, struct task_struct *tsk) { unsigned cpu = smp_processor_id(); +#ifdef CONFIG_SMP + struct mm_struct *active_mm = this_cpu_read(cpu_tlbstate.active_mm); +#endif if (likely(prev != next)) { if (IS_ENABLED(CONFIG_VMAP_STACK)) { @@ -91,6 +95,28 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, set_pgd(pgd, init_mm.pgd[stack_pgd_index]); } + /* + * Avoid user/user BTB poisoning by flushing the branch + * predictor when switching between processes. This stops + * one process from doing Spectre-v2 attacks on another. + * + * As an optimization, flush indirect branches only when + * switching into processes that disable dumping. This + * protects high value processes like gpg, without having + * too high performance overhead. IBPB is *expensive*! + * + * This will not flush branches when switching into kernel + * threads. It will also not flush if we switch to idle + * thread and back to the same process. It will flush if we + * switch to a different non-dumpable process. + */ + if (tsk && tsk->mm && +#ifdef CONFIG_SMP + next != active_mm && +#endif + get_dumpable(tsk->mm) != SUID_DUMP_USER) + indirect_branch_prediction_barrier(); + #ifdef CONFIG_SMP this_cpu_write(cpu_tlbstate.state, TLBSTATE_OK); this_cpu_write(cpu_tlbstate.active_mm, next);