Received: by 2002:ab2:7b86:0:b0:1f7:5705:b850 with SMTP id q6csp11913lqh; Fri, 3 May 2024 11:43:12 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW2g7Cu+OeUnbYX/BcWuzM1nEUiZr5QQu+e7Z0tXq5HwieT5lAOcSseQDWNJlBIZm9SNG6sHB6wu/pUhXSYGtWWkAQJ6Kn2GUGIeL0cDQ== X-Google-Smtp-Source: AGHT+IF64hd6bK8s13OuLaZvFdrm6FusWr0utCyKnfUrelfLuOVvAzQ/bNwLBs+OregiwBYJ6DCD X-Received: by 2002:a05:6358:6916:b0:186:1c4f:29c7 with SMTP id d22-20020a056358691600b001861c4f29c7mr3752547rwh.27.1714761792164; Fri, 03 May 2024 11:43:12 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714761792; cv=pass; d=google.com; s=arc-20160816; b=rYPFywlo4RFi1sAq7Vd+YiwnsYvr2ypLXoIIMj84sgj9ruIJx4JTwpwujw9TUmRcTJ lNAtduvmxOaax7f1Bv+yj35ZM2kSx7R0GYrMaOIIlcvdTt0vrzhmEz0g5GHFuHr0FnAk n88o7tUQpv80dSMay40heQC2KZqGYdYCv6rJDpIkxnljIGwYLnZLSwy/4lfKwxAEbzHH Rs1FMG8dQl6ih90V+xeTXG6XT9W95sJAS/NpB01TYewEHo8fvCVT/pHqY9Xjir5Zlzvh omf5kS0R0hOWMAfaTDWUazAhrB0N4dElwjx0hQIDWnRkc3W7MN0fHIauw00UimOw6Zm+ y+9Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:content-disposition:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature; bh=7jg1F0r+Luam104Y7UVnMDpQtCctuiu4jLX0tf0Er1I=; fh=9JUhzyC3J8q2vBLVVrmIWlN6nRDQMYXUeqojAqcbQgE=; b=kTL3uVTFBl/DgZuSsxgUpnWggZWrhH1JQ/6f8cGsQQIILeDeiYs/EWwGbd5ynL0AUH 0LtdmYizFHW94wHt21CIkS/pGEBEGYEPbCRUMn38ywfZh4+MHyMGaTyv6zjX+vYXh+Pv B9gqnhU9faGk1RMNXEr5lR3c2jtC2ILyTEc4jTXzzUFdg+U2Itwdp8ziDr+UXKhojf6o QggiLhrF4OJjcmL8wBWjuGg8pFTZ35nwchycVF/aKytEiArtnbMAqVCjpvVCn4QsryOR btWXSBCwnCbv0C3HwHWfY2hyNXcPidoMNQ4H1phBny73/nnX1ijrxpa95I0sGyYv+TTq pKkA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JuLdIbmU; 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-168080-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-168080-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id x23-20020a631717000000b005f0c099b6f0si3420993pgl.87.2024.05.03.11.43.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 May 2024 11:43:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-168080-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JuLdIbmU; 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-168080-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-168080-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 7B24D284B71 for ; Fri, 3 May 2024 18:43:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 53E3912F585; Fri, 3 May 2024 18:42:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="JuLdIbmU" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 9C7A52E3E0 for ; Fri, 3 May 2024 18:42:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714761778; cv=none; b=ez5DwhQsF+ACNH0K+f7EaHXuC6ow/GbstauhbFHfke2NNAvSOMJV2gbirw8obhddtDGGtKfyKGgIGk8m6VqFGvSkpFoRGp/Wlw/FijVzFonxpDK5BaVBegScNwEgT4NaKBaAew5DZbds0C62nRYwklj4AyYM0gQTQKc6Dn7DXog= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714761778; c=relaxed/simple; bh=tU6G7JYGWRkG+2O/O2roQvpxabuBZJxtfMZ8Pdnsff0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type:Content-Disposition; b=GAJ5T4lOeAiuHsMojuLDOBlBsX2e4Z4DfL/Khw6i2XXqD5NLg8hfUk+9AL98ulJvH/IlZcVDenz2/d+XajCZLtiso1l89d6m8JKRHKIo+8vTHs8HaQcxvfAv3bWCyNC7KXYRxYM24voRP514MRrMYSCcsRZeA//h30UYk14pvsE= 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=JuLdIbmU; arc=none smtp.client-ip=170.10.129.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=1714761775; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7jg1F0r+Luam104Y7UVnMDpQtCctuiu4jLX0tf0Er1I=; b=JuLdIbmUOWthf/SfUfhZzbHmM8sbDv35ewFqCntRW8JzA5xdoFZXjhtGrrvceHccjfYqni Ww4u41V/TZI3ndQdNBQKmEWFUDuNAQFGPTsLeUqu5ER5W62z6KGH3egaXQN+v9d6t0D8eF htKso3kNlnZltOP4VhIYoR5/Ncki3Zg= Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-275-0uTna2ItPFalFzNgsS5EwQ-1; Fri, 03 May 2024 14:42:54 -0400 X-MC-Unique: 0uTna2ItPFalFzNgsS5EwQ-1 Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-1ec4c65a091so34720395ad.3 for ; Fri, 03 May 2024 11:42:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714761773; x=1715366573; h=content-transfer-encoding:content-disposition:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7jg1F0r+Luam104Y7UVnMDpQtCctuiu4jLX0tf0Er1I=; b=VvPiB/AwN4XhcqffS/c6GdLAIyookTKT07Y/+VJuLtCjo+fH1cBIyJGOLiLK7CCVqa gjUOX6KfpbPAHNjDv30ga7IBw9CyOyo5pu9KdXm4NSKw1rfkno8NtVbHVEzTaNH5HCuz CWV0OXBqP6kMolvMaSqXuzcQKhh9Q1AEosIljiIQL+zfzFwl/QKjvpE2EHlm4ts7EaA2 n1iM95ytKCR0cDUzmGmI0fjK9pQEWKIO1QZViNqYj/hLqit9KX+TdMdp2tHV3ogSv1+g DBsoQqL4AREn9o2smvmBdsUseM6hVzb11AKblQbvaDO+0UUVTm4EYMsLvycTqjclICh8 XrKw== X-Forwarded-Encrypted: i=1; AJvYcCXtxU29yt5mrvTSsMUp0qkCvCW220S0q0gUPPzO6tgTaolZTpjRpoHdinz0bTCVQAmjBP2an1pVndEI0EtsOlWt2al3HMzlw+NIZ/KX X-Gm-Message-State: AOJu0Ywi5BID49kMqjB4sdxYLZXLEhln7gw3j0mFfE+fT0tx+0mg0Cj2 BGk2XbHnHyIlJ/8+ZVhdzp0E3l/yZSXd6VPWNgWv9hO/FycAh+IW2pcTmewxUiPsgbSFq81lJUx LLM+6ARuskuUloNXWLD18NMiP0oO1Fx+t0XvJSPJr6kno1ynpHnhtnlV2uXwkRA== X-Received: by 2002:a17:902:f70f:b0:1dd:81a3:8dc3 with SMTP id h15-20020a170902f70f00b001dd81a38dc3mr4364025plo.46.1714761773116; Fri, 03 May 2024 11:42:53 -0700 (PDT) X-Received: by 2002:a17:902:f70f:b0:1dd:81a3:8dc3 with SMTP id h15-20020a170902f70f00b001dd81a38dc3mr4363995plo.46.1714761772691; Fri, 03 May 2024 11:42:52 -0700 (PDT) Received: from LeoBras.redhat.com ([2804:1b3:a800:4b0a:b7a4:5eb9:b8a9:508d]) by smtp.gmail.com with ESMTPSA id l16-20020a170903245000b001eb6280cb42sm3590633pls.11.2024.05.03.11.42.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 May 2024 11:42:52 -0700 (PDT) From: Leonardo Bras To: Sean Christopherson Cc: Leonardo Bras , Paolo Bonzini , "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Boqun Feng , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , Marcelo Tosatti , 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 Date: Fri, 3 May 2024 15:42:38 -0300 Message-ID: X-Mailer: git-send-email 2.45.0 In-Reply-To: References: <20240328171949.743211-1-leobras@redhat.com> 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 Content-Transfer-Encoding: 8bit Hello Sean, Marcelo and Paul, Thank you for your comments on this thread! I will try to reply some of the questions below: (Sorry for the delay, I was OOO for a while.) On Mon, Apr 01, 2024 at 01:21:25PM -0700, Sean Christopherson wrote: > On Thu, Mar 28, 2024, Leonardo Bras wrote: > > I am dealing with a latency issue inside a KVM guest, which is caused by > > a sched_switch to rcuc[1]. > > > > During guest entry, kernel code will signal to RCU that current CPU was on > > a quiescent state, making sure no other CPU is waiting for this one. > > > > If a vcpu just stopped running (guest_exit), and a syncronize_rcu() was > > issued somewhere since guest entry, there is a chance a timer interrupt > > will happen in that CPU, which will cause rcu_sched_clock_irq() to run. > > > > rcu_sched_clock_irq() will check rcu_pending() which will return true, > > and cause invoke_rcu_core() to be called, which will (in current config) > > cause rcuc/N to be scheduled into the current cpu. > > > > On rcu_pending(), I noticed we can avoid returning true (and thus invoking > > rcu_core()) if the current cpu is nohz_full, and the cpu came from either > > idle or userspace, since both are considered quiescent states. > > > > Since this is also true to guest context, my idea to solve this latency > > issue by avoiding rcu_core() invocation if it was running a guest vcpu. > > > > On the other hand, I could not find a way of reliably saying the current > > cpu was running a guest vcpu, so patch #1 implements a per-cpu variable > > for keeping the time (jiffies) of the last guest exit. > > > > In patch #2 I compare current time to that time, and if less than a second > > has past, we just skip rcu_core() invocation, since there is a high chance > > it will just go back to the guest in a moment. > > What's the downside if there's a false positive? False positive being guest_exit without going back in this CPU, right? If so in WSC, supposing no qs happens and there is a pending request, RCU will take a whole second to run again, possibly making other CPUs wait this long for a synchronize_rcu. This value (1 second) could defined in .config or as a parameter if needed, but does not seem a big deal, > > > What I know it's weird with this patch: > > 1 - Not sure if this is the best way of finding out if the cpu was > > running a guest recently. > > > > 2 - This per-cpu variable needs to get set at each guest_exit(), so it's > > overhead, even though it's supposed to be in local cache. If that's > > an issue, I would suggest having this part compiled out on > > !CONFIG_NO_HZ_FULL, but further checking each cpu for being nohz_full > > enabled seems more expensive than just setting this out. > > A per-CPU write isn't problematic, but I suspect reading jiffies will be quite > imprecise, e.g. it'll be a full tick "behind" on many exits. That would not be a problem, as it would mean 1 tick less waiting in the false positive WSC, and the 1s amount is plenty. > > > 3 - It checks if the guest exit happened over than 1 second ago. This 1 > > second value was copied from rcu_nohz_full_cpu() which checks if the > > grace period started over than a second ago. If this value is bad, > > I have no issue changing it. > > IMO, checking if a CPU "recently" ran a KVM vCPU is a suboptimal heuristic regardless > of what magic time threshold is used. IIUC, what you want is a way to detect if > a CPU is likely to _run_ a KVM vCPU in the near future. That's correct! > KVM can provide that > information with much better precision, e.g. KVM knows when when it's in the core > vCPU run loop. That would not be enough. I need to present the application/problem to make a point: - There is multiple isolated physical CPU (nohz_full) on which we want to run KVM_RT vcpus, which will be running a real-time (low latency) task. - This task should not miss deadlines (RT), so we test the VM to make sure the maximum latency on a long run does not exceed the latency requirement - This vcpu will run on SCHED_FIFO, but has to run on lower priority than rcuc, so we can avoid stalling other cpus. - There may be some scenarios where the vcpu will go back to userspace (from KVM_RUN ioctl), and that does not mean it's good to interrupt the this to run other stuff (like rcuc). Now, I understand it will cover most of our issues if we have a context tracking around the vcpu_run loop, since we can use that to decide not to run rcuc on the cpu if the interruption hapenned inside the loop. But IIUC we can have a thread that "just got out of the loop" getting interrupted by the timer, and asked to run rcu_core which will be bad for latency. I understand that the chance may be statistically low, but happening once may be enough to crush the latency numbers. Now, I can't think on a place to put this context trackers in kvm code that would avoid the chance of rcuc running improperly, that's why the suggested timeout, even though its ugly. About the false-positive, IIUC we could reduce it if we reset the per-cpu last_guest_exit on kvm_put. > > > 4 - Even though I could detect no issue, I included linux/kvm_host.h into > > rcu/tree_plugin.h, which is the first time it's getting included > > outside of kvm or arch code, and can be weird. > > Heh, kvm_host.h isn't included outside of KVM because several architectures can > build KVM as a module, which means referencing global KVM varibles from the kernel > proper won't work. > > > An alternative would be to create a new header for providing data for > > non-kvm code. > > I doubt a new .h or .c file is needed just for this, there's gotta be a decent > landing spot for a one-off variable. You are probably right > E.g. I wouldn't be at all surprised if there > is additional usefulness in knowing if a CPU is in KVM's core run loop and thus > likely to do a VM-Enter in the near future, at which point you could probably make > a good argument for adding a flag in "struct context_tracking". Even without a > separate use case, there's a good argument for adding that info to context_tracking. For the tracking solution, makes sense :) Not sure if the 'timeout' alternative will be that useful outside rcu. Thanks! Leo