From nobody Fri Apr 26 00:19:04 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=1594825440; cv=none; d=zohomail.com; s=zohoarc; b=WC2uNCCy8FMbFOQWgyP7pKiC1LBha9MoS8Dk9RRqH051HlGYvTfz9ht645+NLvm7vshfiATzxv+VgXABpJqqlskM/6CgVGNqf+XmpUo4PtRbyZ9bRazxs7qvZvd/MeOLqvIWc+fiDzkinRlmwNA5/3Lw+9D2WP5txF9FwfL6eus= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594825440; 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=Z2PpamRG0wqHHnFH6TPqPqum/uJ9JpSRTxMEU5HEr7w=; b=cCUybpYFc4Vfd6D6lz/1IEhtNA8fS59JSNAJXLrNNhou3bcRVY66m1eoKqjREDl416d+iYXqx3nEFdgaoouSPgD7JzFg1102wWthXM8w85qM6u6WKU51vEM1LuQp1S7SWmz1Tygc3agc4Sgef1QPev3FArckz9frK+/TFSWcdt0= 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 1594825440832120.48429308388666; Wed, 15 Jul 2020 08:04:00 -0700 (PDT) Received: from localhost ([::1]:38598 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jviww-0006W1-Vy for importer@patchew.org; Wed, 15 Jul 2020 11:03:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35104) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jvivL-0004v2-L9 for qemu-devel@nongnu.org; Wed, 15 Jul 2020 11:02:19 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:51702 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 1jvivJ-0004a8-5D for qemu-devel@nongnu.org; Wed, 15 Jul 2020 11:02:19 -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-437-k6ksDQtTPEqYdGpiDnSf3A-1; Wed, 15 Jul 2020 11:02:12 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6AC1F1005262; Wed, 15 Jul 2020 15:02:11 +0000 (UTC) Received: from starship.redhat.com (unknown [10.35.206.178]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5EB58710BE; Wed, 15 Jul 2020 15:02:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594825334; 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=Z2PpamRG0wqHHnFH6TPqPqum/uJ9JpSRTxMEU5HEr7w=; b=VUH6D0cJSk/IWjKkqafN1S7+4K8C4WmI/VvBPnnUDTZXyVpTicyoGsirSNNTX0yJiFxxye BFaqZa/8ZAAuVZn/uV/DTmHwpoktfFZBsyytAv1lYexji1Sdj7nDTs1sTynGH4v9WQSjki 8hcZ4iyt+SHaxC6MsxKH9WL6YAtuc4g= X-MC-Unique: k6ksDQtTPEqYdGpiDnSf3A-1 From: Maxim Levitsky To: qemu-devel@nongnu.org Subject: [PATCH v3 1/7] scsi/scsi_bus: switch search direction in scsi_device_find Date: Wed, 15 Jul 2020 18:01:53 +0300 Message-Id: <20200715150159.95050-2-mlevitsk@redhat.com> In-Reply-To: <20200715150159.95050-1-mlevitsk@redhat.com> References: <20200715150159.95050-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 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=207.211.31.120; envelope-from=mlevitsk@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/07/15 05:07:28 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -40 X-Spam_score: -4.1 X-Spam_bar: ---- X-Spam_report: (-4.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-1, 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" , 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 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 --- 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 df65cc2223..f8adfbc2a5 100644 --- a/hw/scsi/scsi-bus.c +++ b/hw/scsi/scsi-bus.c @@ -1572,7 +1572,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 @@ -1580,7 +1580,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 Apr 26 00:19:04 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=1594825604; cv=none; d=zohomail.com; s=zohoarc; b=UFPmtTKD/lmIZZefgNU5BlEERllqeNNZOCNMpC4PnEg0RmKXHJx2GYkU88lGG8IN7Tamtv5R3cGyTDXyi8NndQngus/A7sCK8Yi1ozbkfZr2g8barb9vXNDfYrEQ0GvHcpMTuvC5uE7Tlevbmpr5bwxrsRFlzhNRDQVkKTmrtj0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594825604; 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=cqQo+2OZgxKCQyPaEbj3n8KYCTJOI/nyva2eHyIbwgQ=; b=XrvnlIdJtIq91rCn5H6LgO+Bp15hNDfLyZgC/hyFKC9tEtgY8G4ObtSlSWqVssYQelrxsu0sJODA0dHvZy/LDXb0crX3/BI26fE/K6SEWMCFcbkBv3n+L6pi0c1LaAbQxBBpCfUv0A0q+CbA6KkZk8tF6aN60AVkZanLuifZuk0= 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 1594825604089384.226793388411; Wed, 15 Jul 2020 08:06:44 -0700 (PDT) Received: from localhost ([::1]:46422 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jviza-0001nM-Oi for importer@patchew.org; Wed, 15 Jul 2020 11:06:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35170) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jvivQ-00052E-QX for qemu-devel@nongnu.org; Wed, 15 Jul 2020 11:02:24 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:24164 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 1jvivN-0004cu-Qi for qemu-devel@nongnu.org; Wed, 15 Jul 2020 11:02:24 -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-288-BaY9oVgxMhS4-GaBXvkPHA-1; Wed, 15 Jul 2020 11:02:15 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 308C3EED22; Wed, 15 Jul 2020 15:02:14 +0000 (UTC) Received: from starship.redhat.com (unknown [10.35.206.178]) by smtp.corp.redhat.com (Postfix) with ESMTP id CC97F6E9F3; Wed, 15 Jul 2020 15:02:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594825340; 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=cqQo+2OZgxKCQyPaEbj3n8KYCTJOI/nyva2eHyIbwgQ=; b=U1DBIj86skd4ICgOLYtnVP1ClukvYxSoIY0QG8k6hcKF2vH3c12xWvlBzJswzNXbKWyDCL ahMF2Ppo/6mqRQIghOKP7VsrbLh6EMUcQlehbqH/QGRWmpQvghARlKFTy3f/OKUek/cFJ6 0MQAI2SlBDoVwnBJoV3pYA4+EkjkvlQ= X-MC-Unique: BaY9oVgxMhS4-GaBXvkPHA-1 From: Maxim Levitsky To: qemu-devel@nongnu.org Subject: [PATCH v3 2/7] Implement drain_call_rcu and use it in hmp_device_del Date: Wed, 15 Jul 2020 18:01:54 +0300 Message-Id: <20200715150159.95050-3-mlevitsk@redhat.com> In-Reply-To: <20200715150159.95050-1-mlevitsk@redhat.com> References: <20200715150159.95050-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 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=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/07/15 02:37:05 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -40 X-Spam_score: -4.1 X-Spam_bar: ---- X-Spam_report: (-4.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-1, 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 allows to preserve the semantics of hmp_device_del, that the device is deleted immediatly which was changed by previos patch that delayed this to RCU callback Signed-off-by: Maxim Levitsky Suggested-by: Stefan Hajnoczi Reviewed-by: Stefan Hajnoczi --- include/qemu/rcu.h | 1 + qdev-monitor.c | 22 +++++++++++++++++++ util/rcu.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/include/qemu/rcu.h b/include/qemu/rcu.h index 570aa603eb..0e375ebe13 100644 --- a/include/qemu/rcu.h +++ b/include/qemu/rcu.h @@ -133,6 +133,7 @@ struct rcu_head { }; =20 extern void call_rcu1(struct rcu_head *head, RCUCBFunc *func); +extern void drain_call_rcu(void); =20 /* The operands of the minus operator must have the same type, * which must be the one that we specify in the cast. diff --git a/qdev-monitor.c b/qdev-monitor.c index 71ebce19df..a889fbc509 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 diff --git a/util/rcu.c b/util/rcu.c index 60a37f72c3..c4fefa9333 100644 --- a/util/rcu.c +++ b/util/rcu.c @@ -293,6 +293,61 @@ void call_rcu1(struct rcu_head *node, void (*func)(str= uct rcu_head *node)) qemu_event_set(&rcu_call_ready_event); } =20 + +struct rcu_drain { + struct rcu_head rcu; + QemuEvent drain_complete_event; +}; + +static void drain_rcu_callback(struct rcu_head *node) +{ + struct rcu_drain *event =3D (struct rcu_drain *)node; + qemu_event_set(&event->drain_complete_event); +} + +/* + * This function ensures that all pending RCU callbacks + * on the current thread are done executing + + * drops big qemu lock during the wait to allow RCU thread + * to process the callbacks + * + */ + +void drain_call_rcu(void) +{ + struct rcu_drain rcu_drain; + bool locked =3D qemu_mutex_iothread_locked(); + + memset(&rcu_drain, 0, sizeof(struct rcu_drain)); + qemu_event_init(&rcu_drain.drain_complete_event, false); + + if (locked) { + qemu_mutex_unlock_iothread(); + } + + + /* + * RCU callbacks are invoked in the same order as in which they + * are registered, thus we can be sure that when 'drain_rcu_callback' + * is called, all RCU callbacks that were registered on this thread + * prior to calling this function are completed. + * + * Note that since we have only one global queue of the RCU callbacks, + * we also end up waiting for most of RCU callbacks that were register= ed + * on the other threads, but this is a side effect that shoudn't be + * assumed. + */ + + call_rcu1(&rcu_drain.rcu, drain_rcu_callback); + qemu_event_wait(&rcu_drain.drain_complete_event); + + if (locked) { + qemu_mutex_lock_iothread(); + } + +} + void rcu_register_thread(void) { assert(rcu_reader.ctr =3D=3D 0); --=20 2.26.2 From nobody Fri Apr 26 00:19:04 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=1594825445; cv=none; d=zohomail.com; s=zohoarc; b=YtEcmHLeiHe0ZHmUlQJtiH3DokU+dudwDbKBM2rEPLfYcWXih8JD7zIl1TdAcYYUrJ+wQGYd/kX8i3KlqB09UfC6atsbbViXBQT4ph2R44Wpgbkg8gceU08vxkmCC453niZd88Bx3YSmWSMtY3S7DJefpIyoyD0vGRUwdBkV9c4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594825445; 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=gNm+/Sgz5NsXc0H2tZ47ZSPgt3Aq1uKThGxmPJ79Lew=; b=kXAqyFvBZyNmtGvNcpdowwjDDHwWWnLvPNm4y1v26TygciZKq80lP0dwAdk+uHyjowGtxrntUj7QIehMZseQL/Yt5EUobXpXtfBDAaRmQYgEO8fzy5HcqGT4LlJ3s/pbGzIw30SI77tqea4nSNFdwh3nSJOGGeFhBNYK6mGyQQk= 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 1594825445302892.0364506488797; Wed, 15 Jul 2020 08:04:05 -0700 (PDT) Received: from localhost ([::1]:39024 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jvix1-0006h7-OB for importer@patchew.org; Wed, 15 Jul 2020 11:04:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35164) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jvivQ-00050y-2J for qemu-devel@nongnu.org; Wed, 15 Jul 2020 11:02:24 -0400 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:44711 helo=us-smtp-delivery-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 1jvivM-0004cM-Ae for qemu-devel@nongnu.org; Wed, 15 Jul 2020 11:02:23 -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-181-oyhSkjwcMqGb1UmydI915Q-1; Wed, 15 Jul 2020 11:02:17 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7F9A480BCBD; Wed, 15 Jul 2020 15:02:16 +0000 (UTC) Received: from starship.redhat.com (unknown [10.35.206.178]) by smtp.corp.redhat.com (Postfix) with ESMTP id 93DC96E9F3; Wed, 15 Jul 2020 15:02:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594825339; 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=gNm+/Sgz5NsXc0H2tZ47ZSPgt3Aq1uKThGxmPJ79Lew=; b=U1ip33lh6vcuqMnPbu9ZFJOyaFRd7L276hPeL/jrKRn+hiTbwKxootSM/NoPMxat4x1UOH X7SaZ+zL0p4EuC2oM92sMj77ynoZYL42uJnxUCUu67xUGx8T4VKsqrXUTthSJQLNBXIsKU d+rRo4PohA3XCYjR9cI9BRbRjRq7AS8= X-MC-Unique: oyhSkjwcMqGb1UmydI915Q-1 From: Maxim Levitsky To: qemu-devel@nongnu.org Subject: [PATCH v3 3/7] device-core: use RCU for list of childs of a bus Date: Wed, 15 Jul 2020 18:01:55 +0300 Message-Id: <20200715150159.95050-4-mlevitsk@redhat.com> In-Reply-To: <20200715150159.95050-1-mlevitsk@redhat.com> References: <20200715150159.95050-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 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=207.211.31.81; envelope-from=mlevitsk@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/07/15 02:01:40 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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" , Maxim Levitsky , 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 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 --- hw/core/bus.c | 28 ++++++++++++++++++---------- hw/core/qdev.c | 37 +++++++++++++++++++++++-------------- hw/scsi/scsi-bus.c | 17 ++++++++++++++--- hw/scsi/virtio-scsi.c | 6 +++++- include/hw/qdev-core.h | 9 +++++++++ 5 files changed, 69 insertions(+), 28 deletions(-) diff --git a/hw/core/bus.c b/hw/core/bus.c index 6b987b6946..385eb3ad5a 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,9 +92,13 @@ 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) { + rcu_read_lock(); + + QTAILQ_FOREACH_RCU(kid, &bus->children, sibling) { cb(OBJECT(kid->child), opaque, type); } + + rcu_read_unlock(); } =20 static void qbus_init(BusState *bus, DeviceState *parent, const char *name) @@ -194,9 +200,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 01796823b4..ee69b11d11 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); @@ -689,17 +696,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 f8adfbc2a5..92d412b65c 100644 --- a/hw/scsi/scsi-bus.c +++ b/hw/scsi/scsi-bus.c @@ -400,7 +400,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(); + + QTAILQ_FOREACH_RCU(kid, &r->req.bus->qbus.children, sibling) { DeviceState *qdev =3D kid->child; SCSIDevice *dev =3D SCSI_DEVICE(qdev); =20 @@ -421,7 +424,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 @@ -430,6 +433,9 @@ static bool scsi_target_emulate_report_luns(SCSITargetR= eq *r) i +=3D 8; } } + + rcu_read_unlock(); + assert(i =3D=3D n + 8); r->len =3D len; return true; @@ -1572,12 +1578,15 @@ SCSIDevice *scsi_device_find(SCSIBus *bus, int chan= nel, int id, int lun) BusChild *kid; SCSIDevice *target_dev =3D NULL; =20 - QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) { + rcu_read_lock(); + + QTAILQ_FOREACH_RCU(kid, &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) { if (dev->lun =3D=3D lun) { + rcu_read_unlock(); return dev; } =20 @@ -1591,6 +1600,8 @@ SCSIDevice *scsi_device_find(SCSIBus *bus, int channe= l, int id, int lun) } } } + + rcu_read_unlock(); return target_dev; } =20 diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c index b49775269e..d3cfc8dfb6 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 fe78073c70..e252640c1a 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" @@ -230,6 +232,7 @@ struct BusClass { }; =20 typedef struct BusChild { + struct rcu_head rcu; DeviceState *child; int index; QTAILQ_ENTRY(BusChild) sibling; @@ -250,6 +253,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 Apr 26 00:19:04 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=1594825613; cv=none; d=zohomail.com; s=zohoarc; b=bJ9Nf6q8f6eEcqP/Fi8ZQK/kqjIC/OP43x6lCN+aMM/qyVQ1NcZFp0C9gs7bi+XivzDGTnDDlGlw/2pEQNSjkrW+SqcIwO4GmA3JCLU+OPB5v7qb66vHu9B1FtdP/+iHomysiyqDHO3nkCqiYoN9P4Gc37f100GrRVMmyw05dVg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594825613; 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=7Lc3GPEP4WXv0QcCSmMLHeZu/ejwA37Ij+YnrXr6y/0=; b=SWLZ9820fdiuRVzVKZ8jYESr7PavlQwOWtJxbNi7KTzHoM8CQP8ioh+JH1nscDltwuzMtlDs92xTsxu3Cb9b0+fXlN4HGqkPD8DHPwFrRFBV5hiVlLbtVd+koR6xZjGBi/GPiPrUL40AlLajp6RbEU78UiG8ZbGqnkMTttn900Q= 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 1594825613722827.4997682115213; Wed, 15 Jul 2020 08:06:53 -0700 (PDT) Received: from localhost ([::1]:47448 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jvizk-0002DM-Hl for importer@patchew.org; Wed, 15 Jul 2020 11:06:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35166) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jvivQ-00051R-9S for qemu-devel@nongnu.org; Wed, 15 Jul 2020 11:02:24 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:22941 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 1jvivO-0004db-H7 for qemu-devel@nongnu.org; Wed, 15 Jul 2020 11:02:24 -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-53-IlsBy40PN6CyXgG4Sza-_Q-1; Wed, 15 Jul 2020 11:02:19 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D2FBE18C63C0; Wed, 15 Jul 2020 15:02:18 +0000 (UTC) Received: from starship.redhat.com (unknown [10.35.206.178]) by smtp.corp.redhat.com (Postfix) with ESMTP id E2F0D710C8; Wed, 15 Jul 2020 15:02:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594825341; 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=7Lc3GPEP4WXv0QcCSmMLHeZu/ejwA37Ij+YnrXr6y/0=; b=IPaWSGuEwJmeBxs5jfonlVd00fvYurWj1OTvNLVq9yU7jSE8bml27CXSnT7MNYQBEi6qQE Lqqw426AxjxYIuzGJOUKjpfJx44OOy0NQaRcYhJs+Vjyao7KkXezAjs674gBzUrBzTwOBn zrodo/Ez5S0DSv0D+mXomXeXuRDeiNE= X-MC-Unique: IlsBy40PN6CyXgG4Sza-_Q-1 From: Maxim Levitsky To: qemu-devel@nongnu.org Subject: [PATCH v3 4/7] device-core: use atomic_set on .realized property Date: Wed, 15 Jul 2020 18:01:56 +0300 Message-Id: <20200715150159.95050-5-mlevitsk@redhat.com> In-Reply-To: <20200715150159.95050-1-mlevitsk@redhat.com> References: <20200715150159.95050-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mlevitsk@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=207.211.31.120; envelope-from=mlevitsk@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/07/15 05:07:28 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -40 X-Spam_score: -4.1 X-Spam_bar: ---- X-Spam_report: (-4.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-1, 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" , Maxim Levitsky , 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" 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 --- hw/core/qdev.c | 19 ++++++++++++++++++- include/hw/qdev-core.h | 2 ++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/hw/core/qdev.c b/hw/core/qdev.c index ee69b11d11..2ec4971aec 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -966,7 +966,25 @@ static void device_set_realized(Object *obj, bool valu= e, Error **errp) } } =20 + atomic_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 + */ + + atomic_set(&dev->realized, value); + /* + * execute full memory barrier to ensure that concurrent users + * see this update prior to any other changes to the device + */ + smp_mb(); + QLIST_FOREACH(bus, &dev->child_bus, sibling) { qbus_unrealize(bus); } @@ -981,7 +999,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 e252640c1a..5faa715449 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -165,6 +165,8 @@ struct NamedClockList { /** * DeviceState: * @realized: Indicates whether the device has been fully constructed. + * When accessed outsize big qemu lock, must be accessed with + * atomic_load_acquire() * @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 Apr 26 00:19:04 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=1594825686; cv=none; d=zohomail.com; s=zohoarc; b=WsRiJrC+lqdEvcn16IlUbqDOwSwl8F5+N3C7YvaTuviHfbOqHePwLN6L/2hxhd5Z0Vdxsw8IatwNCxrPEZ7gXCBa+0pGsseIQ7apYIkG2+rIANfDumN3YmKUOg1OzjrDnNAhFUkzhg59NzSY9ty7FM9i1im8DvjNnjhaF2YLjqU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594825686; 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=+utk7vdtsJuSzO2tlgUo6VEKGiMN7Q7PQ8wDq9ljiog=; b=fhfu3TGFxN5GpbuqLQtTp+Pypj3VqJwRJlyF0ZDE7XEXz0EdG3mPI3qunSF6IGrlV77OyzQUZXuCkvV7qfY/pl0jZPAPIXz7/e4VYS97qPgtdhrF8/XrvzM4v5Y9X5P4djOu3afs28ONbhAW/ADMJQTyGPl9Wesv8nufgE42+ow= 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 1594825686767972.6662004859248; Wed, 15 Jul 2020 08:08:06 -0700 (PDT) Received: from localhost ([::1]:52902 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jvj0v-0004UB-HP for importer@patchew.org; Wed, 15 Jul 2020 11:08:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35182) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jvivT-00057R-Hu for qemu-devel@nongnu.org; Wed, 15 Jul 2020 11:02:27 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:50855 helo=us-smtp-delivery-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 1jvivR-0004eD-S5 for qemu-devel@nongnu.org; Wed, 15 Jul 2020 11:02:27 -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-126-_DaDs8z-NkaQUDmSs3IxlQ-1; Wed, 15 Jul 2020 11:02:23 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 661FF1005273; Wed, 15 Jul 2020 15:02:21 +0000 (UTC) Received: from starship.redhat.com (unknown [10.35.206.178]) by smtp.corp.redhat.com (Postfix) with ESMTP id 40DA4710C8; Wed, 15 Jul 2020 15:02:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594825345; 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=+utk7vdtsJuSzO2tlgUo6VEKGiMN7Q7PQ8wDq9ljiog=; b=dhYt0dKKiL1+7+tHnIGh3Q+pclJ26xYpXhMlljgEKIL3+pRG2uDqIM2pQc5nyambmG28Yn QuTzO+d+FxxJXBYDepiO0vYLQ8JT4qf5KfL6R7CTOIrm2tAQkfWjw/XtNwz+er/x1hLqhg +M4/h1O5+lXTp25GB52GlKGfGG4lag8= X-MC-Unique: _DaDs8z-NkaQUDmSs3IxlQ-1 From: Maxim Levitsky To: qemu-devel@nongnu.org Subject: [PATCH v3 5/7] virtio-scsi: don't touch scsi devices that are not yet realized or about to be un-realized Date: Wed, 15 Jul 2020 18:01:57 +0300 Message-Id: <20200715150159.95050-6-mlevitsk@redhat.com> In-Reply-To: <20200715150159.95050-1-mlevitsk@redhat.com> References: <20200715150159.95050-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mlevitsk@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=205.139.110.61; envelope-from=mlevitsk@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/07/15 02:37:03 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -40 X-Spam_score: -4.1 X-Spam_bar: ---- X-Spam_report: (-4.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-1, 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" , 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" Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=3D1812399 Suggested-by: Paolo Bonzini Signed-off-by: Maxim Levitsky Reviewed-by: Stefan Hajnoczi --- hw/scsi/virtio-scsi.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c index d3cfc8dfb6..18952e0f1c 100644 --- a/hw/scsi/virtio-scsi.c +++ b/hw/scsi/virtio-scsi.c @@ -35,13 +35,30 @@ static inline int virtio_scsi_get_lun(uint8_t *lun) =20 static inline SCSIDevice *virtio_scsi_device_find(VirtIOSCSI *s, uint8_t *= lun) { + SCSIDevice *device =3D NULL; + if (lun[0] !=3D 1) { return NULL; } 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)); + + device =3D scsi_device_find(&s->bus, 0, lun[1], virtio_scsi_get_lun(lu= n)); + + /* + * 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 (!device || !atomic_load_acquire(&device->qdev.realized)) { + return NULL; + } + + return device; } =20 void virtio_scsi_init_req(VirtIOSCSI *s, VirtQueue *vq, VirtIOSCSIReq *req) --=20 2.26.2 From nobody Fri Apr 26 00:19:04 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=1594825611; cv=none; d=zohomail.com; s=zohoarc; b=Za/2e+KCnxUh8/n22KDMrtPYZkbuZPn1TP5028cG9QObU15Krbxx59PAUXEW9H5AFIyaYfHMH+HZu9ApxHYESRC536PFISwJBNCx5abPudlZXCy4VhSMBiiXw+HsCQGrWg2suVDlW6Gn3trgMZf2XBSzx0J4KU8DFTVfcm/rSTA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594825611; 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=hDEwTu1kqrxJiOyfbadfn4Dmf5+f76KNImVXoqi+IiA=; b=GeZjyACL4zQtZffR0uFkSuE5xkNj2xbXWrMEskPRLgWU8yJx7WpG2oYuNkT/g30S3zPuwQAxfTlCnFIzNIdA5U4VWVVUTd/wCMpI6b+zyjIV3c19pb+vZCsQbqHSpRR0+73pFFlpsI/S6ywJuTqEh3m9sB5NQmf8AFvB5CxH/A8= 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 1594825611215771.388934104896; Wed, 15 Jul 2020 08:06:51 -0700 (PDT) Received: from localhost ([::1]:47110 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jvizh-00024b-SD for importer@patchew.org; Wed, 15 Jul 2020 11:06:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35236) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jvivh-0005Vr-CR for qemu-devel@nongnu.org; Wed, 15 Jul 2020 11:02:41 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:35473 helo=us-smtp-delivery-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 1jvivf-0004jn-Of for qemu-devel@nongnu.org; Wed, 15 Jul 2020 11:02:41 -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-24-i3-bQz63Mk2yRu6CXMLa5Q-1; Wed, 15 Jul 2020 11:02:28 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EF536100CCC3; Wed, 15 Jul 2020 15:02:26 +0000 (UTC) Received: from starship.redhat.com (unknown [10.35.206.178]) by smtp.corp.redhat.com (Postfix) with ESMTP id C86766E9F3; Wed, 15 Jul 2020 15:02:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594825358; 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=hDEwTu1kqrxJiOyfbadfn4Dmf5+f76KNImVXoqi+IiA=; b=a+zvoTyBNGoaYowDmcZnpCvcfMLRFasSsHpguRGrywdBFQxskWyZLt4InZMR1pHvjAcLyc x+tpSBJaKa3e+5xqV+lrlI0jhuf1CJRTbawHmWmvlRMiIony6guoQ4bUobGwQ0sMJ6tIlB JWcKhKrXzr5qN8hV7WDnbbl/bG0+SQg= X-MC-Unique: i3-bQz63Mk2yRu6CXMLa5Q-1 From: Maxim Levitsky To: qemu-devel@nongnu.org Subject: [PATCH v3 6/7] scsi: Add scsi_device_get Date: Wed, 15 Jul 2020 18:01:58 +0300 Message-Id: <20200715150159.95050-7-mlevitsk@redhat.com> In-Reply-To: <20200715150159.95050-1-mlevitsk@redhat.com> References: <20200715150159.95050-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mlevitsk@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=205.139.110.61; envelope-from=mlevitsk@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/07/15 02:37:03 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -40 X-Spam_score: -4.1 X-Spam_bar: ---- X-Spam_report: (-4.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-1, 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 , 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" Add scsi_device_get which finds the scsi device and takes a reference to it. Suggested-by: Stefan Hajnoczi Signed-off-by: Maxim Levitsky Reviewed-by: Stefan Hajnoczi --- hw/scsi/scsi-bus.c | 31 ++++++++++++++++++++++++------- include/hw/scsi/scsi.h | 2 ++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/hw/scsi/scsi-bus.c b/hw/scsi/scsi-bus.c index 92d412b65c..677074eaa6 100644 --- a/hw/scsi/scsi-bus.c +++ b/hw/scsi/scsi-bus.c @@ -1572,8 +1572,13 @@ static char *scsibus_get_fw_dev_path(DeviceState *de= v) return g_strdup_printf("channel@%x/%s@%x,%x", d->channel, qdev_fw_name(dev), d->id, d->lun); } - -SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun) +/* + * Finds a matching channel/id/lun device on scsi bus, and + * takes a reference to it and returns it. + * If we don't find exact match (channel/bus/lun), + * we will return the first device which matches channel/bus + */ +SCSIDevice *scsi_device_get(SCSIBus *bus, int channel, int id, int lun) { BusChild *kid; SCSIDevice *target_dev =3D NULL; @@ -1586,25 +1591,37 @@ SCSIDevice *scsi_device_find(SCSIBus *bus, int chan= nel, int id, int lun) =20 if (dev->channel =3D=3D channel && dev->id =3D=3D id) { if (dev->lun =3D=3D lun) { + object_ref(OBJECT(dev)); rcu_read_unlock(); return dev; } =20 - /* - * 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; } } } =20 + object_ref(OBJECT(target_dev)); rcu_read_unlock(); return target_dev; } =20 +/* + * This function works like scsi_device_get but doesn't take a reference + * to the returned object. + * Devices that run under the QEMU global mutex can use this function. + * Devices that run outside the QEMU global mutex must use + * scsi_device_get() instead. + */ +SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun) +{ + SCSIDevice *dev =3D scsi_device_get(bus, channel, id, lun); + + object_unref(OBJECT(dev)); + return 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, diff --git a/include/hw/scsi/scsi.h b/include/hw/scsi/scsi.h index 2fc23e44ba..6e11ece641 100644 --- a/include/hw/scsi/scsi.h +++ b/include/hw/scsi/scsi.h @@ -193,7 +193,9 @@ void scsi_generic_read_device_inquiry(SCSIDevice *dev); int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fix= ed); 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 Apr 26 00:19:04 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=1594825486; cv=none; d=zohomail.com; s=zohoarc; b=gTqndtsEttmeVu3yLLFnGQK09FOViz3VKv2nxTIZbmXEl2dC0h4RytHqQkT09OkpOimkO9jozfi9xPnLLFxXSZmSi7Jlt4tG5xz2MV6Cbh0qlmqtI+cKaa2eSLDrk7znCDfUkyNQpvw+tlaAso6jpr1R6PhQlynOn/GeedUbeL0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594825486; 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=HciT8LO9Vg/4MpnhSURYrbdgjrUkmbVUYShOMTWNEtw=; b=kcOzxHMCKseBv4QVdDZ8ewIT1f/trI5RgS8dZtzVYqwq8oXwWgSbmKCJSvIws1aWUrimgZziBYZRA5uKifTngCULGq4jAXpXECvfouf+18lr6ci9pPE1VKFOtosLUTfw36j5kkS4TH8IwGp2AhyY1XMQ19jCrcTqm/LjWhX2EBQ= 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 159482548662849.057205497464906; Wed, 15 Jul 2020 08:04:46 -0700 (PDT) Received: from localhost ([::1]:40424 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jvixh-0007cE-0G for importer@patchew.org; Wed, 15 Jul 2020 11:04:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35216) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jvivf-0005TL-Ql for qemu-devel@nongnu.org; Wed, 15 Jul 2020 11:02:39 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:44550 helo=us-smtp-delivery-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 1jvive-0004je-7x for qemu-devel@nongnu.org; Wed, 15 Jul 2020 11:02:39 -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-398-ekLxcARZNYC_tEPdMQGqjg-1; Wed, 15 Jul 2020 11:02:33 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id AE206100CCC0; Wed, 15 Jul 2020 15:02:32 +0000 (UTC) Received: from starship.redhat.com (unknown [10.35.206.178]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5CFDE6E9F3; Wed, 15 Jul 2020 15:02:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594825357; 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=HciT8LO9Vg/4MpnhSURYrbdgjrUkmbVUYShOMTWNEtw=; b=SVAo+vyNg9V82Thh/sWy49/0pnl2yQxusWNdGqjbCpuCnxjFAVJ0waRxAMOSLc2BNS3a+j dVCShsQBM0llEtQGCp4Ds4w7B5oDn/mCUOvumXWlygknTViBSrBEndZDYRkvoP+/hwNNgb IOiy/N8n/GxxTnLKOlbbWhVVNjZrEPE= X-MC-Unique: ekLxcARZNYC_tEPdMQGqjg-1 From: Maxim Levitsky To: qemu-devel@nongnu.org Subject: [PATCH v3 7/7] virtio-scsi: use scsi_device_get Date: Wed, 15 Jul 2020 18:01:59 +0300 Message-Id: <20200715150159.95050-8-mlevitsk@redhat.com> In-Reply-To: <20200715150159.95050-1-mlevitsk@redhat.com> References: <20200715150159.95050-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 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=205.139.110.61; envelope-from=mlevitsk@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/07/15 02:37:03 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -40 X-Spam_score: -4.1 X-Spam_bar: ---- X-Spam_report: (-4.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-1, 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 | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c index 18952e0f1c..a4870b155d 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) { SCSIDevice *device =3D NULL; =20 @@ -44,7 +44,7 @@ static inline SCSIDevice *virtio_scsi_device_find(VirtIOS= CSI *s, uint8_t *lun) return NULL; } =20 - device =3D scsi_device_find(&s->bus, 0, lun[1], virtio_scsi_get_lun(lu= n)); + device =3D scsi_device_get(&s->bus, 0, lun[1], virtio_scsi_get_lun(lun= )); =20 /* * This function might run on the IO thread and we might race against @@ -55,12 +55,15 @@ static inline SCSIDevice *virtio_scsi_device_find(VirtI= OSCSI *s, uint8_t *lun) */ =20 if (!device || !atomic_load_acquire(&device->qdev.realized)) { + object_unref(OBJECT(device)); return NULL; } =20 return device; } =20 + + void virtio_scsi_init_req(VirtIOSCSI *s, VirtQueue *vq, VirtIOSCSIReq *req) { VirtIODevice *vdev =3D VIRTIO_DEVICE(s); @@ -273,7 +276,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; @@ -387,10 +390,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 @@ -403,14 +406,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 @@ -581,7 +587,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); @@ -597,10 +603,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