Received: by 2002:ab2:7855:0:b0:1f9:5764:f03e with SMTP id m21csp63697lqp; Tue, 21 May 2024 18:41:52 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW0xlIV3uEWbPnu7b2ilBlXGeAxvh14v/pY79dHPXFHa4K9gwujbB88agAMtB5cNSOm/6jv+YsECAYhUFqJrqAefejCPfZlI2Bug163Ig== X-Google-Smtp-Source: AGHT+IHpc8iOjbyOMfsehxyK+eU6tg26wXQuF8f6jwNpdqHJ2y4x65qG0rhlxGJv3uEA9ilxRwlg X-Received: by 2002:a17:90b:391:b0:2bd:8803:485b with SMTP id 98e67ed59e1d1-2bd9f46995bmr882237a91.6.1716342112238; Tue, 21 May 2024 18:41:52 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716342112; cv=pass; d=google.com; s=arc-20160816; b=ploce47QMJLbGANry4SCGiFuhdtPuK57T4hloYCzlmU31JOO+Qb+q0ubolazM2q/h0 eLWme+QPPcjK3Dtok2ivJB3yLN7NLfVLLvTrM/cskeen9v5uu+BssV1tgFBVlXwywoJV OPXzjWN134lL3wRNJT4P+zxAxW2bxI3nOGACJ6rejZ1SXGeyJuhuJABcztGdhChHN5C2 X6cd4mRSt4NsaQljg8e7zcibFZTIIg85CKhzdmemPPrNL/jZy67JHW/P/nadyY2qmG98 xsj9Aq0N6OUFoJ+uFA6RKGCGVsQZG7MNFZhl4zXvhGiuB9HM37pZf3I4jOWm11Er4CJy L0fA== 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 :reply-to:dkim-signature; bh=ksrstYm4acNB+vq44uyCoC45OYvJ0DDPXjwdxHvKYos=; fh=QnVR8NtPrXsYeppaf4jcogwpfDj22cVWxVMZrYAcqH8=; b=fD86dYqqjEBQhd0iVi6l6BV+X8W7yTF0ws2U9ElYT45af6AgTCNgZ45D9GSE58nSjd xPWVK177blhHE1wozDhEKTDk2BxHdoP/mJZmAoyGRHsCYMXnWAbBYtmhO04nCyEzFxFx JWGznOIXqr9Y20z/gl++agDqihjnLti4jTuIG2nY+U7FkylYXy3Kirqe1v3L7ntn33C4 bh9Bki7Utjwdrai+XGalj2y/RuSdSyCQjbJQtrn6kNSZFeccgmbmKWUrE9r/Jyc1+b/f MyfD8Ekr6xyHtDD3M6v7TgTkjiJH7H4YTD4xTPSQud0SZ2bPq775z98wimWoVFTor8US 5ljg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=1zkgIPIF; 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-185662-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-185662-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 98e67ed59e1d1-2bd90678074si2616342a91.93.2024.05.21.18.41.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 18:41:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-185662-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=@google.com header.s=20230601 header.b=1zkgIPIF; 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-185662-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-185662-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id C67922835E5 for ; Wed, 22 May 2024 01:40:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2B71218C3D; Wed, 22 May 2024 01:40:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="1zkgIPIF" Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (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 C20C35221 for ; Wed, 22 May 2024 01:40:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716342020; cv=none; b=iAkrjMcjfYkXrKbd6BGy5ieay+FYgHeepsheHQtKnjMpBj10NoL1G/zRKE6Y9efw0VZIRRXd74Cci+QjGtHmM/0QEDvOl+UlpGoN4kStiQ4CBBwL4o5U/Jd5RC8L42RFLBu3/N2RZ+jeA6v/bF4+bl30G78KKAv83KZVdA4x2lQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716342020; c=relaxed/simple; bh=8WkgXvFVZEpynasJJ48aoJTH4+LPLoJyE+T2vpEb3+o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=QJrqe4XRzcUCWbV1QZJfxhDuPxry5wnZiwDBRV7ZEecOacz41T8Q4SK2rDmb6mQebMvjmEIEeh9G78zZtBDdq8e6c59PeFwblK4yM4OK05KrFmnHLlhjstTvPqHzFqciWkTAPcwKEqa4wQhFnVqX5r0WNl6WyKWuw1Uw8AaeNQk= 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=1zkgIPIF; arc=none smtp.client-ip=209.85.215.202 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-pg1-f202.google.com with SMTP id 41be03b00d2f7-5c66a69ec8eso10774995a12.3 for ; Tue, 21 May 2024 18:40:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1716342018; x=1716946818; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=ksrstYm4acNB+vq44uyCoC45OYvJ0DDPXjwdxHvKYos=; b=1zkgIPIFA1ATGYiwT7x71SqKp5i99vZIi4WBoYv5glURwdRwNDTXXNCijRW1H3RfoN uLkv89p9FWdXTjgNcPNoan87La0hhNBAFmckgkCyP4XhQh16av3VfuQgopgquUfwuwwm kDFHbXtRhmHGIFMy9V/45rYAk8sU4Aux4i0oNdNbf3qQb2viz4skoTwKg3U96m/jBCs2 CIKJ5NRiGOvE+m0V9dd9ibWfo2Fc4hURmXJd/4WeKWMGvjNwNZMJLUAoT5ObPSK+bt89 iHSR7N75a6c0gdPaGxVWPrNdZShmWdTkqleNlwKy/3lOX4F40Pk8/yXBODJ4zszm8A6a 20Ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716342018; x=1716946818; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ksrstYm4acNB+vq44uyCoC45OYvJ0DDPXjwdxHvKYos=; b=uP0+mXK18paqYFtTd3nf2xa4PXCroiupWj2CI+D3ooNJZD3laOiZScWElOZOhYVAK5 zd+j0BdjevJR4Z9BorDugLbSpd0XwpSBgBKnmLYcfwjzpyjUClcsTqIrWgVByvlPNRSZ ZhtugFVJfzaZrouTFmaPgIui1o6+kkvw7vam8t00Qx2d6INhCwxaKM1TOhIRxNTrVvHX /Qo0JI2OZELcaBMKc8YniYXTiPFajI7dnpyCNHCcTtZ4500xHNO+prqZyDGzOyf5Wveq n8ICiiAL1bCZa4Gjuirq6VZG8veDh1DJ+o5txNCDQIwh3PryiFsKQ+k+wdDCaWQ8RkWo LNLg== X-Forwarded-Encrypted: i=1; AJvYcCUwjaG6nNPOEOvc1Uzb0RRMlNiskrmmV5xTC8gLxX409Um0h7pwCTdpWCeeR4IHAlhpp0ziE9AJXtrIniAYyqMlTDYnx4rifrGM3rDP X-Gm-Message-State: AOJu0YxhbUxm4/dHUbYjdnQMLSacpYBnVRNPOiZYnGA9o4g513e7fhmM P/9bEHa+6fmpvU6SOFanhdJJijQrk8XoivkVnMUJqtGgvoi0oohHI8uBuuxCLh9y4hrDdDDws95 vcg== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a02:484:b0:5dc:5111:d8b1 with SMTP id 41be03b00d2f7-676492dc83emr2297a12.5.1716342017980; Tue, 21 May 2024 18:40:17 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 21 May 2024 18:40:08 -0700 In-Reply-To: <20240522014013.1672962-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240522014013.1672962-1-seanjc@google.com> X-Mailer: git-send-email 2.45.0.215.g3402c0e53f-goog Message-ID: <20240522014013.1672962-2-seanjc@google.com> Subject: [PATCH v2 1/6] KVM: Add a flag to track if a loaded vCPU is scheduled out From: Sean Christopherson To: Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson , Paolo Bonzini Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm@vger.kernel.org, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Add a kvm_vcpu.scheduled_out flag to track if a vCPU is in the process of being scheduled out (vCPU put path), or if the vCPU is being reloaded after being scheduled out (vCPU load path). In the short term, this will allow dropping kvm_arch_sched_in(), as arch code can query scheduled_out during kvm_arch_vcpu_load(). Longer term, scheduled_out opens up other potential optimizations, without creating subtle/brittle dependencies. E.g. it allows KVM to keep guest state (that is managed via kvm_arch_vcpu_{load,put}()) loaded across kvm_sched_{out,in}(), if KVM knows the state isn't accessed by the host kernel. Forcing arch code to coordinate between kvm_arch_sched_{in,out}() and kvm_arch_vcpu_{load,put}() is awkward, not reusable, and relies on the exact ordering of calls into arch code. Adding scheduled_out also obviates the need for a kvm_arch_sched_out() hook, e.g. if arch code needs to do something novel when putting vCPU state. And even if KVM never uses scheduled_out for anything beyond dropping kvm_arch_sched_in(), just being able to remove all of the arch stubs makes it worth adding the flag. Link: https://lore.kernel.org/all/20240430224431.490139-1-seanjc@google.com Cc: Oliver Upton Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 7b57878c8c18..bde69f74b031 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -380,6 +380,7 @@ struct kvm_vcpu { #endif bool preempted; bool ready; + bool scheduled_out; struct kvm_vcpu_arch arch; struct kvm_vcpu_stat stat; char stats_id[KVM_STATS_NAME_SIZE]; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index a1756d5077ee..7ecea573d121 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -6288,6 +6288,8 @@ static void kvm_sched_in(struct preempt_notifier *pn, int cpu) __this_cpu_write(kvm_running_vcpu, vcpu); kvm_arch_sched_in(vcpu, cpu); kvm_arch_vcpu_load(vcpu, cpu); + + WRITE_ONCE(vcpu->scheduled_out, false); } static void kvm_sched_out(struct preempt_notifier *pn, @@ -6295,6 +6297,8 @@ static void kvm_sched_out(struct preempt_notifier *pn, { struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn); + WRITE_ONCE(vcpu->scheduled_out, true); + if (current->on_rq) { WRITE_ONCE(vcpu->preempted, true); WRITE_ONCE(vcpu->ready, true); -- 2.45.0.215.g3402c0e53f-goog