From nobody Fri Nov 22 11:38:58 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=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1598886212; cv=none; d=zohomail.com; s=zohoarc; b=SUlei47Z/ll6MICyZsfrIiAgAl9nWLsM738m5WnyFFXbbbkIhcN44CgcWYchfHmcqv1vCjGaIre5fOtieGHbQyKIwm0vE+heBs5AzkgDi9mp0gMGIgizEBSSdYgzvpUUUqOTpf5wewZMf9poRP1nW19b0tTOiRLOaJYC1wNFOG4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598886212; 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=8PSON/FTSv0dYjpYRQPNNtDG+cZEs6VPBIXSF62lthU=; b=hvOMzCck0cPcd4unFt8RdkyN7F+6rwdbSEGIHQkk0Cw79g43pheZ9BxXWdvumSNZZWSnh3nsN3X/8bASdszrnPAt1kp/5J8T5DYUyVDMTpoODL+3l05SHf0BkBFFeKoP6HyRX1YNp83k4d9KO+cBxmEJhP++8zuTpBesWg4xSsk= 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 1598886212685944.420239786854; Mon, 31 Aug 2020 08:03:32 -0700 (PDT) Received: from localhost ([::1]:36232 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kClLH-0008FX-A6 for importer@patchew.org; Mon, 31 Aug 2020 11:03:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44334) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kClJw-0006bS-Vs for qemu-devel@nongnu.org; Mon, 31 Aug 2020 11:02:09 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:31596 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kClJu-0004Yc-N6 for qemu-devel@nongnu.org; Mon, 31 Aug 2020 11:02:08 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-274-CopMqEBpMjOhpb2zR3sv5w-1; Mon, 31 Aug 2020 11:02:03 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 693651009441; Mon, 31 Aug 2020 15:02:01 +0000 (UTC) Received: from localhost.localdomain (unknown [10.35.206.190]) by smtp.corp.redhat.com (Postfix) with ESMTP id 24C4C19C4F; Mon, 31 Aug 2020 15:01:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1598886125; 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=8PSON/FTSv0dYjpYRQPNNtDG+cZEs6VPBIXSF62lthU=; b=ajgyn9w/e7nxx9UWsAxwVlLwcwKuJf+XXcwd5t36fDOK2GMNoqrlXs+O9oFOedczr/9ro4 VB+JAl2ZQW5aL5Zf63MH94fMFkXu3JuUTmkVz0QzW5OUwHRY9YTAORDLFymJkxiCAzuH0o fQiX6r/+vRj0ykR37hONXedKZENl5EM= X-MC-Unique: CopMqEBpMjOhpb2zR3sv5w-1 From: Maxim Levitsky To: qemu-devel@nongnu.org Subject: [PATCH v4 8/9] virtio-scsi: use scsi_device_get Date: Mon, 31 Aug 2020 18:01:23 +0300 Message-Id: <20200831150124.206267-9-mlevitsk@redhat.com> In-Reply-To: <20200831150124.206267-1-mlevitsk@redhat.com> References: <20200831150124.206267-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mlevitsk@redhat.com X-Mimecast-Spam-Score: 0.001 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=205.139.110.120; envelope-from=mlevitsk@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/31 02:13:08 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] 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, 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: Fam Zheng , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Eduardo Habkost , "Michael S. Tsirkin" , Stefan Hajnoczi , Maxim Levitsky , Stefan Hajnoczi , Paolo Bonzini 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" This will help us to avoid the scsi device disappearing after we took a reference to it. It doesn't by itself forbid case when we try to access an unrealized device Suggested-by: Stefan Hajnoczi Signed-off-by: Maxim Levitsky Reviewed-by: Stefan Hajnoczi --- hw/scsi/virtio-scsi.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c index 971afbb217..3db9a8aae9 100644 --- a/hw/scsi/virtio-scsi.c +++ b/hw/scsi/virtio-scsi.c @@ -33,7 +33,7 @@ static inline int virtio_scsi_get_lun(uint8_t *lun) return ((lun[2] << 8) | lun[3]) & 0x3FFF; } =20 -static inline SCSIDevice *virtio_scsi_device_find(VirtIOSCSI *s, uint8_t *= lun) +static inline SCSIDevice *virtio_scsi_device_get(VirtIOSCSI *s, uint8_t *l= un) { if (lun[0] !=3D 1) { return NULL; @@ -41,7 +41,7 @@ static inline SCSIDevice *virtio_scsi_device_find(VirtIOS= CSI *s, uint8_t *lun) if (lun[2] !=3D 0 && !(lun[2] >=3D 0x40 && lun[2] < 0x80)) { return NULL; } - return scsi_device_find(&s->bus, 0, lun[1], virtio_scsi_get_lun(lun)); + return scsi_device_get(&s->bus, 0, lun[1], virtio_scsi_get_lun(lun)); } =20 void virtio_scsi_init_req(VirtIOSCSI *s, VirtQueue *vq, VirtIOSCSIReq *req) @@ -256,7 +256,7 @@ static inline void virtio_scsi_ctx_check(VirtIOSCSI *s,= SCSIDevice *d) * case of async cancellation. */ static int virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSCSIReq *req) { - SCSIDevice *d =3D virtio_scsi_device_find(s, req->req.tmf.lun); + SCSIDevice *d =3D virtio_scsi_device_get(s, req->req.tmf.lun); SCSIRequest *r, *next; BusChild *kid; int target; @@ -370,10 +370,10 @@ static int virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSC= SIReq *req) =20 rcu_read_lock(); QTAILQ_FOREACH_RCU(kid, &s->bus.qbus.children, sibling) { - d =3D SCSI_DEVICE(kid->child); - if (d->channel =3D=3D 0 && d->id =3D=3D target) { - qdev_reset_all(&d->qdev); - } + SCSIDevice *d1 =3D SCSI_DEVICE(kid->child); + if (d1->channel =3D=3D 0 && d1->id =3D=3D target) { + qdev_reset_all(&d1->qdev); + } } rcu_read_unlock(); =20 @@ -386,14 +386,17 @@ static int virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSC= SIReq *req) break; } =20 + object_unref(OBJECT(d)); return ret; =20 incorrect_lun: req->resp.tmf.response =3D VIRTIO_SCSI_S_INCORRECT_LUN; + object_unref(OBJECT(d)); return ret; =20 fail: req->resp.tmf.response =3D VIRTIO_SCSI_S_BAD_TARGET; + object_unref(OBJECT(d)); return ret; } =20 @@ -564,7 +567,7 @@ static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCS= I *s, VirtIOSCSIReq *req) } } =20 - d =3D virtio_scsi_device_find(s, req->req.cmd.lun); + d =3D virtio_scsi_device_get(s, req->req.cmd.lun); if (!d) { req->resp.cmd.response =3D VIRTIO_SCSI_S_BAD_TARGET; virtio_scsi_complete_cmd_req(req); @@ -580,10 +583,12 @@ static int virtio_scsi_handle_cmd_req_prepare(VirtIOS= CSI *s, VirtIOSCSIReq *req) req->sreq->cmd.xfer > req->qsgl.size)) { req->resp.cmd.response =3D VIRTIO_SCSI_S_OVERRUN; virtio_scsi_complete_cmd_req(req); + object_unref(OBJECT(d)); return -ENOBUFS; } scsi_req_ref(req->sreq); blk_io_plug(d->conf.blk); + object_unref(OBJECT(d)); return 0; } =20 --=20 2.26.2