Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 312BAC6FD1F for ; Tue, 14 Mar 2023 05:32:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229496AbjCNFc1 (ORCPT ); Tue, 14 Mar 2023 01:32:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229441AbjCNFcZ (ORCPT ); Tue, 14 Mar 2023 01:32:25 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1CCC395459 for ; Mon, 13 Mar 2023 22:31:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678771899; 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=MOakZAQ0JjemJWAizphBYVVMpcV7pcPHKZlLyAecItY=; b=iBrejnvutWRKKNYvB2rDNfs7JEeS6sny+iO2F5MNdd0Q/g+iwdTEKbe0VEiuEP0mslhFwu isOunR2143PcmlpOw/OXPlnMglSENMUbzWhs5muxjN6K9UFcAT9IQWFA71bf3yPDj8IJaM ezhZOVj6QJbRYgtOdk3dm99Z96g9v9U= Received: from mail-oa1-f69.google.com (mail-oa1-f69.google.com [209.85.160.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-522-0bACd2wjNDiSvXQ4064nrg-1; Tue, 14 Mar 2023 01:31:38 -0400 X-MC-Unique: 0bACd2wjNDiSvXQ4064nrg-1 Received: by mail-oa1-f69.google.com with SMTP id 586e51a60fabf-17268e44df1so8439554fac.9 for ; Mon, 13 Mar 2023 22:31:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678771897; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MOakZAQ0JjemJWAizphBYVVMpcV7pcPHKZlLyAecItY=; b=EVJEHt4GguP8QjslDsWkNhuSpqkdd/LGksZPo6PLBy8LaocLeEdNXlLj9jtkW+uAlz LVAwbFgXpcoO6IVozO/AhNnlGNVjVHsfzU8tvChJrisZ5HGNzBKcKdK6K0Ru6+yW0psC ai8tQv3nWqaUUjC6vedPRbjQTZJ9Ox/qdVDxUdSt82R/LLW9vyRYGaGlCNT8eE+oMrPP zoZw77jpQccZ/E9+BUZTXRAHgbM7ywusPQJlaVTb4HCbiWlXeC5c0QtUnQ85qYdzeOxR Bu+DYy2WkiB40XYWd38xOn0F+5E1dUWKF9JnMyGlD3GAJ5BfKOsoSJuhA736gCNRF+I1 IxJA== X-Gm-Message-State: AO0yUKVpCzbrgAk950jOmrLDu9ZeqNkY7zTOuNCEEitVE3HDzfev3/In odXNKzyDxZnV2ngK4ZscGqapQajxWJ7pM7fX75qTfRDN5ln2dhoq1PKjtXXLWBla/4O795w5bOl gfGQpRGmgb4+5zC8Ufv+whXlTWvWZMJCKDaIFSEnj X-Received: by 2002:a05:6808:143:b0:383:fef9:6cac with SMTP id h3-20020a056808014300b00383fef96cacmr12601972oie.9.1678771897237; Mon, 13 Mar 2023 22:31:37 -0700 (PDT) X-Google-Smtp-Source: AK7set9tCKmTMTA//05Xmwt8lO0T8qP9TsZu/f7e0D5UVkMX3KSKeiBkzgin/ted2gP3OO7h+LmYV5SzCywWup/8Pcs= X-Received: by 2002:a05:6808:143:b0:383:fef9:6cac with SMTP id h3-20020a056808014300b00383fef96cacmr12601955oie.9.1678771896892; Mon, 13 Mar 2023 22:31:36 -0700 (PDT) MIME-Version: 1.0 References: <20230302113421.174582-1-sgarzare@redhat.com> <20230302113421.174582-8-sgarzare@redhat.com> In-Reply-To: From: Jason Wang Date: Tue, 14 Mar 2023 13:31:25 +0800 Message-ID: Subject: Re: [PATCH v2 7/8] vdpa_sim: replace the spinlock with a mutex to protect the state To: Stefano Garzarella Cc: virtualization@lists.linux-foundation.org, Andrey Zhadchenko , eperezma@redhat.com, netdev@vger.kernel.org, stefanha@redhat.com, linux-kernel@vger.kernel.org, "Michael S. Tsirkin" , kvm@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Mar 14, 2023 at 1:29=E2=80=AFPM Jason Wang wr= ote: > > On Thu, Mar 2, 2023 at 7:35=E2=80=AFPM Stefano Garzarella wrote: > > > > The spinlock we use to protect the state of the simulator is sometimes > > held for a long time (for example, when devices handle requests). > > > > This also prevents us from calling functions that might sleep (such as > > kthread_flush_work() in the next patch), and thus having to release > > and retake the lock. > > > > For these reasons, let's replace the spinlock with a mutex that gives > > us more flexibility. > > > > Suggested-by: Jason Wang > > Signed-off-by: Stefano Garzarella > > Acked-by: Jason Wang > > Thanks Btw, though it looks fine but we'd better double confirm virtio_vdpa works = well. (I think so since there's transport that might sleep). Thanks > > > --- > > drivers/vdpa/vdpa_sim/vdpa_sim.h | 4 ++-- > > drivers/vdpa/vdpa_sim/vdpa_sim.c | 34 ++++++++++++++-------------- > > drivers/vdpa/vdpa_sim/vdpa_sim_blk.c | 4 ++-- > > drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 4 ++-- > > 4 files changed, 23 insertions(+), 23 deletions(-) > > > > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/v= dpa_sim.h > > index ce83f9130a5d..4774292fba8c 100644 > > --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h > > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h > > @@ -60,8 +60,8 @@ struct vdpasim { > > struct kthread_worker *worker; > > struct kthread_work work; > > struct vdpasim_dev_attr dev_attr; > > - /* spinlock to synchronize virtqueue state */ > > - spinlock_t lock; > > + /* mutex to synchronize virtqueue state */ > > + struct mutex mutex; > > /* virtio config according to device type */ > > void *config; > > struct vhost_iotlb *iommu; > > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/v= dpa_sim.c > > index 6feb29726c2a..a28103a67ae7 100644 > > --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c > > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c > > @@ -166,7 +166,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_a= ttr *dev_attr, > > if (IS_ERR(vdpasim->worker)) > > goto err_iommu; > > > > - spin_lock_init(&vdpasim->lock); > > + mutex_init(&vdpasim->mutex); > > spin_lock_init(&vdpasim->iommu_lock); > > > > dev =3D &vdpasim->vdpa.dev; > > @@ -275,13 +275,13 @@ static void vdpasim_set_vq_ready(struct vdpa_devi= ce *vdpa, u16 idx, bool ready) > > struct vdpasim_virtqueue *vq =3D &vdpasim->vqs[idx]; > > bool old_ready; > > > > - spin_lock(&vdpasim->lock); > > + mutex_lock(&vdpasim->mutex); > > old_ready =3D vq->ready; > > vq->ready =3D ready; > > if (vq->ready && !old_ready) { > > vdpasim_queue_ready(vdpasim, idx); > > } > > - spin_unlock(&vdpasim->lock); > > + mutex_unlock(&vdpasim->mutex); > > } > > > > static bool vdpasim_get_vq_ready(struct vdpa_device *vdpa, u16 idx) > > @@ -299,9 +299,9 @@ static int vdpasim_set_vq_state(struct vdpa_device = *vdpa, u16 idx, > > struct vdpasim_virtqueue *vq =3D &vdpasim->vqs[idx]; > > struct vringh *vrh =3D &vq->vring; > > > > - spin_lock(&vdpasim->lock); > > + mutex_lock(&vdpasim->mutex); > > vrh->last_avail_idx =3D state->split.avail_index; > > - spin_unlock(&vdpasim->lock); > > + mutex_unlock(&vdpasim->mutex); > > > > return 0; > > } > > @@ -398,9 +398,9 @@ static u8 vdpasim_get_status(struct vdpa_device *vd= pa) > > struct vdpasim *vdpasim =3D vdpa_to_sim(vdpa); > > u8 status; > > > > - spin_lock(&vdpasim->lock); > > + mutex_lock(&vdpasim->mutex); > > status =3D vdpasim->status; > > - spin_unlock(&vdpasim->lock); > > + mutex_unlock(&vdpasim->mutex); > > > > return status; > > } > > @@ -409,19 +409,19 @@ static void vdpasim_set_status(struct vdpa_device= *vdpa, u8 status) > > { > > struct vdpasim *vdpasim =3D vdpa_to_sim(vdpa); > > > > - spin_lock(&vdpasim->lock); > > + mutex_lock(&vdpasim->mutex); > > vdpasim->status =3D status; > > - spin_unlock(&vdpasim->lock); > > + mutex_unlock(&vdpasim->mutex); > > } > > > > static int vdpasim_reset(struct vdpa_device *vdpa) > > { > > struct vdpasim *vdpasim =3D vdpa_to_sim(vdpa); > > > > - spin_lock(&vdpasim->lock); > > + mutex_lock(&vdpasim->mutex); > > vdpasim->status =3D 0; > > vdpasim_do_reset(vdpasim); > > - spin_unlock(&vdpasim->lock); > > + mutex_unlock(&vdpasim->mutex); > > > > return 0; > > } > > @@ -430,9 +430,9 @@ static int vdpasim_suspend(struct vdpa_device *vdpa= ) > > { > > struct vdpasim *vdpasim =3D vdpa_to_sim(vdpa); > > > > - spin_lock(&vdpasim->lock); > > + mutex_lock(&vdpasim->mutex); > > vdpasim->running =3D false; > > - spin_unlock(&vdpasim->lock); > > + mutex_unlock(&vdpasim->mutex); > > > > return 0; > > } > > @@ -442,7 +442,7 @@ static int vdpasim_resume(struct vdpa_device *vdpa) > > struct vdpasim *vdpasim =3D vdpa_to_sim(vdpa); > > int i; > > > > - spin_lock(&vdpasim->lock); > > + mutex_lock(&vdpasim->mutex); > > vdpasim->running =3D true; > > > > if (vdpasim->pending_kick) { > > @@ -453,7 +453,7 @@ static int vdpasim_resume(struct vdpa_device *vdpa) > > vdpasim->pending_kick =3D false; > > } > > > > - spin_unlock(&vdpasim->lock); > > + mutex_unlock(&vdpasim->mutex); > > > > return 0; > > } > > @@ -525,14 +525,14 @@ static int vdpasim_set_group_asid(struct vdpa_dev= ice *vdpa, unsigned int group, > > > > iommu =3D &vdpasim->iommu[asid]; > > > > - spin_lock(&vdpasim->lock); > > + mutex_lock(&vdpasim->mutex); > > > > for (i =3D 0; i < vdpasim->dev_attr.nvqs; i++) > > if (vdpasim_get_vq_group(vdpa, i) =3D=3D group) > > vringh_set_iotlb(&vdpasim->vqs[i].vring, iommu, > > &vdpasim->iommu_lock); > > > > - spin_unlock(&vdpasim->lock); > > + mutex_unlock(&vdpasim->mutex); > > > > return 0; > > } > > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c b/drivers/vdpa/vdpa_s= im/vdpa_sim_blk.c > > index eb4897c8541e..568119e1553f 100644 > > --- a/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c > > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_blk.c > > @@ -290,7 +290,7 @@ static void vdpasim_blk_work(struct vdpasim *vdpasi= m) > > bool reschedule =3D false; > > int i; > > > > - spin_lock(&vdpasim->lock); > > + mutex_lock(&vdpasim->mutex); > > > > if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK)) > > goto out; > > @@ -321,7 +321,7 @@ static void vdpasim_blk_work(struct vdpasim *vdpasi= m) > > } > > } > > out: > > - spin_unlock(&vdpasim->lock); > > + mutex_unlock(&vdpasim->mutex); > > > > if (reschedule) > > vdpasim_schedule_work(vdpasim); > > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c b/drivers/vdpa/vdpa_s= im/vdpa_sim_net.c > > index e61a9ecbfafe..7ab434592bfe 100644 > > --- a/drivers/vdpa/vdpa_sim/vdpa_sim_net.c > > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim_net.c > > @@ -201,7 +201,7 @@ static void vdpasim_net_work(struct vdpasim *vdpasi= m) > > u64 rx_drops =3D 0, rx_overruns =3D 0, rx_errors =3D 0, tx_erro= rs =3D 0; > > int err; > > > > - spin_lock(&vdpasim->lock); > > + mutex_lock(&vdpasim->mutex); > > > > if (!vdpasim->running) > > goto out; > > @@ -264,7 +264,7 @@ static void vdpasim_net_work(struct vdpasim *vdpasi= m) > > } > > > > out: > > - spin_unlock(&vdpasim->lock); > > + mutex_unlock(&vdpasim->mutex); > > > > u64_stats_update_begin(&net->tx_stats.syncp); > > net->tx_stats.pkts +=3D tx_pkts; > > -- > > 2.39.2 > >