From nobody Sat May 18 07:31:09 2024 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=1684866694; cv=none; d=zohomail.com; s=zohoarc; b=RE4h9PziJG8O1HRUckxjGPsgcMI2IWW/PIqDFE90FK/m+j3KvfJYj9wx0H6wyPXPXCq8FLQxwmxCS2eRu/xEqoAFGw875L8DazXI7DrRCaSwLTrS+EUT6IsSasni4+wc3GqZfX+6UcfSkI7QIGkyD35Ss9jCeQLK8qjpA1R9y54= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1684866694; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=aA0QnbmjN4g92/LPipOAYjmvR6VPG5jzymsIknO8skc=; b=bpjMC9z2lGpH3W9wQbJh5E3/KlH7GEZckAS04K6bo2n15a0VBcy9q+KWoMNXpT4TfqESxml2ev1ZKam4Zlt0s3FpHRN9kxtSLKNDj/3bFE6Q43MC/M+oooZZfPWgXM6l1/rtLz7lTgn13Uf4ljUX7ZvyJtYRDeNHSFbboUciqZY= 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 1684866694857375.2001264733558; Tue, 23 May 2023 11:31:34 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q1WmX-00029b-3k; Tue, 23 May 2023 14:30:49 -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 1q1WmR-00027N-74 for qemu-devel@nongnu.org; Tue, 23 May 2023 14:30:47 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q1WmP-0004vr-0F for qemu-devel@nongnu.org; Tue, 23 May 2023 14:30:42 -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.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-518-SS9bH59IMVuhhE4pbcnvnA-1; Tue, 23 May 2023 14:30:38 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2EEF385A5AA for ; Tue, 23 May 2023 18:30:38 +0000 (UTC) Received: from t480s.fritz.box (unknown [10.39.194.191]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3D8DC1121315; Tue, 23 May 2023 18:30:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684866639; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=aA0QnbmjN4g92/LPipOAYjmvR6VPG5jzymsIknO8skc=; b=Bpw1zX8EjkOQj1SXRqpFcXqqPKG6cLT74pKAAFwYcyFid6HddCfx/xK25ZDiFfAREi70GW Rws1Zmja1hVwMjSFX8bsI39ubxz8yzuBecD8ER/IogO0RWRIBpWsKrCgM4r27UpwNORqu1 d6qGa2oYX8mMlCHt0lpcbEovoWQBVKU= X-MC-Unique: SS9bH59IMVuhhE4pbcnvnA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , "Michael S. Tsirkin" , Gavin Shan Subject: [PATCH v1] virtio-mem: Simplify bitmap handling and virtio_mem_set_block_state() Date: Tue, 23 May 2023 20:30:36 +0200 Message-Id: <20230523183036.517957-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 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=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1684866696002100001 Content-Type: text/plain; charset="utf-8" Let's separate plug and unplug handling to prepare for future changes and make the code a bit easier to read -- working on block states (plugged/unplugged) instead of on a bitmap. Cc: "Michael S. Tsirkin" Cc: Gavin Shan Signed-off-by: David Hildenbrand Reviewed-by: Michael S. Tsirkin --- hw/virtio/virtio-mem.c | 116 +++++++++++++++++++++++------------------ 1 file changed, 66 insertions(+), 50 deletions(-) diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index 538b695c29..e24269e745 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -399,33 +399,46 @@ static void virtio_mem_notify_unplug_all(VirtIOMEM *v= mem) } } =20 -static bool virtio_mem_test_bitmap(const VirtIOMEM *vmem, uint64_t start_g= pa, - uint64_t size, bool plugged) +static bool virtio_mem_is_range_plugged(const VirtIOMEM *vmem, + uint64_t start_gpa, uint64_t size) { const unsigned long first_bit =3D (start_gpa - vmem->addr) / vmem->blo= ck_size; const unsigned long last_bit =3D first_bit + (size / vmem->block_size)= - 1; unsigned long found_bit; =20 /* We fake a shorter bitmap to avoid searching too far. */ - if (plugged) { - found_bit =3D find_next_zero_bit(vmem->bitmap, last_bit + 1, first= _bit); - } else { - found_bit =3D find_next_bit(vmem->bitmap, last_bit + 1, first_bit); - } + found_bit =3D find_next_zero_bit(vmem->bitmap, last_bit + 1, first_bit= ); return found_bit > last_bit; } =20 -static void virtio_mem_set_bitmap(VirtIOMEM *vmem, uint64_t start_gpa, - uint64_t size, bool plugged) +static bool virtio_mem_is_range_unplugged(const VirtIOMEM *vmem, + uint64_t start_gpa, uint64_t siz= e) +{ + const unsigned long first_bit =3D (start_gpa - vmem->addr) / vmem->blo= ck_size; + const unsigned long last_bit =3D first_bit + (size / vmem->block_size)= - 1; + unsigned long found_bit; + + /* We fake a shorter bitmap to avoid searching too far. */ + found_bit =3D find_next_bit(vmem->bitmap, last_bit + 1, first_bit); + return found_bit > last_bit; +} + +static void virtio_mem_set_range_plugged(VirtIOMEM *vmem, uint64_t start_g= pa, + uint64_t size) { const unsigned long bit =3D (start_gpa - vmem->addr) / vmem->block_siz= e; const unsigned long nbits =3D size / vmem->block_size; =20 - if (plugged) { - bitmap_set(vmem->bitmap, bit, nbits); - } else { - bitmap_clear(vmem->bitmap, bit, nbits); - } + bitmap_set(vmem->bitmap, bit, nbits); +} + +static void virtio_mem_set_range_unplugged(VirtIOMEM *vmem, uint64_t start= _gpa, + uint64_t size) +{ + const unsigned long bit =3D (start_gpa - vmem->addr) / vmem->block_siz= e; + const unsigned long nbits =3D size / vmem->block_size; + + bitmap_clear(vmem->bitmap, bit, nbits); } =20 static void virtio_mem_send_response(VirtIOMEM *vmem, VirtQueueElement *el= em, @@ -475,6 +488,7 @@ static int virtio_mem_set_block_state(VirtIOMEM *vmem, = uint64_t start_gpa, { const uint64_t offset =3D start_gpa - vmem->addr; RAMBlock *rb =3D vmem->memdev->mr.ram_block; + int ret =3D 0; =20 if (virtio_mem_is_busy()) { return -EBUSY; @@ -485,42 +499,43 @@ static int virtio_mem_set_block_state(VirtIOMEM *vmem= , uint64_t start_gpa, return -EBUSY; } virtio_mem_notify_unplug(vmem, offset, size); - } else { - int ret =3D 0; - - if (vmem->prealloc) { - void *area =3D memory_region_get_ram_ptr(&vmem->memdev->mr) + = offset; - int fd =3D memory_region_get_fd(&vmem->memdev->mr); - Error *local_err =3D NULL; - - qemu_prealloc_mem(fd, area, size, 1, NULL, &local_err); - if (local_err) { - static bool warned; - - /* - * Warn only once, we don't want to fill the log with these - * warnings. - */ - if (!warned) { - warn_report_err(local_err); - warned =3D true; - } else { - error_free(local_err); - } - ret =3D -EBUSY; + virtio_mem_set_range_unplugged(vmem, start_gpa, size); + return 0; + } + + if (vmem->prealloc) { + void *area =3D memory_region_get_ram_ptr(&vmem->memdev->mr) + offs= et; + int fd =3D memory_region_get_fd(&vmem->memdev->mr); + Error *local_err =3D NULL; + + qemu_prealloc_mem(fd, area, size, 1, NULL, &local_err); + if (local_err) { + static bool warned; + + /* + * Warn only once, we don't want to fill the log with these + * warnings. + */ + if (!warned) { + warn_report_err(local_err); + warned =3D true; + } else { + error_free(local_err); } + ret =3D -EBUSY; } - if (!ret) { - ret =3D virtio_mem_notify_plug(vmem, offset, size); - } + } =20 - if (ret) { - /* Could be preallocation or a notifier populated memory. */ - ram_block_discard_range(vmem->memdev->mr.ram_block, offset, si= ze); - return -EBUSY; - } + if (!ret) { + ret =3D virtio_mem_notify_plug(vmem, offset, size); } - virtio_mem_set_bitmap(vmem, start_gpa, size, plug); + if (ret) { + /* Could be preallocation or a notifier populated memory. */ + ram_block_discard_range(vmem->memdev->mr.ram_block, offset, size); + return -EBUSY; + } + + virtio_mem_set_range_plugged(vmem, start_gpa, size); return 0; } =20 @@ -539,7 +554,8 @@ static int virtio_mem_state_change_request(VirtIOMEM *v= mem, uint64_t gpa, } =20 /* test if really all blocks are in the opposite state */ - if (!virtio_mem_test_bitmap(vmem, gpa, size, !plug)) { + if ((plug && !virtio_mem_is_range_unplugged(vmem, gpa, size)) || + (!plug && !virtio_mem_is_range_plugged(vmem, gpa, size))) { return VIRTIO_MEM_RESP_ERROR; } =20 @@ -652,9 +668,9 @@ static void virtio_mem_state_request(VirtIOMEM *vmem, V= irtQueueElement *elem, return; } =20 - if (virtio_mem_test_bitmap(vmem, gpa, size, true)) { + if (virtio_mem_is_range_plugged(vmem, gpa, size)) { resp.u.state.state =3D cpu_to_le16(VIRTIO_MEM_STATE_PLUGGED); - } else if (virtio_mem_test_bitmap(vmem, gpa, size, false)) { + } else if (virtio_mem_is_range_unplugged(vmem, gpa, size)) { resp.u.state.state =3D cpu_to_le16(VIRTIO_MEM_STATE_UNPLUGGED); } else { resp.u.state.state =3D cpu_to_le16(VIRTIO_MEM_STATE_MIXED); @@ -1373,7 +1389,7 @@ static bool virtio_mem_rdm_is_populated(const RamDisc= ardManager *rdm, return false; } =20 - return virtio_mem_test_bitmap(vmem, start_gpa, end_gpa - start_gpa, tr= ue); + return virtio_mem_is_range_plugged(vmem, start_gpa, end_gpa - start_gp= a); } =20 struct VirtIOMEMReplayData { --=20 2.40.1