From nobody Mon Feb 9 23:59:56 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1713885206; cv=none; d=zohomail.com; s=zohoarc; b=DmbMGHZHt+j+qnPlRZSa+RwWc11RJ4cNAZu1c/q54nfqhH6BeSOcSLiFbNuumtQMRur2jIXi1S+RkHGuncJniYjwvUGadqhF/m+i1Eo7MrrpTJ0+6Ri4tuHoOKrFXXXi25+su0eP6oopRbdDvn2ne4/Sgc7kHmGeQFjrplxecD0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1713885206; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=cP+1P7Y6S/eXga8mEPV8i4pJLBRlFOrcvRvOHfVnpVY=; b=PRdXbsrfSBHWjsXoRENYFIcVUCMNzEfpQSdT6TscE1+BRw2JCvptKT7GpaqzFb6p5XrFZtiu9tP2E4LZuk/tbuqZiLEuuy6jn7qtbm4S6GFT3AEYxjEuUSfnYQkeqQZQ4gGUpyr7Vb19HwguT2AJ1OP4GjjKndoP5PcnRAchg7Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1713885206424185.77455454301014; Tue, 23 Apr 2024 08:13:26 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rzHnk-0007kL-Dr; Tue, 23 Apr 2024 11:11:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rzHnD-0004l6-5x for qemu-devel@nongnu.org; Tue, 23 Apr 2024 11:10:47 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rzHn9-0000IY-4I for qemu-devel@nongnu.org; Tue, 23 Apr 2024 11:10:46 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-47-QoWoTMGfOny-cYFJts8VTQ-1; Tue, 23 Apr 2024 11:10:40 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7C204812C50; Tue, 23 Apr 2024 15:10:40 +0000 (UTC) Received: from avogadro.lan (unknown [10.39.192.70]) by smtp.corp.redhat.com (Postfix) with ESMTP id EA1B620128F3; Tue, 23 Apr 2024 15:10:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713885042; 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=cP+1P7Y6S/eXga8mEPV8i4pJLBRlFOrcvRvOHfVnpVY=; b=Pt0kd6Isx5dj6fp5vLIDBRSdHSW1Swz2tpaG5g5eaBoY9JeQXQ1j7rxqLqFN/t92noiTgx OiEAofTkAqA0IxDgqbYhyM/kDa62YyIHNz4rMeCo0sqx6BU5edyHDCmzY8xkYRCGp4frih F8f2fTX1+i4QcIRWLodC4RwW5cLR5BM= X-MC-Unique: QoWoTMGfOny-cYFJts8VTQ-1 From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Xiaoyao Li Subject: [PULL 48/63] kvm: Introduce support for memory_attributes Date: Tue, 23 Apr 2024 17:09:36 +0200 Message-ID: <20240423150951.41600-49-pbonzini@redhat.com> In-Reply-To: <20240423150951.41600-1-pbonzini@redhat.com> References: <20240423150951.41600-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.67, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1713885208313100004 Content-Type: text/plain; charset="utf-8" From: Xiaoyao Li Introduce the helper functions to set the attributes of a range of memory to private or shared. This is necessary to notify KVM the private/shared attribute of each gpa range. KVM needs the information to decide the GPA needs to be mapped at hva-based shared memory or guest_memfd based private memory. Signed-off-by: Xiaoyao Li Message-ID: <20240320083945.991426-11-michael.roth@amd.com> Signed-off-by: Paolo Bonzini --- include/sysemu/kvm.h | 4 ++++ accel/kvm/kvm-all.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h index bd247f3a239..594ae9b4605 100644 --- a/include/sysemu/kvm.h +++ b/include/sysemu/kvm.h @@ -536,4 +536,8 @@ void kvm_mark_guest_state_protected(void); * reported for the VM. */ bool kvm_hwpoisoned_mem(void); + +int kvm_set_memory_attributes_private(hwaddr start, uint64_t size); +int kvm_set_memory_attributes_shared(hwaddr start, uint64_t size); + #endif diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index ed50c80b1e4..db0b1a16edd 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -91,6 +91,7 @@ bool kvm_msi_use_devid; static bool kvm_has_guest_debug; static int kvm_sstep_flags; static bool kvm_immediate_exit; +static uint64_t kvm_supported_memory_attributes; static hwaddr kvm_max_slot_size =3D ~0; =20 static const KVMCapabilityInfo kvm_required_capabilites[] =3D { @@ -1266,6 +1267,36 @@ void kvm_set_max_memslot_size(hwaddr max_slot_size) kvm_max_slot_size =3D max_slot_size; } =20 +static int kvm_set_memory_attributes(hwaddr start, uint64_t size, uint64_t= attr) +{ + struct kvm_memory_attributes attrs; + int r; + + assert((attr & kvm_supported_memory_attributes) =3D=3D attr); + attrs.attributes =3D attr; + attrs.address =3D start; + attrs.size =3D size; + attrs.flags =3D 0; + + r =3D kvm_vm_ioctl(kvm_state, KVM_SET_MEMORY_ATTRIBUTES, &attrs); + if (r) { + error_report("failed to set memory (0x%" HWADDR_PRIx "+0x%" PRIx64= ") " + "with attr 0x%" PRIx64 " error '%s'", + start, size, attr, strerror(errno)); + } + return r; +} + +int kvm_set_memory_attributes_private(hwaddr start, uint64_t size) +{ + return kvm_set_memory_attributes(start, size, KVM_MEMORY_ATTRIBUTE_PRI= VATE); +} + +int kvm_set_memory_attributes_shared(hwaddr start, uint64_t size) +{ + return kvm_set_memory_attributes(start, size, 0); +} + /* Called with KVMMemoryListener.slots_lock held */ static void kvm_set_phys_mem(KVMMemoryListener *kml, MemoryRegionSection *section, bool add) @@ -2387,6 +2418,7 @@ static int kvm_init(MachineState *ms) goto err; } =20 + kvm_supported_memory_attributes =3D kvm_check_extension(s, KVM_CAP_MEM= ORY_ATTRIBUTES); kvm_immediate_exit =3D kvm_check_extension(s, KVM_CAP_IMMEDIATE_EXIT); s->nr_slots =3D kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS); =20 --=20 2.44.0