From patchwork Sat Feb 18 21:01:46 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SF Markus Elfring X-Patchwork-Id: 9581275 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 80062600F6 for ; Sat, 18 Feb 2017 21:08:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6E44228779 for ; Sat, 18 Feb 2017 21:08:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5FB9928745; Sat, 18 Feb 2017 21:08:46 +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=ham 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 CD51928745 for ; Sat, 18 Feb 2017 21:08:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753577AbdBRVIo (ORCPT ); Sat, 18 Feb 2017 16:08:44 -0500 Received: from mout.web.de ([212.227.15.4]:56044 "EHLO mout.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753492AbdBRVIn (ORCPT ); Sat, 18 Feb 2017 16:08:43 -0500 Received: from [192.168.1.2] ([77.181.107.93]) by smtp.web.de (mrweb002 [213.165.67.108]) with ESMTPSA (Nemesis) id 0MZUjd-1cw2jJ1HXJ-00LBrd; Sat, 18 Feb 2017 22:01:48 +0100 Subject: [PATCH 13/29] IB/mlx4: Split a condition check in five functions To: linux-rdma@vger.kernel.org, Doug Ledford , Hal Rosenstock , Leon Romanovsky , Matan Barak , Sean Hefty , Yishai Hadas References: <1935365a-bd7c-461e-6a84-0c5d3a501fff@users.sourceforge.net> Cc: LKML , kernel-janitors@vger.kernel.org From: SF Markus Elfring Message-ID: Date: Sat, 18 Feb 2017 22:01:46 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.7.1 MIME-Version: 1.0 In-Reply-To: <1935365a-bd7c-461e-6a84-0c5d3a501fff@users.sourceforge.net> X-Provags-ID: V03:K0:U1vRhrkBcszB2EbIzxDEtDlrHBgtUHpB7c2KYz+2UUM1bJE0Xu+ aXdi0GDv9ua8jmGtCkhDGZCg0Kt9EJB9vulhHY+HDAefVpKU0JHcQvygYEtXRBvYWtLCroq kp33xyOS5XQIrbqWsSjV26a83CUAoJGVXP6+RYmWBRictvY47ADTQjyoU9a7wbbNQDeovJd 0SlbiF+Foynvr0PP2OMHA== X-UI-Out-Filterresults: notjunk:1; V01:K0:kZn+Qtqs1Mk=:lF14QkQhweuzdzFIiUp1ae vkkz1ehMg4DO6xoMynfw24352zn4M/xTfFIYzTT0YizWzct1pVUg/x6w7/1/lwztv3K/SUrUi WaCUjIlElvjtGj1AIb7kvponiZgHIeoSyLBzq6AwB/xd4nPkK4o0nHPsCr3XpXZZxW0Nxbvpv cFdoQigvs+S8CS1Y1o8o60BSrD8bYMkkxl5CAOtnZazbbDAQ3dCnDkDOECp9mhM6Kc1T5YFOX D3nVu7flSQAyjWZDeXN1nJcwrhwDdP+EDO3ORtLda/k39NTZwyITON8TCvwYW0+/aZ8Wbbxa2 Xq4pSiVT28JXj2wy/8WIyJvFliB022MwZ7E0vrVEVSO/lI1qSj/WjkMVznBOBS9Hrlgw9UWsa 8ARR0GFXsBfDN1gfxnXfIq/Um2QtPe7FwCJ3Kmv5KzTh9fEZg0t3KU4/VNoltcapQaHkkxKAC d2e5bmIS5iZTA/HmVVJf00B+F8mnF38ZNxObAAohY+FDwBTgbTVPHVtncad7/c7LsvUz+dSw8 V3hyzViSSlWYk7EfPvVMydF5YbEqo4rBpDNyScJCS9ffB6G5WzNjywiZZr7jdkDVhhq5W9sbq s9KE1ZLZfQK6ju4pb19R6dDA3RlcwJiw9PY5+xHOF3v9nLWSB2X7XtNscTpA9yi9zEOg80Gv4 LxyEdLUupvecLxZ7cIeCVUzYzyr1pS0K6jEhG43Antc9Lj1yLNIWu7CC4MYxFDPse4lqFFcJ/ zQc4VbK9wx0Dtu9Rcq8MIRPy/IBTc3gNhhxyG9g9G08bE+p/QzImboWAuYZAZDHAd/umA8kr9 Bn0WGm6 Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Markus Elfring Date: Sat, 18 Feb 2017 14:43:34 +0100 The kfree() function was called in up to two cases during error handling even if the passed variable contained a null pointer. * Split a condition check for memory allocation failures. * Adjust jump targets according to the Linux coding style convention. * Delete initialisations for variables at the beginning which became unnecessary with this refactoring. Signed-off-by: Markus Elfring Reviewed-by: Majd Dibbiny --- drivers/infiniband/hw/mlx4/main.c | 90 +++++++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 31 deletions(-) diff --git a/drivers/infiniband/hw/mlx4/main.c b/drivers/infiniband/hw/mlx4/main.c index b3b5ded85166..c0c299ffa0f6 100644 --- a/drivers/infiniband/hw/mlx4/main.c +++ b/drivers/infiniband/hw/mlx4/main.c @@ -432,8 +432,8 @@ static int mlx4_ib_query_device(struct ib_device *ibdev, struct ib_udata *uhw) { struct mlx4_ib_dev *dev = to_mdev(ibdev); - struct ib_smp *in_mad = NULL; - struct ib_smp *out_mad = NULL; + struct ib_smp *in_mad; + struct ib_smp *out_mad; int err; int have_ib_ports; struct mlx4_uverbs_ex_query_device cmd; @@ -458,10 +458,14 @@ static int mlx4_ib_query_device(struct ib_device *ibdev, resp.response_length = offsetof(typeof(resp), response_length) + sizeof(resp.response_length); in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL); + if (!in_mad) + return -ENOMEM; + out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL); - err = -ENOMEM; - if (!in_mad || !out_mad) - goto out; + if (!out_mad) { + err = -ENOMEM; + goto free_in_mad; + } init_query_mad(in_mad); in_mad->attr_id = IB_SMP_ATTR_NODE_INFO; @@ -570,9 +574,9 @@ static int mlx4_ib_query_device(struct ib_device *ibdev, goto out; } out: - kfree(in_mad); kfree(out_mad); - +free_in_mad: + kfree(in_mad); return err; } @@ -588,16 +592,21 @@ mlx4_ib_port_link_layer(struct ib_device *device, u8 port_num) static int ib_link_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *props, int netw_view) { - struct ib_smp *in_mad = NULL; - struct ib_smp *out_mad = NULL; + struct ib_smp *in_mad; + struct ib_smp *out_mad; int ext_active_speed; int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS; - int err = -ENOMEM; + int err; in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL); + if (!in_mad) + return -ENOMEM; + out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL); - if (!in_mad || !out_mad) - goto out; + if (!out_mad) { + err = -ENOMEM; + goto free_in_mad; + } init_query_mad(in_mad); in_mad->attr_id = IB_SMP_ATTR_PORT_INFO; @@ -670,8 +679,9 @@ static int ib_link_query_port(struct ib_device *ibdev, u8 port, props->active_speed = IB_SPEED_SDR; out: - kfree(in_mad); kfree(out_mad); +free_in_mad: + kfree(in_mad); return err; } @@ -763,17 +773,22 @@ static int mlx4_ib_query_port(struct ib_device *ibdev, u8 port, int __mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index, union ib_gid *gid, int netw_view) { - struct ib_smp *in_mad = NULL; - struct ib_smp *out_mad = NULL; - int err = -ENOMEM; + struct ib_smp *in_mad; + struct ib_smp *out_mad; + int err; struct mlx4_ib_dev *dev = to_mdev(ibdev); int clear = 0; int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS; in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL); + if (!in_mad) + return -ENOMEM; + out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL); - if (!in_mad || !out_mad) - goto out; + if (!out_mad) { + err = -ENOMEM; + goto free_in_mad; + } init_query_mad(in_mad); in_mad->attr_id = IB_SMP_ATTR_PORT_INFO; @@ -811,8 +826,9 @@ int __mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index, out: if (clear) memset(gid->raw + 8, 0, 8); - kfree(in_mad); kfree(out_mad); +free_in_mad: + kfree(in_mad); return err; } @@ -902,15 +918,20 @@ static void mlx4_init_sl2vl_tbl(struct mlx4_ib_dev *mdev) int __mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey, int netw_view) { - struct ib_smp *in_mad = NULL; - struct ib_smp *out_mad = NULL; + struct ib_smp *in_mad; + struct ib_smp *out_mad; int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS; - int err = -ENOMEM; + int err; in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL); + if (!in_mad) + return -ENOMEM; + out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL); - if (!in_mad || !out_mad) - goto out; + if (!out_mad) { + err = -ENOMEM; + goto free_in_mad; + } init_query_mad(in_mad); in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE; @@ -927,8 +948,9 @@ int __mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index, *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]); out: - kfree(in_mad); kfree(out_mad); +free_in_mad: + kfree(in_mad); return err; } @@ -2083,15 +2105,20 @@ static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) static int init_node_data(struct mlx4_ib_dev *dev) { - struct ib_smp *in_mad = NULL; - struct ib_smp *out_mad = NULL; + struct ib_smp *in_mad; + struct ib_smp *out_mad; int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS; - int err = -ENOMEM; + int err; in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL); + if (!in_mad) + return -ENOMEM; + out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL); - if (!in_mad || !out_mad) - goto out; + if (!out_mad) { + err = -ENOMEM; + goto free_in_mad; + } init_query_mad(in_mad); in_mad->attr_id = IB_SMP_ATTR_NODE_DESC; @@ -2114,8 +2141,9 @@ static int init_node_data(struct mlx4_ib_dev *dev) memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8); out: - kfree(in_mad); kfree(out_mad); +free_in_mad: + kfree(in_mad); return err; }