Received: by 10.192.165.156 with SMTP id m28csp68645imm; Tue, 10 Apr 2018 16:36:07 -0700 (PDT) X-Google-Smtp-Source: AIpwx49EZGPRfccMA7INAJLPY2wdSKKc6WDD3tvSMkhc5UYp6SP5TeenxO/hwt84luAD7C/9yqDL X-Received: by 10.101.76.10 with SMTP id u10mr1688048pgq.445.1523403367828; Tue, 10 Apr 2018 16:36:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523403367; cv=none; d=google.com; s=arc-20160816; b=DU6O0dCockhrHe28isV2g3YiTxmpb0CiNTir4M33PzqEP2e7V627kQu6fMreAqZfYl U486arzV0GGvx5/M42keBTDkCkiA9D2O5cruF5C6+ZbbbCZecA+UsfruM+JDhrskZPih jBkBBUtu2rscpMwCWnmMVUeK8t3kpRh2eXKSPxhmvrjUJGqOxH+8/nH9yWhKbYyAQqDD ViMznzKX282mLoXcLET4KYpEa85fYjd7uNC7QRtuRE4oonnC0I2657G27woNVqRQXgsZ aFvyYhgTM9bEAiwKgLX9FwuTcWwfJPriXqzgTYRa/NyYLWgZ80kopcNA9vaE8SHYhYiu forg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=ZI1X5S+2K2qKmMab1LqzB7V5LFTRQd1Gp3+3JZUzlJ8=; b=xzjFGUW3z6snUOSDoXazTD6Xll8vGlbg7MqCdWdkyvKJfK4MnRBj0UY6unejAv8Vh9 H4+bR1640cxs4ZtnJ36YtLsospABhpWGx645qWxk4ThqU9N48DFNqyd7NNb2bRpxQNLB ajSikYb1zniS5y/p3P3kPNdV69ObCU1fr8hLGD3ABEo6e6lpIwkt/qfp37bgB53udE53 LaokkEhwJZCMmFjVUHMJENOJ2FIMkfHFg3rltf/uYDL/q7nFSDnR2Rm2oID5adoSap1K QjaL8QTmmrlAJeZxK68VZ9y7z4Oc6rYSRTV7bsEDTK+bF8Qvgg9zwKUMWEFdbGq1LP2a USew== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x6si2839855pfx.216.2018.04.10.16.35.31; Tue, 10 Apr 2018 16:36:07 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754139AbeDJW2t (ORCPT + 99 others); Tue, 10 Apr 2018 18:28:49 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:39210 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754123AbeDJW2r (ORCPT ); Tue, 10 Apr 2018 18:28:47 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 3B596D09; Tue, 10 Apr 2018 22:28:46 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Daniel Jurgens , Parav Pandit , Leon Romanovsky , Jason Gunthorpe , Sasha Levin Subject: [PATCH 4.15 067/168] net/mlx5: Fix race for multiple RoCE enable Date: Wed, 11 Apr 2018 00:23:29 +0200 Message-Id: <20180410212803.048734444@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180410212800.144079021@linuxfoundation.org> References: <20180410212800.144079021@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Daniel Jurgens [ Upstream commit 734dc065fc41f6143ff88225aa5d335cb1e0f6aa ] There are two potential problems with the existing implementation. 1. Enable and disable can race after the atomic operations. 2. If a command fails the refcount is left in an inconsistent state. Introduce a lock and perform error checking. Fixes: a6f7d2aff623 ("net/mlx5: Add support for multiple RoCE enable") Signed-off-by: Daniel Jurgens Reviewed-by: Parav Pandit Signed-off-by: Leon Romanovsky Signed-off-by: Jason Gunthorpe Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- drivers/net/ethernet/mellanox/mlx5/core/vport.c | 33 +++++++++++++++++++----- include/linux/mlx5/driver.h | 2 - 2 files changed, 28 insertions(+), 7 deletions(-) --- a/drivers/net/ethernet/mellanox/mlx5/core/vport.c +++ b/drivers/net/ethernet/mellanox/mlx5/core/vport.c @@ -36,6 +36,9 @@ #include #include "mlx5_core.h" +/* Mutex to hold while enabling or disabling RoCE */ +static DEFINE_MUTEX(mlx5_roce_en_lock); + static int _mlx5_query_vport_state(struct mlx5_core_dev *mdev, u8 opmod, u16 vport, u32 *out, int outlen) { @@ -998,17 +1001,35 @@ static int mlx5_nic_vport_update_roce_st int mlx5_nic_vport_enable_roce(struct mlx5_core_dev *mdev) { - if (atomic_inc_return(&mdev->roce.roce_en) != 1) - return 0; - return mlx5_nic_vport_update_roce_state(mdev, MLX5_VPORT_ROCE_ENABLED); + int err = 0; + + mutex_lock(&mlx5_roce_en_lock); + if (!mdev->roce.roce_en) + err = mlx5_nic_vport_update_roce_state(mdev, MLX5_VPORT_ROCE_ENABLED); + + if (!err) + mdev->roce.roce_en++; + mutex_unlock(&mlx5_roce_en_lock); + + return err; } EXPORT_SYMBOL_GPL(mlx5_nic_vport_enable_roce); int mlx5_nic_vport_disable_roce(struct mlx5_core_dev *mdev) { - if (atomic_dec_return(&mdev->roce.roce_en) != 0) - return 0; - return mlx5_nic_vport_update_roce_state(mdev, MLX5_VPORT_ROCE_DISABLED); + int err = 0; + + mutex_lock(&mlx5_roce_en_lock); + if (mdev->roce.roce_en) { + mdev->roce.roce_en--; + if (mdev->roce.roce_en == 0) + err = mlx5_nic_vport_update_roce_state(mdev, MLX5_VPORT_ROCE_DISABLED); + + if (err) + mdev->roce.roce_en++; + } + mutex_unlock(&mlx5_roce_en_lock); + return err; } EXPORT_SYMBOL_GPL(mlx5_nic_vport_disable_roce); --- a/include/linux/mlx5/driver.h +++ b/include/linux/mlx5/driver.h @@ -826,7 +826,7 @@ struct mlx5_core_dev { struct mlx5e_resources mlx5e_res; struct { struct mlx5_rsvd_gids reserved_gids; - atomic_t roce_en; + u32 roce_en; } roce; #ifdef CONFIG_MLX5_FPGA struct mlx5_fpga_device *fpga;