Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp640645lqm; Wed, 1 May 2024 11:01:54 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUDmYgcFYvnGgWpAYSjzZUGo/B3XW6DoMSNsbs+FZd6OAJ+4MAjbrDOofPVZ+w4ZsaknD9kunNZdQWLTSwG8zAhxhAdNlxc1Qea4TT7Kw== X-Google-Smtp-Source: AGHT+IHM2Xuu+mKECeKg+wgp2I9HRc6PnHXqVPdTavRl8LP1deWFGg2w5VDD2Sm6kiifBktMQJVI X-Received: by 2002:a05:6902:1344:b0:de5:122d:dc9c with SMTP id g4-20020a056902134400b00de5122ddc9cmr3407676ybu.48.1714586513216; Wed, 01 May 2024 11:01:53 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714586513; cv=pass; d=google.com; s=arc-20160816; b=uYDG1j3oxwXXReHFHcoO95OqsIXsGLjKXtXh1SUMfyK9JQgGCPOOEF1dAMvmP6892D xWkz+cIZrBb9ImEGbamFKJS7/LN6rI9wxtlL77g2SoaEVwSwtIqCWc/HuxCt2Ax4DPpr Vdn7Sudpb8boTLTK7EOuFMRg/IyxraEd9FfiNd65/vsLw+gBoTVCM3UljB4kXdp0A7fM JV9i9c8OudENbZ7hDJhwaRN7gnXYZG/LvRfo5tLkjPJs+Fxm8OQvJrAyj2VRQFJiwHUk Pk5trv5rbKx3fBkNCOPgHZdAYSRkzajLcxvzStD9a/BwXlduuEZnzS1MSkgpWFXJmx5D 2B9A== 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:dkim-signature:date; bh=69cqu9i0hgyzyuHYDqr+vrRYe6XfjRaXapX+zs6xjnE=; fh=j3GB/m7NbW+xI2Q6rSwMt0ZZ5txZtE2iyXMuuS2qupY=; b=rB559WNPhBDocI5nJahk1AV1igakIM4vA80Lvyi5tzzxSfaT+MqbUCqHgcDy3sZfmj dykvHpK1yuhoe4jvSA9dVm3/jpRMTfAIsELLxOUL1+5/V+MQypr2I1h6aevBGy/gYaFT INUuvUzUKHyDZIWf56PNMDcxzKRHIQoE5CQMbzawdLWKZUpet4OTxkVWV/bnXgI73pGb 9N28ZrI5Q2ZlWBtqon2dbz5ko3XV/Bb3QbtWSTynkURDoacyvT3CqqgQPV26Y+l4vVz6 AZQLrH5CIQTQoXrr3XmfI8Z5nsVWsHxVb1ZyTbfohlfufkasC71L2D6MKumfbxAHfDRQ s14g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b="OKthm/ur"; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-165673-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165673-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id k6-20020a056902070600b00de5224f79dasi12172616ybt.717.2024.05.01.11.01.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 11:01:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-165673-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b="OKthm/ur"; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-165673-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165673-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev 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 E4ABD1C2180F for ; Wed, 1 May 2024 18:01:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E1FD8146D70; Wed, 1 May 2024 18:01:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="OKthm/ur" Received: from out-179.mta0.migadu.com (out-179.mta0.migadu.com [91.218.175.179]) (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 40A8B8F58 for ; Wed, 1 May 2024 18:01:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714586503; cv=none; b=EwBxIZMJzD/eS37M4qIyJjyDQggg8Fg+kGSgfT7HO6Qz8NvvQBhOloMiasOgBMQrELK7MMtw/hvN0IubCbWPK8Z7UeHERrT6mxJ9Thnt2R0q1SvMlWM5Bp6Jo3df4cSFXCBZ5igEUDQ0xbfLtd9Y+eOSdRx30XOmLC6LB55cgjI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714586503; c=relaxed/simple; bh=zjpxgyAK4GAbrd3HGC5s/4QO74a8GhDO3MiVtLjfZEA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=uVu3hMXiE2N2y3+1GWTk1o2FKgMnYFxN/1W/g4fPNpj5puIY8147MbUXhYRv0waxNVjs/rgPjwF75FzvUYWdaBqVH1E8FdtjDCx5BOIWOor10zySA5XWjv/gfCDkAlL7XRxazOXt26/Fz8rkqODpFhEW4ACnq33tler7kN7Mohk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=OKthm/ur; arc=none smtp.client-ip=91.218.175.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Date: Wed, 1 May 2024 18:01:31 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1714586499; 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=69cqu9i0hgyzyuHYDqr+vrRYe6XfjRaXapX+zs6xjnE=; b=OKthm/ur+byGKYcHm2DPTNC5z/URFbkOGPadzI7eiZzZ5mezBuOrZl7iO90ODYz9fRbgEy 6ouYU5nahmPlBewz9DVswxKYUkCE/I5p4/wec5b9+XAdcs6XajZ/4HudNHe28xxU0lQwpd WHY0DzOGslb59/2viaOe877XKsJlqEk= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Oliver Upton To: Sean Christopherson Cc: Marc Zyngier , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Paolo Bonzini , 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 Subject: Re: [PATCH 0/4] KVM: Fold kvm_arch_sched_in() into kvm_arch_vcpu_load() Message-ID: References: <20240430193157.419425-1-seanjc@google.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 In-Reply-To: X-Migadu-Flow: FLOW_OUT On Wed, May 01, 2024 at 07:28:21AM -0700, Sean Christopherson wrote: > On Wed, May 01, 2024, Oliver Upton wrote: > > On Tue, Apr 30, 2024 at 12:31:53PM -0700, Sean Christopherson wrote: > > > Drop kvm_arch_sched_in() and instead pass a @sched_in boolean to > > > kvm_arch_vcpu_load(). > > > > > > While fiddling with an idea for optimizing state management on AMD CPUs, > > > I wanted to skip re-saving certain host state when a vCPU is scheduled back > > > in, as the state (theoretically) shouldn't change for the task while it's > > > scheduled out. Actually doing that was annoying and unnecessarily brittle > > > due to having a separate API for the kvm_sched_in() case (the state save > > > needed to be in kvm_arch_vcpu_load() for the common path). > > > > > > E.g. I could have set a "temporary"-ish flag somewhere in kvm_vcpu, but (a) > > > that's gross and (b) it would rely on the arbitrary ordering between > > > sched_in() and vcpu_load() staying the same. > > > > Another option would be to change the rules around kvm_arch_sched_in() > > where the callee is expected to load the vCPU context. > > > > The default implementation could just call kvm_arch_vcpu_load() directly > > and the x86 implementation can order things the way it wants before > > kvm_arch_vcpu_load(). > > > > I say this because ... > > > > > The only real downside I see is that arm64 and riscv end up having to pass > > > "false" for their direct usage of kvm_arch_vcpu_load(), and passing boolean > > > literals isn't ideal. But that can be solved by adding an inner helper that > > > omits the @sched_in param (I almost added a patch to do that, but I couldn't > > > convince myself it was necessary). > > > > Needing to pass @sched_in for other usage of kvm_arch_vcpu_load() hurts > > readability, especially when no other architecture besides x86 cares > > about it. > > Yeah, that bothers me too. > > I tried your suggestion of having x86's kvm_arch_sched_in() do kvm_arch_vcpu_load(), > and even with an added kvm_arch_sched_out() to provide symmetry, the x86 code is > kludgy, and even the common code is a bit confusing as it's not super obvious > that kvm_sched_{in,out}() is really just kvm_arch_vcpu_{load,put}(). > > Staring a bit more at the vCPU flags we have, adding a "bool scheduled_out" isn't > terribly gross if it's done in common code and persists across load() and put(), > i.e. isn't so blatantly a temporary field. And because it's easy, it could be > set with WRITE_ONCE() so that if it can be read cross-task if there's ever a > reason to do so. > > The x86 code ends up being less ugly, and adding future arch/vendor code for > sched_in() *or* sched_out() requires minimal churn, e.g. arch code doesn't need > to override kvm_arch_sched_in(). > > The only weird part is that vcpu->preempted and vcpu->ready have slightly > different behavior, as they are cleared before kvm_arch_vcpu_load(). But the > weirdness is really with those flags no having symmetry, not with scheduled_out > itself. > > Thoughts? Yeah, this seems reasonable. Perhaps scheduled_out could be a nice hint for guardrails / sanity checks in the future. -- Thanks, Oliver