Received: by 2002:a89:288:0:b0:1f7:eeee:6653 with SMTP id j8csp77884lqh; Mon, 6 May 2024 11:49:02 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUT9AEAk6oTQnzyeMpKgnmcIgCxyWWeOyFpVPl/QPo8oLVFddPwPg0wiLFUm6tznekeZf1DV9Ke5waS09MRJNk5PAewgJO1gwjkNLDPhw== X-Google-Smtp-Source: AGHT+IHEneE02SFfXMo9tWZRmy3qHzhxWKbuQ7XxcvVBabs21fGfTOR2jgpnbQLoqXOVlPt0ZIN1 X-Received: by 2002:a05:620a:47d0:b0:790:e856:7f62 with SMTP id du16-20020a05620a47d000b00790e8567f62mr10564949qkb.52.1715021342254; Mon, 06 May 2024 11:49:02 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715021342; cv=pass; d=google.com; s=arc-20160816; b=EDTQZWQtEq+AEHFN2oJyzEpFm3QJbvC3QPzLSS33H3sgtrnuTrt0kuYQ4SpjGjtKLm j0wJXugyuNapsIk2cUd1EVEEzyYHUHfHcybIfA6D76Ks7spCAf860Go0b9Hjm60TMosm 8SPy5X4zh9SqqShqrR5jdZb/WDfk+kw40n1iRE66c3JV4LPF8HKP7dVWX6W7fZX0wEhf zoMpvYTz28VpaPvxNITy7w/kwNCVAj1DBaxu7XoKy2AaxngyrXFaOcqKCLxt33SKKlKH Ek992YZWPwwozRV8GChViHJtZtSFW/jVeJZF+ZL52lJjlkyGv2dR79NqomB/oh2J4fcw VEUw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=aPOamX9LHTG5t3ZSBhvmzpDMv+bOSB81pKuh6fPgknw=; fh=Cx84n0pq+E/Eb6l9OzcSvpmAHJJ6ikuv46Q7h0VADiU=; b=CIqVnUl2zyq5yQcbJF05Qcj2aSuLbyUV444vxx2H4jkd1c004BakWpIao4CBDfO2xP PVLSrfVe4ZbPDl/m1WNh+NhHWt2SNiCpBkQzv7iRmT5/oZrFUUtpnYVmmnlLJ+fWN4E2 adhzdrxKMJUO2+d/GYr1UMqNe3f/PN6HijsSBzg5GVwhKc3+62Vq5jwB8GIfh03Yc64D Ke2CQ5P9C3Mht3gBvvONrRLmYxb/KXHnddosivJvBjsp3ZooAU9pDsPQ72Bc+pBByO3Z vn6oyPuhrOndCHcCttE/3KpazraGLdT0SQ7CI/81ELkBFV6s+ilV3tN9qJdp26wtnxNu Wb6A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=YPM15xWb; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-170306-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-170306-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id o22-20020a05620a0d5600b007909bf663bdsi9672084qkl.342.2024.05.06.11.49.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 11:49:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-170306-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=YPM15xWb; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-170306-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-170306-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id E92B11C20EBA for ; Mon, 6 May 2024 18:49:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 319A7158D9D; Mon, 6 May 2024 18:48:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="YPM15xWb" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ED1D815886B for ; Mon, 6 May 2024 18:48:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715021334; cv=none; b=km1XBsT5yoqHkyaCitXORnIOOxC+Px9AroFtuvyapcGE1OAt/kkn0CLxRtTjnxKH//DjipD9whDcB2mwANU4W0sjTH+MajQXw+SXDsPm9f3Y8O5HJc59nxNhNAvd+88XYWMECgvG4Qjj3Ldti1piZrBoUqfHD7JojOkYVRAlehw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715021334; c=relaxed/simple; bh=DdMY60eWYf7nFd2XjhGOmHWtIFWpG2Vu5/5hhugXBZ0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=MQ9oKyNtFdSIgJqUYH612+nQK03KDEPTID+v0d52vTEyZcOKQ1cRUmqlH0iNutaVPSXiZhgsihfhcFvgERkoM6btoyeoBVCWzW+XuqDYGE188aVddkHmWkSS+Bo5NhoIL7x/WGV8jE5CRA458gsrrBg8AMktOFf+N9WDSdRU0cM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=YPM15xWb; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715021326; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=aPOamX9LHTG5t3ZSBhvmzpDMv+bOSB81pKuh6fPgknw=; b=YPM15xWbogDpgXPfE+nJG2wWiSLPr1T6vOalmLBIvuNWTrjEzXv23ggcrZc1YaezJec1ZX y2tFxTdsUvEhEgzTntizr2o3oAMOVF3LkFeFUYpq59Bkrs09xIawtAISaDylnpbAU/u+OR owum3zy1Z6HML3BA29mlYMmHFXwr8Dc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-618-JxgjRRKQNJC9G07zoB7YFg-1; Mon, 06 May 2024 14:48:43 -0400 X-MC-Unique: JxgjRRKQNJC9G07zoB7YFg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 077BB8032FA; Mon, 6 May 2024 18:48:43 +0000 (UTC) Received: from tpad.localdomain (unknown [10.96.133.2]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0F3D42024513; Mon, 6 May 2024 18:48:42 +0000 (UTC) Received: by tpad.localdomain (Postfix, from userid 1000) id 0AFB1400DCBB9; Mon, 6 May 2024 15:47:21 -0300 (-03) Date: Mon, 6 May 2024 15:47:21 -0300 From: Marcelo Tosatti To: Leonardo Bras Cc: Sean Christopherson , "Paul E. McKenney" , Paolo Bonzini , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Boqun Feng , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, rcu@vger.kernel.org Subject: Re: [RFC PATCH v1 0/2] Avoid rcu_core() if CPU just left guest vcpu Message-ID: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 On Fri, May 03, 2024 at 05:44:22PM -0300, Leonardo Bras wrote: > On Wed, Apr 17, 2024 at 10:22:18AM -0700, Sean Christopherson wrote: > > On Wed, Apr 17, 2024, Marcelo Tosatti wrote: > > > On Tue, Apr 16, 2024 at 07:07:32AM -0700, Sean Christopherson wrote: > > > > On Tue, Apr 16, 2024, Marcelo Tosatti wrote: > > > > > > Why not have > > > > > > KVM provide a "this task is in KVM_RUN" flag, and then let the existing timeout > > > > > > handle the (hopefully rare) case where KVM doesn't "immediately" re-enter the guest? > > > > > > > > > > Do you mean something like: > > > > > > > > > > diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c > > > > > index d9642dd06c25..0ca5a6a45025 100644 > > > > > --- a/kernel/rcu/tree.c > > > > > +++ b/kernel/rcu/tree.c > > > > > @@ -3938,7 +3938,7 @@ static int rcu_pending(int user) > > > > > return 1; > > > > > > > > > > /* Is this a nohz_full CPU in userspace or idle? (Ignore RCU if so.) */ > > > > > - if ((user || rcu_is_cpu_rrupt_from_idle()) && rcu_nohz_full_cpu()) > > > > > + if ((user || rcu_is_cpu_rrupt_from_idle() || this_cpu->in_kvm_run) && rcu_nohz_full_cpu()) > > > > > return 0; > > > > > > > > Yes. This, https://lore.kernel.org/all/ZhAN28BcMsfl4gm-@google.com, plus logic > > > > in kvm_sched_{in,out}(). > > > > > > Question: where is vcpu->wants_to_run set? (or, where is the full series > > > again?). > > > > Precisely around the call to kvm_arch_vcpu_ioctl_run(). I am planning on applying > > the patch that introduces the code for 6.10[*], I just haven't yet for a variety > > of reasons. > > > > [*] https://lore.kernel.org/all/20240307163541.92138-1-dmatlack@google.com > > > > > So for guest HLT emulation, there is a window between > > > > > > kvm_vcpu_block -> fire_sched_out_preempt_notifiers -> vcpu_put > > > and the idle's task call to ct_cpuidle_enter, where > > > > > > ct_dynticks_nesting() != 0 and vcpu_put has already executed. > > > > > > Even for idle=poll, the race exists. > > > > Is waking rcuc actually problematic? > > Yeah, it may introduce a lot (30us) of latency in some cases, causing a > missed deadline. > > When dealing with RT tasks, missing a deadline can be really bad, so we > need to make sure it will happen as rarely as possible. > > > I agree it's not ideal, but it's a smallish > > window, i.e. is unlikely to happen frequently, and if rcuc is awakened, it will > > effectively steal cycles from the idle thread, not the vCPU thread. > > It would be fine, but sometimes the idle thread will run very briefly, and > stealing microseconds from it will still steal enough time from the vcpu > thread to become a problem. > > > If the vCPU > > gets a wake event before rcuc completes, then the vCPU could experience jitter, > > but that could also happen if the CPU ends up in a deep C-state. > > IIUC, if the scenario calls for a very short HLT, which is kind of usual, > then the CPU will not get into deep C-state. > For the scenarios longer HLT happens, then it would be fine. And it might be that the chosen idle state has low latency. There is interest from customer in using realtime and saving energy as well. For example: https://doc.dpdk.org/guides/sample_app_ug/l3_forward_power_man.html > > And that race exists in general, i.e. any IRQ that arrives just as the idle task > > is being scheduled in will unnecessarily wakeup rcuc. > > That's a race could be solved with the timeout (snapshot) solution, if we > don't zero last_guest_exit on kvm_sched_out(), right? Yes. > > > > > /* Is the RCU core waiting for a quiescent state from this CPU? */ > > > > > > > > > > The problem is: > > > > > > > > > > 1) You should only set that flag, in the VM-entry path, after the point > > > > > where no use of RCU is made: close to guest_state_enter_irqoff call. > > > > > > > > Why? As established above, KVM essentially has 1 second to enter the guest after > > > > setting in_guest_run_loop (or whatever we call it). In the vast majority of cases, > > > > the time before KVM enters the guest can probably be measured in microseconds. > > > > > > OK. > > > > > > > Snapshotting the exit time has the exact same problem of depending on KVM to > > > > re-enter the guest soon-ish, so I don't understand why this would be considered > > > > a problem with a flag to note the CPU is in KVM's run loop, but not with a > > > > snapshot to say the CPU recently exited a KVM guest. > > > > > > See the race above. > > > > Ya, but if kvm_last_guest_exit is zeroed in kvm_sched_out(), then the snapshot > > approach ends up with the same race. And not zeroing kvm_last_guest_exit is > > arguably much more problematic as encountering a false positive doesn't require > > hitting a small window. > > For the false positive (only on nohz_full) the maximum delay for the > rcu_core() to be run would be 1s, and that would be in case we don't schedule out for > some userspace task or idle thread, in which case we have a quiescent state > without the need of rcu_core(). > > Now, for not being an userspace nor idle thread, it would need to be one or > more kernel threads, which I suppose aren't usually many, nor usually > take that long for completing, if we consider to be running on an isolated (nohz_full) cpu. > > So, for the kvm_sched_out() case, I don't actually think we are > statistically introducing that much of a delay in the RCU mechanism. > > (I may be missing some point, though) > > Thanks! > Leo > > > > > > > > 2) While handling a VM-exit, a host timer interrupt can occur before that, > > > > > or after the point where "this_cpu->in_kvm_run" is set to false. > > > > > > > > > > And a host timer interrupt calls rcu_sched_clock_irq which is going to > > > > > wake up rcuc. > > > > > > > > If in_kvm_run is false when the IRQ is handled, then either KVM exited to userspace > > > > or the vCPU was scheduled out. In the former case, rcuc won't be woken up if the > > > > CPU is in userspace. And in the latter case, waking up rcuc is absolutely the > > > > correct thing to do as VM-Enter is not imminent. > > > > > > > > For exits to userspace, there would be a small window where an IRQ could arrive > > > > between KVM putting the vCPU and the CPU actually returning to userspace, but > > > > unless that's problematic in practice, I think it's a reasonable tradeoff. > > > > > > OK, your proposal looks alright except these races. > > > > > > We don't want those races to occur in production (and they likely will). > > > > > > Is there any way to fix the races? Perhaps cmpxchg? > > > > I don't think an atomic switch from the vCPU task to the idle task is feasible, > > e.g. KVM would somehow have to know that the idle task is going to run next. > > This seems like something that needs a generic solution, e.g. to prevent waking > > rcuc if the idle task is in the process of being scheduled in. > > > >