From patchwork Mon Oct 26 16:39:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrian Moreno X-Patchwork-Id: 82215 X-Patchwork-Delegate: maxime.coquelin@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id E3E68A04DD; Mon, 26 Oct 2020 17:42:30 +0100 (CET) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 24EA04C89; Mon, 26 Oct 2020 17:40:08 +0100 (CET) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by dpdk.org (Postfix) with ESMTP id 1760C2C15 for ; Mon, 26 Oct 2020 17:39:59 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1603730397; 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=bAXkpWVRe8bfWzm7TiygIDAussPqnmB1T8YXYNpPSnc=; b=jIvJ3mW4pTML4v5WugveiQQRhcKpp2qCH5nla44laLyXcZjaL5EaeTfQJkzxiUNdwnCdi9 U7hnoyKKrx7fcUxDRXwdBcT+OcnCz6lC1g04drtXrPJc/TYXNR9kGeMkTYIwofOIlrAk0I +rfeztgirE7D4v4SLgpjF5HfB9KrPhM= 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-176-ZvN5Vm1vOiS9qtfYcfXCdA-1; Mon, 26 Oct 2020 12:39:56 -0400 X-MC-Unique: ZvN5Vm1vOiS9qtfYcfXCdA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C96F0809DC0; Mon, 26 Oct 2020 16:39:54 +0000 (UTC) Received: from amorenoz.users.ipa.redhat.com (ovpn-114-135.ams2.redhat.com [10.36.114.135]) by smtp.corp.redhat.com (Postfix) with ESMTP id 42CA15577D; Mon, 26 Oct 2020 16:39:53 +0000 (UTC) From: Adrian Moreno To: dev@dpdk.org Cc: yinan.wang@intel.com, patrick.fu@intel.com, chenbo.xia@intel.com, zhihong.wang@intel.com, maxime.coquelin@redhat.com, Adrian Moreno Date: Mon, 26 Oct 2020 17:39:28 +0100 Message-Id: <20201026163930.94032-5-amorenoz@redhat.com> In-Reply-To: <20201026163930.94032-1-amorenoz@redhat.com> References: <20201026163930.94032-1-amorenoz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=amorenoz@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [dpdk-dev] [PATCH v3 4/6] net/virtio-user: lock-protect status updates X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" In order to safely set and get the device status from different threads (e.g: interrupt handlers). Signed-off-by: Adrian Moreno Reviewed-by: Maxime Coquelin --- drivers/net/virtio/virtio_user/virtio_user_dev.c | 14 +++++++++++--- drivers/net/virtio/virtio_user/virtio_user_dev.h | 4 ++-- drivers/net/virtio/virtio_user_ethdev.c | 6 +++--- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/drivers/net/virtio/virtio_user/virtio_user_dev.c b/drivers/net/virtio/virtio_user/virtio_user_dev.c index 5a1e76006..36e5619df 100644 --- a/drivers/net/virtio/virtio_user/virtio_user_dev.c +++ b/drivers/net/virtio/virtio_user/virtio_user_dev.c @@ -806,11 +806,13 @@ virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx) } int -virtio_user_send_status_update(struct virtio_user_dev *dev, uint8_t status) +virtio_user_dev_set_status(struct virtio_user_dev *dev, uint8_t status) { int ret; uint64_t arg = status; + pthread_mutex_lock(&dev->mutex); + dev->status = status; if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER) ret = dev->ops->send_request(dev, VHOST_USER_SET_STATUS, &arg); @@ -824,22 +826,26 @@ virtio_user_send_status_update(struct virtio_user_dev *dev, uint8_t status) PMD_INIT_LOG(ERR, "VHOST_USER_SET_STATUS failed (%d): %s", ret, strerror(errno)); } + + pthread_mutex_unlock(&dev->mutex); return ret; } int -virtio_user_update_status(struct virtio_user_dev *dev) +virtio_user_dev_update_status(struct virtio_user_dev *dev) { uint64_t ret; uint8_t status; int err; + pthread_mutex_lock(&dev->mutex); if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER) { err = dev->ops->send_request(dev, VHOST_USER_GET_STATUS, &ret); if (!err && ret > UINT8_MAX) { PMD_INIT_LOG(ERR, "Invalid VHOST_USER_GET_STATUS " "response 0x%" PRIx64 "\n", ret); - return -1; + err = -1; + goto error; } status = ret; @@ -873,5 +879,7 @@ virtio_user_update_status(struct virtio_user_dev *dev) strerror(errno)); } +error: + pthread_mutex_unlock(&dev->mutex); return err; } diff --git a/drivers/net/virtio/virtio_user/virtio_user_dev.h b/drivers/net/virtio/virtio_user/virtio_user_dev.h index 998986875..e053897d8 100644 --- a/drivers/net/virtio/virtio_user/virtio_user_dev.h +++ b/drivers/net/virtio/virtio_user/virtio_user_dev.h @@ -81,7 +81,7 @@ void virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx); void virtio_user_handle_cq_packed(struct virtio_user_dev *dev, uint16_t queue_idx); uint8_t virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs); -int virtio_user_send_status_update(struct virtio_user_dev *dev, uint8_t status); -int virtio_user_update_status(struct virtio_user_dev *dev); +int virtio_user_dev_set_status(struct virtio_user_dev *dev, uint8_t status); +int virtio_user_dev_update_status(struct virtio_user_dev *dev); extern const char * const virtio_user_backend_strings[]; #endif diff --git a/drivers/net/virtio/virtio_user_ethdev.c b/drivers/net/virtio/virtio_user_ethdev.c index e870fb2ff..97ddc5651 100644 --- a/drivers/net/virtio/virtio_user_ethdev.c +++ b/drivers/net/virtio/virtio_user_ethdev.c @@ -278,8 +278,8 @@ virtio_user_set_status(struct virtio_hw *hw, uint8_t status) virtio_user_start_device(dev); else if (status == VIRTIO_CONFIG_STATUS_RESET) virtio_user_reset(hw); - dev->status = status; - virtio_user_send_status_update(dev, status); + + virtio_user_dev_set_status(dev, status); } static uint8_t @@ -287,7 +287,7 @@ virtio_user_get_status(struct virtio_hw *hw) { struct virtio_user_dev *dev = virtio_user_get_dev(hw); - virtio_user_update_status(dev); + virtio_user_dev_update_status(dev); return dev->status; }