From nobody Fri Apr 19 13:50:24 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; 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 ARC-Seal: i=1; a=rsa-sha256; t=1578993457; cv=none; d=zohomail.com; s=zohoarc; b=O0pY/xJaDMDxQKeenKpq7vjOi50vpdxkPXUwMH0TXAmTBxRpkZhnHwvqIJV+ZnlIOazKh0bQBchAsW7NeEWkqg/4RSu8LahdJTkRBVjxqkZLALLI166DEQbSPnD7VCyv/ciRP+QlBUaJfRR7OGWfOIBAWHUj0QNoD7AwDkZCbtM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1578993457; h=Content-Type: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=bxjq7D/ef5ywXy9xP808yPXTs9uJQFnQk17LMT8BtTs=; b=MNFcN7tH/CA/aJ62IzPEt6eTEozWMASyK/c8RlOocQDJyLIEdopP/lpkXNc1eL37nlMkXUWJXrlpokdYIZ6L02E6Cp5ARL/aO0uFUny97Hd/xoRmq/+jLqXlo5sc4/LaYqFDixpSuztBUSY86Gz5Tab4EyLrzzLLScoiTz5zzxA= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1578993457967941.1677454420067; Tue, 14 Jan 2020 01:17:37 -0800 (PST) Received: from localhost ([::1]:34855 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1irIKO-0000Dr-D5 for importer@patchew.org; Tue, 14 Jan 2020 04:17:36 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:55605) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1irIJf-0008Eu-Kg for qemu-devel@nongnu.org; Tue, 14 Jan 2020 04:16:52 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1irIJe-0003Gb-62 for qemu-devel@nongnu.org; Tue, 14 Jan 2020 04:16:51 -0500 Received: from szxga07-in.huawei.com ([45.249.212.35]:47498 helo=huawei.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1irIJd-0003En-Rj for qemu-devel@nongnu.org; Tue, 14 Jan 2020 04:16:50 -0500 Received: from DGGEMS404-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id BE548253B4964F20B5A8; Tue, 14 Jan 2020 17:16:44 +0800 (CST) Received: from DESKTOP-9NTIQGG.china.huawei.com (10.173.221.136) by DGGEMS404-HUB.china.huawei.com (10.3.19.204) with Microsoft SMTP Server id 14.3.439.0; Tue, 14 Jan 2020 17:16:36 +0800 From: To: , Subject: [PATCH v2] scsi-disk: define props in scsi_block_disk to avoid memleaks Date: Tue, 14 Jan 2020 17:16:34 +0800 Message-ID: <20200114091634.60856-1-pannengyuan@huawei.com> X-Mailer: git-send-email 2.21.0.windows.1 MIME-Version: 1.0 X-Originating-IP: [10.173.221.136] X-CFilter-Loop: Reflected Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 45.249.212.35 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: Euler Robot , Pan Nengyuan , qemu-devel@nongnu.org, zhang.zhanghailiang@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" From: Pan Nengyuan scsi_block_realize() use scsi_realize() to init some props, but these props is not defined in scsi_block_disk_properties, so they will not be freed. This patch defines these prop in scsi_block_disk_properties and aslo calls scsi_unrealize to avoid memleaks, the leak stack as follow(it's easy to reproduce by attaching/detaching scsi-block-disks): =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3Dqemu-system-x86_64=3D=3D32195=3D=3DERROR: LeakSanitizer: detected mem= ory leaks Direct leak of 57 byte(s) in 3 object(s) allocated from: #0 0x7f19f8bed768 (/lib64/libasan.so.5+0xef768) ??:? #1 0x7f19f64d9445 (/lib64/libglib-2.0.so.0+0x52445) ??:? #2 0x7f19f64f2d92 (/lib64/libglib-2.0.so.0+0x6bd92) ??:? #3 0x55975366e596 (qemu-system-x86_64+0x35c0596) /mnt/sdb/qemu/hw/scsi/s= csi-disk.c:2399 #4 0x559753671201 (emu-system-x86_64+0x35c3201) /mnt/sdb/qemu/hw/scsi/sc= si-disk.c:2681 #5 0x559753687e3e (qemu-system-x86_64+0x35d9e3e) /mnt/sdb/qemu/hw/scsi/s= csi-bus.c:58 #6 0x55975368ac44 (qemu-system-x86_64+0x35dcc44) /mnt/sdb/qemu/hw/scsi/s= csi-bus.c:216 #7 0x5597532a7840 (qemu-system-x86_64+0x31f9840) /mnt/sdb/qemu/hw/core/q= dev.c:876 Direct leak of 15 byte(s) in 3 object(s) allocated from: #0 0x7f19f8bed768 (/lib64/libasan.so.5+0xef768) ??:? #1 0x7f19f64d9445 (/lib64/libglib-2.0.so.0+0x52445) ??:? #2 0x7f19f64f2d92 (/lib64/libglib-2.0.so.0+0x6bd92) ??:? #3 0x55975366e06f (qemu-system-x86_64+0x35c006f) /mnt/sdb/qemu/hw/scsi/s= csi-disk.c:2388 #4 0x559753671201 (qemu-system-x86_64+0x35c3201) /mnt/sdb/qemu/hw/scsi/s= csi-disk.c:2681 #5 0x559753687e3e (qemu-system-x86_64+0x35d9e3e) /mnt/sdb/qemu/hw/scsi/s= csi-bus.c:58 #6 0x55975368ac44 (qemu-system-x86_64+0x35dcc44) /mnt/sdb/qemu/hw/scsi/s= csi-bus.c:216 Reported-by: Euler Robot Signed-off-by: Pan Nengyuan --- Changes V2 to V1: - move 'drive' to the front to keep the original props's order. --- hw/scsi/scsi-disk.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c index e44c61eeb4..a1194b9fa7 100644 --- a/hw/scsi/scsi-disk.c +++ b/hw/scsi/scsi-disk.c @@ -2981,7 +2981,6 @@ static const TypeInfo scsi_disk_base_info =3D { }; =20 #define DEFINE_SCSI_DISK_PROPERTIES() \ - DEFINE_PROP_DRIVE_IOTHREAD("drive", SCSIDiskState, qdev.conf.blk), \ DEFINE_BLOCK_PROPERTIES_BASE(SCSIDiskState, qdev.conf), \ DEFINE_BLOCK_ERROR_PROPERTIES(SCSIDiskState, qdev.conf), \ DEFINE_PROP_STRING("ver", SCSIDiskState, version), \ @@ -2992,6 +2991,7 @@ static const TypeInfo scsi_disk_base_info =3D { =20 =20 static Property scsi_hd_properties[] =3D { + DEFINE_PROP_DRIVE_IOTHREAD("drive", SCSIDiskState, qdev.conf.blk), DEFINE_SCSI_DISK_PROPERTIES(), DEFINE_PROP_BIT("removable", SCSIDiskState, features, SCSI_DISK_F_REMOVABLE, false), @@ -3047,6 +3047,7 @@ static const TypeInfo scsi_hd_info =3D { }; =20 static Property scsi_cd_properties[] =3D { + DEFINE_PROP_DRIVE_IOTHREAD("drive", SCSIDiskState, qdev.conf.blk), DEFINE_SCSI_DISK_PROPERTIES(), DEFINE_PROP_UINT64("wwn", SCSIDiskState, qdev.wwn, 0), DEFINE_PROP_UINT64("port_wwn", SCSIDiskState, qdev.port_wwn, 0), @@ -3079,9 +3080,8 @@ static const TypeInfo scsi_cd_info =3D { =20 #ifdef __linux__ static Property scsi_block_properties[] =3D { - DEFINE_BLOCK_ERROR_PROPERTIES(SCSIDiskState, qdev.conf), \ + DEFINE_SCSI_DISK_PROPERTIES(), DEFINE_PROP_DRIVE("drive", SCSIDiskState, qdev.conf.blk), - DEFINE_PROP_BOOL("share-rw", SCSIDiskState, qdev.conf.share_rw, false), DEFINE_PROP_UINT16("rotation_rate", SCSIDiskState, rotation_rate, 0), DEFINE_PROP_UINT64("max_unmap_size", SCSIDiskState, max_unmap_size, DEFAULT_MAX_UNMAP_SIZE), @@ -3099,6 +3099,7 @@ static void scsi_block_class_initfn(ObjectClass *klas= s, void *data) SCSIDiskClass *sdc =3D SCSI_DISK_BASE_CLASS(klass); =20 sc->realize =3D scsi_block_realize; + sc->unrealize =3D scsi_unrealize; sc->alloc_req =3D scsi_block_new_request; sc->parse_cdb =3D scsi_block_parse_cdb; sdc->dma_readv =3D scsi_block_dma_readv; @@ -3118,6 +3119,7 @@ static const TypeInfo scsi_block_info =3D { #endif =20 static Property scsi_disk_properties[] =3D { + DEFINE_PROP_DRIVE_IOTHREAD("drive", SCSIDiskState, qdev.conf.blk), DEFINE_SCSI_DISK_PROPERTIES(), DEFINE_PROP_BIT("removable", SCSIDiskState, features, SCSI_DISK_F_REMOVABLE, false), --=20 2.21.0.windows.1