From nobody Mon Dec 15 02:55:01 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1615408761; cv=none; d=zohomail.com; s=zohoarc; b=nDShwUlOSm0VdOiqqtYQV7h9XqT3THMLYv060gtWEuscSPgGk8gaMkGNQ8IM/zuUw6MA4HBdBhnfa5oPdA/2oLVImS2Dh9u3C5QvmzR1tQ1Q5kSMpyI9/h9fHgmsr79Cjrp6g5d87naPzNZvg+YfYgwZkF6EqOip//srGzNj7/I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1615408761; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=FYTm0Ygz3+9TFDUpsIdZ9xetLL5/rO9MVJVwiaBlDfA=; b=A1JFiBN5I3b/URNVWtQOQIGTXQqgd2A/ts4grekThbYtJXL+9lnuConuydnth7PZSL4x1piFnNVjOLaUP/sb/OOZaky30jDk/PI9nGZ/jzj8MA+dCSe7aR77ilyhmVeq+vYCtwMl/i7konBywmhxqiXorFM1X5XzJxvwU/2C/2A= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 16154087614841017.1653137926455; Wed, 10 Mar 2021 12:39:21 -0800 (PST) Received: from localhost ([::1]:47676 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lK5c0-0002e9-Dj for importer@patchew.org; Wed, 10 Mar 2021 15:39:20 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:36148) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lK5W7-0005vi-FV for qemu-devel@nongnu.org; Wed, 10 Mar 2021 15:33:15 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:31744) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lK5W2-0000d2-F8 for qemu-devel@nongnu.org; Wed, 10 Mar 2021 15:33:15 -0500 Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-108-D-NIIMWhNt-gNh5vB3y7mw-1; Wed, 10 Mar 2021 15:33:08 -0500 Received: by mail-qk1-f197.google.com with SMTP id k185so13700960qkb.17 for ; Wed, 10 Mar 2021 12:33:08 -0800 (PST) Received: from xz-x1.redhat.com ([142.126.89.138]) by smtp.gmail.com with ESMTPSA id z89sm306746qtd.5.2021.03.10.12.33.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Mar 2021 12:33:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1615408389; 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=FYTm0Ygz3+9TFDUpsIdZ9xetLL5/rO9MVJVwiaBlDfA=; b=NBrKPqjJgbC+/HUo0hfiV7oVUWFnO4poQYu8sfSL9+kvY6WJs562BBd+1VxK8GB90fKi71 GLU7n7sJn0CRDbyrNcYH5v5xoJ2utT0hOuzBGBYFEoOcJlv8NR5PGigkkEKEpLwGAamf3J MjQhkYpJ0bX/VJR4J0lSG1Yz1S1DX4Y= X-MC-Unique: D-NIIMWhNt-gNh5vB3y7mw-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=FYTm0Ygz3+9TFDUpsIdZ9xetLL5/rO9MVJVwiaBlDfA=; b=li3crto/0D7Y1UFTOfqQkbrycNR9jLXwXq1xmn/lEF8zpCufhHKxhzOO8ypg25a4TC QiRCWrypnW3/uS2h9mJ6894H8pBnkrfDYVc3kH7hOJ60Vjjxw6BuhfkBgHjWcQ9GddpX on2luSgYX8q8YyF8mhgSOlhTMwfcKYTdL8nUWh3dmP3AQH9aRgWCI5dbi/C6l773X3jp SGJkWcZ1D9jZ5aPoo4HDj6UXKiLLkO6iuBjS9HoPxUsWiBSMvcZqfBix3jSJL958C647 +Haj3sTSp4/VD1EKImR42DuMlemFqPcCtLJLPow15HUXjdm57XlULJ/xRAJKoy8XWwoQ 0gNA== X-Gm-Message-State: AOAM530o3GI+xHJVRl77BNeo1d8gt0cBGSrTo0R44lfBEgieXj7c/4jg jrltKgkKtAAAf/Yf4i8/7JQ7Fqg7pzLfrwUandzvIfDI7RRWlw82K/s0+wqQtqVLyJflMNCBMTZ EKoDc4p8c+IO90BHJv/0+x+MVg70oUyO/Ui5MZVCENqdN0ZOVuP1o+vLeXlZmQ/0T X-Received: by 2002:ac8:cc5:: with SMTP id o5mr4609603qti.0.1615408387764; Wed, 10 Mar 2021 12:33:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJxZsKyFq64L2fGAfQP0hq3M08rN5MaSCE18JJ89rNQMd3wvrLBASjrUodU48X1fWFyj4drs4A== X-Received: by 2002:ac8:cc5:: with SMTP id o5mr4609585qti.0.1615408387448; Wed, 10 Mar 2021 12:33:07 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH v5 04/10] KVM: Provide helper to get kvm dirty log Date: Wed, 10 Mar 2021 15:32:55 -0500 Message-Id: <20210310203301.194842-5-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210310203301.194842-1-peterx@redhat.com> References: <20210310203301.194842-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable 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.133.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.243, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Hyman , "Dr . David Alan Gilbert" , peterx@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Provide a helper kvm_slot_get_dirty_log() to make the function kvm_physical_sync_dirty_bitmap() clearer. We can even cache the as_id into KVMSlot when it is created, so that we don't even need to pass it down every time. Since at it, remove return value of kvm_physical_sync_dirty_bitmap() because it should never fail. Signed-off-by: Peter Xu --- accel/kvm/kvm-all.c | 52 +++++++++++++++++++++++----------------- include/sysemu/kvm_int.h | 2 ++ 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index fa337418636..853dfb076bd 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -617,6 +617,30 @@ static void kvm_slot_init_dirty_bitmap(KVMSlot *mem) mem->dirty_bmap =3D g_malloc0(bitmap_size); } =20 +/* + * Sync dirty bitmap from kernel to KVMSlot.dirty_bmap, return true if + * succeeded, false otherwise + */ +static bool kvm_slot_get_dirty_log(KVMState *s, KVMSlot *slot) +{ + struct kvm_dirty_log d =3D {}; + int ret; + + d.dirty_bitmap =3D slot->dirty_bmap; + d.slot =3D slot->slot | (slot->as_id << 16); + ret =3D kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d); + + if (ret =3D=3D -ENOENT) { + /* kernel does not have dirty bitmap in this slot */ + ret =3D 0; + } + if (ret) { + error_report_once("%s: KVM_GET_DIRTY_LOG failed with %d", + __func__, ret); + } + return ret =3D=3D 0; +} + /** * kvm_physical_sync_dirty_bitmap - Sync dirty bitmap from kernel space * @@ -628,15 +652,13 @@ static void kvm_slot_init_dirty_bitmap(KVMSlot *mem) * @kml: the KVM memory listener object * @section: the memory section to sync the dirty bitmap with */ -static int kvm_physical_sync_dirty_bitmap(KVMMemoryListener *kml, - MemoryRegionSection *section) +static void kvm_physical_sync_dirty_bitmap(KVMMemoryListener *kml, + MemoryRegionSection *section) { KVMState *s =3D kvm_state; - struct kvm_dirty_log d =3D {}; KVMSlot *mem; hwaddr start_addr, size; hwaddr slot_size, slot_offset =3D 0; - int ret =3D 0; =20 size =3D kvm_align_section(section, &start_addr); while (size) { @@ -646,19 +668,10 @@ static int kvm_physical_sync_dirty_bitmap(KVMMemoryLi= stener *kml, mem =3D kvm_lookup_matching_slot(kml, start_addr, slot_size); if (!mem) { /* We don't have a slot if we want to trap every access. */ - goto out; + return; } =20 - d.dirty_bitmap =3D mem->dirty_bmap; - d.slot =3D mem->slot | (kml->as_id << 16); - ret =3D kvm_vm_ioctl(s, KVM_GET_DIRTY_LOG, &d); - if (ret =3D=3D -ENOENT) { - /* kernel does not have dirty bitmap in this slot */ - ret =3D 0; - } else if (ret < 0) { - error_report("ioctl KVM_GET_DIRTY_LOG failed: %d", errno); - goto out; - } else { + if (kvm_slot_get_dirty_log(s, mem)) { subsection.offset_within_region +=3D slot_offset; subsection.size =3D int128_make64(slot_size); kvm_get_dirty_pages_log_range(&subsection, d.dirty_bitmap); @@ -668,8 +681,6 @@ static int kvm_physical_sync_dirty_bitmap(KVMMemoryList= ener *kml, start_addr +=3D slot_size; size -=3D slot_size; } -out: - return ret; } =20 /* Alignment requirement for KVM_CLEAR_DIRTY_LOG - 64 pages */ @@ -1188,6 +1199,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, do { slot_size =3D MIN(kvm_max_slot_size, size); mem =3D kvm_alloc_slot(kml); + mem->as_id =3D kml->as_id; mem->memory_size =3D slot_size; mem->start_addr =3D start_addr; mem->ram =3D ram; @@ -1230,14 +1242,10 @@ static void kvm_log_sync(MemoryListener *listener, MemoryRegionSection *section) { KVMMemoryListener *kml =3D container_of(listener, KVMMemoryListener, l= istener); - int r; =20 kvm_slots_lock(); - r =3D kvm_physical_sync_dirty_bitmap(kml, section); + kvm_physical_sync_dirty_bitmap(kml, section); kvm_slots_unlock(); - if (r < 0) { - abort(); - } } =20 static void kvm_log_clear(MemoryListener *listener, diff --git a/include/sysemu/kvm_int.h b/include/sysemu/kvm_int.h index 1da30e18841..e13075f738a 100644 --- a/include/sysemu/kvm_int.h +++ b/include/sysemu/kvm_int.h @@ -23,6 +23,8 @@ typedef struct KVMSlot int old_flags; /* Dirty bitmap cache for the slot */ unsigned long *dirty_bmap; + /* Cache of the address space ID */ + int as_id; } KVMSlot; =20 typedef struct KVMMemoryListener { --=20 2.26.2