From nobody Fri Apr 4 05:04:20 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1741699867; cv=none; d=zohomail.com; s=zohoarc; b=ULmS2Da7nEhHUDzf18FClM8BGu5gNXnDE4SazP6hio9xJQwYRXi81Sl4IuCCi8ItQTYjhBlmXYEkStbHbRjRqZYVb1VXdYZ7kro6JEU+GxcNWTufvyIN4OFiax7YPfp75PUzIYQhcnaZeBG1YQVvrZfnIlVWYd5qcvU3AvLBP5Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1741699867; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=rLmh01chR4pJz9pfvW5Ge56La3He1Nnf711wKnicLcw=; b=NNY8N9+OIPoHOiDKJ7+lnUaNS/+tyXMHGezO0e4bIjQxW0GvDAznyerwuYDtDthyV84gt0gePEJYTZwwHXn4h2rcXteNQA2vj5oP68hMxnroo5OnpQ1gWBuR1klUHQZs7tsOTBrF97IeJ3uv5eOZrFLQJ5FyX0VbNK3qHocSjOQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1741699867321313.3137302852574; Tue, 11 Mar 2025 06:31:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1trzeB-0002ui-Ay; Tue, 11 Mar 2025 09:27:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1trzdK-0002Hb-61 for qemu-devel@nongnu.org; Tue, 11 Mar 2025 09:27:01 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1trzdA-0005DM-2K for qemu-devel@nongnu.org; Tue, 11 Mar 2025 09:26:57 -0400 Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-552-MEn9xl2LMqKT58FLIMKEqQ-1; Tue, 11 Mar 2025 09:26:42 -0400 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D3F3B1800A36; Tue, 11 Mar 2025 13:26:40 +0000 (UTC) Received: from localhost (unknown [10.2.16.118]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 19C831828A92; Tue, 11 Mar 2025 13:26:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1741699606; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rLmh01chR4pJz9pfvW5Ge56La3He1Nnf711wKnicLcw=; b=bOkl/pfhliZce9msEa3biopDA3ZUIzuAuVE6Apg5F+4r+fF9nQ7I6Cq/dBNbDBWxfUxJ5s HyEIE32IhM433Tdlh0NB2BdyGtxGEeoBUitu0A7pm8U9oBMVnzeYJRfu6rBODHOPS+R1H5 KW8L2zyut8s0LOQtGKJRsF++xPAMAd0= X-MC-Unique: MEn9xl2LMqKT58FLIMKEqQ-1 X-Mimecast-MFC-AGG-ID: MEn9xl2LMqKT58FLIMKEqQ_1741699600 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: John Snow , Paolo Bonzini , "Michael S. Tsirkin" , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Stefan Hajnoczi , Kevin Wolf , pkrempa@redhat.com, Fam Zheng , , David Hildenbrand , Peter Xu , Hanna Reitz Subject: [PATCH v4 04/13] scsi: introduce requests_lock Date: Tue, 11 Mar 2025 21:26:07 +0800 Message-ID: <20250311132616.1049687-5-stefanha@redhat.com> In-Reply-To: <20250311132616.1049687-1-stefanha@redhat.com> References: <20250311132616.1049687-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 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=170.10.133.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com 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, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1741699868398019000 Content-Type: text/plain; charset="utf-8" SCSIDevice keeps track of in-flight requests for device reset and Task Management Functions (TMFs). The request list requires protection so that multi-threaded SCSI emulation can be implemented in commits that follow. Signed-off-by: Stefan Hajnoczi Reviewed-by: Kevin Wolf --- include/hw/scsi/scsi.h | 7 ++- hw/scsi/scsi-bus.c | 120 +++++++++++++++++++++++++++++------------ 2 files changed, 88 insertions(+), 39 deletions(-) diff --git a/include/hw/scsi/scsi.h b/include/hw/scsi/scsi.h index ffc48203f9..90ee192b4d 100644 --- a/include/hw/scsi/scsi.h +++ b/include/hw/scsi/scsi.h @@ -49,6 +49,8 @@ struct SCSIRequest { bool dma_started; BlockAIOCB *aiocb; QEMUSGList *sg; + + /* Protected by SCSIDevice->requests_lock */ QTAILQ_ENTRY(SCSIRequest) next; }; =20 @@ -77,10 +79,7 @@ struct SCSIDevice uint8_t sense[SCSI_SENSE_BUF_SIZE]; uint32_t sense_len; =20 - /* - * The requests list is only accessed from the AioContext that executes - * requests or from the main loop when IOThread processing is stopped. - */ + QemuMutex requests_lock; /* protects the requests list */ QTAILQ_HEAD(, SCSIRequest) requests; =20 uint32_t channel; diff --git a/hw/scsi/scsi-bus.c b/hw/scsi/scsi-bus.c index 846bbbf0ec..ece1107ee8 100644 --- a/hw/scsi/scsi-bus.c +++ b/hw/scsi/scsi-bus.c @@ -100,8 +100,15 @@ static void scsi_device_for_each_req_sync(SCSIDevice *= s, assert(!runstate_is_running()); assert(qemu_in_main_thread()); =20 - QTAILQ_FOREACH_SAFE(req, &s->requests, next, next_req) { - fn(req, opaque); + /* + * Locking is not necessary because the guest is stopped and no other + * threads can be accessing the requests list, but take the lock for + * consistency. + */ + WITH_QEMU_LOCK_GUARD(&s->requests_lock) { + QTAILQ_FOREACH_SAFE(req, &s->requests, next, next_req) { + fn(req, opaque); + } } } =20 @@ -115,21 +122,29 @@ static void scsi_device_for_each_req_async_bh(void *o= paque) { g_autofree SCSIDeviceForEachReqAsyncData *data =3D opaque; SCSIDevice *s =3D data->s; - AioContext *ctx; - SCSIRequest *req; - SCSIRequest *next; + g_autoptr(GList) reqs =3D NULL; =20 /* - * The BB cannot have changed contexts between this BH being scheduled= and - * now: BBs' AioContexts, when they have a node attached, can only be - * changed via bdrv_try_change_aio_context(), in a drained section. W= hile - * we have the in-flight counter incremented, that drain must block. + * Build a list of requests in this AioContext so fn() can be invoked = later + * outside requests_lock. */ - ctx =3D blk_get_aio_context(s->conf.blk); - assert(ctx =3D=3D qemu_get_current_aio_context()); + WITH_QEMU_LOCK_GUARD(&s->requests_lock) { + AioContext *ctx =3D qemu_get_current_aio_context(); + SCSIRequest *req; + SCSIRequest *next; =20 - QTAILQ_FOREACH_SAFE(req, &s->requests, next, next) { - data->fn(req, data->fn_opaque); + QTAILQ_FOREACH_SAFE(req, &s->requests, next, next) { + if (req->ctx =3D=3D ctx) { + scsi_req_ref(req); /* dropped after calling fn() */ + reqs =3D g_list_prepend(reqs, req); + } + } + } + + /* Call fn() on each request */ + for (GList *elem =3D g_list_first(reqs); elem; elem =3D g_list_next(el= em)) { + data->fn(elem->data, data->fn_opaque); + scsi_req_unref(elem->data); } =20 /* Drop the reference taken by scsi_device_for_each_req_async() */ @@ -139,9 +154,35 @@ static void scsi_device_for_each_req_async_bh(void *op= aque) blk_dec_in_flight(s->conf.blk); } =20 +static void scsi_device_for_each_req_async_do_ctx(gpointer key, gpointer v= alue, + gpointer user_data) +{ + AioContext *ctx =3D key; + SCSIDeviceForEachReqAsyncData *params =3D user_data; + SCSIDeviceForEachReqAsyncData *data; + + data =3D g_new(SCSIDeviceForEachReqAsyncData, 1); + data->s =3D params->s; + data->fn =3D params->fn; + data->fn_opaque =3D params->fn_opaque; + + /* + * Hold a reference to the SCSIDevice until + * scsi_device_for_each_req_async_bh() finishes. + */ + object_ref(OBJECT(data->s)); + + /* Paired with scsi_device_for_each_req_async_bh() */ + blk_inc_in_flight(data->s->conf.blk); + + aio_bh_schedule_oneshot(ctx, scsi_device_for_each_req_async_bh, data); +} + /* * Schedule @fn() to be invoked for each enqueued request in device @s. @f= n() - * runs in the AioContext that is executing the request. + * must be thread-safe because it runs concurrently in each AioContext tha= t is + * executing a request. + * * Keeps the BlockBackend's in-flight counter incremented until everything= is * done, so draining it will settle all scheduled @fn() calls. */ @@ -151,24 +192,26 @@ static void scsi_device_for_each_req_async(SCSIDevice= *s, { assert(qemu_in_main_thread()); =20 - SCSIDeviceForEachReqAsyncData *data =3D - g_new(SCSIDeviceForEachReqAsyncData, 1); + /* The set of AioContexts where the requests are being processed */ + g_autoptr(GHashTable) aio_contexts =3D g_hash_table_new(NULL, NULL); + WITH_QEMU_LOCK_GUARD(&s->requests_lock) { + SCSIRequest *req; + QTAILQ_FOREACH(req, &s->requests, next) { + g_hash_table_add(aio_contexts, req->ctx); + } + } =20 - data->s =3D s; - data->fn =3D fn; - data->fn_opaque =3D opaque; - - /* - * Hold a reference to the SCSIDevice until - * scsi_device_for_each_req_async_bh() finishes. - */ - object_ref(OBJECT(s)); - - /* Paired with blk_dec_in_flight() in scsi_device_for_each_req_async_b= h() */ - blk_inc_in_flight(s->conf.blk); - aio_bh_schedule_oneshot(blk_get_aio_context(s->conf.blk), - scsi_device_for_each_req_async_bh, - data); + /* Schedule a BH for each AioContext */ + SCSIDeviceForEachReqAsyncData params =3D { + .s =3D s, + .fn =3D fn, + .fn_opaque =3D opaque, + }; + g_hash_table_foreach( + aio_contexts, + scsi_device_for_each_req_async_do_ctx, + ¶ms + ); } =20 static void scsi_device_realize(SCSIDevice *s, Error **errp) @@ -349,6 +392,7 @@ static void scsi_qdev_realize(DeviceState *qdev, Error = **errp) dev->lun =3D lun; } =20 + qemu_mutex_init(&dev->requests_lock); QTAILQ_INIT(&dev->requests); scsi_device_realize(dev, &local_err); if (local_err) { @@ -369,6 +413,8 @@ static void scsi_qdev_unrealize(DeviceState *qdev) =20 scsi_device_purge_requests(dev, SENSE_CODE(NO_SENSE)); =20 + qemu_mutex_destroy(&dev->requests_lock); + scsi_device_unrealize(dev); =20 blockdev_mark_auto_del(dev->conf.blk); @@ -965,7 +1011,10 @@ static void scsi_req_enqueue_internal(SCSIRequest *re= q) req->sg =3D NULL; } req->enqueued =3D true; - QTAILQ_INSERT_TAIL(&req->dev->requests, req, next); + + WITH_QEMU_LOCK_GUARD(&req->dev->requests_lock) { + QTAILQ_INSERT_TAIL(&req->dev->requests, req, next); + } } =20 int32_t scsi_req_enqueue(SCSIRequest *req) @@ -985,7 +1034,9 @@ static void scsi_req_dequeue(SCSIRequest *req) trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag); req->retry =3D false; if (req->enqueued) { - QTAILQ_REMOVE(&req->dev->requests, req, next); + WITH_QEMU_LOCK_GUARD(&req->dev->requests_lock) { + QTAILQ_REMOVE(&req->dev->requests, req, next); + } req->enqueued =3D false; scsi_req_unref(req); } @@ -1962,8 +2013,7 @@ static void scsi_device_class_init(ObjectClass *klass= , void *data) =20 static void scsi_dev_instance_init(Object *obj) { - DeviceState *dev =3D DEVICE(obj); - SCSIDevice *s =3D SCSI_DEVICE(dev); + SCSIDevice *s =3D SCSI_DEVICE(obj); =20 device_add_bootindex_property(obj, &s->conf.bootindex, "bootindex", NULL, --=20 2.48.1