Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp687888imm; Wed, 18 Jul 2018 09:01:17 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfXODEhklF49G+1nVv1ME/DVGSmqkEMIjBmBYitviPpoQBb4j9+8Citp5QA6jI9lNh/WW4E X-Received: by 2002:a17:902:b81:: with SMTP id 1-v6mr6519749plr.164.1531929677728; Wed, 18 Jul 2018 09:01:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531929677; cv=none; d=google.com; s=arc-20160816; b=Hry+RrqT9779XKu26mpvqnS9bPjmIGYS26Pk8nYM6aMyAIEH55J7ybawQSCxzjm5VA WPxq/EEWhFuM5zY/29+oKUZQmUOk3XAJ2Ci7QP+KtMVGlZ/jBdjj5Vu+shDLGWjFhjeN l2e8fmk7v5Q9m6FCZK9VqznvS3SlszvLnpPoNubze0D+FS5D2jqWEGKV58stY5yPzr+5 CU98JFT1Kzgxr0z+IvqI8a8c0LT8PXgLKrh16KG1vsIyEUdh6tbkbxaywQ4/zApXEroo yF1D80DOhbbDpT0p5OoIn1D9HxtgWHqUnXDMW9nDgA8x7yJmWenGgTuV9+I+QhpxK0yw fVzA== 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=9RsJjtjGOQbPAeWqHkmOuAlM10q3JCNK2QLUNQ4zqFw=; b=ze0RmPNtiTSjrEsukQQHXAMYP/RA8+WBaBCE5hBGNoC8s6u3NjDNGvmjvZT20BMSgW T7cvU1W3G9Z8wuJwK9uSNzHhNKIYZtLaz+z67YQMu4H7eyNbgyYOKjtfgAOIyPlYT+Wh iP/tviAzcPPLIcvNk487Zk0NCaO9PeuMfCpTI24Xj/CmMaQgFASdEnJ1wgaW+iQJrH6L pO0fkN+ll4IiCwvqyF+OOoh/kpmns5u/nsUKeRAIdPflIYGT9rHRk02d4UzXnw8nTE67 Ftk+vGwNyB+73iekZ4URcCVN0UaDMLp/V9+TpWS1WmZDVEe9C3f16aQd7g86oQUV5UVT jw5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b="nMTgoXZ/"; 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 91-v6si3335817ply.296.2018.07.18.09.01.01; Wed, 18 Jul 2018 09:01:17 -0700 (PDT) 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=merlin.20170209 header.b="nMTgoXZ/"; 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 S1731236AbeGRQi7 (ORCPT + 99 others); Wed, 18 Jul 2018 12:38:59 -0400 Received: from merlin.infradead.org ([205.233.59.134]:44410 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730986AbeGRQi6 (ORCPT ); Wed, 18 Jul 2018 12:38:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.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=9RsJjtjGOQbPAeWqHkmOuAlM10q3JCNK2QLUNQ4zqFw=; b=nMTgoXZ/OgsZa+ZlQ5Ty975K2 8dOSywdX6NGYpXvhi+v+OcrVsTX7p/X7xToen2HHhaH8aGXu8/+ALrdB2uXwqdHYubuQqzL1CD18d xIKbkYi+FmK767ARz+6MFWwBJCT2tw4jy0pnTsSGRY+PE85zJx8KlsYgNrA91Bg7Jj9P8svjpVNpz wAl4Mc/rVheKXQuq4mlkF0N3EPkiLFZfNUJOQhoDPNIzu8BzpkXh3PVjwHtPyl0BMx0tdU6rRFjjI HBblYXjk26bXGVhhbGyGikKcsea29ne/H32gNxWomr3GnX43Ax9PCXrn8aKchHsIfRok9Btp0qeLz HbCTmKf8w==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1ffos6-0007Wx-5y; Wed, 18 Jul 2018 16:00:10 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 66F5620289330; Wed, 18 Jul 2018 18:00:07 +0200 (CEST) Date: Wed, 18 Jul 2018 18:00:07 +0200 From: Peter Zijlstra To: Rik van Riel Cc: songliubraving@fb.com, linux-kernel@vger.kernel.org, dave.hansen@intel.com, hpa@zytor.com, tglx@linutronix.de, mingo@kernel.org, torvalds@linux-foundation.org, linux-tip-commits@vger.kernel.org Subject: Re: [tip:x86/mm] x86/mm/tlb: Make lazy TLB mode lazier Message-ID: <20180718160007.GS2494@hirez.programming.kicks-ass.net> References: <20180716190337.26133-5-riel@surriel.com> <20180717113330.GU2476@hirez.programming.kicks-ass.net> <08AC2AF2-17DE-4416-BBBD-B6B950D20906@surriel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <08AC2AF2-17DE-4416-BBBD-B6B950D20906@surriel.com> User-Agent: Mutt/1.10.0 (2018-05-17) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jul 18, 2018 at 11:33:02AM -0400, Rik van Riel wrote: > The tlb flush code first increments mm->context.tlb_gen, and then sends > shootdown IPIs to CPUs that have this mm loaded and are not in lazy > TLB mode. > > At context switch time, we have to ensure that we check the tlb_gen after > we load the old is_lazy state. > > Maybe something like this? > > /* > * Read the tlb_gen to check whether a flush is needed. > * If the TLB is up to date, just use it. > * The TLB shootdown code first increments tlb_gen, and then > * sends IPIs to CPUs that have this CPU loaded and are not > * in lazy TLB mode. The barrier ensures we handle > * cpu_tlbstate.is_lazy before tlb_gen, keeping this code > * synchronized with the TLB flush code. > */ Let me try and draw a diagram; that always works better for me that text. So the relevant ordering is something like: CPU0 - switch_mm() CPU1 - flush_tlb_mm_range() [W] cpu_tlbstate.is_lazy = false; [RmW] next->tlb_gen++ smp_mb() MB (implied) [R] tlb_gen = next->tlb_gen native_flush_tlb_others() [R] cpu_tlbstate.is_lazy Such that CPU1 either observes !lazy and flushes and/or CPU0 observes the generation increment and forces a flush itself. Either way, CPU0 gets flushed. Also, I don't suppose you've looked at the paravirt instances of flush_tlb_other() ? They don't elide the flushes because of lazy.