Received: by 10.223.185.116 with SMTP id b49csp424897wrg; Wed, 14 Feb 2018 00:59:19 -0800 (PST) X-Google-Smtp-Source: AH8x224UDQP2et2wlzaozbBqfizm7Fy7eQ+j0OS9UgriwQGVpDryh1YIzauN3jMPs/RiYAG6AuFu X-Received: by 10.99.125.74 with SMTP id m10mr3424068pgn.354.1518598759092; Wed, 14 Feb 2018 00:59:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518598759; cv=none; d=google.com; s=arc-20160816; b=SxN4I/LfyO91VdEzr9qA7X83+F4zakQJTBbGWDypQndSvq16iGKTzsEkJAhe5xjPgD sS1U6ooDL+vQNFiMzhOiznaaU9eq316/3BduJ+sVmVtcPTKuskd1Q7YB9zmf6nwjf88v Y30yoww6OzQMSD5R8foOOtxhMkpwGfGxjiEn7FvTdK4UYKT9Qs6zBEmJiSkICNnYuC9q 9IrCIYDso7BMP9Owar111a3uBDZIt9dKCac7aq08vE/zJxRVx5+p9UjBtRPR/wzbxJhm C2u+5TSxFh1fU4M4A9cuSRjoFc+Ar257YWN5ksAUOlBHBGIKUx2pRysXWSI1tO+17mKd AQOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=XeW+h2iKEU30Tb5ZdMchDLLLd9Z+b8zPRFjGhythejY=; b=Tpc8gE404fhiYdl79X4w2YH5pEwGfshlJxbOJahgLLfCfN82F5UNEjAfmgosbs2xg6 BcEQhxEWOElpZS9XLyGuxMuJt5TKm5ioAUzKEYuf/CS1+J0meHVHQ6j9JBCrnPjb0lao frGpOcOb5FgZqbEeOU1uS3CbY75ojZDGsnYPqW+SDK98wwG543itg1Wou5uIlKQDS74L zmWM4SxbbkghJbfQTLwCKnfaDNGM+JzZZp97bGPzsTz4O2pyPyRyoDE2OyPmYglhpZD3 f1qm8ZOFPdh8NGbJVrts1mW25NvlrXFwTP0DE0oYfsO1IwoUEIgd6c0Mhkng4PKE0d7v QmjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=tX5nhybM; 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 m6si2827028pff.273.2018.02.14.00.59.04; Wed, 14 Feb 2018 00:59:19 -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; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=tX5nhybM; 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 S1754627AbeBNI40 (ORCPT + 99 others); Wed, 14 Feb 2018 03:56:26 -0500 Received: from bombadil.infradead.org ([198.137.202.133]:46052 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754400AbeBNI4Y (ORCPT ); Wed, 14 Feb 2018 03:56:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=XeW+h2iKEU30Tb5ZdMchDLLLd9Z+b8zPRFjGhythejY=; b=tX5nhybMV5+VMnMGFfBmeVxM1 LGQcVIxvc79kTqRwiP3yPhiEB2DNiyGoRCTfqb0NpHWYDcaWEUoLnq/yFHRePRY3FI08xzviAyXys QtC+O3/8Ayz2w0+w0l5Oweq6juvF+xG+PD9oeusRShSWKOXMXS7jzt/ohWIGu4KY0v8c3fkOCHn5H MJJ9qvGZZpYu8rYHQt4041DAkAXa28vk8vygd7eZs41cl6dL/B3/YTa2KV+n6Uf3U0mg/QMPz0O1x WkPA9f5TdWQlhaJIIPt5rfi5lE3z5V0mOxsyCBLqDBItt4xQFVDubRQ5l7op1yeqfEVZoFkXjpdWB 9Sd+84zFA==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.89 #1 (Red Hat Linux)) id 1elsrP-0005Gj-T4; Wed, 14 Feb 2018 08:56:16 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 150BB201FB4F4; Wed, 14 Feb 2018 09:56:14 +0100 (CET) Date: Wed, 14 Feb 2018 09:56:14 +0100 From: Peter Zijlstra To: Tim Chen Cc: Ingo Molnar , Dave Hansen , hpa@zytor.com, tglx@linutronix.de, torvalds@linux-foundation.org, linux-kernel@vger.kernel.org, dwmw@amazon.co.uk, linux-tip-commits@vger.kernel.org, Borislav Petkov , Arjan van de Ven Subject: Re: [tip:x86/pti] x86/speculation: Use IBRS if available before calling into firmware Message-ID: <20180214085614.GT25181@hirez.programming.kicks-ass.net> References: <1518362359-1005-1-git-send-email-dwmw@amazon.co.uk> <20180212102211.cdrrqqd4hdw7xu5y@gmail.com> <20180212165835.GO25181@hirez.programming.kicks-ass.net> <20180213075540.3lkikkpgjoe6ocjk@gmail.com> <5c3ba123-abbe-f153-7b75-a89d31d25c72@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <5c3ba123-abbe-f153-7b75-a89d31d25c72@linux.intel.com> User-Agent: Mutt/1.9.2 (2017-12-15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Feb 13, 2018 at 05:49:47PM -0800, Tim Chen wrote: > static inline void firmware_restrict_branch_speculation_start(void) > { > + if (this_cpu_inc_return(spec_ctrl_ibrs_fw_depth) == 1) > + alternative_msr_write(MSR_IA32_SPEC_CTRL, SPEC_CTRL_IBRS, > X86_FEATURE_USE_IBRS_FW); > } > > static inline void firmware_restrict_branch_speculation_end(void) > { > + if (this_cpu_dec_return(spec_ctrl_ibrs_fw_depth) == 0) > + alternative_msr_write(MSR_IA32_SPEC_CTRL, 0, > + X86_FEATURE_USE_IBRS_FW); > } At the very least this must disable and re-enable preemption, such that we guarantee we inc/dec the same counter. ISTR some firmware calls (EFI) actually are preemptible so that wouldn't work. Further, consider: this_cpu_inc_return() // 0->1 this_cpu_inc_return() // 1->2 call_broken_arse_firmware() this_cpu_dec_return() // 2->1 wrmsr(SPEC_CTRL, IBRS); /* from dodgy firmware crap */ this_cpu_dec_return() // 1->0 wrmsr(SPEC_CTRL, 0);