From nobody Fri May 17 03:39:50 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=1601054928; cv=none; d=zohomail.com; s=zohoarc; b=YeK2+jbS6vGVdWOmK15LcX6VpzuHGYKfukrx0WkiwjAn3/JY+G9IM+TgGEeZGPTEyN6ucOHk4e01mEwq6mGCMVWEH4WI8qDkh5IuGHlKqaySaSUfYht/IR0vd3QY+Xs+y+HtJyCjJdRQhMBU5dAsECzznaCbnlWxAEWqXSkmg+M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601054928; 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=BJsOJtdUq0tUG2aZuW0ACSP+CoOcPXw/lA+VjROBOnY=; b=jl+mZW8xbIRW+KHxEgzEsLGSDyk/KrPlu+QY+Ab9x9J7oJxtQRwbt0is1yZ9mfUI18qfyFrIRmD3HoxuxJePiWOstl8MP81VBldar2NMoIj621JfGTS9cM0hJ1nUY6tIbg0l2sfYi308qui4PPjWK3TIwzTqPpFCAFOGltPmNDQ= 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) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1601054928822382.2536531795512; Fri, 25 Sep 2020 10:28:48 -0700 (PDT) Received: from localhost ([::1]:55506 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kLrWZ-0004yZ-GM for importer@patchew.org; Fri, 25 Sep 2020 13:28:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35092) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kLrU7-0002If-I7 for qemu-devel@nongnu.org; Fri, 25 Sep 2020 13:26:15 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:51651) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kLrU5-0007AN-77 for qemu-devel@nongnu.org; Fri, 25 Sep 2020 13:26:15 -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-425-WtZn1hmBMYeqIrYsCF1iKg-1; Fri, 25 Sep 2020 13:26:10 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0955887137B for ; Fri, 25 Sep 2020 17:26:09 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9CC955D9F7; Fri, 25 Sep 2020 17:26:08 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1601054771; 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=BJsOJtdUq0tUG2aZuW0ACSP+CoOcPXw/lA+VjROBOnY=; b=I/LvndCgZ+YpKQmCD4B5OuOVMZWvPwWk03xjWpChOCpoqfIVEi34tADYkqisqGktI8lWWY 45Sbqz8UNA2uECDIp3fRPExARgbF57OqFfKEc3er4HMkRkEKb+eTg6Mmh6sR9GK82twUz3 Z6ig8nA+UXjZx06iInZss6g6NJ4bqjU= X-MC-Unique: WtZn1hmBMYeqIrYsCF1iKg-1 From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 01/10] qdev: add "check if address free" callback for buses Date: Fri, 25 Sep 2020 13:25:55 -0400 Message-Id: <20200925172604.2142227-2-pbonzini@redhat.com> In-Reply-To: <20200925172604.2142227-1-pbonzini@redhat.com> References: <20200925172604.2142227-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=pbonzini@redhat.com X-Mimecast-Spam-Score: 0 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=216.205.24.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/25 02:48:20 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -32 X-Spam_score: -3.3 X-Spam_bar: --- X-Spam_report: (-3.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.199, 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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, 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: stefanha@redhat.com, mlevitsk@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Check if an address is free on the bus before plugging in the device. This makes it possible to do the check without any side effects, and to detect the problem early without having to do it in the realize callback. Signed-off-by: Paolo Bonzini Reviewed-by: Maxim Levitsky --- hw/core/qdev.c | 17 +++++++++++++++-- hw/net/virtio-net.c | 2 +- hw/sd/core.c | 3 ++- include/hw/qdev-core.h | 3 ++- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/hw/core/qdev.c b/hw/core/qdev.c index 96772a15bd..74db78df36 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -94,13 +94,23 @@ static void bus_add_child(BusState *bus, DeviceState *c= hild) 0); } =20 -void qdev_set_parent_bus(DeviceState *dev, BusState *bus) +static bool bus_check_address(BusState *bus, DeviceState *child, Error **e= rrp) +{ + BusClass *bc =3D BUS_GET_CLASS(bus); + return !bc->check_address || bc->check_address(bus, child, errp); +} + +bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp) { BusState *old_parent_bus =3D dev->parent_bus; DeviceClass *dc =3D DEVICE_GET_CLASS(dev); =20 assert(dc->bus_type && object_dynamic_cast(OBJECT(bus), dc->bus_type)); =20 + if (!bus_check_address(bus, dev, errp)) { + return false; + } + if (old_parent_bus) { trace_qdev_update_parent_bus(dev, object_get_typename(OBJECT(dev)), old_parent_bus, object_get_typename(OBJECT(old_parent_bus)), @@ -126,6 +136,7 @@ void qdev_set_parent_bus(DeviceState *dev, BusState *bu= s) object_unref(OBJECT(old_parent_bus)); object_unref(OBJECT(dev)); } + return true; } =20 DeviceState *qdev_new(const char *name) @@ -371,7 +382,9 @@ bool qdev_realize(DeviceState *dev, BusState *bus, Erro= r **errp) assert(!dev->realized && !dev->parent_bus); =20 if (bus) { - qdev_set_parent_bus(dev, bus); + if (!qdev_set_parent_bus(dev, bus, errp)) { + return false; + } } else { assert(!DEVICE_GET_CLASS(dev)->bus_type); } diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c index 7bf27b9db7..268cecc498 100644 --- a/hw/net/virtio-net.c +++ b/hw/net/virtio-net.c @@ -3142,7 +3142,7 @@ static bool failover_replug_primary(VirtIONet *n, Err= or **errp) error_setg(errp, "virtio_net: couldn't find primary bus"); return false; } - qdev_set_parent_bus(n->primary_dev, n->primary_bus); + qdev_set_parent_bus(n->primary_dev, n->primary_bus, &error_abort); n->primary_should_be_hidden =3D false; if (!qemu_opt_set_bool(n->primary_device_opts, "partially_hotplugged", true, errp)) { diff --git a/hw/sd/core.c b/hw/sd/core.c index 957d116f1a..08c93b5903 100644 --- a/hw/sd/core.c +++ b/hw/sd/core.c @@ -23,6 +23,7 @@ #include "hw/qdev-core.h" #include "hw/sd/sd.h" #include "qemu/module.h" +#include "qapi/error.h" #include "trace.h" =20 static inline const char *sdbus_name(SDBus *sdbus) @@ -240,7 +241,7 @@ void sdbus_reparent_card(SDBus *from, SDBus *to) readonly =3D sc->get_readonly(card); =20 sdbus_set_inserted(from, false); - qdev_set_parent_bus(DEVICE(card), &to->qbus); + qdev_set_parent_bus(DEVICE(card), &to->qbus, &error_abort); sdbus_set_inserted(to, true); sdbus_set_readonly(to, readonly); } diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h index 72064f4dd4..e62da68a26 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -217,6 +217,7 @@ struct BusClass { */ char *(*get_fw_dev_path)(DeviceState *dev); void (*reset)(BusState *bus); + bool (*check_address)(BusState *bus, DeviceState *dev, Error **errp); BusRealize realize; BusUnrealize unrealize; =20 @@ -788,7 +789,7 @@ const char *qdev_fw_name(DeviceState *dev); Object *qdev_get_machine(void); =20 /* FIXME: make this a link<> */ -void qdev_set_parent_bus(DeviceState *dev, BusState *bus); +bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp); =20 extern bool qdev_hotplug; extern bool qdev_hot_removed; --=20 2.26.2 From nobody Fri May 17 03:39:50 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=1601054894; cv=none; d=zohomail.com; s=zohoarc; b=YZq5/yZ/c/WX4RJoypDyAgPNuKgnHwUeoFPiP1xFnL8MDF20PFiP2T+aqvJmikL7ADb+siPEd09RyNM3ptMqKDd4jd6OmIQfuy8TkDApZXexSxZz1vY/YQUMe5rRDsCBUygPkIFrkCvKb7rreLhrtdzQ1zcyXTVDerqi7Zrn5rA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601054894; 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=7jnEPHNQolXrIggvM1boO0VCWcKwcGVqxDfZAb/xQC0=; b=DGYMIsAWVgunh31FO4E3ZFW5qHCq0cwU9oz9cauYxbN+nMiXr4NjrjaoSQc/FNPqQ2wa5As8vtE91OZ6n2kQPvvYAtpg2yyuk4ScVAcCW/+yp4uGWMgso+fAPfx/j8DOFOH6vvhaz0++5MFNFpnmho++xY+Ct1h21E1juy9Z8gs= 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) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1601054894934223.4771218159823; Fri, 25 Sep 2020 10:28:14 -0700 (PDT) Received: from localhost ([::1]:53288 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kLrW1-00042t-8v for importer@patchew.org; Fri, 25 Sep 2020 13:28:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35140) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kLrU9-0002Kw-6j for qemu-devel@nongnu.org; Fri, 25 Sep 2020 13:26:17 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:54931) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kLrU6-0007An-CM for qemu-devel@nongnu.org; Fri, 25 Sep 2020 13:26:16 -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-457-SSxZRQcxOqyhxY9N19Whaw-1; Fri, 25 Sep 2020 13:26:10 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8A83E801AAC for ; Fri, 25 Sep 2020 17:26:09 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 29F4A5D9F3; Fri, 25 Sep 2020 17:26:09 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1601054773; 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=7jnEPHNQolXrIggvM1boO0VCWcKwcGVqxDfZAb/xQC0=; b=blVYiYTb3iWust/Iua+DL91fOyeqVBRU9ZAvJIHTOr9SITf5/OVVDjP9zfKXNcOmLV+/03 witu3WbIKtY9dHMcbzBhmkI8ADck+GXOxOFtG6oLfoP+Z/1N0rw30XxBs9OtMLrNlsijgh /YYUkExJoQmLN5ZN0dyDxrvmZ9vjga8= X-MC-Unique: SSxZRQcxOqyhxY9N19Whaw-1 From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 02/10] scsi: switch to bus->check_address Date: Fri, 25 Sep 2020 13:25:56 -0400 Message-Id: <20200925172604.2142227-3-pbonzini@redhat.com> In-Reply-To: <20200925172604.2142227-1-pbonzini@redhat.com> References: <20200925172604.2142227-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=pbonzini@redhat.com X-Mimecast-Spam-Score: 0 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=216.205.24.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/25 02:48:20 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -32 X-Spam_score: -3.3 X-Spam_bar: --- X-Spam_report: (-3.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.199, 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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, 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: stefanha@redhat.com, mlevitsk@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Signed-off-by: Paolo Bonzini Reviewed-by: Maxim Levitsky Reviewed-by: Stefan Hajnoczi --- hw/scsi/scsi-bus.c | 122 ++++++++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 47 deletions(-) diff --git a/hw/scsi/scsi-bus.c b/hw/scsi/scsi-bus.c index 3284a5d1fb..94921c04b1 100644 --- a/hw/scsi/scsi-bus.c +++ b/hw/scsi/scsi-bus.c @@ -22,33 +22,6 @@ static void scsi_req_dequeue(SCSIRequest *req); static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len); static void scsi_target_free_buf(SCSIRequest *req); =20 -static Property scsi_props[] =3D { - DEFINE_PROP_UINT32("channel", SCSIDevice, channel, 0), - DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1), - DEFINE_PROP_UINT32("lun", SCSIDevice, lun, -1), - DEFINE_PROP_END_OF_LIST(), -}; - -static void scsi_bus_class_init(ObjectClass *klass, void *data) -{ - BusClass *k =3D BUS_CLASS(klass); - HotplugHandlerClass *hc =3D HOTPLUG_HANDLER_CLASS(klass); - - k->get_dev_path =3D scsibus_get_dev_path; - k->get_fw_dev_path =3D scsibus_get_fw_dev_path; - hc->unplug =3D qdev_simple_device_unplug_cb; -} - -static const TypeInfo scsi_bus_info =3D { - .name =3D TYPE_SCSI_BUS, - .parent =3D TYPE_BUS, - .instance_size =3D sizeof(SCSIBus), - .class_init =3D scsi_bus_class_init, - .interfaces =3D (InterfaceInfo[]) { - { TYPE_HOTPLUG_HANDLER }, - { } - } -}; static int next_scsi_bus; =20 static void scsi_device_realize(SCSIDevice *s, Error **errp) @@ -160,35 +133,68 @@ static void scsi_dma_restart_cb(void *opaque, int run= ning, RunState state) } } =20 -static void scsi_qdev_realize(DeviceState *qdev, Error **errp) +static bool scsi_bus_is_address_free(SCSIBus *bus, + int channel, int target, int lun, + SCSIDevice **p_dev) +{ + SCSIDevice *d =3D scsi_device_find(bus, channel, target, lun); + if (d && d->lun =3D=3D lun) { + if (p_dev) { + *p_dev =3D d; + } + return false; + } + if (p_dev) { + *p_dev =3D NULL; + } + return true; +} + +static bool scsi_bus_check_address(BusState *qbus, DeviceState *qdev, Erro= r **errp) { SCSIDevice *dev =3D SCSI_DEVICE(qdev); - SCSIBus *bus =3D DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus); - SCSIDevice *d; - Error *local_err =3D NULL; + SCSIBus *bus =3D SCSI_BUS(qbus); =20 if (dev->channel > bus->info->max_channel) { error_setg(errp, "bad scsi channel id: %d", dev->channel); - return; + return false; } if (dev->id !=3D -1 && dev->id > bus->info->max_target) { error_setg(errp, "bad scsi device id: %d", dev->id); - return; + return false; } if (dev->lun !=3D -1 && dev->lun > bus->info->max_lun) { error_setg(errp, "bad scsi device lun: %d", dev->lun); - return; + return false; + } + + if (dev->id !=3D -1 && dev->lun !=3D -1) { + SCSIDevice *d; + if (!scsi_bus_is_address_free(bus, dev->channel, dev->id, dev->lun= , &d)) { + error_setg(errp, "lun already used by '%s'", d->qdev.id); + return false; + } } =20 + return true; +} + +static void scsi_qdev_realize(DeviceState *qdev, Error **errp) +{ + SCSIDevice *dev =3D SCSI_DEVICE(qdev); + SCSIBus *bus =3D DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus); + bool is_free; + Error *local_err =3D NULL; + if (dev->id =3D=3D -1) { int id =3D -1; if (dev->lun =3D=3D -1) { dev->lun =3D 0; } do { - d =3D scsi_device_find(bus, dev->channel, ++id, dev->lun); - } while (d && d->lun =3D=3D dev->lun && id < bus->info->max_target= ); - if (d && d->lun =3D=3D dev->lun) { + is_free =3D scsi_bus_is_address_free(bus, dev->channel, ++id, = dev->lun, NULL); + } while (!is_free && id < bus->info->max_target); + if (!is_free) { error_setg(errp, "no free target"); return; } @@ -196,20 +202,13 @@ static void scsi_qdev_realize(DeviceState *qdev, Erro= r **errp) } else if (dev->lun =3D=3D -1) { int lun =3D -1; do { - d =3D scsi_device_find(bus, dev->channel, dev->id, ++lun); - } while (d && d->lun =3D=3D lun && lun < bus->info->max_lun); - if (d && d->lun =3D=3D lun) { + is_free =3D scsi_bus_is_address_free(bus, dev->channel, dev->i= d, ++lun, NULL); + } while (!is_free && lun < bus->info->max_lun); + if (!is_free) { error_setg(errp, "no free lun"); return; } dev->lun =3D lun; - } else { - d =3D scsi_device_find(bus, dev->channel, dev->id, dev->lun); - assert(d); - if (d->lun =3D=3D dev->lun && dev !=3D d) { - error_setg(errp, "lun already used by '%s'", d->qdev.id); - return; - } } =20 QTAILQ_INIT(&dev->requests); @@ -1709,6 +1708,13 @@ const VMStateDescription vmstate_scsi_device =3D { } }; =20 +static Property scsi_props[] =3D { + DEFINE_PROP_UINT32("channel", SCSIDevice, channel, 0), + DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1), + DEFINE_PROP_UINT32("lun", SCSIDevice, lun, -1), + DEFINE_PROP_END_OF_LIST(), +}; + static void scsi_device_class_init(ObjectClass *klass, void *data) { DeviceClass *k =3D DEVICE_CLASS(klass); @@ -1739,6 +1745,28 @@ static const TypeInfo scsi_device_type_info =3D { .instance_init =3D scsi_dev_instance_init, }; =20 +static void scsi_bus_class_init(ObjectClass *klass, void *data) +{ + BusClass *k =3D BUS_CLASS(klass); + HotplugHandlerClass *hc =3D HOTPLUG_HANDLER_CLASS(klass); + + k->get_dev_path =3D scsibus_get_dev_path; + k->get_fw_dev_path =3D scsibus_get_fw_dev_path; + k->check_address =3D scsi_bus_check_address; + hc->unplug =3D qdev_simple_device_unplug_cb; +} + +static const TypeInfo scsi_bus_info =3D { + .name =3D TYPE_SCSI_BUS, + .parent =3D TYPE_BUS, + .instance_size =3D sizeof(SCSIBus), + .class_init =3D scsi_bus_class_init, + .interfaces =3D (InterfaceInfo[]) { + { TYPE_HOTPLUG_HANDLER }, + { } + } +}; + static void scsi_register_types(void) { type_register_static(&scsi_bus_info); --=20 2.26.2 From nobody Fri May 17 03:39:50 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=1601055172; cv=none; d=zohomail.com; s=zohoarc; b=SX2m4feEvPoLi+TvkFDza8JYKFE8pFJ1tUq5oMPI6v4KJrv+BnS8TVLLtpCpE3fOsXbL8lJkuRx/1bOLeytcDZBb6gYj6rcBxvueNPAalD8iF27UG3TQ+N71Q66RjeLxMfiXmFHcvyZRUUdRXuwfi+P06i9g839rBj8wP1Gfjyo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601055172; 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=hDip1TuoGE2uTB5vESfk4LG+Rb+6jGwMeV9JdwzMKi4=; b=F9JX5QtZg89OAD1DVZd8c5JbKrnnPEcv83NJ2hJ5khz7atMnpaez3lCl7oskyi3oia3Au/mSjjZS4M6sTAqDzB+JDnEmH+NzrRsanFdwFowlDWseiKVzZqB6XQ1R0n3lG7CtYMQ5y/BM9bVYXg6HHKsxplgOH4mwZ6LeT2YVFN8= 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) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1601055172993430.8192879013353; Fri, 25 Sep 2020 10:32:52 -0700 (PDT) Received: from localhost ([::1]:36442 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kLraV-0000Yl-Ff for importer@patchew.org; Fri, 25 Sep 2020 13:32:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35264) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kLrUT-0002nd-3F for qemu-devel@nongnu.org; Fri, 25 Sep 2020 13:26:37 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:21133) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kLrUR-0007Cy-A4 for qemu-devel@nongnu.org; Fri, 25 Sep 2020 13:26:36 -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-589-OLv0-_Z5Mpae7zEfbhecHg-1; Fri, 25 Sep 2020 13:26:11 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 189C71902EBD for ; Fri, 25 Sep 2020 17:26:10 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id AB3D35D9F3; Fri, 25 Sep 2020 17:26:09 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1601054790; 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=hDip1TuoGE2uTB5vESfk4LG+Rb+6jGwMeV9JdwzMKi4=; b=hA32hLJRkSi6Gmlq6geyKM+ypAPR2hupA3FPSCCefaKC5MKWyZkRMxP1mbipmWGkWEXWEY 1fTBRvDcx4B0z9QIt+Iz0GTAdZr8jIE/FMlBZMzVyg+zTKvehP+YcBAFIgUDSTIOj7KqvT OEkGKGusYqmn1HAd0Ny0CQTogbsZyY8= X-MC-Unique: OLv0-_Z5Mpae7zEfbhecHg-1 From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 03/10] scsi/scsi_bus: switch search direction in scsi_device_find Date: Fri, 25 Sep 2020 13:25:57 -0400 Message-Id: <20200925172604.2142227-4-pbonzini@redhat.com> In-Reply-To: <20200925172604.2142227-1-pbonzini@redhat.com> References: <20200925172604.2142227-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=pbonzini@redhat.com X-Mimecast-Spam-Score: 0 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=216.205.24.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/25 02:48:20 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -32 X-Spam_score: -3.3 X-Spam_bar: --- X-Spam_report: (-3.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.199, 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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, 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: stefanha@redhat.com, mlevitsk@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" From: Maxim Levitsky This change will allow us to convert the bus children list to RCU, while not changing the logic of this function Signed-off-by: Maxim Levitsky Reviewed-by: Stefan Hajnoczi Message-Id: <20200913160259.32145-2-mlevitsk@redhat.com> Signed-off-by: Paolo Bonzini --- hw/scsi/scsi-bus.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/hw/scsi/scsi-bus.c b/hw/scsi/scsi-bus.c index 94921c04b1..69d7c3f90c 100644 --- a/hw/scsi/scsi-bus.c +++ b/hw/scsi/scsi-bus.c @@ -1571,7 +1571,7 @@ SCSIDevice *scsi_device_find(SCSIBus *bus, int channe= l, int id, int lun) BusChild *kid; SCSIDevice *target_dev =3D NULL; =20 - QTAILQ_FOREACH_REVERSE(kid, &bus->qbus.children, sibling) { + QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) { DeviceState *qdev =3D kid->child; SCSIDevice *dev =3D SCSI_DEVICE(qdev); =20 @@ -1579,7 +1579,15 @@ SCSIDevice *scsi_device_find(SCSIBus *bus, int chann= el, int id, int lun) if (dev->lun =3D=3D lun) { return dev; } - target_dev =3D dev; + + /* + * If we don't find exact match (channel/bus/lun), + * we will return the first device which matches channel/bus + */ + + if (!target_dev) { + target_dev =3D dev; + } } } return target_dev; --=20 2.26.2 From nobody Fri May 17 03:39:50 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=1601055068; cv=none; d=zohomail.com; s=zohoarc; b=mJGOkTY7APDHChogyUffORy0JcMvY/yDBVf3nmKAlMi31nKKbulFuEKEdphwsvPn993ALXoLT5XSvm9HqvWvoitlbd3ayGoo39pqH3C7GMOt454FsD2eE4rVV5r+QIxVYgyp80cjtqrZI7vnxyD6L9OJwjxiBlOoMBQVlAp1PfI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601055068; 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=pXR9qHf9CmmeH9lTSpzRuY3wNjR3PLqQbicL6xK9Yjw=; b=Xz0tibmoU2LzC5agav2QwNHsrFkoVSr1d+1vNgzO/7y1OAEHdj7ffJ89xcPKIy3RKHJ9ZQyHWxKMgkz01nvIErqzdBXtX37d1XoMBDZ5MM8TYL+MvAaes3IkhSFYSfct7+GuUtpMS6+qNMlBKYaIJIU98FDb9dqVZiix5lbSeU4= 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 1601055068276960.7587386134353; Fri, 25 Sep 2020 10:31:08 -0700 (PDT) Received: from localhost ([::1]:33452 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kLrYo-0007dY-S9 for importer@patchew.org; Fri, 25 Sep 2020 13:31:06 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35172) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kLrUA-0002Ll-4E for qemu-devel@nongnu.org; Fri, 25 Sep 2020 13:26:18 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:27666) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kLrU7-0007Au-6r for qemu-devel@nongnu.org; Fri, 25 Sep 2020 13:26:17 -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-506-p2LIVo6dMTa3yzq0fxHxNw-1; Fri, 25 Sep 2020 13:26:11 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id ACC4C802B67; Fri, 25 Sep 2020 17:26:10 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 38E4B5D9F3; Fri, 25 Sep 2020 17:26:10 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1601054774; 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=pXR9qHf9CmmeH9lTSpzRuY3wNjR3PLqQbicL6xK9Yjw=; b=GFkKZ9Dw+Yajy/cBbHB/gDw1QiURdNsJXd1chkCPIoNC+nbWUa3i7jQo0jWMSwYccjjqiu MTazua8hcTdEDtAfxQSIXvkR/MtYSegi/bXp+gLReViNV2YeaLNoDwYw297937Efyp61xR jl9SsLuRBg7f1u85pMMoVRi9noA6Cmw= X-MC-Unique: p2LIVo6dMTa3yzq0fxHxNw-1 From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 04/10] device_core: use drain_call_rcu in in hmp_device_del/qmp_device_add Date: Fri, 25 Sep 2020 13:25:58 -0400 Message-Id: <20200925172604.2142227-5-pbonzini@redhat.com> In-Reply-To: <20200925172604.2142227-1-pbonzini@redhat.com> References: <20200925172604.2142227-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=pbonzini@redhat.com X-Mimecast-Spam-Score: 0 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=63.128.21.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/25 01:07:33 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -32 X-Spam_score: -3.3 X-Spam_bar: --- X-Spam_report: (-3.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.199, 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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, 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: Stefan Hajnoczi , stefanha@redhat.com, mlevitsk@redhat.com 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" From: Maxim Levitsky Soon, a device removal might only happen on RCU callback execution, but to avoid chanding HMP semantics, just drain all pending RCU callbacks Signed-off-by: Maxim Levitsky Suggested-by: Stefan Hajnoczi Reviewed-by: Stefan Hajnoczi Message-Id: <20200913160259.32145-4-mlevitsk@redhat.com> Signed-off-by: Paolo Bonzini --- qdev-monitor.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/qdev-monitor.c b/qdev-monitor.c index e9b7228480..c66ba13780 100644 --- a/qdev-monitor.c +++ b/qdev-monitor.c @@ -803,6 +803,18 @@ void qmp_device_add(QDict *qdict, QObject **ret_data, = Error **errp) return; } dev =3D qdev_device_add(opts, errp); + + /* + * Drain all pending RCU callbacks. This is done because + * some bus related operations can delay a device removal + * (in this case this can happen if device is added and then + * removed due to a configuration error) + * to a RCU callback, but user might expect that this interface + * will finish its job completely once qmp command returns result + * to the user + */ + drain_call_rcu(); + if (!dev) { qemu_opts_del(opts); return; @@ -894,6 +906,16 @@ void qmp_device_del(const char *id, Error **errp) } =20 qdev_unplug(dev, errp); + + /* + * Drain all pending RCU callbacks. This is done because + * some bus related operations can delay a device removal + * to a RCU callback, but user might expect that this interface + * will finish its job completely once qmp command returns result + * to the user + */ + + drain_call_rcu(); } } =20 --=20 2.26.2 From nobody Fri May 17 03:39:50 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=1601054937; cv=none; d=zohomail.com; s=zohoarc; b=WYIJnLo67ZZx/DHuwLTTEbG+TkcQVjtTg8LJ8xyiWX15sQVf35bM/DzNJEr5Kj+JrfS02tFGvR2D++oPlCFcgwtQ0kX1PI6IKWjGqvQv4qs8uS0Io8cKNkpxBBftuWWzJsKm3uYpuWxrvXMEWI6lTa2SpN87vPJ0kBByKGrtXjs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601054937; 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=T6FsGBuKouQQB30ht/asEi8mqll9dtqJrQ6O1uHPob4=; b=jrAPGaADy2q33puztFDXEjXWF26Mzu5Q3FaHQGceDgfwCch+Y+eT6+xTTjYO2o/dFzviEEQ2e//BufyPzGla+bzhtG0hTMW8sX3KUdI64lgwzh4F2IFB2+9JeerRCN6QAobYti92dgfYUILvlc2++lTQrEhWTAtDmd/1XKOssTw= 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) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 16010549371991004.8303139002819; Fri, 25 Sep 2020 10:28:57 -0700 (PDT) Received: from localhost ([::1]:56246 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kLrWg-0005Gz-Lq for importer@patchew.org; Fri, 25 Sep 2020 13:28:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35174) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kLrUA-0002MJ-Gr for qemu-devel@nongnu.org; Fri, 25 Sep 2020 13:26:18 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:32492) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kLrU7-0007Ap-43 for qemu-devel@nongnu.org; Fri, 25 Sep 2020 13:26:18 -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-426-lhct7wilN7qnGvW2rX9oJQ-1; Fri, 25 Sep 2020 13:26:12 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3A41980EF9F for ; Fri, 25 Sep 2020 17:26:11 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id CD91A5D9F3; Fri, 25 Sep 2020 17:26:10 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1601054773; 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=T6FsGBuKouQQB30ht/asEi8mqll9dtqJrQ6O1uHPob4=; b=jHCNng8J6VG2zOk52tGpIdIc5wNmuaxQdlYHH+H0HRjTj8kqJ2djbMsFhW+5oiTwkWbRd4 XeVm12PXVacWnmSmP3z1SoGcN1nC02NNIo6b2z+WEw5y8kDqnyH2zKy6/tWDpfjifr21YJ xQ5L0gM34X5h3jHGOamjdzJIUhXZ0b8= X-MC-Unique: lhct7wilN7qnGvW2rX9oJQ-1 From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 05/10] device-core: use RCU for list of children of a bus Date: Fri, 25 Sep 2020 13:25:59 -0400 Message-Id: <20200925172604.2142227-6-pbonzini@redhat.com> In-Reply-To: <20200925172604.2142227-1-pbonzini@redhat.com> References: <20200925172604.2142227-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=pbonzini@redhat.com X-Mimecast-Spam-Score: 0 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=63.128.21.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/25 01:07:33 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -32 X-Spam_score: -3.3 X-Spam_bar: --- X-Spam_report: (-3.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.199, 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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, 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: stefanha@redhat.com, mlevitsk@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" From: Maxim Levitsky This fixes the race between device emulation code that tries to find a child device to dispatch the request to (e.g a scsi disk), and hotplug of a new device to that bus. Note that this doesn't convert all the readers of the list but only these that might go over that list without BQL held. This is a very small first step to make this code thread safe. Suggested-by: Paolo Bonzini Signed-off-by: Maxim Levitsky Reviewed-by: Stefan Hajnoczi Message-Id: <20200913160259.32145-5-mlevitsk@redhat.com> [Use RCU_READ_LOCK_GUARD in more places. - Paolo] Signed-off-by: Paolo Bonzini --- hw/core/bus.c | 28 +++++++++++++++++----------- hw/core/qdev.c | 37 +++++++++++++++++++++++-------------- hw/scsi/scsi-bus.c | 12 +++++++++--- hw/scsi/virtio-scsi.c | 6 +++++- include/hw/qdev-core.h | 9 +++++++++ 5 files changed, 63 insertions(+), 29 deletions(-) diff --git a/hw/core/bus.c b/hw/core/bus.c index 6b987b6946..a0483859ae 100644 --- a/hw/core/bus.c +++ b/hw/core/bus.c @@ -49,12 +49,14 @@ int qbus_walk_children(BusState *bus, } } =20 - QTAILQ_FOREACH(kid, &bus->children, sibling) { - err =3D qdev_walk_children(kid->child, - pre_devfn, pre_busfn, - post_devfn, post_busfn, opaque); - if (err < 0) { - return err; + WITH_RCU_READ_LOCK_GUARD() { + QTAILQ_FOREACH_RCU(kid, &bus->children, sibling) { + err =3D qdev_walk_children(kid->child, + pre_devfn, pre_busfn, + post_devfn, post_busfn, opaque); + if (err < 0) { + return err; + } } } =20 @@ -90,8 +92,10 @@ static void bus_reset_child_foreach(Object *obj, Resetta= bleChildCallback cb, BusState *bus =3D BUS(obj); BusChild *kid; =20 - QTAILQ_FOREACH(kid, &bus->children, sibling) { - cb(OBJECT(kid->child), opaque, type); + WITH_RCU_READ_LOCK_GUARD() { + QTAILQ_FOREACH_RCU(kid, &bus->children, sibling) { + cb(OBJECT(kid->child), opaque, type); + } } } =20 @@ -194,9 +198,11 @@ static void bus_set_realized(Object *obj, bool value, = Error **errp) =20 /* TODO: recursive realization */ } else if (!value && bus->realized) { - QTAILQ_FOREACH(kid, &bus->children, sibling) { - DeviceState *dev =3D kid->child; - qdev_unrealize(dev); + WITH_RCU_READ_LOCK_GUARD() { + QTAILQ_FOREACH_RCU(kid, &bus->children, sibling) { + DeviceState *dev =3D kid->child; + qdev_unrealize(dev); + } } if (bc->unrealize) { bc->unrealize(bus); diff --git a/hw/core/qdev.c b/hw/core/qdev.c index 74db78df36..59e5e710b7 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -51,6 +51,12 @@ const VMStateDescription *qdev_get_vmsd(DeviceState *dev) return dc->vmsd; } =20 +static void bus_free_bus_child(BusChild *kid) +{ + object_unref(OBJECT(kid->child)); + g_free(kid); +} + static void bus_remove_child(BusState *bus, DeviceState *child) { BusChild *kid; @@ -60,15 +66,16 @@ static void bus_remove_child(BusState *bus, DeviceState= *child) char name[32]; =20 snprintf(name, sizeof(name), "child[%d]", kid->index); - QTAILQ_REMOVE(&bus->children, kid, sibling); + QTAILQ_REMOVE_RCU(&bus->children, kid, sibling); =20 bus->num_children--; =20 /* This gives back ownership of kid->child back to us. */ object_property_del(OBJECT(bus), name); - object_unref(OBJECT(kid->child)); - g_free(kid); - return; + + /* free the bus kid, when it is safe to do so*/ + call_rcu(kid, bus_free_bus_child, rcu); + break; } } } @@ -83,7 +90,7 @@ static void bus_add_child(BusState *bus, DeviceState *chi= ld) kid->child =3D child; object_ref(OBJECT(kid->child)); =20 - QTAILQ_INSERT_HEAD(&bus->children, kid, sibling); + QTAILQ_INSERT_HEAD_RCU(&bus->children, kid, sibling); =20 /* This transfers ownership of kid->child to the property. */ snprintf(name, sizeof(name), "child[%d]", kid->index); @@ -672,17 +679,19 @@ DeviceState *qdev_find_recursive(BusState *bus, const= char *id) DeviceState *ret; BusState *child; =20 - QTAILQ_FOREACH(kid, &bus->children, sibling) { - DeviceState *dev =3D kid->child; + WITH_RCU_READ_LOCK_GUARD() { + QTAILQ_FOREACH_RCU(kid, &bus->children, sibling) { + DeviceState *dev =3D kid->child; =20 - if (dev->id && strcmp(dev->id, id) =3D=3D 0) { - return dev; - } + if (dev->id && strcmp(dev->id, id) =3D=3D 0) { + return dev; + } =20 - QLIST_FOREACH(child, &dev->child_bus, sibling) { - ret =3D qdev_find_recursive(child, id); - if (ret) { - return ret; + QLIST_FOREACH(child, &dev->child_bus, sibling) { + ret =3D qdev_find_recursive(child, id); + if (ret) { + return ret; + } } } } diff --git a/hw/scsi/scsi-bus.c b/hw/scsi/scsi-bus.c index 69d7c3f90c..4ab9811cd8 100644 --- a/hw/scsi/scsi-bus.c +++ b/hw/scsi/scsi-bus.c @@ -399,7 +399,10 @@ static bool scsi_target_emulate_report_luns(SCSITarget= Req *r) id =3D r->req.dev->id; found_lun0 =3D false; n =3D 0; - QTAILQ_FOREACH(kid, &r->req.bus->qbus.children, sibling) { + + RCU_READ_LOCK_GUARD(); + + QTAILQ_FOREACH_RCU(kid, &r->req.bus->qbus.children, sibling) { DeviceState *qdev =3D kid->child; SCSIDevice *dev =3D SCSI_DEVICE(qdev); =20 @@ -420,7 +423,7 @@ static bool scsi_target_emulate_report_luns(SCSITargetR= eq *r) memset(r->buf, 0, len); stl_be_p(&r->buf[0], n); i =3D found_lun0 ? 8 : 16; - QTAILQ_FOREACH(kid, &r->req.bus->qbus.children, sibling) { + QTAILQ_FOREACH_RCU(kid, &r->req.bus->qbus.children, sibling) { DeviceState *qdev =3D kid->child; SCSIDevice *dev =3D SCSI_DEVICE(qdev); =20 @@ -429,6 +432,7 @@ static bool scsi_target_emulate_report_luns(SCSITargetR= eq *r) i +=3D 8; } } + assert(i =3D=3D n + 8); r->len =3D len; return true; @@ -1571,7 +1575,8 @@ SCSIDevice *scsi_device_find(SCSIBus *bus, int channe= l, int id, int lun) BusChild *kid; SCSIDevice *target_dev =3D NULL; =20 - QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) { + RCU_READ_LOCK_GUARD(); + QTAILQ_FOREACH_RCU(kid, &bus->qbus.children, sibling) { DeviceState *qdev =3D kid->child; SCSIDevice *dev =3D SCSI_DEVICE(qdev); =20 @@ -1590,6 +1595,7 @@ SCSIDevice *scsi_device_find(SCSIBus *bus, int channe= l, int id, int lun) } } } + return target_dev; } =20 diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c index 3a71ea7097..971afbb217 100644 --- a/hw/scsi/virtio-scsi.c +++ b/hw/scsi/virtio-scsi.c @@ -367,12 +367,16 @@ static int virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSC= SIReq *req) case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET: target =3D req->req.tmf.lun[1]; s->resetting++; - QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) { + + 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); } } + rcu_read_unlock(); + s->resetting--; break; =20 diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h index e62da68a26..8067497074 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -3,6 +3,8 @@ =20 #include "qemu/queue.h" #include "qemu/bitmap.h" +#include "qemu/rcu.h" +#include "qemu/rcu_queue.h" #include "qom/object.h" #include "hw/hotplug.h" #include "hw/resettable.h" @@ -228,6 +230,7 @@ struct BusClass { }; =20 typedef struct BusChild { + struct rcu_head rcu; DeviceState *child; int index; QTAILQ_ENTRY(BusChild) sibling; @@ -248,6 +251,12 @@ struct BusState { int max_index; bool realized; int num_children; + + /* + * children is a RCU QTAILQ, thus readers must use RCU to access it, + * and writers must hold the big qemu lock + */ + QTAILQ_HEAD(, BusChild) children; QLIST_ENTRY(BusState) sibling; ResettableState reset; --=20 2.26.2 From nobody Fri May 17 03:39:50 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=1601055071; cv=none; d=zohomail.com; s=zohoarc; b=KNFarxW5FUWG39b1oLI0F4UKvXBjq4ET4ApkhUDfuTIPCVZNAGnWaJj35mFVA5Xlz4zEENHBR91HIXl0mbh7nS09wprc38ojlYdRUV18EC3tgKGJtk47WeZadD1yz5ZZC+wimrpxqWvtrt1N9IL9daBmiLPloHtv+c18ttVV5RI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601055071; 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=JTZvVYbHCtKEZqWrhqdVMAWFhECFVonAtlKnivNWpWs=; b=PcOWacA2vo/JeeqESYM0ZEBi3zWwgWP7i999qZTwZ1uS116adAXPI643bOh6Vr+RCOSsDMxw+cShlU6uIEXVS7kcHyZe74/mensVCLc64NzGK4Mh1JytPkIXfC/cSPBCLYBXn0cGQrlAKNyl3OwBVlbLv47oTIN1Yjaq+XeO9Y4= 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) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1601055071005743.292659184328; Fri, 25 Sep 2020 10:31:11 -0700 (PDT) Received: from localhost ([::1]:33606 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kLrYr-0007hS-OD for importer@patchew.org; Fri, 25 Sep 2020 13:31:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35168) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kLrUA-0002Lh-1C for qemu-devel@nongnu.org; Fri, 25 Sep 2020 13:26:18 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:21569) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kLrU7-0007Az-47 for qemu-devel@nongnu.org; Fri, 25 Sep 2020 13:26:17 -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-565-xF1Bchd1OCKQIY_LGIJidA-1; Fri, 25 Sep 2020 13:26:12 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BB737107B769 for ; Fri, 25 Sep 2020 17:26:11 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5AD885D9F3; Fri, 25 Sep 2020 17:26:11 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1601054774; 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=JTZvVYbHCtKEZqWrhqdVMAWFhECFVonAtlKnivNWpWs=; b=eYggmk9u43WqjBIz4XVEEse+CeO/xyXEw/EqNGQwMAXrYfoz5HYX44rfNwVHxTeEngwGsY o3ueCktnWqUSQ7a+HDR6IbTMFzSDO3zK4GjX4FvFua7ZC7tvHjooSr8I56RiJ3FZW5+svv HCm2W/3Y/0Kg8JL5SKdttH3KXFd+obI= X-MC-Unique: xF1Bchd1OCKQIY_LGIJidA-1 From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 06/10] device-core: use atomic_set on .realized property Date: Fri, 25 Sep 2020 13:26:00 -0400 Message-Id: <20200925172604.2142227-7-pbonzini@redhat.com> In-Reply-To: <20200925172604.2142227-1-pbonzini@redhat.com> References: <20200925172604.2142227-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=pbonzini@redhat.com X-Mimecast-Spam-Score: 0 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=216.205.24.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/25 02:48:20 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -32 X-Spam_score: -3.3 X-Spam_bar: --- X-Spam_report: (-3.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.199, 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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, 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: stefanha@redhat.com, mlevitsk@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" From: Maxim Levitsky Some code might race with placement of new devices on a bus. We currently first place a (unrealized) device on the bus and then realize it. As a workaround, users that scan the child device list, can check the realized property to see if it is safe to access such a device. Use an atomic write here too to aid with this. A separate discussion is what to do with devices that are unrealized: It looks like for this case we only call the hotplug handler's unplug callback and its up to it to unrealize the device. An atomic operation doesn't cause harm for this code path though. Signed-off-by: Maxim Levitsky Reviewed-by: Stefan Hajnoczi Message-Id: <20200913160259.32145-6-mlevitsk@redhat.com> Signed-off-by: Paolo Bonzini --- hw/core/qdev.c | 19 ++++++++++++++++++- include/hw/qdev-core.h | 3 +++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/hw/core/qdev.c b/hw/core/qdev.c index 59e5e710b7..fc4daa36fa 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -946,7 +946,25 @@ static void device_set_realized(Object *obj, bool valu= e, Error **errp) } } =20 + qatomic_store_release(&dev->realized, value); + } else if (!value && dev->realized) { + + /* + * Change the value so that any concurrent users are aware + * that the device is going to be unrealized + * + * TODO: change .realized property to enum that states + * each phase of the device realization/unrealization + */ + + qatomic_set(&dev->realized, value); + /* + * Ensure that concurrent users see this update prior to + * any other changes done by unrealize. + */ + smp_wmb(); + QLIST_FOREACH(bus, &dev->child_bus, sibling) { qbus_unrealize(bus); } @@ -961,7 +979,6 @@ static void device_set_realized(Object *obj, bool value= , Error **errp) } =20 assert(local_err =3D=3D NULL); - dev->realized =3D value; return; =20 child_realize_fail: diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h index 8067497074..39490e76ee 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -163,6 +163,9 @@ struct NamedClockList { /** * DeviceState: * @realized: Indicates whether the device has been fully constructed. + * When accessed without the iothread mutex, consider using + * qatomic_load_acquire() before accessing any other field in + * the device. * @reset: ResettableState for the device; handled by Resettable interface. * * This structure should not be accessed directly. We declare it here --=20 2.26.2 From nobody Fri May 17 03:39:50 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=1601054894; cv=none; d=zohomail.com; s=zohoarc; b=lpvjKFEHGD6/+8ajqd6OMJrWA/2rnCK6r/7a/KXS6amrlEkS2RZfxPXpRAKI115TCL6nca/IRCf1Oh9E0XW1CZHHNaBSh0FwnCTu5B6X/WuexXZ8wyzh5YnTasjDT4jGieGEIG2pyRcfGEmZV/qahbndmd1cKzgCpWfGLVRtbaU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601054894; 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=XUpo1DJesgvRqw3d+/1xweFRc1qsON2eoPTXlfvGNrs=; b=D1jP/Y22VkHWUiMQR9B7TVVJd+TtFzT5KK3/3EA5GEaTECRRdCEzCMrCa1cCUnKrz5VdPI0iix788k6HEGCWiUujZ/NhYxjFmdbdyY3wtzIk2eUual01tBIQKXhncyd6ZENbe/oDDixeJx7l+36Rit9hN6kvTO7gLL5xhwU6y9c= 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) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1601054894897699.438385807412; Fri, 25 Sep 2020 10:28:14 -0700 (PDT) Received: from localhost ([::1]:53318 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kLrW1-00043f-CX for importer@patchew.org; Fri, 25 Sep 2020 13:28:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35176) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kLrUA-0002MY-NR for qemu-devel@nongnu.org; Fri, 25 Sep 2020 13:26:18 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:33015) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kLrU7-0007BD-Ok for qemu-devel@nongnu.org; Fri, 25 Sep 2020 13:26:18 -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-238-rqt3pe73M-eZ3_RKk1IEZQ-1; Fri, 25 Sep 2020 13:26:13 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 48CFA10BBED5 for ; Fri, 25 Sep 2020 17:26:12 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id DC8A25D9F3; Fri, 25 Sep 2020 17:26:11 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1601054775; 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=XUpo1DJesgvRqw3d+/1xweFRc1qsON2eoPTXlfvGNrs=; b=EeLu15oZ7QvmhkR/QOOKeVAc1W3fLkoJ1O/qjbpwMem/+k6hBSod9Is7EZ3i3z2pJmMZKa VGlrSrP4S8ZtowAjJ6STBi/lukJk9ZNm3xlZptpGXcumjFGOBEasqvEmXgZnLzrJ3q6NZo gPIqc/8aw/fBq6CM4yIqxKNWtwckoeg= X-MC-Unique: rqt3pe73M-eZ3_RKk1IEZQ-1 From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 07/10] scsi/scsi-bus: scsi_device_find: don't return unrealized devices Date: Fri, 25 Sep 2020 13:26:01 -0400 Message-Id: <20200925172604.2142227-8-pbonzini@redhat.com> In-Reply-To: <20200925172604.2142227-1-pbonzini@redhat.com> References: <20200925172604.2142227-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=pbonzini@redhat.com X-Mimecast-Spam-Score: 0 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=63.128.21.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/25 01:07:33 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -32 X-Spam_score: -3.3 X-Spam_bar: --- X-Spam_report: (-3.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.199, 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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, 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: stefanha@redhat.com, mlevitsk@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" From: Maxim Levitsky The device core first places a device on the bus and then realizes it. Make scsi_device_find avoid returing such devices to avoid races in drivers that use an iothread (currently virtio-scsi) Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=3D1812399 Suggested-by: Paolo Bonzini Signed-off-by: Maxim Levitsky Reviewed-by: Stefan Hajnoczi Message-Id: <20200913160259.32145-7-mlevitsk@redhat.com> Signed-off-by: Paolo Bonzini --- hw/scsi/scsi-bus.c | 83 +++++++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 30 deletions(-) diff --git a/hw/scsi/scsi-bus.c b/hw/scsi/scsi-bus.c index 4ab9811cd8..7599113efe 100644 --- a/hw/scsi/scsi-bus.c +++ b/hw/scsi/scsi-bus.c @@ -24,6 +24,55 @@ static void scsi_target_free_buf(SCSIRequest *req); =20 static int next_scsi_bus; =20 +static SCSIDevice *do_scsi_device_find(SCSIBus *bus, + int channel, int id, int lun, + bool include_unrealized) +{ + BusChild *kid; + SCSIDevice *retval =3D NULL; + + QTAILQ_FOREACH_RCU(kid, &bus->qbus.children, sibling) { + DeviceState *qdev =3D kid->child; + SCSIDevice *dev =3D SCSI_DEVICE(qdev); + + if (dev->channel =3D=3D channel && dev->id =3D=3D id) { + if (dev->lun =3D=3D lun) { + retval =3D dev; + break; + } + + /* + * If we don't find exact match (channel/bus/lun), + * we will return the first device which matches channel/bus + */ + + if (!retval) { + retval =3D dev; + } + } + } + + /* + * This function might run on the IO thread and we might race against + * main thread hot-plugging the device. + * We assume that as soon as .realized is set to true we can let + * the user access the device. + */ + + if (retval && !include_unrealized && + !qatomic_load_acquire(&retval->qdev.realized)) { + retval =3D NULL; + } + + return retval; +} + +SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun) +{ + RCU_READ_LOCK_GUARD(); + return do_scsi_device_find(bus, channel, id, lun, false); +} + static void scsi_device_realize(SCSIDevice *s, Error **errp) { SCSIDeviceClass *sc =3D SCSI_DEVICE_GET_CLASS(s); @@ -137,7 +186,10 @@ static bool scsi_bus_is_address_free(SCSIBus *bus, int channel, int target, int lun, SCSIDevice **p_dev) { - SCSIDevice *d =3D scsi_device_find(bus, channel, target, lun); + SCSIDevice *d; + + RCU_READ_LOCK_GUARD(); + d =3D do_scsi_device_find(bus, channel, target, lun, true); if (d && d->lun =3D=3D lun) { if (p_dev) { *p_dev =3D d; @@ -1570,35 +1622,6 @@ static char *scsibus_get_fw_dev_path(DeviceState *de= v) qdev_fw_name(dev), d->id, d->lun); } =20 -SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun) -{ - BusChild *kid; - SCSIDevice *target_dev =3D NULL; - - RCU_READ_LOCK_GUARD(); - QTAILQ_FOREACH_RCU(kid, &bus->qbus.children, sibling) { - DeviceState *qdev =3D kid->child; - SCSIDevice *dev =3D SCSI_DEVICE(qdev); - - if (dev->channel =3D=3D channel && dev->id =3D=3D id) { - if (dev->lun =3D=3D lun) { - return dev; - } - - /* - * If we don't find exact match (channel/bus/lun), - * we will return the first device which matches channel/bus - */ - - if (!target_dev) { - target_dev =3D dev; - } - } - } - - return target_dev; -} - /* SCSI request list. For simplicity, pv points to the whole device */ =20 static int put_scsi_requests(QEMUFile *f, void *pv, size_t size, --=20 2.26.2 From nobody Fri May 17 03:39:50 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=1601055231; cv=none; d=zohomail.com; s=zohoarc; b=XOF6BiqLwOFt1n52myZDSkTZatjXCONkw1MO/oAtinml6TnqHNVUe5aDx1xoVRdwXfJ5/BtF3Mwfuda93YDa/vmJqg5R+QPbolIZt140KjihMIA7L/ObKdu1RlR0Ws08M+uJHPwbLruXNW4Lhju348628mp3LKhXgzBJj2FYNTM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601055231; 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=dBNCCiCzyYyKKYPSo5jU6dgdBar99zKyX5RHFc77kmI=; b=Hph/cjODbNjBQ/An7dC/wWxWtqooG3axCgwCTB9+3glnoKflK73yBnbRJksm1gEoVgyeJhKwQG0DcBx8yv7HIEX5KSkYqEQ9NW1fxZQPGAtPteJTApVFxOMvVe8PZrRryya6G/okUeMSkOINlQP01yDYVxXnYUK4EmU/kPpaudY= 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 1601055231110937.7726919842303; Fri, 25 Sep 2020 10:33:51 -0700 (PDT) Received: from localhost ([::1]:41278 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kLrbR-0002a6-Mj for importer@patchew.org; Fri, 25 Sep 2020 13:33:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35178) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kLrUB-0002NJ-7l for qemu-devel@nongnu.org; Fri, 25 Sep 2020 13:26:19 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:50502) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kLrU8-0007BV-U5 for qemu-devel@nongnu.org; Fri, 25 Sep 2020 13:26:18 -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-240-f7tWWXISNBOITv0fPq6Kbw-1; Fri, 25 Sep 2020 13:26:13 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DD56A1902EBB; Fri, 25 Sep 2020 17:26:12 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 69EB05D9F3; Fri, 25 Sep 2020 17:26:12 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1601054776; 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=dBNCCiCzyYyKKYPSo5jU6dgdBar99zKyX5RHFc77kmI=; b=jT4zaWRIXreU0KXHJxxQIdUyke3b16pFwOZ7EDF9QIUCsDj+ffWPG0Z8JWMfFDUhVR8YPr Ek9eyYb0aJAbzBM4sjKLe1bd8LeOOlH/4zobxRzAdErov1IGzymX2XqRVkIJcxdUqA/LGR bgkGwHfKHsVB8RP4wk83+KbSwrBcZO8= X-MC-Unique: f7tWWXISNBOITv0fPq6Kbw-1 From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 08/10] scsi/scsi_bus: Add scsi_device_get Date: Fri, 25 Sep 2020 13:26:02 -0400 Message-Id: <20200925172604.2142227-9-pbonzini@redhat.com> In-Reply-To: <20200925172604.2142227-1-pbonzini@redhat.com> References: <20200925172604.2142227-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=pbonzini@redhat.com X-Mimecast-Spam-Score: 0 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=63.128.21.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/25 01:07:33 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -32 X-Spam_score: -3.3 X-Spam_bar: --- X-Spam_report: (-3.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.199, 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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, 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: Stefan Hajnoczi , stefanha@redhat.com, mlevitsk@redhat.com 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" From: Maxim Levitsky Add scsi_device_get which finds the scsi device and takes a reference to it. Suggested-by: Stefan Hajnoczi Signed-off-by: Maxim Levitsky Message-Id: <20200913160259.32145-8-mlevitsk@redhat.com> Signed-off-by: Paolo Bonzini --- Compared to Maxim's patch, I am avoiding the extra argument to do_scsi_device_find by moving the RCU_READ_LOCK_GUARD() out of do_scsi_device_find itself. hw/scsi/scsi-bus.c | 11 +++++++++++ include/hw/scsi/scsi.h | 1 + 2 files changed, 12 insertions(+) diff --git a/hw/scsi/scsi-bus.c b/hw/scsi/scsi-bus.c index 7599113efe..eda8cb7e70 100644 --- a/hw/scsi/scsi-bus.c +++ b/hw/scsi/scsi-bus.c @@ -73,6 +73,17 @@ SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, = int id, int lun) return do_scsi_device_find(bus, channel, id, lun, false); } =20 +SCSIDevice *scsi_device_get(SCSIBus *bus, int channel, int id, int lun) +{ + SCSIDevice *d; + RCU_READ_LOCK_GUARD(); + d =3D do_scsi_device_find(bus, channel, id, lun, false); + if (d) { + object_ref(d); + } + return d; +} + static void scsi_device_realize(SCSIDevice *s, Error **errp) { SCSIDeviceClass *sc =3D SCSI_DEVICE_GET_CLASS(s); diff --git a/include/hw/scsi/scsi.h b/include/hw/scsi/scsi.h index 7a55cdbd74..09fa5c9d2a 100644 --- a/include/hw/scsi/scsi.h +++ b/include/hw/scsi/scsi.h @@ -190,6 +190,7 @@ int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf= , int len, bool fixed); int scsi_SG_IO_FROM_DEV(BlockBackend *blk, uint8_t *cmd, uint8_t cmd_size, uint8_t *buf, uint8_t buf_size); SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int target, int lu= n); +SCSIDevice *scsi_device_get(SCSIBus *bus, int channel, int target, int lun= ); =20 /* scsi-generic.c. */ extern const SCSIReqOps scsi_generic_req_ops; --=20 2.26.2 From nobody Fri May 17 03:39:50 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=1601055232; cv=none; d=zohomail.com; s=zohoarc; b=eG4c5jJHMIWwr+z4V6Ks9JWxwDwP5mFZPuVFnWfTgEuvth/zAvqqFLZNmKDRwu1ZNvY35IydThKriyNlqfQD/AHpfghNiWxls4F9c8He++ZvQw9Hz2S55j3YQCvs5lhAXvm1oMLtf0l616S4giLyXoO0JlLFdCRgIUebNZBLiOE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601055232; 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=Pt5SqnBPWAurqH7od5/LojSGd2ksGXHGLBemqG3ZaZc=; b=Ep8H4lcqtwwQr3Zm5BJbvKJTTnlE0vXtYiojU4aHzuMGLR/OHq2PuRcuDt8fE+NDwCbS5fs/zrvtoJq36LRGQtHwJ5HtmxUR106GLSJNEXfnwrpMzvWcILR6UUPwBCcG4eQft/UT+46sDXuy+D1DS4ugJkSqn3diXvGJdZh8uFA= 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 1601055232509491.9276757787576; Fri, 25 Sep 2020 10:33:52 -0700 (PDT) Received: from localhost ([::1]:41376 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kLrbT-0002cS-93 for importer@patchew.org; Fri, 25 Sep 2020 13:33:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35192) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kLrUD-0002TE-Fu for qemu-devel@nongnu.org; Fri, 25 Sep 2020 13:26:21 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:42277) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kLrUB-0007Bx-H8 for qemu-devel@nongnu.org; Fri, 25 Sep 2020 13:26:21 -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-58-L5GL7B-LOPOnskjCa6nRng-1; Fri, 25 Sep 2020 13:26:14 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7C8B810BBECF; Fri, 25 Sep 2020 17:26:13 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0A5C35D9F3; Fri, 25 Sep 2020 17:26:12 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1601054778; 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=Pt5SqnBPWAurqH7od5/LojSGd2ksGXHGLBemqG3ZaZc=; b=Dwvd/73kg9wrPuYaFhKXWb6rg39yrEd0kMlvyC6DcEB8Bujy3hS0bpPsYdqYIOHaCOkvUz uSYn8XJsHQgDQG1v9F4DIhfQ5hpor+deaGUJJlRLcen4rGB2Wkz8y5NM67OopnMyWxT2Nq BnLDMudkYQHRqc8DoRz5oPe2+5V/Zvs= X-MC-Unique: L5GL7B-LOPOnskjCa6nRng-1 From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 09/10] virtio-scsi: use scsi_device_get Date: Fri, 25 Sep 2020 13:26:03 -0400 Message-Id: <20200925172604.2142227-10-pbonzini@redhat.com> In-Reply-To: <20200925172604.2142227-1-pbonzini@redhat.com> References: <20200925172604.2142227-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=pbonzini@redhat.com X-Mimecast-Spam-Score: 0 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=63.128.21.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/25 01:07:33 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -32 X-Spam_score: -3.3 X-Spam_bar: --- X-Spam_report: (-3.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.199, 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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, 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: Stefan Hajnoczi , stefanha@redhat.com, mlevitsk@redhat.com 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" From: Maxim Levitsky 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 Message-Id: <20200913160259.32145-9-mlevitsk@redhat.com> Signed-off-by: Paolo Bonzini --- 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 From nobody Fri May 17 03:39:50 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=1601055328; cv=none; d=zohomail.com; s=zohoarc; b=QTV4MoVuP6je7PrD+vCFnyabzxHEBALQRjWa9ONLVrOsNGQZYJ1ImsW5ASkwAa6FQUn6e09+hkS2ny4eyUFlnhHCx7t8Po7eUuFTEeb37GZmlvRY4eF+2h3pV8wu0RoujezDdfAcZmN7FYLWDbivgMbavcr8F/7n4mw86tuyRd0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601055328; 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=3dT+wfpw6iT83ImSmB/t0Y9AbcGGqtgwVgpoltnvTmU=; b=fz8cJYLjXIs4OF3FyURsfTlyY5HGCNOEX76u9xgutykr2G7uCjCi8Fqn/j+Ic91VQHTUSF0cstcz4Iy/6OVjEljnUFpZnWZDOAryTVRdnYWJOvtI++AWRnoYfaWEsPGgNPqR5+GuF7bvIFTRv8cyzOlE8MqSUUpIgMgvEjfMNr4= 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) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1601055328682555.8662715572915; Fri, 25 Sep 2020 10:35:28 -0700 (PDT) Received: from localhost ([::1]:46080 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kLrd0-0004bK-CD for importer@patchew.org; Fri, 25 Sep 2020 13:35:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35202) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kLrUE-0002XI-TE for qemu-devel@nongnu.org; Fri, 25 Sep 2020 13:26:22 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:24142) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kLrUC-0007C6-TZ for qemu-devel@nongnu.org; Fri, 25 Sep 2020 13:26:22 -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-329-6fMmWXPNMtCG3rU_ny1OVg-1; Fri, 25 Sep 2020 13:26:18 -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 346ADEA324 for ; Fri, 25 Sep 2020 17:26:17 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id E63F619C66; Fri, 25 Sep 2020 17:26:13 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1601054780; 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=3dT+wfpw6iT83ImSmB/t0Y9AbcGGqtgwVgpoltnvTmU=; b=ZeXzSQ4zDblZ1j0ZpNeZDItaUOwpZ39V0E5p8ColNlu12O2Q3XCn/lHJ2pV6XmGx7XVgUA XcTrCDYF8H1btOK5kyClmvwWBpIl8VReCb/xsKLVmbGmiL1FKpmEw0UtF7S+h0XIRN3jbw wDpqHIvhVfAzhou1eOYtU7U0R83sslI= X-MC-Unique: 6fMmWXPNMtCG3rU_ny1OVg-1 From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 10/10] scsi/scsi_bus: fix races in REPORT LUNS Date: Fri, 25 Sep 2020 13:26:04 -0400 Message-Id: <20200925172604.2142227-11-pbonzini@redhat.com> In-Reply-To: <20200925172604.2142227-1-pbonzini@redhat.com> References: <20200925172604.2142227-1-pbonzini@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=pbonzini@redhat.com X-Mimecast-Spam-Score: 0 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=216.205.24.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/25 02:48:20 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -32 X-Spam_score: -3.3 X-Spam_bar: --- X-Spam_report: (-3.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.199, 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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, 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: stefanha@redhat.com, mlevitsk@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" From: Maxim Levitsky Currently scsi_target_emulate_report_luns iterates over the child device li= st twice, and there is no guarantee that this list is the same in both iterati= ons. The reason for iterating twice is that the first iteration calculates how much memory to allocate. However if we use a dynamic array we can avoid iterating twice, and therefore we avoid this race. Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=3D1866707 Signed-off-by: Maxim Levitsky Reviewed-by: Stefan Hajnoczi Message-Id: <20200913160259.32145-10-mlevitsk@redhat.com> Signed-off-by: Paolo Bonzini --- hw/scsi/scsi-bus.c | 68 ++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/hw/scsi/scsi-bus.c b/hw/scsi/scsi-bus.c index eda8cb7e70..b901e701f0 100644 --- a/hw/scsi/scsi-bus.c +++ b/hw/scsi/scsi-bus.c @@ -438,19 +438,23 @@ struct SCSITargetReq { static void store_lun(uint8_t *outbuf, int lun) { if (lun < 256) { + /* Simple logical unit addressing method*/ + outbuf[0] =3D 0; outbuf[1] =3D lun; - return; + } else { + /* Flat space addressing method */ + outbuf[0] =3D 0x40 | (lun >> 8); + outbuf[1] =3D (lun & 255); } - outbuf[1] =3D (lun & 255); - outbuf[0] =3D (lun >> 8) | 0x40; } =20 static bool scsi_target_emulate_report_luns(SCSITargetReq *r) { BusChild *kid; - int i, len, n; int channel, id; - bool found_lun0; + uint8_t tmp[8] =3D {0}; + int len =3D 0; + GByteArray *buf; =20 if (r->req.cmd.xfer < 16) { return false; @@ -458,46 +462,40 @@ static bool scsi_target_emulate_report_luns(SCSITarge= tReq *r) if (r->req.cmd.buf[2] > 2) { return false; } + + /* reserve space for 63 LUNs*/ + buf =3D g_byte_array_sized_new(512); + channel =3D r->req.dev->channel; id =3D r->req.dev->id; - found_lun0 =3D false; - n =3D 0; =20 - RCU_READ_LOCK_GUARD(); + /* add size (will be updated later to correct value */ + g_byte_array_append(buf, tmp, 8); + len +=3D 8; =20 - QTAILQ_FOREACH_RCU(kid, &r->req.bus->qbus.children, sibling) { - DeviceState *qdev =3D kid->child; - SCSIDevice *dev =3D SCSI_DEVICE(qdev); + /* add LUN0 */ + g_byte_array_append(buf, tmp, 8); + len +=3D 8; =20 - if (dev->channel =3D=3D channel && dev->id =3D=3D id) { - if (dev->lun =3D=3D 0) { - found_lun0 =3D true; + WITH_RCU_READ_LOCK_GUARD() { + QTAILQ_FOREACH_RCU(kid, &r->req.bus->qbus.children, sibling) { + DeviceState *qdev =3D kid->child; + SCSIDevice *dev =3D SCSI_DEVICE(qdev); + + if (dev->channel =3D=3D channel && dev->id =3D=3D id && dev->l= un !=3D 0) { + store_lun(tmp, dev->lun); + g_byte_array_append(buf, tmp, 8); + len +=3D 8; } - n +=3D 8; } } - if (!found_lun0) { - n +=3D 8; - } - - scsi_target_alloc_buf(&r->req, n + 8); - - len =3D MIN(n + 8, r->req.cmd.xfer & ~7); - memset(r->buf, 0, len); - stl_be_p(&r->buf[0], n); - i =3D found_lun0 ? 8 : 16; - QTAILQ_FOREACH_RCU(kid, &r->req.bus->qbus.children, sibling) { - DeviceState *qdev =3D kid->child; - SCSIDevice *dev =3D SCSI_DEVICE(qdev); =20 - if (dev->channel =3D=3D channel && dev->id =3D=3D id) { - store_lun(&r->buf[i], dev->lun); - i +=3D 8; - } - } + r->buf_len =3D len; + r->buf =3D g_byte_array_free(buf, FALSE); + r->len =3D MIN(len, r->req.cmd.xfer & ~7); =20 - assert(i =3D=3D n + 8); - r->len =3D len; + /* store the LUN list length */ + stl_be_p(&r->buf[0], len - 8); return true; } =20 --=20 2.26.2