From patchwork Tue Mar 28 08:56:34 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Reshetova, Elena" X-Patchwork-Id: 9648507 X-Patchwork-Delegate: kvalo@adurom.com Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 6582860349 for ; Tue, 28 Mar 2017 09:06:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 581D028338 for ; Tue, 28 Mar 2017 09:06:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4CC352837F; Tue, 28 Mar 2017 09:06:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E1F1C28334 for ; Tue, 28 Mar 2017 09:06:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754926AbdC1I6i (ORCPT ); Tue, 28 Mar 2017 04:58:38 -0400 Received: from mga05.intel.com ([192.55.52.43]:45041 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754730AbdC1I62 (ORCPT ); Tue, 28 Mar 2017 04:58:28 -0400 Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga105.fm.intel.com with ESMTP; 28 Mar 2017 01:57:40 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.36,236,1486454400"; d="scan'208";a="81798056" Received: from unknown (HELO elena-ThinkPad-X230.ger.corp.intel.com) ([10.249.44.56]) by fmsmga006.fm.intel.com with ESMTP; 28 Mar 2017 01:57:34 -0700 From: Elena Reshetova To: netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-rdma@vger.kernel.org, linux-hams@vger.kernel.org, linux-ppp@vger.kernel.org, linux-wireless@vger.kernel.org, ganeshgr@chelsio.com, nbd@openwrt.org, blogic@openwrt.org, matthias.bgg@gmail.com, yishaih@mellanox.com, saeedm@mellanox.com, matanb@mellanox.com, leonro@mellanox.com, ajk@comnets.uni-bremen.de, paulus@samba.org, j@w1.fi, kvalo@codeaurora.org, peterz@infradead.org, keescook@chromium.org, gregkh@linuxfoundation.org, Elena Reshetova , Hans Liljestrand , David Windsor Subject: [PATCH 07/16] drivers, net, mlx5: convert fs_node.refcount from atomic_t to refcount_t Date: Tue, 28 Mar 2017 11:56:34 +0300 Message-Id: <1490691403-4016-8-git-send-email-elena.reshetova@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1490691403-4016-1-git-send-email-elena.reshetova@intel.com> References: <1490691403-4016-1-git-send-email-elena.reshetova@intel.com> Sender: linux-wireless-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP refcount_t type and corresponding API should be used instead of atomic_t when the variable is used as a reference counter. This allows to avoid accidental refcounter overflows that might lead to use-after-free situations. Signed-off-by: Elena Reshetova Signed-off-by: Hans Liljestrand Signed-off-by: Kees Cook Signed-off-by: David Windsor --- drivers/net/ethernet/mellanox/mlx5/core/fs_core.c | 24 +++++++++++------------ drivers/net/ethernet/mellanox/mlx5/core/fs_core.h | 3 ++- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/drivers/net/ethernet/mellanox/mlx5/core/fs_core.c b/drivers/net/ethernet/mellanox/mlx5/core/fs_core.c index 2478516..4f74d15 100644 --- a/drivers/net/ethernet/mellanox/mlx5/core/fs_core.c +++ b/drivers/net/ethernet/mellanox/mlx5/core/fs_core.c @@ -163,7 +163,7 @@ static void tree_init_node(struct fs_node *node, unsigned int refcount, void (*remove_func)(struct fs_node *)) { - atomic_set(&node->refcount, refcount); + refcount_set(&node->refcount, refcount); INIT_LIST_HEAD(&node->list); INIT_LIST_HEAD(&node->children); mutex_init(&node->lock); @@ -173,7 +173,7 @@ static void tree_init_node(struct fs_node *node, static void tree_add_node(struct fs_node *node, struct fs_node *parent) { if (parent) - atomic_inc(&parent->refcount); + refcount_inc(&parent->refcount); node->parent = parent; /* Parent is the root */ @@ -185,7 +185,7 @@ static void tree_add_node(struct fs_node *node, struct fs_node *parent) static void tree_get_node(struct fs_node *node) { - atomic_inc(&node->refcount); + refcount_inc(&node->refcount); } static void nested_lock_ref_node(struct fs_node *node, @@ -193,7 +193,7 @@ static void nested_lock_ref_node(struct fs_node *node, { if (node) { mutex_lock_nested(&node->lock, class); - atomic_inc(&node->refcount); + refcount_inc(&node->refcount); } } @@ -201,14 +201,14 @@ static void lock_ref_node(struct fs_node *node) { if (node) { mutex_lock(&node->lock); - atomic_inc(&node->refcount); + refcount_inc(&node->refcount); } } static void unlock_ref_node(struct fs_node *node) { if (node) { - atomic_dec(&node->refcount); + refcount_dec(&node->refcount); mutex_unlock(&node->lock); } } @@ -218,7 +218,7 @@ static void tree_put_node(struct fs_node *node) struct fs_node *parent_node = node->parent; lock_ref_node(parent_node); - if (atomic_dec_and_test(&node->refcount)) { + if (refcount_dec_and_test(&node->refcount)) { if (parent_node) list_del_init(&node->list); if (node->remove_func) @@ -233,8 +233,8 @@ static void tree_put_node(struct fs_node *node) static int tree_remove_node(struct fs_node *node) { - if (atomic_read(&node->refcount) > 1) { - atomic_dec(&node->refcount); + if (refcount_read(&node->refcount) > 1) { + refcount_dec(&node->refcount); return -EEXIST; } tree_put_node(node); @@ -982,7 +982,7 @@ static void destroy_flow_handle(struct fs_fte *fte, int i) { for (; --i >= 0;) { - if (atomic_dec_and_test(&handle->rule[i]->node.refcount)) { + if (refcount_dec_and_test(&handle->rule[i]->node.refcount)) { fte->dests_size--; list_del(&handle->rule[i]->node.list); kfree(handle->rule[i]); @@ -1013,7 +1013,7 @@ create_flow_handle(struct fs_fte *fte, if (dest) { rule = find_flow_rule(fte, dest + i); if (rule) { - atomic_inc(&rule->node.refcount); + refcount_inc(&rule->node.refcount); goto rule_found; } } @@ -1282,7 +1282,7 @@ static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg, list_add(&fte->node.list, prev); add_rules: for (i = 0; i < handle->num_rules; i++) { - if (atomic_read(&handle->rule[i]->node.refcount) == 1) + if (refcount_read(&handle->rule[i]->node.refcount) == 1) tree_add_node(&handle->rule[i]->node, &fte->node); } unlock_fte: diff --git a/drivers/net/ethernet/mellanox/mlx5/core/fs_core.h b/drivers/net/ethernet/mellanox/mlx5/core/fs_core.h index 8e668c6..86bc743b 100644 --- a/drivers/net/ethernet/mellanox/mlx5/core/fs_core.h +++ b/drivers/net/ethernet/mellanox/mlx5/core/fs_core.h @@ -33,6 +33,7 @@ #ifndef _MLX5_FS_CORE_ #define _MLX5_FS_CORE_ +#include #include enum fs_node_type { @@ -80,7 +81,7 @@ struct fs_node { struct fs_node *root; /* lock the node for writing and traversing */ struct mutex lock; - atomic_t refcount; + refcount_t refcount; void (*remove_func)(struct fs_node *); };