From patchwork Mon Aug 15 11:38:56 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dotan Barak X-Patchwork-Id: 1067122 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter2.kernel.org (8.14.4/8.14.4) with ESMTP id p7FBd22i016463 for ; Mon, 15 Aug 2011 11:39:03 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752387Ab1HOLjB (ORCPT ); Mon, 15 Aug 2011 07:39:01 -0400 Received: from mail-wy0-f174.google.com ([74.125.82.174]:65292 "EHLO mail-wy0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751736Ab1HOLjA (ORCPT ); Mon, 15 Aug 2011 07:39:00 -0400 Received: by wyg24 with SMTP id 24so3427216wyg.19 for ; Mon, 15 Aug 2011 04:38:59 -0700 (PDT) Received: by 10.227.11.137 with SMTP id t9mr3531223wbt.9.1313408339453; Mon, 15 Aug 2011 04:38:59 -0700 (PDT) Received: from vnc11.lab.mtl.com ([82.166.227.17]) by mx.google.com with ESMTPS id 8sm4488772wbx.34.2011.08.15.04.38.58 (version=SSLv3 cipher=OTHER); Mon, 15 Aug 2011 04:38:58 -0700 (PDT) From: Dotan Barak To: Sean Hefty Subject: [PATCH 2/2] librdmacm: fix resource leaks when CMA_CREATE_MSG_CMD_RESP fails Date: Mon, 15 Aug 2011 14:38:56 +0300 User-Agent: KMail/1.9.4 Cc: linux-rdma@vger.kernel.org MIME-Version: 1.0 Content-Disposition: inline Message-Id: <201108151438.56670.dotanb@sw.voltaire.com> Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter2.kernel.org [140.211.167.43]); Mon, 15 Aug 2011 11:39:03 +0000 (UTC) If the macro CMA_CREATE_MSG_CMD_RESP is being called and there is a failure, the macro should release the allocated resources before returning from the called function Signed-off-by: Dotan Barak --- -- To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/src/cma.c b/src/cma.c index bad1ba3..0512187 100755 --- a/src/cma.c +++ b/src/cma.c @@ -59,14 +59,16 @@ #include #include -#define CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, type, size) \ +#define CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, type, size, clean_cmd) \ do { \ - struct ucma_abi_cmd_hdr *hdr; \ + struct ucma_abi_cmd_hdr *hdr; \ \ size = sizeof(*hdr) + sizeof(*cmd); \ msg = alloca(size); \ - if (!msg) \ + if (!msg) { \ + clean_cmd; \ return ERR(ENOMEM); \ + } \ hdr = msg; \ cmd = msg + sizeof(*hdr); \ hdr->cmd = type; \ @@ -74,8 +76,10 @@ do { \ hdr->out = sizeof(*resp); \ memset(cmd, 0, sizeof(*cmd)); \ resp = alloca(sizeof(*resp)); \ - if (!resp) \ + if (!resp) { \ + clean_cmd; \ return ERR(ENOMEM); \ + } \ cmd->response = (uintptr_t)resp;\ } while (0) @@ -460,7 +464,7 @@ static int rdma_create_id2(struct rdma_event_channel *channel, if (!id_priv) return ERR(ENOMEM); - CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_CREATE_ID, size); + CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_CREATE_ID, size, ucma_free_id(id_priv)); cmd->uid = (uintptr_t) id_priv; cmd->ps = ps; cmd->qp_type = qp_type; @@ -497,7 +501,7 @@ static int ucma_destroy_kern_id(int fd, uint32_t handle) void *msg; int ret, size; - CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_DESTROY_ID, size); + CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_DESTROY_ID, size, ); cmd->id = handle; ret = write(fd, msg, size); @@ -556,7 +560,7 @@ static int ucma_query_addr(struct rdma_cm_id *id) void *msg; int ret, size; - CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_QUERY, size); + CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_QUERY, size, ); id_priv = container_of(id, struct cma_id_private, id); cmd->id = id_priv->handle; cmd->option = UCMA_QUERY_ADDR; @@ -590,7 +594,7 @@ static int ucma_query_gid(struct rdma_cm_id *id) void *msg; int ret, size; - CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_QUERY, size); + CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_QUERY, size, ); id_priv = container_of(id, struct cma_id_private, id); cmd->id = id_priv->handle; cmd->option = UCMA_QUERY_GID; @@ -702,7 +706,7 @@ static int ucma_query_route(struct rdma_cm_id *id) void *msg; int ret, size, i; - CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_QUERY_ROUTE, size); + CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_QUERY_ROUTE, size, ); id_priv = container_of(id, struct cma_id_private, id); cmd->id = id_priv->handle; @@ -936,7 +940,7 @@ static int rdma_init_qp_attr(struct rdma_cm_id *id, struct ibv_qp_attr *qp_attr, void *msg; int ret, size; - CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_INIT_QP_ATTR, size); + CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_INIT_QP_ATTR, size, ); id_priv = container_of(id, struct cma_id_private, id); cmd->id = id_priv->handle; cmd->qp_state = qp_attr->qp_state; @@ -1613,7 +1617,7 @@ static int rdma_join_multicast2(struct rdma_cm_id *id, struct sockaddr *addr, if (af_ib_support) { struct ucma_abi_join_mcast *cmd; - CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_JOIN_MCAST, size); + CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_JOIN_MCAST, size, free(mc)); cmd->id = id_priv->handle; memcpy(&cmd->addr, addr, addrlen); cmd->addr_size = addrlen; @@ -1622,7 +1626,7 @@ static int rdma_join_multicast2(struct rdma_cm_id *id, struct sockaddr *addr, } else { struct ucma_abi_join_ip_mcast *cmd; - CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_JOIN_IP_MCAST, size); + CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_JOIN_IP_MCAST, size, free(mc)); cmd->id = id_priv->handle; memcpy(&cmd->addr, addr, addrlen); cmd->uid = (uintptr_t) mc; @@ -1691,7 +1695,7 @@ int rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr) if (id->qp) ibv_detach_mcast(id->qp, &mc->mgid, mc->mlid); - CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_LEAVE_MCAST, size); + CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_LEAVE_MCAST, size, ); cmd->id = mc->handle; ret = write(id->channel->fd, msg, size); @@ -1940,7 +1944,7 @@ int rdma_get_cm_event(struct rdma_event_channel *channel, retry: memset(evt, 0, sizeof *evt); - CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_GET_EVENT, size); + CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_GET_EVENT, size, free(evt)); ret = write(channel->fd, msg, size); if (ret != size) { free(evt); @@ -2117,7 +2121,7 @@ int rdma_migrate_id(struct rdma_cm_id *id, struct rdma_event_channel *channel) return -1; } - CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_MIGRATE_ID, size); + CMA_CREATE_MSG_CMD_RESP(msg, cmd, resp, UCMA_CMD_MIGRATE_ID, size, if (sync) rdma_destroy_event_channel(channel)); cmd->id = id_priv->handle; cmd->fd = id->channel->fd;