Received: by 10.192.165.156 with SMTP id m28csp43920imm; Tue, 10 Apr 2018 16:02:25 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/tiGoX0C/gqDfU3I9VUBP5J8zrRqXpm7jMomRnOq9jz4UGPUnAETgcY/KWYwFLViTK2Gan X-Received: by 2002:a17:902:8490:: with SMTP id c16-v6mr2411585plo.132.1523401345723; Tue, 10 Apr 2018 16:02:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523401345; cv=none; d=google.com; s=arc-20160816; b=1FOJawjXOx4HLdYm2D13iWMYGR37cq06WnTsfuUWFdHgzOsF0SjJ0MMwouXaPkBSO5 oMK9797e2LtPU/31sOwrFCNgrwye3kxhkgAfhixCRHxzdvrpemziKgij/UXe5eMNXt8E nYun6E53ewfCpwyS0KuO0e4/Pe8NCUSnSsVyjtCuLUacSdCwOKpBILRp3D21lVEGY+/e /6hJd5nRXT4AIsuLOIyZsaWZKXlcrwYtiVams3Rjaf8u6vkzInP4tGX0po89W9xXAkZY eq5KiBN5ShDw7CfUhlukESLYHXGYKEYZnrT0C5og93FTkxuVAOH9GBEpJbMG8xK2lbZ2 4PJg== 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=boe27rqBEzu3WxklJ+68FjwyhcM8zB2CoowH/idjOpU=; b=fE2mfYWu5kSgTf2P+/vhC6k/okuS/fLr0Jt+1NkJoAYCASyADmOi8f0yF5tbhsSShU f2JC2UB9iTFe3vEMj2glxR6w9qv0JVtlwltqljd3IkLkHf/Ow4WUjNPtvIZKcb1tdU/w P5FykebUDORCyWlTXHfJPTDq/tIUAm39Z7rkROyDu/iRKsC7Y1Vvanxsv+j4nUj6U/pi T4eMD7a37mnPjKksr9/rtJ3odOgnSnlCc1NY6bS3gFd6J0DwqtkaiTTWmjk3CacrAPe7 4jXClNz2OTe2TJRug8KqYs/PsvESeT2I+jc3m3/Q/MD551tmhQ/7mU/VBGIKBEFps9RO r4wg== 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 p91-v6si3511753plb.705.2018.04.10.16.01.40; Tue, 10 Apr 2018 16:02:25 -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 S1756370AbeDJW4i (ORCPT + 99 others); Tue, 10 Apr 2018 18:56:38 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:43534 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755679AbeDJWgr (ORCPT ); Tue, 10 Apr 2018 18:36: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 477F212; Tue, 10 Apr 2018 22:36: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.14 052/138] net/mlx5: Fix race for multiple RoCE enable Date: Wed, 11 Apr 2018 00:24:02 +0200 Message-Id: <20180410212908.138081833@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180410212902.121524696@linuxfoundation.org> References: <20180410212902.121524696@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.14-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 @@ -794,7 +794,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;