Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp594488lqh; Thu, 28 Mar 2024 10:21:25 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVMljKPd+eeD/g347Xx5lFiB7BDrRb+e6rWJE1xJ7ERqxcRTpYuI6EP7WqMjBU5cYmpuSIWt0XsFRAkLy9YJmrZtyt3DIg8eJ0KM/CQ1w== X-Google-Smtp-Source: AGHT+IE5gIhifqQ7LRPheczDpeQKwcSeD7v40BjzUSsdQO0LyojibiLwCXJLBDDkNbWoNNCm3/6E X-Received: by 2002:a05:6a00:3a11:b0:6ea:90dd:fc3a with SMTP id fj17-20020a056a003a1100b006ea90ddfc3amr3739626pfb.25.1711646484870; Thu, 28 Mar 2024 10:21:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711646484; cv=pass; d=google.com; s=arc-20160816; b=ECRVI0/G/kKxbo6HrZW0P7eiYrG0aYbFUSmfBUh3HNu+oJWcaOepzUQqYuZw7khPNc 8XcqmHAXEW+3Eixhviiz+y7OvegE86UUIDBtgV3rI4CaXe5CE9bMEpgyCNtx8+AVdExg S93idCzw/vzT6cHGX/shpPYYfNWD8vc5kMyJd7+yQNUTo+8wdmQJqK165Mo4RkZxVaAH C/8cvbcs9k6HrzWf8ov546tLPfV3TMFf8v1vk1MnkE9EEmd1HacBE8UWoA7iLanU4wkh haNXO/oZEF5a4U/5prmkCVDnpQUe0ssaqmTLxSkovE6rpm9Be2GE8iJxQUpy0WXM4iRa vPLQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=FsmXoDAazFRAkcA6ArNgYSzB+6AfTdpq8nAX83CAIS4=; fh=F/CENbuXcWyXwM6dA//Jx/m8fK4f9tdKLeksUQiBRCU=; b=ObDtKY5XEd5NOddMxH3P618AFVekbsaX6hiMle08+C/p2W3Ylkt3IbXj9jluOGttML iJhAsIdefeU/TfPa9VAzWtbBX5e+bzPRsdZEgN3OPy1ocZmdPTs2hUaNHVPTPrdGDTld vf4m3yFwFIlA8w4YDYi82GhDk2wIE14LQbSpYHk7aIFQr4MSooggTfIChxTZVQitCWIU SHY+oBoAWlwU+WaWqiE+YzDQA68Ydgq0CINrJC+xcW2V5v/jtuchMXR5x5Qg9GChaTb4 +SpKWXtUEafp4gQF8N0Li6kzpWUUVptvBB62ZohUB9IXXPM2apapPeuZgQgcfTHFNc7Z zUGA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JCPJwRD2; 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-123353-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-123353-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. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id ln20-20020a056a003cd400b006e564168e64si2011606pfb.176.2024.03.28.10.21.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 10:21:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-123353-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JCPJwRD2; 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-123353-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-123353-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 7059029F4D3 for ; Thu, 28 Mar 2024 17:20:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 12F4B7F46F; Thu, 28 Mar 2024 17:20:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="JCPJwRD2" 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 846BE4436B for ; Thu, 28 Mar 2024 17:20:03 +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=1711646405; cv=none; b=u7O2N7keLdnijPhOPbn8P6ANzUpNvB2Z0Vhmz89JG5c5G7Owg+zyLNnA7OmFOPbPAy3yQ7s3zYuyM8gGTfNREe9yzlk5kLQwgu1kokikdMkjNgccyG0W3Hw+5tuLvEAfVxBEgUX3KCVuyWkCOvvkF5pxAJmf/w1ShZOeATusECI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711646405; c=relaxed/simple; bh=4fOayRX3S5Jxu3sgYNPny0lYFK450ICrctDD0gaS9/w=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=H+e77O4KXQrVE17oE/EWW1BSI5oSV8q3t3/SbUHkpN0+a3sCHeyGlvLGLRpJ86WzJRvQF/66POLYiRq17nARoQI+CBGT0eW9wna3LF9Tg8QkFOr6T7ATCbs38p1SHurq77KCDfDTmSnJUfaK5yYAqV4bGNqhUNiY9fmvQlC+ZNQ= 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=JCPJwRD2; 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=1711646402; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=FsmXoDAazFRAkcA6ArNgYSzB+6AfTdpq8nAX83CAIS4=; b=JCPJwRD2SA+NUn+qBesqDGaxmBuE22hNgVXb90Sa4ELYgGeyWNQczdVm5DPafxYo59co0M ru3JCt/0V1hyFzjB28Zz1eiCHOlcoDeU/0lI1Y4Ze9v06WeTD7iezQQE166/5W/PMXJN1X g3Q6YZNXtM2WLXImYhQ94O/uV37Qgmk= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-207-xxXWwooRPTCrghJT6ZDahA-1; Thu, 28 Mar 2024 13:20:01 -0400 X-MC-Unique: xxXWwooRPTCrghJT6ZDahA-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-69181af8ceaso16362846d6.2 for ; Thu, 28 Mar 2024 10:20:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711646401; x=1712251201; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=FsmXoDAazFRAkcA6ArNgYSzB+6AfTdpq8nAX83CAIS4=; b=QWhXWyf90PF3oDop2g6EPKB3JBH6RBFh5rlfANqTWGO09X/HKg/l9L0JkDKiW5sL6h Jvpq3eCglAiN/dlu9W25IEz08BYz7U9c+22PMUTXbuYIDoi53u9X3IaOqxDCT8oWVfp1 hTvanJZjiWSLSWS8gwkLLcV55nPi6I8q4H3xnpAWnjV8B4/LYlFHBoieu/mxceOIBeyS BjPRImh0yZGcxvzN7GztfdxEBnjG8KcpJDS6JC3D//7f9QUhF3xTLl8YsR7OvR3SGi4T NjzNuxP/ulhSv0U1tWuhuTQuQJjy705Mrpkzz7LBOrw0aUQIjN30YO5BK+gbKg1/D3zu 55OA== X-Forwarded-Encrypted: i=1; AJvYcCWfYWKQ4vofpGQgmn294OBas55x6NZIykQSr4rvNIAWmEb+vyOot8sSSm94PmCkwZ25211r7IDQn5PnyfLzRVPwKkekzUVqJW1fumwL X-Gm-Message-State: AOJu0YztVWKQk2pWqYx7lX/eY93FOMSFHsudH0Svugk1Yf7z2UZEwcYS /IfgdmX3KaH6JJWQT5ax6sZbxTWGj9PndfmXWRkQHaELvgoh22uuZw9oy7Di9gj5zh0FKecv2JK LLHq0dd+Cd/MhHyt08on42pSySo81nQK/xXDZWvHRnLbi5mamIvSf6KE5lNH55Q== X-Received: by 2002:a0c:e283:0:b0:696:533e:b003 with SMTP id r3-20020a0ce283000000b00696533eb003mr3622580qvl.18.1711646400882; Thu, 28 Mar 2024 10:20:00 -0700 (PDT) X-Received: by 2002:a0c:e283:0:b0:696:533e:b003 with SMTP id r3-20020a0ce283000000b00696533eb003mr3622550qvl.18.1711646400549; Thu, 28 Mar 2024 10:20:00 -0700 (PDT) Received: from LeoBras.redhat.com ([2804:1b3:a801:d7ed:4b57:3fcd:d5e6:a613]) by smtp.gmail.com with ESMTPSA id m13-20020ad45dcd000000b00696944e3ce6sm809078qvh.74.2024.03.28.10.19.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 10:19:59 -0700 (PDT) From: Leonardo Bras To: Paolo Bonzini , "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Boqun Feng , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , Marcelo Tosatti Cc: Leonardo Bras , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, rcu@vger.kernel.org Subject: [RFC PATCH v1 0/2] Avoid rcu_core() if CPU just left guest vcpu Date: Thu, 28 Mar 2024 14:19:45 -0300 Message-ID: <20240328171949.743211-1-leobras@redhat.com> X-Mailer: git-send-email 2.44.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 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. 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. 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. An alternative would be to create a new header for providing data for non-kvm code. Please provide feedback. Thanks! Leo ... [1]: It uses a PREEMPT_RT kernel, with the guest cpus running on isolated, rcu_nocbs, nohz_full cpus. Leonardo Bras (2): kvm: Implement guest_exit_last_time() rcu: Ignore RCU in nohz_full cpus if it was running a guest recently include/linux/kvm_host.h | 13 +++++++++++++ kernel/rcu/tree_plugin.h | 14 ++++++++++++++ kernel/rcu/tree.c | 4 +++- virt/kvm/kvm_main.c | 3 +++ 4 files changed, 33 insertions(+), 1 deletion(-) base-commit: 8d025e2092e29bfd13e56c78e22af25fac83c8ec -- 2.44.0