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: 9648575 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 24ED7602C8 for ; Tue, 28 Mar 2017 09:20:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1377F1FF8E for ; Tue, 28 Mar 2017 09:20:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 082F227F4B; Tue, 28 Mar 2017 09:20:22 +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=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id C78631FF8E for ; Tue, 28 Mar 2017 09:20:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=z1dAVSIULl3j3y6vNZLoK0+O0PFpVVtH0BunhwIHBW0=; b=adpkLlyaxYgko0UNUKoZaqnq5O 2oZ/zf7UezWAL2mp4spFKQeOmzGIh2klUd8Oko0RWDfiLTzcMdlT9OPC9FXuKdKSktz8TB/PKuCIV w/ATpvsA57WG47dzly8ki80M2xQsXbeC/jS24Tzop3IhxyxtB6PXZY8O6TpKcqJwW1KM4Q1SF0cg3 Azu7P23Ta9r6OH5DtT+MM7vajRIe9Up1tyh94zUWZ7yMTzRIVKNKPhSTqqZqrpRrxUed1o6idDIHH q9hJcDRjaH5UjMlTX/WJXN0T29NCP5iZhoNnU+TSzn54BKHA1Q7Kj96gTvjb5bEW5/rEXDxB4yc5T 2DkzDy5g==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1csnIa-0006Su-DQ; Tue, 28 Mar 2017 09:20:20 +0000 Received: from merlin.infradead.org ([2001:4978:20e::2]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1csnHF-0004U3-C4 for linux-arm-kernel@bombadil.infradead.org; Tue, 28 Mar 2017 09:18:57 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=lla7+dOs1hwRE56UpNQg9oKV8vF7cTUi46OoFYEz5aU=; b=uvgliLcB9a2rKc1PnmA2ZY9rU e4YWFYe+zmQXpLiN+mHLY4nt9JbpiZNeLs5tDbahoCnAei1N3x1hbsCwLf0j2MZLIhIoK/iCUgGRY jazE23TEB/n1EGeBGnkyx6kGKag0tjvhmouF6QqSQopWbDkDS9L98LEhAFJRK6l/UjzLnuxht0Dj+ +ZvffuyaQQmvvqnFpBCn3QBKwJ1G4qqvkHW7uQTWpjQgbD+saYbUtnv+8TU/TBhjZgTjLS7ciYLLJ 91V32SRdlaxQkunUdejHZGsFll+ZeMC8Z2VDogpDrv/0w0rX+9+Ao/Ew62W96e+Z38jVZOFmuYAUy L4XNBgqpw==; Received: from mga09.intel.com ([134.134.136.24]) by merlin.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1csmx5-0006OZ-Ji for linux-arm-kernel@lists.infradead.org; Tue, 28 Mar 2017 08:58:08 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=intel.com; i=@intel.com; q=dns/txt; s=intel; t=1490691487; x=1522227487; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=9THUsX1QBn8iKvehfaTTkTBs0Aq1+dI2E2CEeI7OwAc=; b=OdlWPyMyX8D6cB5QUqv9HiAeJj1WHzEAV6/aKvzHv8G3eGCxDhGNL0lI yRA9snapD4QDyX1vgFKmRbtMzGShhQ==; Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 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 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> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170328_045807_922453_61FB2314 X-CRM114-Status: GOOD ( 16.52 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peterz@infradead.org, matanb@mellanox.com, paulus@samba.org, Elena Reshetova , nbd@openwrt.org, linux-rdma@vger.kernel.org, saeedm@mellanox.com, ganeshgr@chelsio.com, Hans Liljestrand , David Windsor , keescook@chromium.org, j@w1.fi, ajk@comnets.uni-bremen.de, leonro@mellanox.com, matthias.bgg@gmail.com, linux-hams@vger.kernel.org, kvalo@codeaurora.org, blogic@openwrt.org, linux-arm-kernel@lists.infradead.org, linux-ppp@vger.kernel.org, yishaih@mellanox.com, linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org, gregkh@linuxfoundation.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.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 *); };