From patchwork Mon May 12 08:30:50 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 4156991 Return-Path: X-Original-To: patchwork-linux-rdma@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 53CEABFF02 for ; Mon, 12 May 2014 09:07:20 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 661142022A for ; Mon, 12 May 2014 09:07:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8113E2021B for ; Mon, 12 May 2014 09:07:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750939AbaELJHQ (ORCPT ); Mon, 12 May 2014 05:07:16 -0400 Received: from sf1.bxl.stone.is ([87.238.167.36]:51336 "EHLO sf1.bxl.stone.is" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750762AbaELJHP (ORCPT ); Mon, 12 May 2014 05:07:15 -0400 X-Greylist: delayed 2169 seconds by postgrey-1.27 at vger.kernel.org; Mon, 12 May 2014 05:07:15 EDT Received: from smtp03.stone-is.org ([87.238.162.6] helo=smtpgw.stone-is.be) by sf1.bxl.stone.is with esmtp (Exim 4.82) (envelope-from ) id 1WjldW-0004Di-SN; Mon, 12 May 2014 10:31:05 +0200 Received: from localhost (unknown [127.0.0.1]) by smtpgw.stone-is.be (Postfix) with ESMTP id 5A4C6334BBB; Mon, 12 May 2014 08:30:52 +0000 (UTC) Received: from smtpgw.stone-is.be ([127.0.0.1]) by localhost (smtpgw.stone-is.be [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id nM9o6FWzfwaX; Mon, 12 May 2014 10:30:51 +0200 (CEST) Received: from vz19.stone-is.net (vz19.stone-is.net [87.238.162.57]) by smtpgw.stone-is.be (Postfix) with ESMTP id ABB7B334BFE; Mon, 12 May 2014 10:30:50 +0200 (CEST) X-No-Relay: not in my network X-No-Relay: not in my network X-No-Relay: not in my network Received: from [192.168.1.117] (178-119-65-67.access.telenet.be [178.119.65.67]) by vz19.stone-is.net (Postfix) with ESMTPSA id 808F62D503B; Mon, 12 May 2014 10:30:50 +0200 (CEST) Message-ID: <537086BA.3020807@acm.org> Date: Mon, 12 May 2014 10:30:50 +0200 From: Bart Van Assche User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 MIME-Version: 1.0 To: Roland Dreier CC: Alex Chiang , linux-rdma Subject: [PATCH 2/3] IB/umad: Fix error handling References: <53708666.6060209@acm.org> In-Reply-To: <53708666.6060209@acm.org> X-Enigmail-Version: 1.6 X-Filter-ID: XtLePq6GTMn8G68F0EmQvdthj6rk7ohlAkaRQG+cDaLZlN3ufQ0ZRHdy43341LQ0JYRGiu4wDuX/ sGw1Y7RAsb1SR70Wkof6QMErSa5awdkG42j8At+vc1CnB5mb3BSJepitAFwwEbMGYUpxADAT6yBk 0ydfoHV3ZNUPsk+Z52roCkoJ9G4OvGb2HIs11FkfMlUS0dQK1WpVL+8woncDCFWIDYWfrAQkOHrK EdgQK9aSIh5RqtMPSr5drLxMMmpKZuM7jUXIESohoO51xWmU8RJAVQYcOmAyRxcS32YydgD3I84v mkP3oh+YQqB4J59UsV2kVJ6iI4C9lEspssReek9NT0wnz2h8ADwycfME7g7Q08vWrPDgMXSp0PD/ +0YjOyzxI2kbjN9NHAai2rh92SivUpI7xYBewOHb5FYgdqqjrcITHcksS7joBcHFiJvPoXKxVxxs Ub1NautSP08O+Rz9D+/9oSt5jUrIaWXjdPREcrtd1OJc8clu1cTxQ0e5UzNQey813JRaAeaBSS7l 0j3WJzM9TQ2QEdWAURl0Sg657IUNtZsmYlZe2TrEPH/I5Djdh90lkiY12gKagMfROw6LUO0JT1q/ AWrYG3aFTnrY3JzFJCHR3HpTyYBZpumpLMyO3ZTuK9Swav1ZU5rUoxgD/FVEY7v4laIdTJ8d+KD7 wgeRkzvLcTEd6PfP2bk7MS+4ayUpOtEhdxekWDmK9g== X-Report-Abuse-To: spam@sf1.bxl.stone.is X-Originating-IP: 87.238.162.6 X-SpamExperts-Domain: stone.is X-SpamExperts-Username: 87.238.162.6 Authentication-Results: bxl.stone.is; auth=pass smtp.auth=87.238.162.6 X-SpamExperts-Outgoing-Class: ham X-SpamExperts-Outgoing-Evidence: Combined (0.00) X-Recommended-Action: accept Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Avoid leaking a kref count in ib_umad_open() if port->ib_dev == NULL or if nonseekable_open() fails. Avoid leaking a kref count, that sm_sem is kept down and also that the IB_PORT_SM capability mask is not cleared in ib_umad_sm_open() if nonseekable_open() fails. Signed-off-by: Bart Van Assche Cc: --- drivers/infiniband/core/user_mad.c | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/drivers/infiniband/core/user_mad.c b/drivers/infiniband/core/user_mad.c index e61287c..5c67d80 100644 --- a/drivers/infiniband/core/user_mad.c +++ b/drivers/infiniband/core/user_mad.c @@ -780,24 +780,20 @@ static int ib_umad_open(struct inode *inode, struct file *filp) { struct ib_umad_port *port; struct ib_umad_file *file; - int ret; + int ret = -ENXIO; port = container_of(inode->i_cdev, struct ib_umad_port, cdev); kref_get(&port->umad_dev->ref); mutex_lock(&port->file_mutex); - if (!port->ib_dev) { - ret = -ENXIO; + if (!port->ib_dev) goto out; - } + ret = -ENOMEM; file = kzalloc(sizeof *file, GFP_KERNEL); - if (!file) { - kref_put(&port->umad_dev->ref, ib_umad_release_dev); - ret = -ENOMEM; + if (!file) goto out; - } mutex_init(&file->mutex); spin_lock_init(&file->send_lock); @@ -814,6 +810,10 @@ static int ib_umad_open(struct inode *inode, struct file *filp) out: mutex_unlock(&port->file_mutex); + + if (ret) + kref_put(&port->umad_dev->ref, ib_umad_release_dev); + return ret; } @@ -892,18 +892,27 @@ static int ib_umad_sm_open(struct inode *inode, struct file *filp) } ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props); - if (ret) { - up(&port->sm_sem); - goto fail; - } + if (ret) + goto up_sem; filp->private_data = port; - return nonseekable_open(inode, filp); + ret = nonseekable_open(inode, filp); + if (ret) + goto clr_sm_cap; fail: - kref_put(&port->umad_dev->ref, ib_umad_release_dev); + if (ret) + kref_put(&port->umad_dev->ref, ib_umad_release_dev); return ret; + +clr_sm_cap: + swap(props.set_port_cap_mask, props.clr_port_cap_mask); + ib_modify_port(port->ib_dev, port->port_num, 0, &props); + +up_sem: + up(&port->sm_sem); + goto fail; } static int ib_umad_sm_close(struct inode *inode, struct file *filp)