Received: by 10.223.164.202 with SMTP id h10csp151523wrb; Mon, 13 Nov 2017 22:13:25 -0800 (PST) X-Google-Smtp-Source: AGs4zMZNaFVXZ7DmbaSx4a8qelOXdjjptRcwPWNAELSHYzP7pt1Jvh+UWI1jDSpYMgP2YiZ13A0h X-Received: by 10.98.185.10 with SMTP id z10mr12500346pfe.8.1510640005101; Mon, 13 Nov 2017 22:13:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510640005; cv=none; d=google.com; s=arc-20160816; b=ecspC0BbCEEA2j5JfN+WLLqWkNmwe1Wc0hm09QL+6Imigf/eO2YF9NjPpmzfwcZ2y6 DtjACH2/FX8QyIFnBUB1cr8E6vIxekzlNyRb3KeKE4QGqT5bSn3MH1YW8XiVIMB53knJ QEfAC3dghIfNch/Arj3bvVrGsPRZrDmKVW0aChlhTHOqKGZZ0+C7OS2njDU35U7SE492 WSrtPd4xxDxwdRoFJCWgTYW80Act730DitQDugVEVN0Av+M4u1ojY6cYHIMmQshhB6aV FJESLPowbHtUJ5+4uGa3KmG6U7dj3e/C4FTH/5rTAXkR643ntgrGMiroWFPVQcFezToD PLyA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=h4eVNPWy0ldvB5CGmcWJ1iJ3c4n9mKdoln31dthh4pY=; b=PDN85bDtOChe0zVoJOznfWL1nQfptlECEppENPM82AdF6lviAZUNIY1GlZWfns6LxU 019AJrSb/1EMfybxxRZisXzE5tM8B/ofBUzQ34OK2a32sma6RWxTWq+ObG+4/+UjGFiz Uqk7GxvVwdgWJuH922ykY9UV5wpFcZsPLcjVek/pmHgfqh2ImZYJoWvoKHDBUtiTWhTW 7ufNXZuqs96Xu+3G/IMRihO3EtwEu93gw87JbwBJ9tI8VSCSOE912kCZCigBoo2iP5R7 ZCYS1XuCW3Ws0CbTj2XhuCSYIrCWZLphcTrb6bNyBIZekFhkSJMxivYF8slLMY2um7F0 +MLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ekEOqYEv; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u10si5403535plz.134.2017.11.13.22.13.12; Mon, 13 Nov 2017 22:13: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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ekEOqYEv; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753437AbdKNGK0 (ORCPT + 89 others); Tue, 14 Nov 2017 01:10:26 -0500 Received: from mail-oi0-f67.google.com ([209.85.218.67]:49541 "EHLO mail-oi0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750790AbdKNGKR (ORCPT ); Tue, 14 Nov 2017 01:10:17 -0500 Received: by mail-oi0-f67.google.com with SMTP id r190so3899863oie.6; Mon, 13 Nov 2017 22:10:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=h4eVNPWy0ldvB5CGmcWJ1iJ3c4n9mKdoln31dthh4pY=; b=ekEOqYEvGygnpDJKzleEGVh8VSOR3NhCsiWqoKasaYuJmrG01mXluwVwriRvJ2X8V5 WJ8GVNPjGe3mn0ZnitMlFHYDs4L+ZaDdFG+UWSQ/YK+2xVbe4PbY6IA0CxPKXTky5a1W j2Jel9lfmsR20KDCcgfkuUbDPeNtJDx5O9tECF/7OMOGqX7UESO31+us6bAVkJNzsHDg NG4vUi6TLH0jtVVtk3YMwQxPKu/j2cTnxo9CHCjtXOqJpT0bHpHdbWJkLB6IUJTccVIO jMKMKX5JgV/tK11u1l4v1tGORm3kvhictYopiYIs3KBc4fwGUC5jP07YZXul5eRFaAZ1 kpEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=h4eVNPWy0ldvB5CGmcWJ1iJ3c4n9mKdoln31dthh4pY=; b=QT/zS/rpgfHes1ZtE/sg0Amv4cJsFCihwL6BQHVgv9WAz4h3b7WNzYjDBdtG6PsKh0 H4mywbw8umgtpRM3uozF0FOZYhnKSK0EatlUeBmGAyYepfUeaPZqm/pF+5bD8ZGLABCv 34mA3wrKt2+f+OczkW0rweZb0dnyGOgoRAgqV2r+JAGSRxqVrKxxqP38r6looJkPNg8h RODGQjHI9ZYqVZaTTr84DIUIQ40t/sUlF95mj3ojU1Jnv2D+HuB7c1jJp175eLstP6KA V2C5ET2ZEJKuCmmvCK2V1ILm5bNLRYntBSKeH2iafUOjZXfTQOXYH5OCgxPNEbqvB2rV ouEQ== X-Gm-Message-State: AJaThX5hUhkQhErumzl6OYJrQPKEXisyCFHv2A2mIFjdamNEFwd/CstF lOFP4m2qGUFGUMP/pKINpnTMiTctbPQyy+btUVQ= X-Received: by 10.202.198.6 with SMTP id w6mr3790648oif.349.1510639816828; Mon, 13 Nov 2017 22:10:16 -0800 (PST) MIME-Version: 1.0 Received: by 10.74.53.27 with HTTP; Mon, 13 Nov 2017 22:10:16 -0800 (PST) In-Reply-To: <20171113130226.ijxoxwozeoxqdsxw@hirez.programming.kicks-ass.net> References: <1510533206-9821-1-git-send-email-wanpeng.li@hotmail.com> <1510533206-9821-3-git-send-email-wanpeng.li@hotmail.com> <20171113080403.ici7nt5r4b5fpxa6@hirez.programming.kicks-ass.net> <20171113104634.62cmjalnj73scfnl@hirez.programming.kicks-ass.net> <20171113130226.ijxoxwozeoxqdsxw@hirez.programming.kicks-ass.net> From: Wanpeng Li Date: Tue, 14 Nov 2017 14:10:16 +0800 Message-ID: Subject: Re: [PATCH v4 2/4] KVM: X86: Add paravirt remote TLB flush To: Peter Zijlstra Cc: "linux-kernel@vger.kernel.org" , kvm , Paolo Bonzini , "Radim Kr??m????" , Wanpeng Li Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 2017-11-13 21:02 GMT+08:00 Peter Zijlstra : > On Mon, Nov 13, 2017 at 11:46:34AM +0100, Peter Zijlstra wrote: >> On Mon, Nov 13, 2017 at 04:26:57PM +0800, Wanpeng Li wrote: >> > 2017-11-13 16:04 GMT+08:00 Peter Zijlstra : >> >> > > So if at this point a vCPU gets preempted we'll still spin-wait for it, >> > > which is sub-optimal. >> > > >> > > I think we can come up with something to get around that 'problem' if >> > > indeed it is a problem. But we can easily do that as follow up patches. >> > > Just let me know if you think its worth spending more time on. >> > >> > You can post your idea, it is always smart. :) Then we can evaluate >> > the complexity and gains. >> >> I'm not sure I have a fully baked idea just yet, but the general idea >> would be something like: >> >> - switch (back) to a dedicated TLB invalidate IPI > > Just for PV that is; the !PV code can continue doing what it does today. > >> - introduce KVM_VCPU_IPI_PENDING >> >> - change flush_tlb_others() into something like: >> >> for_each_cpu(cpu, flushmask) { >> src = &per_cpu(steal_time, cpu); >> state = READ_ONCE(src->preempted); >> do { >> if (state & KVM_VCPU_PREEMPTED) { >> if (try_cmpxchg(&src->preempted, &state, >> state | KVM_VCPU_SHOULD_FLUSH)) { >> __cpumask_clear_cpu(cpu, flushmask); >> break; >> } >> } >> } while (!try_cmpxchg(&src->preempted, &state, >> state | KVM_VCPU_IPI_PENDING)); > > That can be written like: > > do { > if (state & KVM_VCPU_PREEMPTED) > new_state = state | KVM_VCPU_SHOULD_FLUSH; > else > new_state = state | KVM_VCPU_IPI_PENDING; > } while (!try_cmpxchg(&src->preempted, state, new_state); > > if (new_state & KVM_VCPU_IPI_PENDING) Should be new_state & KVM_VCPU_SHOULD_FLUSH I think. Regards, Wanpeng Li > __cpumask_clear_cpu(cpu, flushmask); > >> } >> >> apic->send_IPI_mask(flushmask, CALL_TLB_VECTOR); >> >> for_each_cpu(cpu, flushmask) { >> src = &per_cpu(steal_time, cpu); > > /* > * The ACQUIRE pairs with the cmpxchg clearing IPI_PENDING, > * which is either the TLB IPI handler, or the VMEXIT path. > * It ensure that the invalidate happens-before. > */ >> smp_cond_load_acquire(&src->preempted, !(VAL & KVM_VCPU_IPI_PENDING); >> } > > And here we wait for completion of the invalidate; but because of the > VMEXIT change below, this will never stall on a !running vCPU. > > Note that PLE will not help (much) here, without this extra IPI_PENDING > state and the VMEXIT transferring it to SHOULD_FLUSH this vCPU's progress > will be held up until all vCPU's you've IPI'd will have ran the IPI > handler, which in the worst case is still a very long time. > >> - have the TLB invalidate handler do something like: >> >> state = READ_ONCE(src->preempted); >> if (!(state & KVM_VCPU_IPI_PENDING)) >> return; >> >> local_flush_tlb(); >> >> do { >> } while (!try_cmpxchg(&src->preempted, &state, >> state & ~KVM_VCPU_IPI_PENDING)); > > That needs to be: > > /* > * Clear KVM_VCPU_IPI_PENDING to 'complete' flush_tlb_others(). > */ > do { > /* > * VMEXIT could have cleared this for us, in which case > * we're done. > */ > if (!(state & KVM_VCPU_IPI_PENDING)) > return; > > } while (!try_cmpxchg(&src->preempted, state, > state & ~KVM_VCPU_IPI_PENDING)); > >> - then at VMEXIT time do something like: >> > /* > * If we have IPI_PENDING set at VMEXIT time, transfer it to > * SHOULD_FLUSH. Clearing IPI_PENDING here allows the > * flush_others() vCPU to continue while the SHOULD_FLUSH > * guarantees this vCPU will flush TLBs before it continues > * execution. > */ > >> state = READ_ONCE(src->preempted); >> do { >> if (!(state & KVM_VCPU_IPI_PENDING)) >> break; >> } while (!try_cmpxchg(&src->preempted, state, >> (state & ~KVM_VCPU_IPI_PENDING) | >> KVM_VCPU_SHOULD_FLUSH)); >> >> and clear any possible pending TLB_VECTOR in the guest state to avoid >> raising that IPI spuriously on enter again. >> > > From 1583956054865568578@xxx Mon Nov 13 13:03:27 +0000 2017 X-GM-THRID: 1583909069046568739 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread