Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp5988555rwl; Tue, 4 Apr 2023 06:31:15 -0700 (PDT) X-Google-Smtp-Source: AKy350YKk1LVBTQ9sb2w0+ag6xoBtez54MLqosSiksCOwGokrjjDgQocV8RUOgOsz5/7xA6MDX8N X-Received: by 2002:a05:6402:3d5:b0:501:c547:2135 with SMTP id t21-20020a05640203d500b00501c5472135mr2289308edw.36.1680615075408; Tue, 04 Apr 2023 06:31:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680615075; cv=none; d=google.com; s=arc-20160816; b=EfgUaKZaXvF4Q3uXlgxMuDcH98nDGMmv3KaNfATTsdlrB494TSmkhxZWwywSZZps1s qk27ODe+qRHzqRLQYCAQ+KhNPulUgmF1V97eimjEEFqfddVDhFM0AynuxzJjxknzF3Hs 3A171lgtTYJiworzTz/TYvsM3BagEyXgfdyc8Nt82DZWflfT6PI2Q3xkIXwNrxvVmuK6 jOCdcYhvULPGVAUdJzHVNWHGk1ihI33V67x5pqpqbRZB1/iHPceDEhyNqfawUoJysQmQ n4SPipZh9ciVX7JnWzXQOgqB1lcGa20VUXN1j/OZLcwFGplN8G6Jcv8oUGJapIUpxNvC bzJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=tQ8fDN2h9p4iTg0iFPY/+DuAnJcVPz4edknSv7QjGbU=; b=Z7gGkO1IT85/4tU6h00Iufs83p2Evuj9hmelmOdIJkJx471T/1x0gU6oy4ktEnXNHy rM4jTfJIK35OFlMcFRuu6PXjjCVpM8l6ZbL2jjBNsSvW5a1euyF63aOAL5181GObA9SY 63i8jBZBTzjNjmTHez9JsdTffPugXh85eGf4gUKBxwm4sTqGSBqt9hCjyx8RMt6QQuPk MzaR/HNBmpFfXejQdWHbAmpeD8eMvZhC1MN+OTAZ0FE5IAe8sT3ly4Ev3clfenjIOEDa SwlaHz/R3q1/KS4Z4s8EbrqfFqHfLEmrzPzfBGYR9FZb3DpwE7Lo04xxy647AOKLNsmp vwig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=A0Jzp41p; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c21-20020aa7c995000000b004c5248cbebfsi10195940edt.101.2023.04.04.06.30.26; Tue, 04 Apr 2023 06:31:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=A0Jzp41p; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235188AbjDDNS6 (ORCPT + 99 others); Tue, 4 Apr 2023 09:18:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235174AbjDDNSe (ORCPT ); Tue, 4 Apr 2023 09:18:34 -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 21D184216 for ; Tue, 4 Apr 2023 06:17:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680614256; 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=tQ8fDN2h9p4iTg0iFPY/+DuAnJcVPz4edknSv7QjGbU=; b=A0Jzp41pY57ux8IiInqx9omOLLXbbQs6cBJPrgQRiMWaMHZ1vVWELd53YQyd9UphUoc8+C kr+Lpa5X/PCS/WFNm8bXyxn79CPmxaPdXgjZ/UDbYjswnZ/wF9gu3yyNleD4iAPf+PACw7 B66vVN8pJQgYKqpG2znY2P0PUc4sdIc= Received: from mail-yb1-f197.google.com (mail-yb1-f197.google.com [209.85.219.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-122-QPwKJHP1OlKldQW7QsFUNg-1; Tue, 04 Apr 2023 09:17:34 -0400 X-MC-Unique: QPwKJHP1OlKldQW7QsFUNg-1 Received: by mail-yb1-f197.google.com with SMTP id 3-20020a250b03000000b00b5f1fab9897so32154656ybl.19 for ; Tue, 04 Apr 2023 06:17:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680614254; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tQ8fDN2h9p4iTg0iFPY/+DuAnJcVPz4edknSv7QjGbU=; b=b6/yoZSM7TaAW3JUWq8CM9OAJGwJ3n5O7yRzxMeIHdkyJ1Yv651dqOlHKT4989Ph6i cVu8OmlqHxa2MGiBfdQRrtbvwDCD47rP1dPRYFinqvRnyErVqqPJpf0S3Y7/z0TeFbKS t5VxQ/kG13N0yH39VdSR0MErjSLb9ZCagjfqsZ2p1JKl87+b6Gf/kU+MZo2DGjaGS27+ qj7m24qWCeBVbmshfVGL1nl4IYP+GH6BMcqn3SulRjPLb6XxSNhJUiHuiYkHCnkO1AxQ hbbDfry2VP2doD7phXvk2KPe2rEqb7vs+VJiWhLxyIajf6AaMqdbVndWOltY+ZabQmRW /7YQ== X-Gm-Message-State: AAQBX9cQI79666hnzmR1ojRfsVBEYZod09/cSuedycvtzxY309UsJxvo Wx7JgHS67Lnpid1pydqIvh/1sjR3Vwxuvm0RaYQ/L0JnQXJ5puXzsYLaerh1IqCElndb9lNXj/h zMHaHraUDfUD+mAPckF5cYs0v X-Received: by 2002:a0d:e003:0:b0:544:9cfb:72bb with SMTP id j3-20020a0de003000000b005449cfb72bbmr2116753ywe.52.1680614254243; Tue, 04 Apr 2023 06:17:34 -0700 (PDT) X-Received: by 2002:a0d:e003:0:b0:544:9cfb:72bb with SMTP id j3-20020a0de003000000b005449cfb72bbmr2116728ywe.52.1680614253971; Tue, 04 Apr 2023 06:17:33 -0700 (PDT) Received: from step1.redhat.com (host-82-53-134-157.retail.telecomitalia.it. [82.53.134.157]) by smtp.gmail.com with ESMTPSA id v67-20020a379346000000b007460093cccdsm3586891qkd.25.2023.04.04.06.17.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 06:17:33 -0700 (PDT) From: Stefano Garzarella To: virtualization@lists.linux-foundation.org Cc: stefanha@redhat.com, Andrey Zhadchenko , kvm@vger.kernel.org, eperezma@redhat.com, "Michael S. Tsirkin" , linux-kernel@vger.kernel.org, Jason Wang , netdev@vger.kernel.org, Stefano Garzarella Subject: [PATCH v5 8/9] vdpa_sim: replace the spinlock with a mutex to protect the state Date: Tue, 4 Apr 2023 15:17:29 +0200 Message-Id: <20230404131730.45920-1-sgarzare@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230404131326.44403-1-sgarzare@redhat.com> References: <20230404131326.44403-1-sgarzare@redhat.com> MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Acked-by: Jason Wang Signed-off-by: Stefano Garzarella --- 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/vdpa_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/vdpa_sim.c index bd9f9054de94..2b2e439a66f7 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c @@ -178,7 +178,7 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *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->dma_mask = &dev->coherent_dma_mask; @@ -286,13 +286,13 @@ static void vdpasim_set_vq_ready(struct vdpa_device *vdpa, u16 idx, bool ready) struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx]; bool old_ready; - spin_lock(&vdpasim->lock); + mutex_lock(&vdpasim->mutex); old_ready = vq->ready; vq->ready = 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) @@ -310,9 +310,9 @@ static int vdpasim_set_vq_state(struct vdpa_device *vdpa, u16 idx, struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx]; struct vringh *vrh = &vq->vring; - spin_lock(&vdpasim->lock); + mutex_lock(&vdpasim->mutex); vrh->last_avail_idx = state->split.avail_index; - spin_unlock(&vdpasim->lock); + mutex_unlock(&vdpasim->mutex); return 0; } @@ -409,9 +409,9 @@ static u8 vdpasim_get_status(struct vdpa_device *vdpa) struct vdpasim *vdpasim = vdpa_to_sim(vdpa); u8 status; - spin_lock(&vdpasim->lock); + mutex_lock(&vdpasim->mutex); status = vdpasim->status; - spin_unlock(&vdpasim->lock); + mutex_unlock(&vdpasim->mutex); return status; } @@ -420,19 +420,19 @@ static void vdpasim_set_status(struct vdpa_device *vdpa, u8 status) { struct vdpasim *vdpasim = vdpa_to_sim(vdpa); - spin_lock(&vdpasim->lock); + mutex_lock(&vdpasim->mutex); vdpasim->status = status; - spin_unlock(&vdpasim->lock); + mutex_unlock(&vdpasim->mutex); } static int vdpasim_reset(struct vdpa_device *vdpa) { struct vdpasim *vdpasim = vdpa_to_sim(vdpa); - spin_lock(&vdpasim->lock); + mutex_lock(&vdpasim->mutex); vdpasim->status = 0; vdpasim_do_reset(vdpasim); - spin_unlock(&vdpasim->lock); + mutex_unlock(&vdpasim->mutex); return 0; } @@ -441,9 +441,9 @@ static int vdpasim_suspend(struct vdpa_device *vdpa) { struct vdpasim *vdpasim = vdpa_to_sim(vdpa); - spin_lock(&vdpasim->lock); + mutex_lock(&vdpasim->mutex); vdpasim->running = false; - spin_unlock(&vdpasim->lock); + mutex_unlock(&vdpasim->mutex); return 0; } @@ -453,7 +453,7 @@ static int vdpasim_resume(struct vdpa_device *vdpa) struct vdpasim *vdpasim = vdpa_to_sim(vdpa); int i; - spin_lock(&vdpasim->lock); + mutex_lock(&vdpasim->mutex); vdpasim->running = true; if (vdpasim->pending_kick) { @@ -464,7 +464,7 @@ static int vdpasim_resume(struct vdpa_device *vdpa) vdpasim->pending_kick = false; } - spin_unlock(&vdpasim->lock); + mutex_unlock(&vdpasim->mutex); return 0; } @@ -536,14 +536,14 @@ static int vdpasim_set_group_asid(struct vdpa_device *vdpa, unsigned int group, iommu = &vdpasim->iommu[asid]; - spin_lock(&vdpasim->lock); + mutex_lock(&vdpasim->mutex); for (i = 0; i < vdpasim->dev_attr.nvqs; i++) if (vdpasim_get_vq_group(vdpa, i) == 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_sim/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 *vdpasim) bool reschedule = 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 *vdpasim) } } 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_sim/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 *vdpasim) u64 rx_drops = 0, rx_overruns = 0, rx_errors = 0, tx_errors = 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 *vdpasim) } out: - spin_unlock(&vdpasim->lock); + mutex_unlock(&vdpasim->mutex); u64_stats_update_begin(&net->tx_stats.syncp); net->tx_stats.pkts += tx_pkts; -- 2.39.2