From patchwork Fri Oct 20 07:23:41 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Reshetova, Elena" X-Patchwork-Id: 10019109 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 7191860224 for ; Fri, 20 Oct 2017 07:41:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6328828DFE for ; Fri, 20 Oct 2017 07:41:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 57EBA28EC7; Fri, 20 Oct 2017 07:41:25 +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=-3.7 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, RCVD_IN_DNSWL_MED, RCVD_IN_SORBS_SPAM 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 C70F428DFE for ; Fri, 20 Oct 2017 07:41:24 +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=5HAFfKmO7lgoYSvzsoT6M36Ul1GF819j32ojtP6Ee6w=; b=F76eh1nOJJopXKSFqYqOVBmzL1 vPkOGf5/e7F1SzqUOK0N/1nmSHRKcZjR2Ou0k2dEwMloPnP8TGLh8FlDY0fqR1iv3+03kyCs44XYk Tx828ja/CjU2Rew0Klg/E+dvs71JPZSVhSz3uYgXNjCqh9HT/tnjA116OMppKjKwRQHjy1XvP5fF6 T8i1remsgLQfvrp6nR5J5PWxnq7QTbjw2q0DgriB6LTQ4nr4ISts6UIVMoUK8cIP44gH0fA+wHzbD tCbCMcxWJMeViGMyfUZOv1f5rTnP+SJPPIQDl3j5H9hToAFUOZd+Fr6UugxZXmc5P+DfonxvMCFME SAc7Xw+A==; 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 1e5Rvo-0003OT-0w; Fri, 20 Oct 2017 07:41:24 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1e5Ruv-0000Vg-It; Fri, 20 Oct 2017 07:40:29 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.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=qsgy2JvCL3YGxJ7NTXOoYqGIz5pkUwE3lJHTuQS9I0A=; b=SeJReOR4AncyyOraKflk7JOSF J7HPiE8CziiukPzoHN4Uj7YIYGmLyvM0rg8+G2AO3or39eC3e9CDZVwY7am7BN4jtRVng/h+2fKVH O7q4L1QUZRnesWuu6u/PFtEfZm6nAQCqNdD4W6JJQ5GF9nKAmTXjT8MmPWMfZrRtotXfkHaNMQ3s3 c1NwP/NulnI/pHTuI1n4q/ScbR2kdSVaqSS8I087mXj+hpeulFt872EW3P3dNE/0XW8M/fs5SRmk4 LfRw6YIN/6bRnI9+FoI8WwowCdpA4nJ7/P1XcKgxq156UqnRJXctmmdZ6LilDbXAcc+FMuVwEW7wZ dfBaC9Xsw==; Received: from mga03.intel.com ([134.134.136.65]) by casper.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1e5RgR-0005la-S0; Fri, 20 Oct 2017 07:25:36 +0000 Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Oct 2017 00:25:09 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.43,405,1503385200"; d="scan'208";a="911830774" Received: from elena-thinkpad-x230.fi.intel.com ([10.237.72.87]) by FMSMGA003.fm.intel.com with ESMTP; 20 Oct 2017 00:24:33 -0700 From: Elena Reshetova To: davem@davemloft.net Subject: [PATCH 07/15] drivers, net, mlx5: convert fs_node.refcount from atomic_t to refcount_t Date: Fri, 20 Oct 2017 10:23:41 +0300 Message-Id: <1508484229-19706-8-git-send-email-elena.reshetova@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1508484229-19706-1-git-send-email-elena.reshetova@intel.com> References: <1508484229-19706-1-git-send-email-elena.reshetova@intel.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20171020_082532_339220_F03E9B31 X-CRM114-Status: GOOD ( 18.90 ) 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, zbr@ioremap.net, Elena Reshetova , nbd@openwrt.org, linux-rdma@vger.kernel.org, saeedm@mellanox.com, ganeshgr@chelsio.com, ajk@comnets.uni-bremen.de, keescook@chromium.org, sean.wang@mediatek.com, leonro@mellanox.com, linux-mediatek@lists.infradead.org, john@phrozen.org, matthias.bgg@gmail.com, linux-hams@vger.kernel.org, linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org, yishaih@mellanox.com, linux-kernel@vger.kernel.org, linux-ppp@vger.kernel.org, tariqt@mellanox.com 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 atomic_t variables are currently used to implement reference counters with the following properties: - counter is initialized to 1 using atomic_set() - a resource is freed upon counter reaching zero - once counter reaches zero, its further increments aren't allowed - counter schema uses basic atomic operations (set, inc, inc_not_zero, dec_and_test, etc.) Such atomic variables should be converted to a newly provided refcount_t type and API that prevents accidental counter overflows and underflows. This is important since overflows and underflows can lead to use-after-free situation and be exploitable. The variable fs_node.refcount is used as pure reference counter. Convert it to refcount_t and fix up the operations. Suggested-by: Kees Cook Reviewed-by: David Windsor Reviewed-by: Hans Liljestrand Signed-off-by: Elena Reshetova --- drivers/net/ethernet/mellanox/mlx5/core/fs_core.c | 28 +++++++++++------------ drivers/net/ethernet/mellanox/mlx5/core/fs_core.h | 3 ++- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/drivers/net/ethernet/mellanox/mlx5/core/fs_core.c b/drivers/net/ethernet/mellanox/mlx5/core/fs_core.c index f77e496..c7fa1389 100644 --- a/drivers/net/ethernet/mellanox/mlx5/core/fs_core.c +++ b/drivers/net/ethernet/mellanox/mlx5/core/fs_core.c @@ -188,7 +188,7 @@ static void tree_init_node(struct fs_node *node, void (*del_hw_func)(struct fs_node *), void (*del_sw_func)(struct fs_node *)) { - atomic_set(&node->refcount, 1); + refcount_set(&node->refcount, 1); INIT_LIST_HEAD(&node->list); INIT_LIST_HEAD(&node->children); init_rwsem(&node->lock); @@ -200,7 +200,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 */ @@ -212,7 +212,7 @@ static void tree_add_node(struct fs_node *node, struct fs_node *parent) static int tree_get_node(struct fs_node *node) { - return atomic_add_unless(&node->refcount, 1, 0); + return refcount_inc_not_zero(&node->refcount); } static void nested_down_read_ref_node(struct fs_node *node, @@ -220,7 +220,7 @@ static void nested_down_read_ref_node(struct fs_node *node, { if (node) { down_read_nested(&node->lock, class); - atomic_inc(&node->refcount); + refcount_inc(&node->refcount); } } @@ -229,7 +229,7 @@ static void nested_down_write_ref_node(struct fs_node *node, { if (node) { down_write_nested(&node->lock, class); - atomic_inc(&node->refcount); + refcount_inc(&node->refcount); } } @@ -237,19 +237,19 @@ static void down_write_ref_node(struct fs_node *node) { if (node) { down_write(&node->lock); - atomic_inc(&node->refcount); + refcount_inc(&node->refcount); } } static void up_read_ref_node(struct fs_node *node) { - atomic_dec(&node->refcount); + refcount_dec(&node->refcount); up_read(&node->lock); } static void up_write_ref_node(struct fs_node *node) { - atomic_dec(&node->refcount); + refcount_dec(&node->refcount); up_write(&node->lock); } @@ -257,7 +257,7 @@ static void tree_put_node(struct fs_node *node) { struct fs_node *parent_node = node->parent; - if (atomic_dec_and_test(&node->refcount)) { + if (refcount_dec_and_test(&node->refcount)) { if (node->del_hw_func) node->del_hw_func(node); if (parent_node) { @@ -280,8 +280,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); @@ -1184,7 +1184,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]); @@ -1215,7 +1215,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; } } @@ -1466,7 +1466,7 @@ static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg, trace_mlx5_fs_set_fte(fte, false); 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); trace_mlx5_fs_add_rule(handle->rule[i]); } diff --git a/drivers/net/ethernet/mellanox/mlx5/core/fs_core.h b/drivers/net/ethernet/mellanox/mlx5/core/fs_core.h index 80f6f3c7..397d24a 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 #include @@ -84,7 +85,7 @@ struct fs_node { struct fs_node *root; /* lock the node for writing and traversing */ struct rw_semaphore lock; - atomic_t refcount; + refcount_t refcount; bool active; void (*del_hw_func)(struct fs_node *); void (*del_sw_func)(struct fs_node *);