Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp1669770lqz; Mon, 1 Apr 2024 13:21:42 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV3mO1n4vrmnwDzuJGBBibr1dOyx88U0r2qotl2GqGwOWOyYdbOkMCTR3Utw9KBXXc1kIJlObrighE4aeeOBOXB+oLaNh3+lcShfGSpeQ== X-Google-Smtp-Source: AGHT+IGFhKGjK0eZ7W1DJ4B70Dy9hq/3sU/0D3t6vwZdL+cM3lRh2Z7OXNKMpX+9C7uT4iO9g18A X-Received: by 2002:a17:906:5448:b0:a46:bdd8:64ef with SMTP id d8-20020a170906544800b00a46bdd864efmr6139717ejp.19.1712002902781; Mon, 01 Apr 2024 13:21:42 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712002902; cv=pass; d=google.com; s=arc-20160816; b=Osep7n9OpatormpsN5uyLT53QBhxEQcKRyB3aTI3DQAeitDBSOk33r1/4Gt2lw2FDQ +L6HuHOSzGa4WtbuFz0A9+AZBFPh7MPywtqCH1oQWOJyWECkrjXYnCXeq1TohJz7BtF5 9pMdTFAL25fJjXxS+oKjj3L3lGy4BPx+AVOQugCpO0uOlgg7/+wmHoh2hcbwOshHK65+ 1pUS/lmeqnYaQMIk1A20KARgIf3L+iUYj42bllyyyEdKn9G0eIYuhu5vnCBbXMgKZU6E wnVewA3A6bgz1VN3zSnW3NWHq4JU/J8eIW6dUvQQSnFqAKuW6zajE4jqFY1xK5BPwT7r F3uw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=EXuQrJxq8hh4Q4PrrL+1pUvmc/UlgSCH8mpU+iMNc/A=; fh=teuxxMUynqIbqJ/SCZUtnPqPns5BY7if1FvYBHZUiwE=; b=eAo7JhmnSWM758RPny9gZq+V1lKXH+vnMg53GXja/oR8Nvxf+pOT3HH5ZNyxlBvGfM ejKjcxn2wf66Q07lbelfKTd/9ii4ISr1H/HpmG+nmxukSV3nVQe2RoGtsA+vGc+rDrup qyBEa6ZK8uvQ8aL9R/I5Oni2xfSpvbHAHNRE1hahny5UxfTQNwVcQcrIheXkJWXLKZap FwEbvIqgm7R+td+sE6Zok112K58664iEufrCQpjaXwI/cf/zngJgPctYQ7YVXYXMkHHF Nd1TNXkH+XnFtLpjc0EIBnt/g5DeHgo2CJ7TmhFAyUfYyeVkKPBHWNmRSacAcLF2Kufr dHRw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xq+anBcx; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-127022-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-127022-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id ss4-20020a170907038400b00a4e4c97bd5csi2635732ejb.165.2024.04.01.13.21.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Apr 2024 13:21:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-127022-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xq+anBcx; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-127022-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-127022-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 am.mirrors.kernel.org (Postfix) with ESMTPS id 5B7921F2211D for ; Mon, 1 Apr 2024 20:21:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E089553E2C; Mon, 1 Apr 2024 20:21:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="xq+anBcx" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7399846535 for ; Mon, 1 Apr 2024 20:21:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712002889; cv=none; b=MjO6YnqeZfvzv3zcGau1JU5MG4h1l+OAzRoEThHhoqhNxyurvGeaiizpjyme0hYDPza88yllzSw0LWemcXmx3Qpf+3O80ST94p1ydls9ai0JXwo43lNCUrIRoI7a4lW31STTYsBDVeBKRCzaKWMtfT0Co7U/gjOXuLh9SJC/BA4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712002889; c=relaxed/simple; bh=xi8fNfb+f7ixAYxOIg8qOE321OOUGkU+4oppUXoLRsU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WhfB65KVSLZgayJaXSOMHGWJRYqZpJ2C1Sii4nHJukISvQUoNobRkQYdGUYg08PeeXPIpVaJ1k4zBIdaLNXGDQLOEbL9GIMDLrSNZ7oZLWqrGps/qIVCACU9MXPLATxJ3jMzeV2QOVyvMwI8zPNWF6T9bwhRLz+RqoxQ8dfnwd0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=xq+anBcx; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc6b26845cdso6512570276.3 for ; Mon, 01 Apr 2024 13:21:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712002886; x=1712607686; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=EXuQrJxq8hh4Q4PrrL+1pUvmc/UlgSCH8mpU+iMNc/A=; b=xq+anBcx66qyZQercVDaUpKLfhNQcMjDe9r/pF6FsIdAtQkudxq3maBQbSZorqZvmT jKmG6Gdmtt0FGbFKmwiFqEc+TR7OcM484R2qtboyVX7xZ14iGnj1BB5wbRYnY6VVgxQF 6h1nBWVlrdqyMoijeIzIHwz5SvKaUSdg+bxs63BQqb7FqrpGcOwi1lXNVJzhqewhZLOs w+ZyPSap7dXcSfBUopbT0wRr4yonEEQqtj/P6YUgAmZ9daRhXLEbM7+nF5qo8NYLRZmN tPD4/sX4OHNKtHdz+l+mxNULNvUS2+HqqbJTy6lFUn+fFmbGPFKsqitnMosApJFCfHxf nHYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712002886; x=1712607686; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=EXuQrJxq8hh4Q4PrrL+1pUvmc/UlgSCH8mpU+iMNc/A=; b=aIkfD3uND0S+/sWXZp1YMxqvg8wjZKjMHwPO7pxsK2a+tgPKPW4/fLCWOhSlz05TM7 FdrEgNMgwR8Gnbiar0XmzFgI3XYfRa7En/ZVwHuZZixsHlEdX77dBqiNGTtpaF87qw82 8rQ203AZIrMEVzmuQf41A7E9U0tGmq/9Dq0Iik77aAPAqXHpfL7Z5KJY7bl6BLxGrgGZ czsYlsjVxRP9O0ELPn8gnULBO9J3opEyEZ2BfS1q+COymb0ws5YOLEZfngO8cwnXONHG GU05c9WcNIY/jAmMo2iyfOLb/iCI3Ev42elguHxCyWisHbNaHOy20NSIqGMWRpTCEV3a lkqQ== X-Forwarded-Encrypted: i=1; AJvYcCV44co5ZBRTjSi+nDyEPF0XfQgJhhbZQMaZT597E8dcGc7+gcCIXYWkfV8hhokBrB58z4ALfCnUaa0M4+W6fOKAbcJ6GFctrGAychBi X-Gm-Message-State: AOJu0Yx/vXmn/PZTThTT//MSx/CkVPDP3M7jtOyg6zdJ2Vr9lFeJOO5X MZ/E6IkBHeTbfa9K66/EDbEjmgzlaRiAWFAU5uY8JN0ULNk8+igQdsH0l9qtuPyag6BLhQRkZ67 Tzg== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1002:b0:dc9:5ef8:2b2d with SMTP id w2-20020a056902100200b00dc95ef82b2dmr3291799ybt.4.1712002886627; Mon, 01 Apr 2024 13:21:26 -0700 (PDT) Date: Mon, 1 Apr 2024 13:21:25 -0700 In-Reply-To: <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 References: <20240328171949.743211-1-leobras@redhat.com> Message-ID: Subject: Re: [RFC PATCH v1 0/2] Avoid rcu_core() if CPU just left guest vcpu From: Sean Christopherson To: Leonardo Bras Cc: 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 Content-Type: text/plain; charset="us-ascii" 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? > 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. > 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. KVM can provide that information with much better precision, e.g. KVM knows when when it's in the core vCPU run loop. > 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. 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.