From nobody Fri Nov 15 03:18:09 2024 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=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1646227377687434.24729669726946; Wed, 2 Mar 2022 05:22:57 -0800 (PST) Received: from localhost ([::1]:51998 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nPOwS-0003PE-Ew for importer@patchew.org; Wed, 02 Mar 2022 08:22:56 -0500 Received: from eggs.gnu.org ([209.51.188.92]:57490) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nPOet-0003eX-Vq for qemu-devel@nongnu.org; Wed, 02 Mar 2022 08:04:49 -0500 Received: from [2607:f8b0:4864:20::42c] (port=36403 helo=mail-pf1-x42c.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nPOes-0004zX-EQ for qemu-devel@nongnu.org; Wed, 02 Mar 2022 08:04:47 -0500 Received: by mail-pf1-x42c.google.com with SMTP id z16so1918498pfh.3 for ; Wed, 02 Mar 2022 05:04:45 -0800 (PST) Received: from localhost.localdomain (2001-b011-e000-59d7-a02b-4f1b-c415-11a0.dynamic-ip6.hinet.net. [2001:b011:e000:59d7:a02b:4f1b:c415:11a0]) by smtp.gmail.com with ESMTPSA id l13-20020a056a00140d00b004e13da93eaasm21947964pfu.62.2022.03.02.05.04.42 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 02 Mar 2022 05:04:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=gV9Cvlo7ft4KghRJ8ZPgxvTZ4fIVH8yA6Q+ea3wVuzI=; b=hoLCZtxpvRqCQc1lggmmuOcsy7rscV7eWzbFit5SpckJgvBL3859Hvql0wGc/wQifT gHWliqynQ/d6w8OQQi4HYsebB0iw9eP6UaXBJi4PkGxQ8N89Xm7495U13Cffkn/69SnP ERSS9ue9Mb1kewmWQQePMrZjALNil2FgnbNNocIufVChlHK+VCIhBI1p344qNAEYInop vJNLEgNQFKCN8rwv0WXZQUDvHmEkCEhn3wOOet/Ouht0EJhIvCUtTF1DXoE9oa8J61ko GOqjEdXSI7ijk6ctCfjvn2unzU9ovGCexwNDOalKVKDd7i2PZUif/6T7MhpYfA1ziiph Wzyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gV9Cvlo7ft4KghRJ8ZPgxvTZ4fIVH8yA6Q+ea3wVuzI=; b=bvCiQ9SWw2Iq+HKyWl3ilhBJ55gQK0pNkb6bXDuX8HtPORjaydH8Y1UsdPMFso1Ktl wKHgjCsQTkcKKP5sruhVYd18G21XOdig6fnKmWjt55RdbmsdTGSsdLWg/jglYrTqkOsy ci5sblCq8WMJ365+cwTzE0vlVMWtbhxgnv9v34siGJejJVrmydSYuU+JrrqdVlE2xLps 2LhNNsrGaOeKjlw1jwxrOk27InsqnT2482fnDUsnYsWsn3FpZBI/4s01WKAiGWg0xPus ESL/apRNMH/ZjUhIPBRFyrrfIceUypT8hCQO4bbYoyIGKNP1HrCibmBzh3RLXrxg3+SS sN/Q== X-Gm-Message-State: AOAM530GQ3R/A9vpn7xp/VwWA5zQB51X+velEUbkJumG+N9a1OU8HOEb 3ihs4ryNZFoQNptS9iFDsCaNOiVKJt6jZQ== X-Google-Smtp-Source: ABdhPJx4uBsLIPWwG5IX9pRwq5fYlszspY/vY5MaIIM5PuKp/CK53dhC4MK2SJu5tlBYATqc/Ha2EA== X-Received: by 2002:a05:6a00:b92:b0:4f1:4b2:737f with SMTP id g18-20020a056a000b9200b004f104b2737fmr33070701pfj.31.1646226284529; Wed, 02 Mar 2022 05:04:44 -0800 (PST) From: Yan-Jie Wang To: qemu-devel@nongnu.org Subject: [PATCH v3 7/9] hvf: use GTree to store memory slots instead of fixed-size array Date: Wed, 2 Mar 2022 21:04:15 +0800 Message-Id: <20220302130417.18551-8-ubzeme@gmail.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20220302130417.18551-1-ubzeme@gmail.com> References: <20220302130417.18551-1-ubzeme@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::42c (failed) 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=2607:f8b0:4864:20::42c; envelope-from=ubzeme@gmail.com; helo=mail-pf1-x42c.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no 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: , Cc: Peter Maydell , Roman Bolshakov , Alexander Graf , Cameron Esfahani , Yan-Jie Wang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1646227378451100001 Content-Type: text/plain; charset="utf-8" Currently, there are only 32 memory slots in the fixed size array. It is not scalable. Instead of using fixed size array, use GTree (from glib library) and dynamically-allocated structures to store memory slots. Signed-off-by: Yan-Jie Wang --- accel/hvf/hvf-mem.c | 63 +++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/accel/hvf/hvf-mem.c b/accel/hvf/hvf-mem.c index 081029ba98..2f70ceb307 100644 --- a/accel/hvf/hvf-mem.c +++ b/accel/hvf/hvf-mem.c @@ -28,8 +28,6 @@ =20 /* Memory slots */ =20 -#define HVF_NUM_SLOTS 32 - /* HVFSlot flags */ #define HVF_SLOT_LOG (1 << 0) #define HVF_SLOT_READONLY (1 << 1) @@ -42,35 +40,24 @@ typedef struct HVFSlot { MemoryRegion *region; } HVFSlot; =20 -static HVFSlot memslots[HVF_NUM_SLOTS]; +static GTree *memslots; static QemuMutex memlock; =20 static HVFSlot *hvf_find_overlap_slot(hwaddr start, hwaddr size) { - HVFSlot *slot; - int x; - for (x =3D 0; x < HVF_NUM_SLOTS; ++x) { - slot =3D &memslots[x]; - if (slot->size && start < (slot->start + slot->size) && - (start + size) > slot->start) { - return slot; - } - } - return NULL; + HVFSlot key =3D {.start =3D start, .size =3D 1}; + return g_tree_lookup(memslots, &key); } =20 -static HVFSlot *hvf_find_free_slot(void) +static void hvf_insert_slot(HVFSlot *slot) { - HVFSlot *slot; - int x; - for (x =3D 0; x < HVF_NUM_SLOTS; x++) { - slot =3D &memslots[x]; - if (!slot->size) { - return slot; - } - } + g_tree_insert(memslots, slot, slot); +} =20 - return NULL; +static bool hvf_remove_slot(hwaddr start) +{ + HVFSlot key =3D {.start =3D start, .size =3D 1}; + return g_tree_remove(memslots, &key); } =20 /* @@ -141,9 +128,7 @@ static void hvf_set_phys_mem(MemoryRegionSection *secti= on, bool add) readonly =3D memory_region_is_rom(area) || memory_region_is_romd(a= rea); =20 /* setup a slot */ - qemu_mutex_lock(&memlock); - - slot =3D hvf_find_free_slot(); + slot =3D g_new0(HVFSlot, 1); if (!slot) { error_report("No free slots"); abort(); @@ -170,6 +155,10 @@ static void hvf_set_phys_mem(MemoryRegionSection *sect= ion, bool add) flags =3D HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC; } =20 + qemu_mutex_lock(&memlock); + + hvf_insert_slot(slot); + ret =3D hv_vm_map(host_addr, start, size, flags); assert_hvf_ok(ret); =20 @@ -178,13 +167,9 @@ static void hvf_set_phys_mem(MemoryRegionSection *sect= ion, bool add) /* remove memory region */ qemu_mutex_lock(&memlock); =20 - slot =3D hvf_find_overlap_slot(start, size); - - if (slot) { + if (hvf_remove_slot(start)) { ret =3D hv_vm_unmap(start, size); assert_hvf_ok(ret); - - slot->size =3D 0; } =20 qemu_mutex_unlock(&memlock); @@ -310,8 +295,24 @@ bool hvf_access_memory(hwaddr address, bool write) return true; } =20 +/* compare function for GTree */ +static gint _hvf_slot_compare(gconstpointer a, gconstpointer b, gpointer d= ata) +{ + const HVFSlot *m1 =3D (const HVFSlot *)a; + const HVFSlot *m2 =3D (const HVFSlot *)b; + + if (m2->start >=3D m1->start + m1->size) { + return -1; + } else if (m1->start >=3D m2->start + m2->size) { + return 1; + } + + return 0; +} + void hvf_init_memslots(void) { qemu_mutex_init(&memlock); + memslots =3D g_tree_new_full(_hvf_slot_compare, NULL, g_free, NULL); memory_listener_register(&hvf_memory_listener, &address_space_memory); } --=20 2.32.0 (Apple Git-132)