Received: by 10.223.176.46 with SMTP id f43csp1435721wra; Wed, 24 Jan 2018 16:58:26 -0800 (PST) X-Google-Smtp-Source: AH8x226groZSasC6HrCtX1GvCypwP++3E5qvkAfT7hPR4dxpnGjGh1rPTNS+pwmJkXZDg0QTx30X X-Received: by 10.99.99.129 with SMTP id x123mr12272437pgb.437.1516841905859; Wed, 24 Jan 2018 16:58:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516841905; cv=none; d=google.com; s=arc-20160816; b=uA0sM02XpE7HsLCcRdMaRfO15Q7kFtHMZRg37+vRixWKORPEkQV42eci67qn3oR2EY M/HwiQYzGajVWuetMQR1elt1y6gV/yEB+NskkUUI12c8epw0jHjkRfsfi8kAbwIs13bL ZUrCgP5CV6c9QIKV2zRgl4E6/2k1L/HCZb9LiNSryz8395p3hvP6U7ovAnjUyDk1TMKG UC99QFwMOIAWEJoNJQrImb/qY7LRvfP9rVXktOQTZ9ih/z9YZjUVpzh4ArOn0ktR7NNE +Jk/hg6CbIjrOpS4mQwJsUuYKf7D2snUk9DDe/1MI32vswK1NxFjGAPGpdFldC09pB6f eElg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=Vfe3FM9qIS+MhW/av9vKOt8HYiJpgwjLkutV06Hz4BY=; b=W67Mngrx0DyrprnZGbhSz/kJcqfR5Z/bRbRUVoJaIg2/eO7KtOc/mqKwXoMRY/pJIg krgujIOxjVzozLoeVBBf6WRw3754xImJPy/SK0JCjYcZE9+VIQUe9qe6Ro5gt0KlY+Ct ic/PHxcPbrmZaUcVsbf/4nCKIqRsKPMbE/XJl3XsPJHg77X6NX6OoJrx4voM8kTUntK9 0/Y/rvG56mJBpRIZ83x0vo7w0LxRqiOyvhvdktELAZVR53zCkmf/J0UFsljPQFXwl2kB zax2ArpTGy1LRFITPrTpRdUNoubS+0AmNusAsRrRI8Oz5WkewdRid2gnx7KP8CJusQHZ S5Rg== 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 q12-v6si1035374plk.632.2018.01.24.16.58.11; Wed, 24 Jan 2018 16:58:25 -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 S932955AbeAYA5P (ORCPT + 99 others); Wed, 24 Jan 2018 19:57:15 -0500 Received: from mga09.intel.com ([134.134.136.24]:55857 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932250AbeAYA5O (ORCPT ); Wed, 24 Jan 2018 19:57:14 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Jan 2018 16:57:14 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,409,1511856000"; d="scan'208";a="22116101" Received: from skl-02.jf.intel.com ([10.54.74.43]) by orsmga003.jf.intel.com with ESMTP; 24 Jan 2018 16:57:14 -0800 From: Tim Chen To: linux-kernel@vger.kernel.org Cc: Tim Chen , 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 , rkrcmar@redhat.com, Thomas Gleixner , Tom Lendacky , x86@kernel.org Subject: [RFC PATCH 1/2] x86/ibpb: Skip IBPB when we switch back to same user process Date: Wed, 24 Jan 2018 16:36:41 -0800 Message-Id: X-Mailer: git-send-email 2.9.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org These two patches provide optimization to skip IBPB for this commonly encountered scenario: We could switch to a kernel idle thread and then back to the original process such as: process A -> idle -> process A In such scenario, we do not have to do IBPB here even though the process is non-dumpable, as we are switching back to the same process after an hiatus. The cost is to have an extra pointer to track the last mm we were using before switching to the init_mm used by idle. But avoiding the extra IBPB is probably worth the extra memory for such a common scenario. Signed-off-by: Tim Chen --- arch/x86/include/asm/tlbflush.h | 2 ++ arch/x86/mm/tlb.c | 13 +++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h index 3effd3c..c5e325f 100644 --- a/arch/x86/include/asm/tlbflush.h +++ b/arch/x86/include/asm/tlbflush.h @@ -174,6 +174,8 @@ struct tlb_state { struct mm_struct *loaded_mm; u16 loaded_mm_asid; u16 next_asid; + /* last user mm */ + struct mm_struct *last_usr_mm; /* * We can be in one of several states: diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index d5a35fe..86ed07f 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -220,6 +220,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, } else { u16 new_asid; bool need_flush; + struct mm_struct *last = this_cpu_read(cpu_tlbstate.last_usr_mm); /* * Avoid user/user BTB poisoning by flushing the branch predictor @@ -229,15 +230,17 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, * As an optimization flush indirect branches only when * switching into processes that disable dumping. * - * This will not flush branches when switching into kernel - * threads, but it would flush them when switching to the - * idle thread and back. + * 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. * * It might be useful to have a one-off cache here * to also not flush the idle case, but we would need some * kind of stable sequence number to remember the previous mm. */ - if (tsk && tsk->mm && get_dumpable(tsk->mm) != SUID_DUMP_USER) + if (tsk && tsk->mm && (tsk->mm != last) + && get_dumpable(tsk->mm) != SUID_DUMP_USER) indirect_branch_prediction_barrier(); if (IS_ENABLED(CONFIG_VMAP_STACK)) { @@ -288,6 +291,8 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, trace_tlb_flush_rcuidle(TLB_FLUSH_ON_TASK_SWITCH, 0); } + if (next != &init_mm && next != last) + this_cpu_write(cpu_tlbstate.last_usr_mm, next); this_cpu_write(cpu_tlbstate.loaded_mm, next); this_cpu_write(cpu_tlbstate.loaded_mm_asid, new_asid); } -- 2.9.4