Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp2300250pxp; Mon, 21 Mar 2022 16:17:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyhebs0YrKY81niccQvmKpneWOKXLGCc+lMeUBPW3g8OF+SbB1riR8ROoXUSEDuO9k0GEOa X-Received: by 2002:a05:6a00:24cc:b0:4fa:a9c5:4b04 with SMTP id d12-20020a056a0024cc00b004faa9c54b04mr4770945pfv.63.1647904629904; Mon, 21 Mar 2022 16:17:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647904629; cv=none; d=google.com; s=arc-20160816; b=ccz2sFreynMCboQ5kV2U8DWACcmR4lMHCjfkDYxgbyXc9qRnX3XuJQIpOM4LNBa04b k499NUtzBeixe4D1SNjjH5ZgXnWM/gDOwQ7w+IUo/7QetAAAulcasYOU3pWWp4Ut978E lyI7VwwVb94j9IeYHfbpE3w58LCogcQJanrLhOuPjAKN/lwShYSmUfGEYhLjYuFbbYP3 DDoXa/a0oJsKFiKAnxYEQgybccRsNenxJX9MseU1szgIdoOTzxlUHxz5hDMWYuVOh2kn qf7uMu8Re+YFY4mIqdhLVnLbTmB4sI8JZ4CSivxA4E+hjT0Jo5VhAdo36vS/irfMgmWy ICgQ== 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=3QGzl2Y6fwZNaUGV3ooqk8roHKG0eglLUaLpVPAugo4=; b=QoS4AayFGltWkh3wFqosBODVBFSeJvLdFVv+S0PnYK+YXUExpHMfW7F4gszYD2UG0L nX5KIEl7u1ngibkvoCaEB7/NQhzgbUtwYULP78QJgkQBXwBEy0cgWXTPxcnzdKpAy85s ziMJY3hvSi82xULjA+Ufxrl6sjj+yn+abiBoxC82UFhu8ZtDUm4Ud2MiMZhqP7gGjz3P XCdwpTSjqK+xbZkJZPDepOTJdjkLUyrF9L0aBGw+vJpiHiNF9npUuWweSEUW9ntT9BV4 EIsmp+kTFi9/FnYKNOaPT5V+/nQYSpAp518Po0zAjG891VUbVYrTHnC/LMnbbHMx3Df9 G6Lg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bN3BoLGt; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id y2-20020a17090a86c200b001bd14e03044si570671pjv.28.2022.03.21.16.17.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 16:17:09 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bN3BoLGt; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 636C044C072; Mon, 21 Mar 2022 15:14:36 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344525AbiCUGGO (ORCPT + 99 others); Mon, 21 Mar 2022 02:06:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344520AbiCUGGK (ORCPT ); Mon, 21 Mar 2022 02:06:10 -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 ESMTP id 4F0951A394 for ; Sun, 20 Mar 2022 23:04:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647842683; 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=3QGzl2Y6fwZNaUGV3ooqk8roHKG0eglLUaLpVPAugo4=; b=bN3BoLGtJSqie4KCYxAClf7E46fw7TWe9gPB4x36ox92pDK+0IsKywEjUKWEWtzGd5TaXS 7gRjjiZcm+NhLn3PLTUEobSJp4bLvSaryn9CULjojL4PWQU15KygIhhiC4oUt7RD8j/Wxa HMyZdpvSevnBLv7YLJu6WaCAMi4qxtE= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-318-eU5-CX3_OkCXm5O0y3jazQ-1; Mon, 21 Mar 2022 02:04:41 -0400 X-MC-Unique: eU5-CX3_OkCXm5O0y3jazQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E5D953811A21; Mon, 21 Mar 2022 06:04:40 +0000 (UTC) Received: from localhost.localdomain (ovpn-14-31.pek2.redhat.com [10.72.14.31]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9DF462166B2D; Mon, 21 Mar 2022 06:04:38 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: elic@nvidia.com, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] vdpa: mlx5: synchronize driver status with CVQ Date: Mon, 21 Mar 2022 14:04:29 +0800 Message-Id: <20220321060429.10457-2-jasowang@redhat.com> In-Reply-To: <20220321060429.10457-1-jasowang@redhat.com> References: <20220321060429.10457-1-jasowang@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE 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 Currently, CVQ doesn't have any synchronization with the driver status. Then CVQ emulation code run in the middle of: 1) device reset 2) device status changed 3) map updating The will lead several unexpected issue like trying to execute CVQ command after the driver has been teared down. Fixing this by using reslock to synchronize CVQ emulation code with the driver status changing: - protect the whole device reset, status changing and map updating with reslock - protect the CVQ handler with the reslock and check VIRTIO_CONFIG_S_DRIVER_OK in the CVQ handler This will guarantee that: 1) CVQ handler won't work if VIRTIO_CONFIG_S_DRIVER_OK is not set 2) CVQ handler will see a consistent state of the driver instead of the partial one when it is running in the middle of the teardown_driver() or setup_driver(). Cc: 5262912ef3cfc ("vdpa/mlx5: Add support for control VQ and MAC setting") Signed-off-by: Jason Wang --- drivers/vdpa/mlx5/net/mlx5_vnet.c | 42 +++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/drivers/vdpa/mlx5/net/mlx5_vnet.c b/drivers/vdpa/mlx5/net/mlx5_vnet.c index d5a6fb3f9c41..524240f55c1c 100644 --- a/drivers/vdpa/mlx5/net/mlx5_vnet.c +++ b/drivers/vdpa/mlx5/net/mlx5_vnet.c @@ -1618,11 +1618,17 @@ static void mlx5_cvq_kick_handler(struct work_struct *work) mvdev = wqent->mvdev; ndev = to_mlx5_vdpa_ndev(mvdev); cvq = &mvdev->cvq; + + mutex_lock(&ndev->reslock); + + if (!(mvdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) + goto done; + if (!(ndev->mvdev.actual_features & BIT_ULL(VIRTIO_NET_F_CTRL_VQ))) - return; + goto done; if (!cvq->ready) - return; + goto done; while (true) { err = vringh_getdesc_iotlb(&cvq->vring, &cvq->riov, &cvq->wiov, &cvq->head, @@ -1663,6 +1669,9 @@ static void mlx5_cvq_kick_handler(struct work_struct *work) break; } } + +done: + mutex_unlock(&ndev->reslock); } static void mlx5_vdpa_kick_vq(struct vdpa_device *vdev, u16 idx) @@ -2125,6 +2134,8 @@ static int mlx5_vdpa_change_map(struct mlx5_vdpa_dev *mvdev, struct vhost_iotlb struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); int err; + mutex_lock(&ndev->reslock); + suspend_vqs(ndev); err = save_channels_info(ndev); if (err) @@ -2137,18 +2148,20 @@ static int mlx5_vdpa_change_map(struct mlx5_vdpa_dev *mvdev, struct vhost_iotlb goto err_mr; if (!(mvdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) - return 0; + goto err_mr; restore_channels_info(ndev); err = setup_driver(mvdev); if (err) goto err_setup; + mutex_unlock(&ndev->reslock); return 0; err_setup: mlx5_vdpa_destroy_mr(mvdev); err_mr: + mutex_unlock(&ndev->reslock); return err; } @@ -2157,7 +2170,8 @@ static int setup_driver(struct mlx5_vdpa_dev *mvdev) struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev); int err; - mutex_lock(&ndev->reslock); + WARN_ON(!mutex_is_locked(&ndev->reslock)); + if (ndev->setup) { mlx5_vdpa_warn(mvdev, "setup driver called for already setup driver\n"); err = 0; @@ -2187,7 +2201,6 @@ static int setup_driver(struct mlx5_vdpa_dev *mvdev) goto err_fwd; } ndev->setup = true; - mutex_unlock(&ndev->reslock); return 0; @@ -2198,23 +2211,22 @@ static int setup_driver(struct mlx5_vdpa_dev *mvdev) err_rqt: teardown_virtqueues(ndev); out: - mutex_unlock(&ndev->reslock); return err; } static void teardown_driver(struct mlx5_vdpa_net *ndev) { - mutex_lock(&ndev->reslock); + + WARN_ON(!mutex_is_locked(&ndev->reslock)); + if (!ndev->setup) - goto out; + return; remove_fwd_to_tir(ndev); destroy_tir(ndev); destroy_rqt(ndev); teardown_virtqueues(ndev); ndev->setup = false; -out: - mutex_unlock(&ndev->reslock); } static void clear_vqs_ready(struct mlx5_vdpa_net *ndev) @@ -2235,6 +2247,8 @@ static void mlx5_vdpa_set_status(struct vdpa_device *vdev, u8 status) print_status(mvdev, status, true); + mutex_lock(&ndev->reslock); + if ((status ^ ndev->mvdev.status) & VIRTIO_CONFIG_S_DRIVER_OK) { if (status & VIRTIO_CONFIG_S_DRIVER_OK) { err = setup_driver(mvdev); @@ -2244,16 +2258,19 @@ static void mlx5_vdpa_set_status(struct vdpa_device *vdev, u8 status) } } else { mlx5_vdpa_warn(mvdev, "did not expect DRIVER_OK to be cleared\n"); - return; + goto err_clear; } } ndev->mvdev.status = status; + mutex_unlock(&ndev->reslock); return; err_setup: mlx5_vdpa_destroy_mr(&ndev->mvdev); ndev->mvdev.status |= VIRTIO_CONFIG_S_FAILED; +err_clear: + mutex_unlock(&ndev->reslock); } static int mlx5_vdpa_reset(struct vdpa_device *vdev) @@ -2263,6 +2280,8 @@ static int mlx5_vdpa_reset(struct vdpa_device *vdev) print_status(mvdev, 0, true); mlx5_vdpa_info(mvdev, "performing device reset\n"); + + mutex_lock(&ndev->reslock); teardown_driver(ndev); clear_vqs_ready(ndev); mlx5_vdpa_destroy_mr(&ndev->mvdev); @@ -2275,6 +2294,7 @@ static int mlx5_vdpa_reset(struct vdpa_device *vdev) if (mlx5_vdpa_create_mr(mvdev, NULL)) mlx5_vdpa_warn(mvdev, "create MR failed\n"); } + mutex_unlock(&ndev->reslock); return 0; } -- 2.18.1