From nobody Sun Feb 8 23:40:50 2026 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=1598886380; cv=none; d=zohomail.com; s=zohoarc; b=Yvc64+t7PsDq1pJ2Q31U1nbfKYuLjxxK0rDhS3IL2gt3qELc6l+04/Lj3KEHaa9y7cEsZUoLraISPsksk+oE3VBLQ4iSv5YvsCPHh/jhaYPBcGTVdAwgWpcyrfmA+JWUQQ6hkn1e9/rARIv6rIqYrF/2jnepvX0lVo2mGPhmF48= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598886380; 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=UYBxwTQyyGOtQGAqlQ5/TvlKhMJaYSGTdQ/u9XBWqSZgJKgYqexNAnonhwpfZSLpGsCUgcpYDsxbwoRQuOPOcpXksm2f/Vj56P7nK61N4vsnQ9EeKsnAcgHVSHLUJN29wNJqLdJq+AVgoI6jIGYEOVNCGQHkDoB2oDDIyd9cXDw= 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 1598886380077217.8993265442483; Mon, 31 Aug 2020 08:06:20 -0700 (PDT) Received: from localhost ([::1]:44734 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kClNy-0003l2-RG for importer@patchew.org; Mon, 31 Aug 2020 11:06:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44176) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kClJX-0006BD-NY for qemu-devel@nongnu.org; Mon, 31 Aug 2020 11:01:45 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:46879) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kClJW-0004VD-2N for qemu-devel@nongnu.org; Mon, 31 Aug 2020 11:01:43 -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-310-5We50y58NeWRRyTG-dG5xg-1; Mon, 31 Aug 2020 11:01:37 -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 35D28801ADD; Mon, 31 Aug 2020 15:01:36 +0000 (UTC) Received: from localhost.localdomain (unknown [10.35.206.190]) by smtp.corp.redhat.com (Postfix) with ESMTP id E906D19C4F; Mon, 31 Aug 2020 15:01:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1598886101; 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=O/jWW8W/0+lsWBp3f423ajdZeh4hMpzvZDcf3OZzplJuZAzoG2KAIcUMUl0kq6P2U9yN41 MOavhpt+4sSTBSnLd37suORtHWfJdu9ER7B6dIK07ESy7K/qM3HXxXe+eV+myt9k4Howek 2aKvHXXDvcaz6vj7CKxXw1SwIIVFe6M= X-MC-Unique: 5We50y58NeWRRyTG-dG5xg-1 From: Maxim Levitsky To: qemu-devel@nongnu.org Subject: [PATCH v4 1/9] scsi/scsi_bus: switch search direction in scsi_device_find Date: Mon, 31 Aug 2020 18:01:16 +0300 Message-Id: <20200831150124.206267-2-mlevitsk@redhat.com> In-Reply-To: <20200831150124.206267-1-mlevitsk@redhat.com> References: <20200831150124.206267-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mlevitsk@redhat.com X-Mimecast-Spam-Score: 0.001 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=63.128.21.124; envelope-from=mlevitsk@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/31 03:02:42 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, 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: 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 Sun Feb 8 23:40:50 2026 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=1598886197; cv=none; d=zohomail.com; s=zohoarc; b=EjQCSrnwMCScz9Le3SrwJa4Tdb7NXRKL1b5w3qxx7qsP8FsHOLsfinN+93/Tik+EDMzqtailN1PEHg/lxfGBzEUYMtotIogibHovZemgawetxmBBLSzNX61oJFxQ3buzj4A9AVzv7NlqVBKh8bMrZtjGo5lDal3Fk7NrvM1Tesg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598886197; 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=qFxxKXv45+YeuWDgfHdSeowkU7NVsNR1P4xUgcB+O18=; b=e8eKuIybueYXbHawgqHycJdWjNn+JIprMWSq4RLwLt0by4aYFOJU5HyRWxAJEDF606MVvBojDxfl2oE1jXPWSELD1yNoI9iJgvXy5EqvJbXcXlhQSyRXqWXIkYBFrc24pLH/91M5seKGZeMEyIdxrij3EQ8DL6gLGICnllvYiTo= 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 1598886197239608.2411561980524; Mon, 31 Aug 2020 08:03:17 -0700 (PDT) Received: from localhost ([::1]:34966 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kClL1-0007hu-Qn for importer@patchew.org; Mon, 31 Aug 2020 11:03:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44194) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kClJZ-0006Bf-JK for qemu-devel@nongnu.org; Mon, 31 Aug 2020 11:01:46 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:25507) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kClJX-0004VH-As for qemu-devel@nongnu.org; Mon, 31 Aug 2020 11:01:45 -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-395-CeA76xckOCqariznq96bNQ-1; Mon, 31 Aug 2020 11:01:40 -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 30C71801AC5; Mon, 31 Aug 2020 15:01:39 +0000 (UTC) Received: from localhost.localdomain (unknown [10.35.206.190]) by smtp.corp.redhat.com (Postfix) with ESMTP id A069019C4F; Mon, 31 Aug 2020 15:01:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1598886102; 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=qFxxKXv45+YeuWDgfHdSeowkU7NVsNR1P4xUgcB+O18=; b=P5XVRr+3Q3/iXeLA2SJwb4bmrSG6tmcyVc1YQ0eW6GnRFh+JU4kt+f+jOWTJBWxZGEONvZ wmaYZFhrg4nATGgrkAjJN3Byb/kL/fYzq+or1O+GEfoAizWM0EDt4IwtBmpecPLjBij4gz sCyhzPRdzvFTSNpPj91LRmGaqHGMcWA= X-MC-Unique: CeA76xckOCqariznq96bNQ-1 From: Maxim Levitsky To: qemu-devel@nongnu.org Subject: [PATCH v4 2/9] rcu: Implement drain_call_rcu Date: Mon, 31 Aug 2020 18:01:17 +0300 Message-Id: <20200831150124.206267-3-mlevitsk@redhat.com> In-Reply-To: <20200831150124.206267-1-mlevitsk@redhat.com> References: <20200831150124.206267-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mlevitsk@redhat.com X-Mimecast-Spam-Score: 0.002 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=mlevitsk@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/31 03:43:46 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, 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: 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 allow is 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 + util/rcu.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 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/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 Sun Feb 8 23:40:50 2026 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=1598886287; cv=none; d=zohomail.com; s=zohoarc; b=iTJ1tk/R/qTX1VBpT14GwlLg6TxrARJTCiqx9G4kp+KX1zG6r9UKaf9a4AQJphUT6GE0rkudMeR5VlgDbR5Dw34vd7zHmSvqgDN5fQaIj+wokeE6mLbNgnNR3u/Ly/6h1fUKTVPuxt1zxc0iyaj2YI45NvSOMqVCUe2yqCnDkY8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598886287; 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=T9AFma23LBkYxDC875+y6zL/RyokUGeWCTcMzCRTxYM=; b=Q+chOF8DvEKkrwmCWqgAb1xrt0dl9o90ie5ruu1wV8NQaLDQGNksNS5dK4I65ZcdYUw0mseKWoQ8PEjPQsqDlkcr8qCwu9w0mK5wr81RzGrA0NnF/B3K4yrKYo3ZrJ4TgYT+pZuh7u9r1SGoHSe6Pv6zZ+ECHiKfOzLtlmoUReg= 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 159888628713351.759329012742; Mon, 31 Aug 2020 08:04:47 -0700 (PDT) Received: from localhost ([::1]:40732 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kClMT-00021e-T1 for importer@patchew.org; Mon, 31 Aug 2020 11:04:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44208) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kClJc-0006D8-RB for qemu-devel@nongnu.org; Mon, 31 Aug 2020 11:01:48 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:27502) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kClJb-0004VV-0Z for qemu-devel@nongnu.org; Mon, 31 Aug 2020 11:01:48 -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-23-Us6tPSUeMwOqoIvWM3hLuA-1; Mon, 31 Aug 2020 11:01:44 -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 2A06618A224C; Mon, 31 Aug 2020 15:01:42 +0000 (UTC) Received: from localhost.localdomain (unknown [10.35.206.190]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9A52219C4F; Mon, 31 Aug 2020 15:01:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1598886106; 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=T9AFma23LBkYxDC875+y6zL/RyokUGeWCTcMzCRTxYM=; b=c5UdXglhy4cTwqbbHBFpNYgz64j2w9GZLOW1fz4Kbb3a0JG1Y0Noz6CO5PpVSSb8XojI5o NeQctQ2bJzUV5j5PjQWDpdZdymHlgFp6mMoLDPcgs9dtUrLToMczXFkLr46TkCnl+S5oVX 5CCzEe7sDQgE7gmWqi9NOUDJaD+VCW0= X-MC-Unique: Us6tPSUeMwOqoIvWM3hLuA-1 From: Maxim Levitsky To: qemu-devel@nongnu.org Subject: [PATCH v4 3/9] device_core: use drain_call_rcu in in hmp_device_del/qmp_device_add Date: Mon, 31 Aug 2020 18:01:18 +0300 Message-Id: <20200831150124.206267-4-mlevitsk@redhat.com> In-Reply-To: <20200831150124.206267-1-mlevitsk@redhat.com> References: <20200831150124.206267-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mlevitsk@redhat.com X-Mimecast-Spam-Score: 0.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=mlevitsk@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/31 03:43:46 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, 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: 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" 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 --- 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 Sun Feb 8 23:40:50 2026 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=1598886448; cv=none; d=zohomail.com; s=zohoarc; b=Rxzhvj2h6oADvGeTdou9/x8erxlFEgtVqwJNG9oHAIoBLqMndloxivL8fSNPQaLCDZw3PCzF0q7pTKKzYy/fvrpEHNb9mSW4sHJEiKbACpLn1/g+Z3GSX1fDOYbsw0sep3NHjgZfMYzkHxssmZ4+L0wyGigptIt0YkceBNQ4OvY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598886448; 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=FZYL7a+wM3VyXEfeS23vdxhnPrcCvz6aLxgaGhEQPJ4=; b=J6AvyQitmfjyoZn44C4G3JDLZCMEnv65JuK++u2EDzWNGBC+ab7o3lgMBmZlapRYpHGThLEYhHh1Cvf22SppKzNg7Hz6XbeKYhEMLOI30B1ecmMAvctt1niuQ896Atg4T+9excgjTxkbjzOcgvybdhW1mpNRIys2aForxomSfMw= 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 1598886448655643.2614606382192; Mon, 31 Aug 2020 08:07:28 -0700 (PDT) Received: from localhost ([::1]:48940 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kClP5-0005TA-7w for importer@patchew.org; Mon, 31 Aug 2020 11:07:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44222) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kClJf-0006I0-G5 for qemu-devel@nongnu.org; Mon, 31 Aug 2020 11:01:51 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:40532 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 1kClJd-0004WJ-2L for qemu-devel@nongnu.org; Mon, 31 Aug 2020 11:01:51 -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-307-HkyID_c7OKe-Hz_xEX3a0g-1; Mon, 31 Aug 2020 11:01:45 -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 E178210ABDA0; Mon, 31 Aug 2020 15:01:44 +0000 (UTC) Received: from localhost.localdomain (unknown [10.35.206.190]) by smtp.corp.redhat.com (Postfix) with ESMTP id 93ECF19C4F; Mon, 31 Aug 2020 15:01:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1598886107; 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=FZYL7a+wM3VyXEfeS23vdxhnPrcCvz6aLxgaGhEQPJ4=; b=GruEH7Q2Hd/+fwOw6QNc776Yv49SVmNg/6novRI3+/5hEBX/6TrHOO1nkVMyGV+vejmsPy UaVKeChBq163R+HJPklGm4Fc31itN7aoNZVgGV2kv0hyj8/A1sbsSIIH3RP6+9pQVXhN/p PmMezIcm8fJE7WqnXLWtidy/KLcuwCI= X-MC-Unique: HkyID_c7OKe-Hz_xEX3a0g-1 From: Maxim Levitsky To: qemu-devel@nongnu.org Subject: [PATCH v4 4/9] device-core: use RCU for list of childs of a bus Date: Mon, 31 Aug 2020 18:01:19 +0300 Message-Id: <20200831150124.206267-5-mlevitsk@redhat.com> In-Reply-To: <20200831150124.206267-1-mlevitsk@redhat.com> References: <20200831150124.206267-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mlevitsk@redhat.com X-Mimecast-Spam-Score: 0.001 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=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/08/31 10:30:34 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-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: 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 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 96772a15bd..28e5fff5ed 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); @@ -659,17 +666,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 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 ea3f73a282..7c7728ff86 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 Sun Feb 8 23:40:50 2026 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=1598886382; cv=none; d=zohomail.com; s=zohoarc; b=aMe0WszVqbdQDgFO6yUlWvD6Ny65hE0dXOwQBJQw2SPoXFAudFZIaIoW2qEAEJPtZOWObsGurKN823/g0bZtXT/8aC86vtCvIYGUdlqUxY8kBzCEZvsqZGqEf/liWdGU8q3piXkJVP7rMSWeC4b15lLeqQEbbMSECmABCrCeJQA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598886382; 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=7EfBnwKQc+32+iSqLtKlcHE/aLlf69ZB7pfzOBZeNNo=; b=ggwWd27S0aCq8mmlmweuNJPvDXXo9Fd5BynuHqj+zraby3cwERm/P7qUHXVj09pbLtRxl5y4FzDvHkvAg+Z+GjSvAIKRu8KICFlsAtngchfjnPukGOqpwzxet1KM+mrGEazQCbQCuWr0H7NTV/XiCjfLo6e1bWgXsNKJjv/jnrI= 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 1598886382754618.9389672720014; Mon, 31 Aug 2020 08:06:22 -0700 (PDT) Received: from localhost ([::1]:44918 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kClO1-0003pV-HL for importer@patchew.org; Mon, 31 Aug 2020 11:06:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44246) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kClJl-0006Pq-9T for qemu-devel@nongnu.org; Mon, 31 Aug 2020 11:01:57 -0400 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:36359 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 1kClJi-0004XB-JK for qemu-devel@nongnu.org; Mon, 31 Aug 2020 11:01:56 -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-387-htEtujCsMfWOHdb_HbLp3Q-1; Mon, 31 Aug 2020 11:01:51 -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 72BD718A2242; Mon, 31 Aug 2020 15:01:50 +0000 (UTC) Received: from localhost.localdomain (unknown [10.35.206.190]) by smtp.corp.redhat.com (Postfix) with ESMTP id 571C319C4F; Mon, 31 Aug 2020 15:01:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1598886113; 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=7EfBnwKQc+32+iSqLtKlcHE/aLlf69ZB7pfzOBZeNNo=; b=cyu8L+Eyjrc2Kq8GWYYYXGGb/XmkS5w253HTloZgjpQORqi+z0t9ERj+tiawifb79ZWj3J 2a5tQd3hCK9gqMiYga7H/yBVPospTj2ca6iJlqJNS2sZ6NzvOcs7JbxL8aQNlyB4AYJqJ/ Lim/BvqoRht5GoD9PZy++tbOyxf2DGE= X-MC-Unique: htEtujCsMfWOHdb_HbLp3Q-1 From: Maxim Levitsky To: qemu-devel@nongnu.org Subject: [PATCH v4 5/9] device-core: use atomic_set on .realized property Date: Mon, 31 Aug 2020 18:01:20 +0300 Message-Id: <20200831150124.206267-6-mlevitsk@redhat.com> In-Reply-To: <20200831150124.206267-1-mlevitsk@redhat.com> References: <20200831150124.206267-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mlevitsk@redhat.com X-Mimecast-Spam-Score: 0.001 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=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/08/31 03:06:35 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-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: 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" 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 28e5fff5ed..97165a556d 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -933,7 +933,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); } @@ -948,7 +966,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 7c7728ff86..08e14e122c 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 Sun Feb 8 23:40:50 2026 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=1598886677; cv=none; d=zohomail.com; s=zohoarc; b=FV40io0n0K6BxImdEjfSjepjdHr31CBQq1nz7AXBD2yebkYzgaXP+WDxuWcaxvJfGnyDz+N/2D4OgFxkHJ9KuTktKzDDnF8TVKTl7NL9bTCvZt0dhuHrCcyv49GL35JhXvDQEWzW2+o7Tyj2g5zVSE9Jf3aopGw/6HWN5s7a47k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598886677; 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=VpX/PWJFUx7NdHOUX10cT7htrNNEu4lX7nluG+6/dKI=; b=d94SUdTip2mnR0d2MhfWYhHp0DsTiuwielzonP+YoGw8BKT5QQ3RwVuBrqTmYN2qJ9izcc1tF3mPCbEluSyDMQzyPjOP72yw472clfLR6+W0vCsj2H7BgCp6yjn7davFrWEdPJHg09kg8LxiuERFRX5D3oXBE3OOGXAS8MT9sj0= 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 1598886677201801.5032398278227; Mon, 31 Aug 2020 08:11:17 -0700 (PDT) Received: from localhost ([::1]:60214 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kClSl-0001mA-ST for importer@patchew.org; Mon, 31 Aug 2020 11:11:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44374) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kClK0-0006i1-R3 for qemu-devel@nongnu.org; Mon, 31 Aug 2020 11:02:12 -0400 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:40237 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 1kClJy-0004av-OD for qemu-devel@nongnu.org; Mon, 31 Aug 2020 11:02:12 -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-64-B6BWyGr_MbqGUPsoqsK0Vw-1; Mon, 31 Aug 2020 11:01:53 -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 EEE83425DF; Mon, 31 Aug 2020 15:01:52 +0000 (UTC) Received: from localhost.localdomain (unknown [10.35.206.190]) by smtp.corp.redhat.com (Postfix) with ESMTP id DBDA419C4F; Mon, 31 Aug 2020 15:01:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1598886130; 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=VpX/PWJFUx7NdHOUX10cT7htrNNEu4lX7nluG+6/dKI=; b=S5SaWBrIrA7NTjHlT+/13azDlQ/q8vPvFQGirNRR7YKsWhKegGkEcQIYRY5wDxr0ow9LY1 pn2tc58LQ1vqISKNDdfGIo0O6Oierge0ZmtUMkY2OlfD2y0jf2b0A1hjWYccE0iohvGUvE /6bf7Re6DZ2eQWtn68w3gYzfsaNJilE= X-MC-Unique: B6BWyGr_MbqGUPsoqsK0Vw-1 From: Maxim Levitsky To: qemu-devel@nongnu.org Subject: [PATCH v4 6/9] scsi/scsi-bus: scsi_device_find: don't return unrealized devices Date: Mon, 31 Aug 2020 18:01:21 +0300 Message-Id: <20200831150124.206267-7-mlevitsk@redhat.com> In-Reply-To: <20200831150124.206267-1-mlevitsk@redhat.com> References: <20200831150124.206267-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mlevitsk@redhat.com X-Mimecast-Spam-Score: 0.002 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/08/31 11:01:14 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=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: 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" 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 --- hw/scsi/scsi-bus.c | 88 ++++++++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 35 deletions(-) diff --git a/hw/scsi/scsi-bus.c b/hw/scsi/scsi-bus.c index 92d412b65c..7ceae2c92b 100644 --- a/hw/scsi/scsi-bus.c +++ b/hw/scsi/scsi-bus.c @@ -51,6 +51,56 @@ static const TypeInfo scsi_bus_info =3D { }; static int next_scsi_bus; =20 +static SCSIDevice *_scsi_device_find(SCSIBus *bus, int channel, int id, in= t lun, + bool include_unrealized) +{ + BusChild *kid; + SCSIDevice *retval =3D NULL; + + rcu_read_lock(); + + 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; + goto out; + } + + /* + * 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; + } + } + } +out: + /* + * 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 && + !atomic_load_acquire(&retval->qdev.realized)) { + retval =3D NULL; + } + + rcu_read_unlock(); + return retval; +} + +SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun) +{ + return _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); @@ -186,7 +236,7 @@ static void scsi_qdev_realize(DeviceState *qdev, Error = **errp) dev->lun =3D 0; } do { - d =3D scsi_device_find(bus, dev->channel, ++id, dev->lun); + d =3D _scsi_device_find(bus, dev->channel, ++id, dev->lun, tru= e); } while (d && d->lun =3D=3D dev->lun && id < bus->info->max_target= ); if (d && d->lun =3D=3D dev->lun) { error_setg(errp, "no free target"); @@ -196,7 +246,7 @@ static void scsi_qdev_realize(DeviceState *qdev, Error = **errp) } else if (dev->lun =3D=3D -1) { int lun =3D -1; do { - d =3D scsi_device_find(bus, dev->channel, dev->id, ++lun); + d =3D _scsi_device_find(bus, dev->channel, dev->id, ++lun, tru= e); } while (d && d->lun =3D=3D lun && lun < bus->info->max_lun); if (d && d->lun =3D=3D lun) { error_setg(errp, "no free lun"); @@ -204,7 +254,7 @@ static void scsi_qdev_realize(DeviceState *qdev, Error = **errp) } dev->lun =3D lun; } else { - d =3D scsi_device_find(bus, dev->channel, dev->id, dev->lun); + d =3D _scsi_device_find(bus, dev->channel, dev->id, dev->lun, true= ); assert(d); if (d->lun =3D=3D dev->lun && dev !=3D d) { error_setg(errp, "lun already used by '%s'", d->qdev.id); @@ -1573,38 +1623,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(); - - 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) { - rcu_read_unlock(); - 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; - } - } - } - - rcu_read_unlock(); - 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 Sun Feb 8 23:40:50 2026 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=1598886508; cv=none; d=zohomail.com; s=zohoarc; b=PF5Lob1hv3Vw8vO2todizaIBakBdJZBj2kTwgLBm+fPZ9UYbE2obUzNtGTMiNtYK2vhLGyeMu3P7AaYReyOiF0OlnGjNMHN0/ZcEx+ZKN80ny9MRGZtQG/g38dFfRgbbbHfyhj0IA2B04MMsGMBSMkn2lw39uFodzK42vl4hQW4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598886508; 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=DTPbw5Stpehj5o9nWR+nVRhC09lOXlzzSCGaNl5AOQ0=; b=lAJ87mnKwrE4zEDPbG//lF3UvCKHuLhy9OMRwO2/EXe0wgl9T58bHhghOi+TzFMU2gdbPugdIz+ckT7CxA/IUT4FVn+vUJb0D/R4z55Pi8+vvqUEWjlqFJcqwdeTr5A5jNjdsoeZjzASCX3q09R0F76HPNuLsv21zSbQVlEo88A= 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 1598886508213968.0594762348469; Mon, 31 Aug 2020 08:08:28 -0700 (PDT) Received: from localhost ([::1]:52216 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kClQ2-0006p8-NZ for importer@patchew.org; Mon, 31 Aug 2020 11:08:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44306) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kClJv-0006ZD-8Y for qemu-devel@nongnu.org; Mon, 31 Aug 2020 11:02:07 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:45301 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 1kClJt-0004Xv-3u for qemu-devel@nongnu.org; Mon, 31 Aug 2020 11:02:06 -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-184-_Xa-f0oUN1uuMCwURKgigw-1; Mon, 31 Aug 2020 11:01:56 -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 AF9C18015DB; Mon, 31 Aug 2020 15:01:55 +0000 (UTC) Received: from localhost.localdomain (unknown [10.35.206.190]) by smtp.corp.redhat.com (Postfix) with ESMTP id 64FB219C4F; Mon, 31 Aug 2020 15:01:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1598886124; 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=DTPbw5Stpehj5o9nWR+nVRhC09lOXlzzSCGaNl5AOQ0=; b=KAlRG44g0nQQxxQ5uWSDZOciyn1eJwc+d0z2PYrT2Lh6TeJ1/6ZoEEDrBqXuxK7fbbN3nO qB/h9j+TDXUuvA2eJ/N4LTjurZUr7Z1lTWVDoWwEzflyJa3PJvhyxBHOXECcI6+oMVLpVM rmRhx/JmkP77gwxwXIHA7e6smiZFBc4= X-MC-Unique: _Xa-f0oUN1uuMCwURKgigw-1 From: Maxim Levitsky To: qemu-devel@nongnu.org Subject: [PATCH v4 7/9] scsi/scsi_bus: Add scsi_device_get Date: Mon, 31 Aug 2020 18:01:22 +0300 Message-Id: <20200831150124.206267-8-mlevitsk@redhat.com> In-Reply-To: <20200831150124.206267-1-mlevitsk@redhat.com> References: <20200831150124.206267-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mlevitsk@redhat.com X-Mimecast-Spam-Score: 0.001 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=205.139.110.61; envelope-from=mlevitsk@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/31 11:01:14 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=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: 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 --- hw/scsi/scsi-bus.c | 22 +++++++++++++++++----- include/hw/scsi/scsi.h | 1 + 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/hw/scsi/scsi-bus.c b/hw/scsi/scsi-bus.c index 7ceae2c92b..feab20b76d 100644 --- a/hw/scsi/scsi-bus.c +++ b/hw/scsi/scsi-bus.c @@ -52,7 +52,7 @@ static const TypeInfo scsi_bus_info =3D { static int next_scsi_bus; =20 static SCSIDevice *_scsi_device_find(SCSIBus *bus, int channel, int id, in= t lun, - bool include_unrealized) + bool include_unrealized, bool take_re= f) { BusChild *kid; SCSIDevice *retval =3D NULL; @@ -92,13 +92,22 @@ out: retval =3D NULL; } =20 + if (take_ref) { + object_ref(OBJECT(retval)); + } + rcu_read_unlock(); return retval; } =20 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun) { - return _scsi_device_find(bus, channel, id, lun, false); + return _scsi_device_find(bus, channel, id, lun, false, false); +} + +SCSIDevice *scsi_device_get(SCSIBus *bus, int channel, int id, int lun) +{ + return _scsi_device_find(bus, channel, id, lun, false, true); } =20 static void scsi_device_realize(SCSIDevice *s, Error **errp) @@ -236,7 +245,8 @@ static void scsi_qdev_realize(DeviceState *qdev, Error = **errp) dev->lun =3D 0; } do { - d =3D _scsi_device_find(bus, dev->channel, ++id, dev->lun, tru= e); + d =3D _scsi_device_find(bus, dev->channel, ++id, dev->lun, + true, false); } while (d && d->lun =3D=3D dev->lun && id < bus->info->max_target= ); if (d && d->lun =3D=3D dev->lun) { error_setg(errp, "no free target"); @@ -246,7 +256,8 @@ static void scsi_qdev_realize(DeviceState *qdev, Error = **errp) } else if (dev->lun =3D=3D -1) { int lun =3D -1; do { - d =3D _scsi_device_find(bus, dev->channel, dev->id, ++lun, tru= e); + d =3D _scsi_device_find(bus, dev->channel, dev->id, ++lun, + true, false); } while (d && d->lun =3D=3D lun && lun < bus->info->max_lun); if (d && d->lun =3D=3D lun) { error_setg(errp, "no free lun"); @@ -254,7 +265,8 @@ static void scsi_qdev_realize(DeviceState *qdev, Error = **errp) } dev->lun =3D lun; } else { - d =3D _scsi_device_find(bus, dev->channel, dev->id, dev->lun, true= ); + d =3D _scsi_device_find(bus, dev->channel, dev->id, dev->lun, + true, false); assert(d); if (d->lun =3D=3D dev->lun && dev !=3D d) { error_setg(errp, "lun already used by '%s'", d->qdev.id); diff --git a/include/hw/scsi/scsi.h b/include/hw/scsi/scsi.h index 2fc23e44ba..e939a81789 100644 --- a/include/hw/scsi/scsi.h +++ b/include/hw/scsi/scsi.h @@ -194,6 +194,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 Sun Feb 8 23:40:50 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1598886212; cv=none; d=zohomail.com; s=zohoarc; b=SUlei47Z/ll6MICyZsfrIiAgAl9nWLsM738m5WnyFFXbbbkIhcN44CgcWYchfHmcqv1vCjGaIre5fOtieGHbQyKIwm0vE+heBs5AzkgDi9mp0gMGIgizEBSSdYgzvpUUUqOTpf5wewZMf9poRP1nW19b0tTOiRLOaJYC1wNFOG4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598886212; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=8PSON/FTSv0dYjpYRQPNNtDG+cZEs6VPBIXSF62lthU=; b=hvOMzCck0cPcd4unFt8RdkyN7F+6rwdbSEGIHQkk0Cw79g43pheZ9BxXWdvumSNZZWSnh3nsN3X/8bASdszrnPAt1kp/5J8T5DYUyVDMTpoODL+3l05SHf0BkBFFeKoP6HyRX1YNp83k4d9KO+cBxmEJhP++8zuTpBesWg4xSsk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1598886212685944.420239786854; Mon, 31 Aug 2020 08:03:32 -0700 (PDT) Received: from localhost ([::1]:36232 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kClLH-0008FX-A6 for importer@patchew.org; Mon, 31 Aug 2020 11:03:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44334) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kClJw-0006bS-Vs for qemu-devel@nongnu.org; Mon, 31 Aug 2020 11:02:09 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:31596 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kClJu-0004Yc-N6 for qemu-devel@nongnu.org; Mon, 31 Aug 2020 11:02:08 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-274-CopMqEBpMjOhpb2zR3sv5w-1; Mon, 31 Aug 2020 11:02:03 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 693651009441; Mon, 31 Aug 2020 15:02:01 +0000 (UTC) Received: from localhost.localdomain (unknown [10.35.206.190]) by smtp.corp.redhat.com (Postfix) with ESMTP id 24C4C19C4F; Mon, 31 Aug 2020 15:01:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1598886125; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8PSON/FTSv0dYjpYRQPNNtDG+cZEs6VPBIXSF62lthU=; b=ajgyn9w/e7nxx9UWsAxwVlLwcwKuJf+XXcwd5t36fDOK2GMNoqrlXs+O9oFOedczr/9ro4 VB+JAl2ZQW5aL5Zf63MH94fMFkXu3JuUTmkVz0QzW5OUwHRY9YTAORDLFymJkxiCAzuH0o fQiX6r/+vRj0ykR37hONXedKZENl5EM= X-MC-Unique: CopMqEBpMjOhpb2zR3sv5w-1 From: Maxim Levitsky To: qemu-devel@nongnu.org Subject: [PATCH v4 8/9] virtio-scsi: use scsi_device_get Date: Mon, 31 Aug 2020 18:01:23 +0300 Message-Id: <20200831150124.206267-9-mlevitsk@redhat.com> In-Reply-To: <20200831150124.206267-1-mlevitsk@redhat.com> References: <20200831150124.206267-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mlevitsk@redhat.com X-Mimecast-Spam-Score: 0.001 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=205.139.110.120; envelope-from=mlevitsk@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/31 02:13:08 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Fam Zheng , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Eduardo Habkost , "Michael S. Tsirkin" , Stefan Hajnoczi , Maxim Levitsky , Stefan Hajnoczi , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This will help us to avoid the scsi device disappearing after we took a reference to it. It doesn't by itself forbid case when we try to access an unrealized device Suggested-by: Stefan Hajnoczi Signed-off-by: Maxim Levitsky Reviewed-by: Stefan Hajnoczi --- hw/scsi/virtio-scsi.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c index 971afbb217..3db9a8aae9 100644 --- a/hw/scsi/virtio-scsi.c +++ b/hw/scsi/virtio-scsi.c @@ -33,7 +33,7 @@ static inline int virtio_scsi_get_lun(uint8_t *lun) return ((lun[2] << 8) | lun[3]) & 0x3FFF; } =20 -static inline SCSIDevice *virtio_scsi_device_find(VirtIOSCSI *s, uint8_t *= lun) +static inline SCSIDevice *virtio_scsi_device_get(VirtIOSCSI *s, uint8_t *l= un) { if (lun[0] !=3D 1) { return NULL; @@ -41,7 +41,7 @@ static inline SCSIDevice *virtio_scsi_device_find(VirtIOS= CSI *s, uint8_t *lun) if (lun[2] !=3D 0 && !(lun[2] >=3D 0x40 && lun[2] < 0x80)) { return NULL; } - return scsi_device_find(&s->bus, 0, lun[1], virtio_scsi_get_lun(lun)); + return scsi_device_get(&s->bus, 0, lun[1], virtio_scsi_get_lun(lun)); } =20 void virtio_scsi_init_req(VirtIOSCSI *s, VirtQueue *vq, VirtIOSCSIReq *req) @@ -256,7 +256,7 @@ static inline void virtio_scsi_ctx_check(VirtIOSCSI *s,= SCSIDevice *d) * case of async cancellation. */ static int virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSCSIReq *req) { - SCSIDevice *d =3D virtio_scsi_device_find(s, req->req.tmf.lun); + SCSIDevice *d =3D virtio_scsi_device_get(s, req->req.tmf.lun); SCSIRequest *r, *next; BusChild *kid; int target; @@ -370,10 +370,10 @@ static int virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSC= SIReq *req) =20 rcu_read_lock(); QTAILQ_FOREACH_RCU(kid, &s->bus.qbus.children, sibling) { - d =3D SCSI_DEVICE(kid->child); - if (d->channel =3D=3D 0 && d->id =3D=3D target) { - qdev_reset_all(&d->qdev); - } + SCSIDevice *d1 =3D SCSI_DEVICE(kid->child); + if (d1->channel =3D=3D 0 && d1->id =3D=3D target) { + qdev_reset_all(&d1->qdev); + } } rcu_read_unlock(); =20 @@ -386,14 +386,17 @@ static int virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSC= SIReq *req) break; } =20 + object_unref(OBJECT(d)); return ret; =20 incorrect_lun: req->resp.tmf.response =3D VIRTIO_SCSI_S_INCORRECT_LUN; + object_unref(OBJECT(d)); return ret; =20 fail: req->resp.tmf.response =3D VIRTIO_SCSI_S_BAD_TARGET; + object_unref(OBJECT(d)); return ret; } =20 @@ -564,7 +567,7 @@ static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCS= I *s, VirtIOSCSIReq *req) } } =20 - d =3D virtio_scsi_device_find(s, req->req.cmd.lun); + d =3D virtio_scsi_device_get(s, req->req.cmd.lun); if (!d) { req->resp.cmd.response =3D VIRTIO_SCSI_S_BAD_TARGET; virtio_scsi_complete_cmd_req(req); @@ -580,10 +583,12 @@ static int virtio_scsi_handle_cmd_req_prepare(VirtIOS= CSI *s, VirtIOSCSIReq *req) req->sreq->cmd.xfer > req->qsgl.size)) { req->resp.cmd.response =3D VIRTIO_SCSI_S_OVERRUN; virtio_scsi_complete_cmd_req(req); + object_unref(OBJECT(d)); return -ENOBUFS; } scsi_req_ref(req->sreq); blk_io_plug(d->conf.blk); + object_unref(OBJECT(d)); return 0; } =20 --=20 2.26.2 From nobody Sun Feb 8 23:40:50 2026 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=1598886558; cv=none; d=zohomail.com; s=zohoarc; b=UUQq6jq3468ezAEX+r/lyr7m24ORkgUEbArQjKsBRMDW6SifBUgo2cq49guuSd5r4IeFMFNRQaiTor222t8SvY3spJ+EUYN7iwWhbcSzDiELvixfHKTAjgJbDJRx2VhE/PsU6AEKx5DSLAfhpeF6+Gusj595sDQ1GFNXfkvflUI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598886558; 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=sgznJQ2I4Fklag8gmk8CI9JqaVIi77SBCPkLErOkHL4=; b=gphXY4IROnxr/hcrxhXsZ0ECJfZpq1gqVHGQ5t35HYt7qIxH9w2EQqCQ/3EpN5MxpqRLwGUU7JRZ7vbcGKuTOcyg7fJ0FAWXgg2iwrO6wMDlr5+cHXTiOeB2QA2CkQCzgYqhdBKwAZAorNKYIgVuM8w/ixmopNobOuGc8c4xiaM= 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 1598886558989465.9713251043978; Mon, 31 Aug 2020 08:09:18 -0700 (PDT) Received: from localhost ([::1]:54302 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kClQr-0007fV-NK for importer@patchew.org; Mon, 31 Aug 2020 11:09:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44354) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kClJy-0006dV-LU for qemu-devel@nongnu.org; Mon, 31 Aug 2020 11:02:10 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:42000) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kClJw-0004ZR-KL for qemu-devel@nongnu.org; Mon, 31 Aug 2020 11:02:10 -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-564-YATnqnyrMEe0qutg9264ew-1; Mon, 31 Aug 2020 11:02:04 -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 E5FC3801AAC; Mon, 31 Aug 2020 15:02:03 +0000 (UTC) Received: from localhost.localdomain (unknown [10.35.206.190]) by smtp.corp.redhat.com (Postfix) with ESMTP id D327C19C4F; Mon, 31 Aug 2020 15:02:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1598886127; 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=sgznJQ2I4Fklag8gmk8CI9JqaVIi77SBCPkLErOkHL4=; b=M+fN1wA5Z5UoG1gFljjIiBY5OXY7T9p7NBxC/HaBb3oi6Ifd1E6rfUvCdsGUIhS4OAr9wL +XykCiuQzfsMmeZgFJjqucaWSsTEjNs/v7eTSGkZlHPZ6zHbIiBAeQNT3+CWXIIm5rHN7p tRutCNErp47yXVIFEIgHezrZ8G3L5V4= X-MC-Unique: YATnqnyrMEe0qutg9264ew-1 From: Maxim Levitsky To: qemu-devel@nongnu.org Subject: [PATCH v4 9/9] scsi/scsi_bus: fix races in REPORT LUNS Date: Mon, 31 Aug 2020 18:01:24 +0300 Message-Id: <20200831150124.206267-10-mlevitsk@redhat.com> In-Reply-To: <20200831150124.206267-1-mlevitsk@redhat.com> References: <20200831150124.206267-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mlevitsk@redhat.com X-Mimecast-Spam-Score: 0.001 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=63.128.21.124; envelope-from=mlevitsk@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/31 03:02:42 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, 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: 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" Currently scsi_target_emulate_report_luns iterates over child devices list twice, and there is guarantee, that it will not be changed meanwhile. This reason for two loops is that it needs to know how much memory to allocate. Avoid this by iterating once, and allocating the memory for the output dynamically with reserving enought memory so that in practice it won't be reallocated often. Bugzilla for reference: https://bugzilla.redhat.com/show_bug.cgi?id=3D18667= 07 Signed-off-by: Maxim Levitsky Reviewed-by: Stefan Hajnoczi --- hw/scsi/scsi-bus.c | 62 ++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/hw/scsi/scsi-bus.c b/hw/scsi/scsi-bus.c index feab20b76d..150dee2e6a 100644 --- a/hw/scsi/scsi-bus.c +++ b/hw/scsi/scsi-bus.c @@ -438,19 +438,25 @@ 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; + + /* reserve space for 63 LUNs*/ + GByteArray *buf =3D g_byte_array_sized_new(512); =20 if (r->req.cmd.xfer < 16) { return false; @@ -460,46 +466,36 @@ static bool scsi_target_emulate_report_luns(SCSITarge= tReq *r) } channel =3D r->req.dev->channel; id =3D r->req.dev->id; - found_lun0 =3D false; - n =3D 0; =20 - rcu_read_lock(); =20 - QTAILQ_FOREACH_RCU(kid, &r->req.bus->qbus.children, sibling) { - DeviceState *qdev =3D kid->child; - SCSIDevice *dev =3D SCSI_DEVICE(qdev); + /* add size (will be updated later to correct value */ + 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; - } - n +=3D 8; - } - } - if (!found_lun0) { - n +=3D 8; - } - - scsi_target_alloc_buf(&r->req, n + 8); + /* add LUN0 */ + g_byte_array_append(buf, tmp, 8); + len +=3D 8; =20 - 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; + 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 - if (dev->channel =3D=3D channel && dev->id =3D=3D id) { - store_lun(&r->buf[i], dev->lun); - i +=3D 8; + if (dev->channel =3D=3D channel && dev->id =3D=3D id && dev->lun != =3D 0) { + store_lun(tmp, dev->lun); + g_byte_array_append(buf, tmp, 8); + len +=3D 8; } } - rcu_read_unlock(); =20 - assert(i =3D=3D n + 8); - r->len =3D len; + r->buf_len =3D len; + r->buf =3D g_byte_array_free(buf, FALSE); + r->len =3D MIN(len, r->req.cmd.xfer & ~7); + + /* store the LUN list length */ + stl_be_p(&r->buf[0], len - 8); + return true; } =20 --=20 2.26.2