From nobody Thu Sep 19 01:04:11 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 8B84C14F9FD for ; Fri, 26 Jul 2024 18:52:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722019936; cv=none; b=NM7/jlZys7Mg0foohuERe7XTwjTQIAorLfYjcRT+cvobfaTOUiCQJp1TYwENG4IoLdppZPEr7JhA0IcS21GeGaeB16kKS5f46BORApxD9vltP4WYUTAhUcQDeMUw0sfijwiEYkmdLNNLiTTvHOJspLs4C58MB1GgohAa6T7uO84= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722019936; c=relaxed/simple; bh=tRmOfP51wV+9jZPLASWdFle6gQpf383/WN4M1AOGJtY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=QyfjpWwvucpivPKQ9EiWX0EwGAvT4Zt7WNARI+CfkCyE9ljjkqFbqo1025U1joh2KkL5BK+uZ1tneZ4jZ5RX+vhq+UsqO+A5ohwF+dzndD5RYyTg7JztqggzXciJ3zA8FRbZrAxgTAP6loRXWrMjhXPlnW+brlaNIM43RtXraOg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=K+RioNmS; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="K+RioNmS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722019933; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=g+8+16JprI1cjYByMUu0g/0uxoZ4686g4X0q/5qRlUc=; b=K+RioNmSRBUxfZnazHrJ7p5Z6PgvtrB7yh/gS2gvuobczps7hwF8tXMbbGIrKbAUBujufd 6QXDV9wCBDb3JJrV5Xn+R+Z+Uxtw5ZGJGVcZiKTPbMjZKifi4EyHUAF4W8bOrRBb7mXl5u tGPtLGQANsFysxizbXdg1rHbS9FKecI= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-303-91BFzPAdN0uohizw7eeM_Q-1; Fri, 26 Jul 2024 14:52:10 -0400 X-MC-Unique: 91BFzPAdN0uohizw7eeM_Q-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C42FA195609F; Fri, 26 Jul 2024 18:52:08 +0000 (UTC) Received: from virtlab1023.lab.eng.rdu2.redhat.com (virtlab1023.lab.eng.rdu2.redhat.com [10.8.1.187]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id F37401955D42; Fri, 26 Jul 2024 18:52:07 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, michael.roth@amd.com Subject: [PATCH v2 11/14] KVM: cleanup and add shortcuts to kvm_range_has_memory_attributes() Date: Fri, 26 Jul 2024 14:51:54 -0400 Message-ID: <20240726185157.72821-12-pbonzini@redhat.com> In-Reply-To: <20240726185157.72821-1-pbonzini@redhat.com> References: <20240726185157.72821-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 Content-Type: text/plain; charset="utf-8" Use a guard to simplify early returns, and add two more easy shortcuts. If the requested attributes are invalid, the attributes xarray will never show them as set. And if testing a single page, kvm_get_memory_attributes() is more efficient. Reviewed-by: Michael Roth Signed-off-by: Paolo Bonzini --- virt/kvm/kvm_main.c | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index d0788d0a72cc..30328ff2d840 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2398,6 +2398,14 @@ static int kvm_vm_ioctl_clear_dirty_log(struct kvm *= kvm, #endif /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */ =20 #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +static u64 kvm_supported_mem_attributes(struct kvm *kvm) +{ + if (!kvm || kvm_arch_has_private_mem(kvm)) + return KVM_MEMORY_ATTRIBUTE_PRIVATE; + + return 0; +} + /* * Returns true if _all_ gfns in the range [@start, @end) have attributes * matching @attrs. @@ -2406,40 +2414,30 @@ bool kvm_range_has_memory_attributes(struct kvm *kv= m, gfn_t start, gfn_t end, unsigned long attrs) { XA_STATE(xas, &kvm->mem_attr_array, start); + unsigned long mask =3D kvm_supported_mem_attributes(kvm); unsigned long index; - bool has_attrs; void *entry; =20 - rcu_read_lock(); + if (attrs & ~mask) + return false; =20 - if (!attrs) { - has_attrs =3D !xas_find(&xas, end - 1); - goto out; - } + if (end =3D=3D start + 1) + return kvm_get_memory_attributes(kvm, start) =3D=3D attrs; + + guard(rcu)(); + if (!attrs) + return !xas_find(&xas, end - 1); =20 - has_attrs =3D true; for (index =3D start; index < end; index++) { do { entry =3D xas_next(&xas); } while (xas_retry(&xas, entry)); =20 - if (xas.xa_index !=3D index || xa_to_value(entry) !=3D attrs) { - has_attrs =3D false; - break; - } + if (xas.xa_index !=3D index || xa_to_value(entry) !=3D attrs) + return false; } =20 -out: - rcu_read_unlock(); - return has_attrs; -} - -static u64 kvm_supported_mem_attributes(struct kvm *kvm) -{ - if (!kvm || kvm_arch_has_private_mem(kvm)) - return KVM_MEMORY_ATTRIBUTE_PRIVATE; - - return 0; + return true; } =20 static __always_inline void kvm_handle_gfn_range(struct kvm *kvm, --=20 2.43.0