From nobody Tue Dec 16 13:24:15 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 B7633266B4E for ; Thu, 8 May 2025 14:10:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746713425; cv=none; b=lBQlIWtaxPwznlIHyeHg3uFSPDZ2JmVb9H5gjqTY/iWY7vqzVwwWsOBa2jtbuZ0lS+0kG6fo6gc+SSHffxODqJf3+5Oxww72uH38TtCUAB+MxF0UZzE5mEHMC2y3C/q5QWb9WmzjxJ9ZSH54mJJOPhA0/gLuJSiP0LmFnEaISYo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746713425; c=relaxed/simple; bh=awSJS8WConudI9j96iI1tSZciOtqzk/WMv+Azjyuw5A=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jiVzEBnXGKSiG9TJYlfANUfcWkH6J0YNnrzhSVpspOtukdnm+8EvgHASnn1/4P/5Z4FykEf1Ii89GyRAsTptNTz2f8UviUNRJMVuihQIXf3KvHQ1cdU8ISfGbJW34oHCD7mKBOTMuT3QKtEMPsBUufkgH6DtRL+qEB3UJOUZlBs= 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=JkkAsPuL; arc=none smtp.client-ip=209.85.210.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="JkkAsPuL" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-73bfc657aefso852733b3a.1 for ; Thu, 08 May 2025 07:10:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746713423; x=1747318223; 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=ickIymo1Jt+rHn8phqijkm5gyPMPS3XGUhpL5W5szxc=; b=JkkAsPuLfXVqfoTGbXl996mYyAQa5A2IVn1pgWLhb3oB3Of9WQXA5TOJV8oO42N4G3 EGQzQB2SCUstHYbetR0vHko+tauX4E3RHMO2G3aQGYQIvoRmanHZY5nmAcxMusFVQEIJ Z7TIYSVOH8GrHzAUXpjKles2P2v8mLOjRP/ZikR0iUnN0Lw0yhjpjHPaGeqP3NB7GULL u/A7pZ0h6nxRBcGoBfY7DmLbB5oK38f3PXOqRHcio3LNgAf7F5oDY3BP37gtza+0wU8N I29evWZCKHLnaHx1rcRXaHA0AksnonIv8j134C4r/TCZXbIXay1n9xN6RaRrCUNAjNZv Poog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746713423; x=1747318223; 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=ickIymo1Jt+rHn8phqijkm5gyPMPS3XGUhpL5W5szxc=; b=SFpFVU6uwelmCnCOGozOq5Lr/nVkGSl7/wqLUi985EoVMA4fsVxM0Y2lU3b90yIE0o HBpnHJplP0NFr4gGONwnPIwvgTKMs72WIpgDOOilTKaYHcdg+PbSlQUT/uzWYaQ3gVlo nCC6ZWZm1CPRnIYBrJ017vrw2FkJLXsxPGpHpvCTQuARNLx49T1XpnD99sWRwPE+VwvI FArOSEFAE63a9IccKkJsuGLORo3Udm/nkxarWA+lvpgKlpcFl9xrE9qq/OA/8Ld0RkYP t9rn2kV9/Jyf0XAN9Tv4zh+Rn36QdhpeZ+shfs72SDDZ4DXaRVAdsRagl2kXT0eQxciB ykbA== X-Forwarded-Encrypted: i=1; AJvYcCXxurjaLlQpUOHYi6gnPzWc4EV2upoIvp5VrLct9CdCkyEJaCjVeefdE2Y4OdKKd+kDZKN4FDl7YB+OEq0=@vger.kernel.org X-Gm-Message-State: AOJu0YzujxjMjThwhENwFMyzAyecHF+AxbapLwbKjIw9s+AHYfyNtf8I a0skK/Pg8q/W9FrHPY3BQSR7pUTBT63/8JAmslgGA2VsFuz1BFSoZZV55YY1mc4nUabHQMgGwQV rFA== X-Google-Smtp-Source: AGHT+IHLiymXHmxCwlVp1XiwMzwsV4UPdVj2tb8haL51LmOJpuCCiwihi6XjLonfWdsyG+RYO6/C9IKGO2I= X-Received: from pfbgs18.prod.google.com ([2002:a05:6a00:4d92:b0:739:45ba:a49a]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:35cd:b0:732:2923:b70f with SMTP id d2e1a72fcca58-740a99c4bd6mr4703644b3a.11.1746713422488; Thu, 08 May 2025 07:10:22 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 8 May 2025 07:10:11 -0700 In-Reply-To: <20250508141012.1411952-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: <20250508141012.1411952-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1015.ga840276032-goog Message-ID: <20250508141012.1411952-5-seanjc@google.com> Subject: [PATCH v2 4/5] KVM: Check for empty mask of harvested dirty ring entries in caller From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Peter Xu , Yan Zhao , Maxim Levitsky , Sean Christopherson Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When resetting a dirty ring, explicitly check that there is work to be done before calling kvm_reset_dirty_gfn(), e.g. if no harvested entries are found and/or on the loop's first iteration, and delete the extremely misleading comment "This is only needed to make compilers happy". KVM absolutely relies on mask to be zero-initialized, i.e. the comment is an outright lie. Furthermore, the compiler is right to complain that KVM is calling a function with uninitialized data, as there are no guarantees the implementation details of kvm_reset_dirty_gfn() will be visible to kvm_dirty_ring_reset(). While the flaw could be fixed by simply deleting (or rewording) the comment, and duplicating the check is unfortunate, checking mask in the caller will allow for additional cleanups. Opportunisticaly drop the zero-initialization of cur_slot and cur_offset. If a bug were introduced where either the slot or offset was consumed before mask is set to a non-zero value, then it is highly desirable for the compiler (or some other sanitizer) to yell. Cc: Peter Xu Cc: Yan Zhao Cc: Maxim Levitsky Signed-off-by: Sean Christopherson Reviewed-by: Pankaj Gupta --- virt/kvm/dirty_ring.c | 44 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/virt/kvm/dirty_ring.c b/virt/kvm/dirty_ring.c index 97cca0c02fd1..a3434be8f00d 100644 --- a/virt/kvm/dirty_ring.c +++ b/virt/kvm/dirty_ring.c @@ -55,9 +55,6 @@ static void kvm_reset_dirty_gfn(struct kvm *kvm, u32 slot= , u64 offset, u64 mask) struct kvm_memory_slot *memslot; int as_id, id; =20 - if (!mask) - return; - as_id =3D slot >> 16; id =3D (u16)slot; =20 @@ -108,15 +105,24 @@ static inline bool kvm_dirty_gfn_harvested(struct kvm= _dirty_gfn *gfn) int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring, int *nr_entries_reset) { + /* + * To minimize mmu_lock contention, batch resets for harvested entries + * whose gfns are in the same slot, and are within N frame numbers of + * each other, where N is the number of bits in an unsigned long. For + * simplicity, process the current set of entries when the next entry + * can't be included in the batch. + * + * Track the current batch slot, the gfn offset into the slot for the + * batch, and the bitmask of gfns that need to be reset (relative to + * offset). Note, the offset may be adjusted backwards, e.g. so that + * a sequence of gfns X, X-1, ... X-N can be batched. + */ u32 cur_slot, next_slot; u64 cur_offset, next_offset; - unsigned long mask; + unsigned long mask =3D 0; struct kvm_dirty_gfn *entry; bool first_round =3D true; =20 - /* This is only needed to make compilers happy */ - cur_slot =3D cur_offset =3D mask =3D 0; - while (likely((*nr_entries_reset) < INT_MAX)) { if (signal_pending(current)) return -EINTR; @@ -164,14 +170,34 @@ int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_= dirty_ring *ring, continue; } } - kvm_reset_dirty_gfn(kvm, cur_slot, cur_offset, mask); + + /* + * Reset the slot for all the harvested entries that have been + * gathered, but not yet fully processed. + */ + if (mask) + kvm_reset_dirty_gfn(kvm, cur_slot, cur_offset, mask); + + /* + * The current slot was reset or this is the first harvested + * entry, (re)initialize the metadata. + */ cur_slot =3D next_slot; cur_offset =3D next_offset; mask =3D 1; first_round =3D false; } =20 - kvm_reset_dirty_gfn(kvm, cur_slot, cur_offset, mask); + /* + * Perform a final reset if there are harvested entries that haven't + * been processed, which is guaranteed if at least one harvested was + * found. The loop only performs a reset when the "next" entry can't + * be batched with "current" the entry(s), and that reset processes the + * _current_ entry(s), i.e. the last harvested entry, a.k.a. next, will + * always be left pending. + */ + if (mask) + kvm_reset_dirty_gfn(kvm, cur_slot, cur_offset, mask); =20 /* * The request KVM_REQ_DIRTY_RING_SOFT_FULL will be cleared --=20 2.49.0.1015.ga840276032-goog