From nobody Wed Nov 27 07:36:44 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=1699865272; cv=none; d=zohomail.com; s=zohoarc; b=SpE4Nbu6Tj7PiAarrb4qob4NR6Ew6ASlZ+WjQQJNPX/3jwqRlTPLR/PNxSBtrOXlj4rvcMkJBTL7ucqwOQ2QG1ivYWy8byIbi53u+ADhTsRxoLPLwcoAousCJJgcI4ogLC8IAcDyoDVna8qtN2/AjbY/sFh8pJjHrKV82rNfxwk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699865272; h=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=f3pnhiVn+oXshJaAfi0nBYu1HESNTh+w6UcqfG6KYW8=; b=Itjefpvn1ETf5Iy7E2hKI9Y1yJVOQENPjkaiKDdXZdLHyOP01Mv7ZHjlIb6WqxteygntHlJE2A5sGSc/WZ4A27CJOaTQS01R1Ui3jsvUtJLu/owS83u0FeYiH+ew2mNUpgcxRCtutvDtEpCDblEFBp6jGUeGNaUoQmq7EUjHLoA= 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 1699865272632745.6662429835254; Mon, 13 Nov 2023 00:47:52 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r2Sax-00036S-Lj; Mon, 13 Nov 2023 03:46:59 -0500 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 1r2Sap-00035n-6w for qemu-devel@nongnu.org; Mon, 13 Nov 2023 03:46:52 -0500 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 1r2San-0006in-Kj for qemu-devel@nongnu.org; Mon, 13 Nov 2023 03:46:50 -0500 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-564-qi_pdC7PMpyi4ojEENHFHw-1; Mon, 13 Nov 2023 03:46:44 -0500 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (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 714BD185A785; Mon, 13 Nov 2023 08:46:44 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.108]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8383D492BFA; Mon, 13 Nov 2023 08:46:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699865208; 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: in-reply-to:in-reply-to:references:references; bh=f3pnhiVn+oXshJaAfi0nBYu1HESNTh+w6UcqfG6KYW8=; b=O6oqbEuNHDUHiaYISuXgcQgHrlu0zXtVF1ose9RgXpyeRV4jI5CYcgSQgr5Osu+v0zbvzk 8dIJUPduJPa3b7ET9+e8OTE12GaZT05PIvh471RLIQjm0iVRuHOZEeHoqJwfj1Xfsab7Ts EsURTDoT5QiBgScTXc/fbxpMfinoHzM= X-MC-Unique: qi_pdC7PMpyi4ojEENHFHw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Mario Casquero , "Michael S . Tsirkin" , "Maciej S . Szmigiero" Subject: [GIT PULL 1/1] virtio-mem: fix division by zero in virtio_mem_activate_memslots_to_plug() Date: Mon, 13 Nov 2023 09:46:42 +0100 Message-ID: <20231113084642.150354-2-david@redhat.com> In-Reply-To: <20231113084642.150354-1-david@redhat.com> References: <20231113084642.150354-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 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_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, 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: 1699865274966100006 Content-Type: text/plain; charset="utf-8" When running with "dynamic-memslots=3Doff", we enter virtio_mem_activate_memslots_to_plug() to return immediately again because "vmem->dynamic_memslots =3D=3D false". However, the compiler might not optimize out calculating start_idx+end_idx, where we divide by vmem->memslot_size. In such a configuration, the memslot size is 0 and we'll get a division by zero: (qemu) qom-set vmem0 requested-size 3G (qemu) q35.sh: line 38: 622940 Floating point exception(core dumped) The same is true for virtio_mem_deactivate_unplugged_memslots(), however we never really reach that code without a prior virtio_mem_activate_memslots_to_plug() call. Let's fix it by simply calling these functions only with "dynamic-memslots=3Don". This was found when using a debug build of QEMU. Message-ID: <20231023111341.219317-1-david@redhat.com> Reprted-by: Mario Casquero Fixes: 177f9b1ee464 ("virtio-mem: Expose device memory dynamically via mult= iple memslots if enabled") Reviewed-by: Michael S. Tsirkin Reviewed-by: Maciej S. Szmigiero Tested-by: Mario Casquero Signed-off-by: David Hildenbrand --- hw/virtio/virtio-mem.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index a5ea3be414..75ee38aa46 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -525,9 +525,7 @@ static void virtio_mem_activate_memslots_to_plug(VirtIO= MEM *vmem, vmem->memslot_size; unsigned int idx; =20 - if (!vmem->dynamic_memslots) { - return; - } + assert(vmem->dynamic_memslots); =20 /* Activate all involved memslots in a single transaction. */ memory_region_transaction_begin(); @@ -547,9 +545,7 @@ static void virtio_mem_deactivate_unplugged_memslots(Vi= rtIOMEM *vmem, vmem->memslot_size; unsigned int idx; =20 - if (!vmem->dynamic_memslots) { - return; - } + assert(vmem->dynamic_memslots); =20 /* Deactivate all memslots with unplugged blocks in a single transacti= on. */ memory_region_transaction_begin(); @@ -598,7 +594,9 @@ static int virtio_mem_set_block_state(VirtIOMEM *vmem, = uint64_t start_gpa, virtio_mem_notify_unplug(vmem, offset, size); virtio_mem_set_range_unplugged(vmem, start_gpa, size); /* Deactivate completely unplugged memslots after updating the sta= te. */ - virtio_mem_deactivate_unplugged_memslots(vmem, offset, size); + if (vmem->dynamic_memslots) { + virtio_mem_deactivate_unplugged_memslots(vmem, offset, size); + } return 0; } =20 @@ -635,9 +633,11 @@ static int virtio_mem_set_block_state(VirtIOMEM *vmem,= uint64_t start_gpa, * blocks we are plugging here. The following notification will in= form * registered listeners about the blocks we're plugging. */ - virtio_mem_activate_memslots_to_plug(vmem, offset, size); + if (vmem->dynamic_memslots) { + virtio_mem_activate_memslots_to_plug(vmem, offset, size); + } ret =3D virtio_mem_notify_plug(vmem, offset, size); - if (ret) { + if (ret && vmem->dynamic_memslots) { virtio_mem_deactivate_unplugged_memslots(vmem, offset, size); } } @@ -749,7 +749,9 @@ static int virtio_mem_unplug_all(VirtIOMEM *vmem) notifier_list_notify(&vmem->size_change_notifiers, &vmem->size); =20 /* Deactivate all memslots after updating the state. */ - virtio_mem_deactivate_unplugged_memslots(vmem, 0, region_size); + if (vmem->dynamic_memslots) { + virtio_mem_deactivate_unplugged_memslots(vmem, 0, region_size); + } } =20 trace_virtio_mem_unplugged_all(); --=20 2.41.0