Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp366024lqo; Fri, 10 May 2024 02:13:22 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX1VWXsgi9dkMWripgsy3A1NgoQVViYWRRLLlpeSDNgLDQ4u2IXBdAbq7jiw/N63uqpltDlHUZ/9aX5Ec0dqEv1FniOTStGl+/32oOUow== X-Google-Smtp-Source: AGHT+IGOMCKzdjcN6gsvlItQXTzCfTtfvxMKN7dv7zoDCSYKDcrvrzGq0qJEpu4Zhjk0OI8j1r+x X-Received: by 2002:a17:902:8507:b0:1e5:562d:425b with SMTP id d9443c01a7336-1ef43d29885mr18645085ad.22.1715332401925; Fri, 10 May 2024 02:13:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715332401; cv=pass; d=google.com; s=arc-20160816; b=tZV/v86yGkv0C9YmscHfjP1gmhKXFGU5/ppjs4+PJRIs/j/3dbecnKqnixpiR84vIv bjh4hXl80i7TsktF2qyATXpiypkadehTMWzu2GMx7+++/HhDqoWg8lhRS2GUQGXSbTiB R20poRNodRJXHvAdtMWxzbN0bIQ2dw2o4Ki+uZGtgIzKh1RDkt895+Y6OtoB5H3mb/7x EFEImHCq8C3SzByRdKAW+W0BFC5i5J1VXCqDF1mU14bBybZ8/11yKNbApsBUPLyDIVfE gGR4Y4UF6B4495aKqzv1hbw+5xUGvG39jpOFDCxEzLPV9xAVcMkZmf3OfXw/Pp9u9sr3 pkLA== 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:date; bh=XR6Ovo4ZWCEwmB95aywDLbYo5COB1k5E4akUH/B0OJI=; fh=TeqqgvqYF8Nmjoi33oxx4FnClseIGMDgVdjpFwlKZ5k=; b=fhFWC9DlvyZg+sV+CV8Dp/ttkubwGtQ4wgUf8lKedjHOxrGpnm1FQZPmE8MA4uJH5U XpuWW1ck2DCKmNWtek3A5mcVbq+t912Jyy8+xmWudhelBJuiXzkLcLoIXRg28vduoSGE +SGrtxYyHUecIE65dbwnTqiL5T4wOc3uBEaANLTSK+jfujK8NVxxcxldDLQiP5yS0BL6 yMXcoOH1DcCk5M1Pie6Dzf8UcwCF/bbVHDd1mIko5AHfffTrYgyDQ0FAMqJuJtlKQ7ZV y7qCpuErXmsuI69r+pNBL0ZoIenq7+pVvxjSzT6y/uDof80BfjHpcSeRiLudih06462M kGMQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-175492-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-175492-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d9443c01a7336-1ef0bf3144fsi31228435ad.313.2024.05.10.02.13.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 May 2024 02:13:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-175492-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; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-175492-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-175492-linux.lists.archive=gmail.com@vger.kernel.org" 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 838412837A3 for ; Fri, 10 May 2024 09:12:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4EF08168AF3; Fri, 10 May 2024 09:11:20 +0000 (UTC) Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) (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 E2D4A165FCA; Fri, 10 May 2024 09:11:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715332279; cv=none; b=Uou5GsIlNOr8gm2UQtGh27Uhnug1qCyxYngcusQYwhR6cfqcU34+nfZZFaTFv9wKFRJ2ncy66wP4RsMAedIQpxFxW+mmUY2xa+bbdtbaUwq6v2Tv69/5R9oLpY9p9g2ADJkPo5mKODwF5Sm2G0Dk2iv9FfvVBCUB5ZykeJKCoi8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715332279; c=relaxed/simple; bh=oy8zZZH1tqYq6Cm1un3+J+lAe8+qsVk75JbBU5aux74=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=XOkekPyA75ASsreLMwu++GV8dPB/5IX2TrfGUmp1LPFI0O7KXpc0/0TEnHQISl5GzopMmwByqu15Cmt+wKqc7f2O5LPSdNgJZEaJAgTw/er02G1VbLWB5vaKdVmU/8+hkg86B9WCdd6igH+zeOuSsj3PJLI7P02K6ghMNnl5/8k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=debian.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.218.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=debian.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ej1-f46.google.com with SMTP id a640c23a62f3a-a59b58fe083so416279766b.0; Fri, 10 May 2024 02:11:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715332276; x=1715937076; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=XR6Ovo4ZWCEwmB95aywDLbYo5COB1k5E4akUH/B0OJI=; b=Z5w9nbK5tbOb0ovsARhbVPzI8WEh9YadVpkBDgfuB+G505KNu6Nqb6dXOMQHRkI6hO 4zOGA8G5TpG8VOj2e6Yzxh6eOHootpFnzt5kFCS09y9Q7pDwPdk6LeggZJXVXQ+qfOZm dRf3eTcMXxW0MUrabVfDyEhPNdeZ/0dvJuuWVRcaWmpeLJy/gKGBEoifg7Haig8YRwys cVqaVCsIn2fg2zaI4whhD78BpASHmLbtsTCVvtqFdIEpiTGpop8xAmEUB2HhqnogP4Fi dsMh/AzX7g+aSJocAd08D14zi8zYwCPY7WhsSZQqc5NR9khU5+qa+KYMUK1pt6+YhkJK DCsQ== X-Forwarded-Encrypted: i=1; AJvYcCV5SrHLYe84n8jidTYp/qe9XYpIjC96kFVzx7InpW9tH79MbSPC8/FkVHISs9ZOzWELrNK5PV6JCAiqFg6G7eaKfHGYHJX+awTU9afw3FaOPbXZraJPMic7529FsX3AGM0g X-Gm-Message-State: AOJu0YwvoI/k7Gn874GJxgwVE57Rg3qgpf5gELR9Vw4czSAtAvI+Yn5v n2f5m9WnasagxACS7wr76R4r/EAibKXT1j+OCpgX7krg4yoTPuD0 X-Received: by 2002:a17:906:7853:b0:a59:cdc9:6fd9 with SMTP id a640c23a62f3a-a5a2d5750acmr117965166b.21.1715332275933; Fri, 10 May 2024 02:11:15 -0700 (PDT) Received: from gmail.com (fwdproxy-lla-006.fbsv.net. [2a03:2880:30ff:6::face:b00c]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a5a179c7deasm160048566b.103.2024.05.10.02.11.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 May 2024 02:11:15 -0700 (PDT) Date: Fri, 10 May 2024 02:11:13 -0700 From: Breno Leitao To: Sean Christopherson Cc: Paolo Bonzini , rbc@meta.com, paulmck@kernel.org, "open list:KERNEL VIRTUAL MACHINE (KVM)" , open list Subject: Re: [PATCH] KVM: Addressing a possible race in kvm_vcpu_on_spin: Message-ID: References: <20240509090146.146153-1-leitao@debian.org> 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: Hello Sean, On Thu, May 09, 2024 at 09:42:48AM -0700, Sean Christopherson wrote: > On Thu, May 09, 2024, Breno Leitao wrote: > > kvm_vcpu_set_in_spin_loop(me, true); > > /* > > * We boost the priority of a VCPU that is runnable but not > > @@ -4109,7 +4110,7 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode) > > > > yielded = kvm_vcpu_yield_to(vcpu); > > if (yielded > 0) { > > - kvm->last_boosted_vcpu = i; > > + WRITE_ONCE(kvm->last_boosted_vcpu, i); > > break; > > } else if (yielded < 0) { > > try--; > > Side topic #1: am I the only one that finds these loops unnecessarily hard to > read? No. :-) In fact, when I skimmed over the code, I though that maybe the code was not covering the vCPUs before last_boosted_vcpu in the array. Now that I am looking at it carefully, the code is using `pass` to track if the vCPU passed last_boosted_vcpu in the index. > Unless I'm misreading the code, it's really just an indirect way of looping > over all vCPUs, starting at last_boosted_vcpu+1 and the wrapping. > > IMO, reworking it to be like this is more straightforward: > > int nr_vcpus, start, i, idx, yielded; > struct kvm *kvm = me->kvm; > struct kvm_vcpu *vcpu; > int try = 3; > > nr_vcpus = atomic_read(&kvm->online_vcpus); > if (nr_vcpus < 2) > return; > > /* Pairs with the smp_wmb() in kvm_vm_ioctl_create_vcpu(). */ > smp_rmb(); Why do you need this now? Isn't the RCU read lock in xa_load() enough? > kvm_vcpu_set_in_spin_loop(me, true); > > start = READ_ONCE(kvm->last_boosted_vcpu) + 1; > for (i = 0; i < nr_vcpus; i++) { Why do you need to started at the last boosted vcpu? I.e, why not starting at 0 and skipping me->vcpu_idx and kvm->last_boosted_vcpu? > idx = (start + i) % nr_vcpus; > if (idx == me->vcpu_idx) > continue; > > vcpu = xa_load(&kvm->vcpu_array, idx); > if (!READ_ONCE(vcpu->ready)) > continue; > if (kvm_vcpu_is_blocking(vcpu) && !vcpu_dy_runnable(vcpu)) > continue; > > /* > * Treat the target vCPU as being in-kernel if it has a pending > * interrupt, as the vCPU trying to yield may be spinning > * waiting on IPI delivery, i.e. the target vCPU is in-kernel > * for the purposes of directed yield. > */ > if (READ_ONCE(vcpu->preempted) && yield_to_kernel_mode && > !kvm_arch_dy_has_pending_interrupt(vcpu) && > !kvm_arch_vcpu_preempted_in_kernel(vcpu)) > continue; > > if (!kvm_vcpu_eligible_for_directed_yield(vcpu)) > continue; > > yielded = kvm_vcpu_yield_to(vcpu); > if (yielded > 0) { > WRITE_ONCE(kvm->last_boosted_vcpu, i); > break; > } else if (yielded < 0 && !--try) { > break; > } > } > > kvm_vcpu_set_in_spin_loop(me, false); > > /* Ensure vcpu is not eligible during next spinloop */ > kvm_vcpu_set_dy_eligible(me, false); I didn't tested it, but I reviewed it, and it seems sane and way easier to read. I agree this code is easier to read, from someone that has little KVM background.