From patchwork Thu Nov 22 23:24:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 10694989 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C58EF13B5 for ; Thu, 22 Nov 2018 23:24:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B2F562C196 for ; Thu, 22 Nov 2018 23:24:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A74682C3A1; Thu, 22 Nov 2018 23:24:28 +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=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,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 4CA0C2C196 for ; Thu, 22 Nov 2018 23:24:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2439018AbeKWKGI (ORCPT ); Fri, 23 Nov 2018 05:06:08 -0500 Received: from mail-pl1-f193.google.com ([209.85.214.193]:44181 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2407889AbeKWKGI (ORCPT ); Fri, 23 Nov 2018 05:06:08 -0500 Received: by mail-pl1-f193.google.com with SMTP id s5-v6so9787239plq.11 for ; Thu, 22 Nov 2018 15:24:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=GC39Uc7P3gi/7p1CcD37cNNGJJnALDeGhpvP20OXJ10=; b=Kdvte6tL3jMSNczSEa88TX7MTSQOzo/Iuy1sJajJ3Dj0gu9n2Vo6K+tc4JwFPvY1h2 y0NCvDTIcTtXpHCzDC2h0mxRLjbTnZCSEx47pj/g5wcGaPVmOdjHDudsB7d4H6FNcUjN BRQlKPpAZDQ0WKQkGgGzTx+DJSEBy4QRBa9nntuHeduGQdiE88N8tmlwePzsicb0n942 KsxRhIGLAejrg7uS3lZu3Dg0h4Kz+Egs0UBJsB5Mm5M+NmpdrvvBZKYTW+dJ1EcEURKU QWls9JLTc67JtnyyseR9Lnnpy3o3XdQ+zrFDUFth6iFvqvHREcZ5amTWGatzrV3NZbcB cJVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=GC39Uc7P3gi/7p1CcD37cNNGJJnALDeGhpvP20OXJ10=; b=De6yzQVPRQevGz3bhy7BJiZIMNIRt8DTMUN1Ko9lerZ/u5Gx6LR7yEGm7AQZdW/nFI KUvMKFzH0hScnuMXh4JJu2prig9w8AvZV57mwYl7AZtREtGGtFzfXfhavoGKvEFk2n1r mnYO09F9aqSL6OBlguIN+n9ylt0m+IvIcRU2nboVQ1OSXXaov5Xn7fDbixGXkpBjspbp qiugN4fLRxtki5bK1Il38Lw8G2aYq+LakkKEhvjhpHF5eFCoLwvcJJND9Y/t2Tf1TYUY NH7mttqcQuqWiX0+U+l3/Iwb6pcjGxxE3biBfiWqe9Hv2Yvffnckf8HqrL2VCHAEStLt o9Hw== X-Gm-Message-State: AA+aEWZQdMemTuO1nDOr3avnmyDMHAn7CSsq5g3pnPrnne20B+oQPib+ aY6n6gEoW2E2B2599SvJ1zLkG/4V+6s= X-Google-Smtp-Source: AFSGD/WNp6Us8de/NWwdwfRss2cJl80QjRNeLKS5RIw/fFpwYIczIMeoX/1POr7yC6yUuXwbTc3oAg== X-Received: by 2002:a17:902:541:: with SMTP id 59mr5062699plf.88.1542929064326; Thu, 22 Nov 2018 15:24:24 -0800 (PST) Received: from ziepe.ca (S010614cc2056d97f.ed.shawcable.net. [174.3.196.123]) by smtp.gmail.com with ESMTPSA id b27sm48210842pfh.113.2018.11.22.15.24.19 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 22 Nov 2018 15:24:19 -0800 (PST) Received: from jgg by mlx.ziepe.ca with local (Exim 4.90_1) (envelope-from ) id 1gPyKY-0007sn-E3; Thu, 22 Nov 2018 16:24:18 -0700 From: Jason Gunthorpe To: linux-rdma@vger.kernel.org Cc: Jason Gunthorpe Subject: [PATCH rdma-core 05/10] verbs: Replace IBV_INIT_CMD_RESP() with execute_cmd_write Date: Thu, 22 Nov 2018 16:24:11 -0700 Message-Id: <20181122232416.30132-6-jgg@ziepe.ca> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181122232416.30132-1-jgg@ziepe.ca> References: <20181122232416.30132-1-jgg@ziepe.ca> MIME-Version: 1.0 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: Jason Gunthorpe Replace the entire IBV_INIT_CMD_RESP(); write(); VALGRIND_MAKE_MEM_DEFINED(); Sequence with a single function call. This signature has enough information for the execute call to compute the location of all 5 components of a write command. Signed-off-by: Jason Gunthorpe --- libibverbs/cmd.c | 214 ++++++++++++++++++-------------------- libibverbs/cmd_fallback.c | 17 +++ libibverbs/cmd_write.h | 38 +++++++ libibverbs/ibverbs.h | 8 -- 4 files changed, 158 insertions(+), 119 deletions(-) diff --git a/libibverbs/cmd.c b/libibverbs/cmd.c index d485b934a63818..8123aae1ad89b8 100644 --- a/libibverbs/cmd.c +++ b/libibverbs/cmd.c @@ -51,12 +51,13 @@ int ibv_cmd_get_context(struct verbs_context *context_ex, struct ibv_get_context *cmd, size_t cmd_size, struct ib_uverbs_get_context_resp *resp, size_t resp_size) { - IBV_INIT_CMD_RESP(cmd, cmd_size, GET_CONTEXT, resp, resp_size); - - if (write(context_ex->context.cmd_fd, cmd, cmd_size) != cmd_size) - return errno; + int ret; - (void) VALGRIND_MAKE_MEM_DEFINED(resp, resp_size); + ret = execute_cmd_write(&context_ex->context, + IB_USER_VERBS_CMD_GET_CONTEXT, cmd, cmd_size, + resp, resp_size); + if (ret) + return ret; context_ex->context.async_fd = resp->async_fd; context_ex->context.num_comp_vectors = resp->num_comp_vectors; @@ -116,13 +117,12 @@ int ibv_cmd_query_device(struct ibv_context *context, struct ibv_query_device *cmd, size_t cmd_size) { struct ib_uverbs_query_device_resp resp; + int ret; - IBV_INIT_CMD_RESP(cmd, cmd_size, QUERY_DEVICE, &resp, sizeof resp); - - if (write(context->cmd_fd, cmd, cmd_size) != cmd_size) - return errno; - - (void) VALGRIND_MAKE_MEM_DEFINED(&resp, sizeof resp); + ret = execute_cmd_write(context, IB_USER_VERBS_CMD_QUERY_DEVICE, cmd, + cmd_size, &resp, sizeof(resp)); + if (ret) + return ret; memset(device_attr->fw_ver, 0, sizeof device_attr->fw_ver); copy_query_dev_fields(device_attr, &resp, raw_fw_ver); @@ -282,16 +282,16 @@ int ibv_cmd_query_port(struct ibv_context *context, uint8_t port_num, struct ibv_query_port *cmd, size_t cmd_size) { struct ib_uverbs_query_port_resp resp; + int ret; - IBV_INIT_CMD_RESP(cmd, cmd_size, QUERY_PORT, &resp, sizeof resp); cmd->port_num = port_num; memset(cmd->reserved, 0, sizeof cmd->reserved); memset(&resp, 0, sizeof(resp)); - if (write(context->cmd_fd, cmd, cmd_size) != cmd_size) - return errno; - - (void) VALGRIND_MAKE_MEM_DEFINED(&resp, sizeof resp); + ret = execute_cmd_write(context, IB_USER_VERBS_CMD_QUERY_PORT, cmd, + cmd_size, &resp, sizeof(resp)); + if (ret) + return ret; port_attr->state = resp.state; port_attr->max_mtu = resp.max_mtu; @@ -322,12 +322,12 @@ int ibv_cmd_alloc_pd(struct ibv_context *context, struct ibv_pd *pd, struct ibv_alloc_pd *cmd, size_t cmd_size, struct ib_uverbs_alloc_pd_resp *resp, size_t resp_size) { - IBV_INIT_CMD_RESP(cmd, cmd_size, ALLOC_PD, resp, resp_size); - - if (write(context->cmd_fd, cmd, cmd_size) != cmd_size) - return errno; + int ret; - (void) VALGRIND_MAKE_MEM_DEFINED(resp, resp_size); + ret = execute_cmd_write(context, IB_USER_VERBS_CMD_ALLOC_PD, cmd, + cmd_size, resp, resp_size); + if (ret) + return ret; pd->handle = resp->pd_handle; pd->context = context; @@ -356,7 +356,7 @@ int ibv_cmd_open_xrcd(struct ibv_context *context, struct verbs_xrcd *xrcd, struct ibv_open_xrcd *cmd, size_t cmd_size, struct ib_uverbs_open_xrcd_resp *resp, size_t resp_size) { - IBV_INIT_CMD_RESP(cmd, cmd_size, OPEN_XRCD, resp, resp_size); + int ret; if (attr->comp_mask >= IBV_XRCD_INIT_ATTR_RESERVED) return ENOSYS; @@ -367,10 +367,10 @@ int ibv_cmd_open_xrcd(struct ibv_context *context, struct verbs_xrcd *xrcd, cmd->fd = attr->fd; cmd->oflags = attr->oflags; - if (write(context->cmd_fd, cmd, cmd_size) != cmd_size) - return errno; - - (void) VALGRIND_MAKE_MEM_DEFINED(resp, resp_size); + ret = execute_cmd_write(context, IB_USER_VERBS_CMD_OPEN_XRCD, cmd, + cmd_size, resp, resp_size); + if (ret) + return ret; xrcd->xrcd.context = context; xrcd->comp_mask = 0; @@ -403,8 +403,7 @@ int ibv_cmd_reg_mr(struct ibv_pd *pd, void *addr, size_t length, size_t cmd_size, struct ib_uverbs_reg_mr_resp *resp, size_t resp_size) { - - IBV_INIT_CMD_RESP(cmd, cmd_size, REG_MR, resp, resp_size); + int ret; cmd->start = (uintptr_t) addr; cmd->length = length; @@ -412,10 +411,10 @@ int ibv_cmd_reg_mr(struct ibv_pd *pd, void *addr, size_t length, cmd->pd_handle = pd->handle; cmd->access_flags = access; - if (write(pd->context->cmd_fd, cmd, cmd_size) != cmd_size) - return errno; - - (void) VALGRIND_MAKE_MEM_DEFINED(resp, resp_size); + ret = execute_cmd_write(pd->context, IB_USER_VERBS_CMD_REG_MR, cmd, + cmd_size, resp, resp_size); + if (ret) + return ret; vmr->ibv_mr.handle = resp->mr_handle; vmr->ibv_mr.lkey = resp->lkey; @@ -432,7 +431,7 @@ int ibv_cmd_rereg_mr(struct verbs_mr *vmr, uint32_t flags, void *addr, size_t cmd_sz, struct ib_uverbs_rereg_mr_resp *resp, size_t resp_sz) { - IBV_INIT_CMD_RESP(cmd, cmd_sz, REREG_MR, resp, resp_sz); + int ret; cmd->mr_handle = vmr->ibv_mr.handle; cmd->flags = flags; @@ -442,10 +441,10 @@ int ibv_cmd_rereg_mr(struct verbs_mr *vmr, uint32_t flags, void *addr, cmd->pd_handle = (flags & IBV_REREG_MR_CHANGE_PD) ? pd->handle : 0; cmd->access_flags = access; - if (write(vmr->ibv_mr.context->cmd_fd, cmd, cmd_sz) != cmd_sz) - return errno; - - (void)VALGRIND_MAKE_MEM_DEFINED(resp, resp_sz); + ret = execute_cmd_write(vmr->ibv_mr.context, IB_USER_VERBS_CMD_REREG_MR, + cmd, cmd_sz, resp, resp_sz); + if (ret) + return ret; vmr->ibv_mr.lkey = resp->lkey; vmr->ibv_mr.rkey = resp->rkey; @@ -475,15 +474,16 @@ int ibv_cmd_alloc_mw(struct ibv_pd *pd, enum ibv_mw_type type, size_t cmd_size, struct ib_uverbs_alloc_mw_resp *resp, size_t resp_size) { - IBV_INIT_CMD_RESP(cmd, cmd_size, ALLOC_MW, resp, resp_size); + int ret; + cmd->pd_handle = pd->handle; cmd->mw_type = type; memset(cmd->reserved, 0, sizeof(cmd->reserved)); - if (write(pd->context->cmd_fd, cmd, cmd_size) != cmd_size) - return errno; - - (void) VALGRIND_MAKE_MEM_DEFINED(resp, resp_size); + ret = execute_cmd_write(pd->context, IB_USER_VERBS_CMD_ALLOC_MW, cmd, + cmd_size, resp, resp_size); + if (ret) + return ret; mw->context = pd->context; mw->pd = pd; @@ -522,17 +522,16 @@ int ibv_cmd_poll_cq(struct ibv_cq *ibcq, int ne, struct ibv_wc *wc) if (!resp) return -1; - IBV_INIT_CMD_RESP(&cmd, sizeof cmd, POLL_CQ, resp, rsize); cmd.cq_handle = ibcq->handle; cmd.ne = ne; - if (write(ibcq->context->cmd_fd, &cmd, sizeof cmd) != sizeof cmd) { + ret = execute_cmd_write_no_uhw(ibcq->context, IB_USER_VERBS_CMD_POLL_CQ, + &cmd, sizeof(cmd), resp, rsize); + if (ret) { ret = -1; goto out; } - (void) VALGRIND_MAKE_MEM_DEFINED(resp, rsize); - for (i = 0; i < resp->count; i++) { wc[i].wr_id = resp->wc[i].wr_id; wc[i].status = resp->wc[i].status; @@ -571,14 +570,15 @@ int ibv_cmd_resize_cq(struct ibv_cq *cq, int cqe, struct ibv_resize_cq *cmd, size_t cmd_size, struct ib_uverbs_resize_cq_resp *resp, size_t resp_size) { - IBV_INIT_CMD_RESP(cmd, cmd_size, RESIZE_CQ, resp, resp_size); + int ret; + cmd->cq_handle = cq->handle; cmd->cqe = cqe; - if (write(cq->context->cmd_fd, cmd, cmd_size) != cmd_size) - return errno; - - (void) VALGRIND_MAKE_MEM_DEFINED(resp, resp_size); + ret = execute_cmd_write(cq->context, IB_USER_VERBS_CMD_RESIZE_CQ, cmd, + cmd_size, resp, resp_size); + if (ret) + return ret; cq->cqe = resp->cqe; @@ -590,17 +590,18 @@ int ibv_cmd_create_srq(struct ibv_pd *pd, struct ibv_create_srq *cmd, size_t cmd_size, struct ib_uverbs_create_srq_resp *resp, size_t resp_size) { - IBV_INIT_CMD_RESP(cmd, cmd_size, CREATE_SRQ, resp, resp_size); + int ret; + cmd->user_handle = (uintptr_t) srq; cmd->pd_handle = pd->handle; cmd->max_wr = attr->attr.max_wr; cmd->max_sge = attr->attr.max_sge; cmd->srq_limit = attr->attr.srq_limit; - if (write(pd->context->cmd_fd, cmd, cmd_size) != cmd_size) - return errno; - - (void) VALGRIND_MAKE_MEM_DEFINED(resp, resp_size); + ret = execute_cmd_write(pd->context, IB_USER_VERBS_CMD_CREATE_SRQ, cmd, + cmd_size, resp, resp_size); + if (ret) + return ret; srq->handle = resp->srq_handle; srq->context = pd->context; @@ -627,8 +628,7 @@ int ibv_cmd_create_srq_ex(struct ibv_context *context, struct ib_uverbs_create_srq_resp *resp, size_t resp_size) { struct verbs_xrcd *vxrcd = NULL; - - IBV_INIT_CMD_RESP(cmd, cmd_size, CREATE_XSRQ, resp, resp_size); + int ret; if (attr_ex->comp_mask >= IBV_SRQ_INIT_ATTR_RESERVED) return ENOSYS; @@ -664,10 +664,10 @@ int ibv_cmd_create_srq_ex(struct ibv_context *context, return EINVAL; } - if (write(context->cmd_fd, cmd, cmd_size) != cmd_size) - return errno; - - (void) VALGRIND_MAKE_MEM_DEFINED(resp, resp_size); + ret = execute_cmd_write(context, IB_USER_VERBS_CMD_CREATE_XSRQ, cmd, + cmd_size, resp, resp_size); + if (ret) + return ret; srq->srq.handle = resp->srq_handle; srq->srq.context = context; @@ -760,15 +760,15 @@ int ibv_cmd_query_srq(struct ibv_srq *srq, struct ibv_srq_attr *srq_attr, struct ibv_query_srq *cmd, size_t cmd_size) { struct ib_uverbs_query_srq_resp resp; + int ret; - IBV_INIT_CMD_RESP(cmd, cmd_size, QUERY_SRQ, &resp, sizeof resp); cmd->srq_handle = srq->handle; cmd->reserved = 0; - if (write(srq->context->cmd_fd, cmd, cmd_size) != cmd_size) - return errno; - - (void) VALGRIND_MAKE_MEM_DEFINED(&resp, sizeof resp); + ret = execute_cmd_write(srq->context, IB_USER_VERBS_CMD_QUERY_SRQ, cmd, + cmd_size, &resp, sizeof(resp)); + if (ret) + return ret; srq_attr->max_wr = resp.max_wr; srq_attr->max_sge = resp.max_sge; @@ -962,8 +962,6 @@ int ibv_cmd_create_qp_ex(struct ibv_context *context, struct verbs_xrcd *vxrcd = NULL; int err; - IBV_INIT_CMD_RESP(cmd, cmd_size, CREATE_QP, resp, resp_size); - if (attr_ex->comp_mask > (IBV_QP_INIT_ATTR_XRCD | IBV_QP_INIT_ATTR_PD)) return ENOSYS; @@ -972,10 +970,10 @@ int ibv_cmd_create_qp_ex(struct ibv_context *context, if (err) return err; - if (write(context->cmd_fd, cmd, cmd_size) != cmd_size) - return errno; - - (void)VALGRIND_MAKE_MEM_DEFINED(resp, resp_size); + err = execute_cmd_write(context, IB_USER_VERBS_CMD_CREATE_QP, cmd, + cmd_size, resp, resp_size); + if (err) + return err; if (abi_ver == 4) { struct ibv_create_qp_resp_v4 *resp_v4 = @@ -1003,7 +1001,7 @@ int ibv_cmd_create_qp(struct ibv_pd *pd, struct ibv_create_qp *cmd, size_t cmd_size, struct ib_uverbs_create_qp_resp *resp, size_t resp_size) { - IBV_INIT_CMD_RESP(cmd, cmd_size, CREATE_QP, resp, resp_size); + int ret; cmd->user_handle = (uintptr_t) qp; cmd->pd_handle = pd->handle; @@ -1020,10 +1018,10 @@ int ibv_cmd_create_qp(struct ibv_pd *pd, cmd->is_srq = !!attr->srq; cmd->reserved = 0; - if (write(pd->context->cmd_fd, cmd, cmd_size) != cmd_size) - return errno; - - (void) VALGRIND_MAKE_MEM_DEFINED(resp, resp_size); + ret = execute_cmd_write(pd->context, IB_USER_VERBS_CMD_CREATE_QP, cmd, + cmd_size, resp, resp_size); + if (ret) + return ret; qp->handle = resp->qp_handle; qp->qp_num = resp->qpn; @@ -1063,7 +1061,7 @@ int ibv_cmd_open_qp(struct ibv_context *context, struct verbs_qp *qp, struct ib_uverbs_create_qp_resp *resp, size_t resp_size) { struct verbs_xrcd *xrcd; - IBV_INIT_CMD_RESP(cmd, cmd_size, OPEN_QP, resp, resp_size); + int ret; if (attr->comp_mask >= IBV_QP_OPEN_ATTR_RESERVED) return ENOSYS; @@ -1079,10 +1077,10 @@ int ibv_cmd_open_qp(struct ibv_context *context, struct verbs_qp *qp, cmd->qpn = attr->qp_num; cmd->qp_type = attr->qp_type; - if (write(context->cmd_fd, cmd, cmd_size) != cmd_size) - return errno; - - (void) VALGRIND_MAKE_MEM_DEFINED(resp, resp_size); + ret = execute_cmd_write(context, IB_USER_VERBS_CMD_OPEN_QP, cmd, + cmd_size, resp, resp_size); + if (ret) + return ret; qp->qp.handle = resp->qp_handle; qp->qp.context = context; @@ -1112,6 +1110,7 @@ int ibv_cmd_query_qp(struct ibv_qp *qp, struct ibv_qp_attr *attr, struct ibv_query_qp *cmd, size_t cmd_size) { struct ib_uverbs_query_qp_resp resp; + int ret; /* * Starting with IBV_QP_RATE_LIMIT the attribute must go through the @@ -1120,14 +1119,13 @@ int ibv_cmd_query_qp(struct ibv_qp *qp, struct ibv_qp_attr *attr, if (attr_mask & ~(IBV_QP_RATE_LIMIT - 1)) return EOPNOTSUPP; - IBV_INIT_CMD_RESP(cmd, cmd_size, QUERY_QP, &resp, sizeof resp); cmd->qp_handle = qp->handle; cmd->attr_mask = attr_mask; - if (write(qp->context->cmd_fd, cmd, cmd_size) != cmd_size) - return errno; - - (void) VALGRIND_MAKE_MEM_DEFINED(&resp, sizeof resp); + ret = execute_cmd_write(qp->context, IB_USER_VERBS_CMD_QUERY_QP, cmd, + cmd_size, &resp, sizeof(resp)); + if (ret) + return ret; attr->qkey = resp.qkey; attr->rq_psn = resp.rq_psn; @@ -1342,7 +1340,7 @@ int ibv_cmd_post_send(struct ibv_qp *ibqp, struct ibv_send_wr *wr, unsigned wr_count = 0; unsigned sge_count = 0; int cmd_size; - int ret = 0; + int ret; for (i = wr; i; i = i->next) { wr_count++; @@ -1352,7 +1350,6 @@ int ibv_cmd_post_send(struct ibv_qp *ibqp, struct ibv_send_wr *wr, cmd_size = sizeof *cmd + wr_count * sizeof *n + sge_count * sizeof *s; cmd = alloca(cmd_size); - IBV_INIT_CMD_RESP(cmd, cmd_size, POST_SEND, &resp, sizeof resp); cmd->qp_handle = ibqp->handle; cmd->wr_count = wr_count; cmd->sge_count = sge_count; @@ -1404,10 +1401,9 @@ int ibv_cmd_post_send(struct ibv_qp *ibqp, struct ibv_send_wr *wr, } resp.bad_wr = 0; - if (write(ibqp->context->cmd_fd, cmd, cmd_size) != cmd_size) - ret = errno; - - (void) VALGRIND_MAKE_MEM_DEFINED(&resp, sizeof resp); + ret = execute_cmd_write_no_uhw(ibqp->context, + IB_USER_VERBS_CMD_POST_SEND, cmd, + cmd_size, &resp, sizeof(resp)); wr_count = resp.bad_wr; if (wr_count) { @@ -1432,7 +1428,7 @@ int ibv_cmd_post_recv(struct ibv_qp *ibqp, struct ibv_recv_wr *wr, unsigned wr_count = 0; unsigned sge_count = 0; int cmd_size; - int ret = 0; + int ret; for (i = wr; i; i = i->next) { wr_count++; @@ -1442,7 +1438,6 @@ int ibv_cmd_post_recv(struct ibv_qp *ibqp, struct ibv_recv_wr *wr, cmd_size = sizeof *cmd + wr_count * sizeof *n + sge_count * sizeof *s; cmd = alloca(cmd_size); - IBV_INIT_CMD_RESP(cmd, cmd_size, POST_RECV, &resp, sizeof resp); cmd->qp_handle = ibqp->handle; cmd->wr_count = wr_count; cmd->sge_count = sge_count; @@ -1465,10 +1460,9 @@ int ibv_cmd_post_recv(struct ibv_qp *ibqp, struct ibv_recv_wr *wr, } resp.bad_wr = 0; - if (write(ibqp->context->cmd_fd, cmd, cmd_size) != cmd_size) - ret = errno; - - (void) VALGRIND_MAKE_MEM_DEFINED(&resp, sizeof resp); + ret = execute_cmd_write_no_uhw(ibqp->context, + IB_USER_VERBS_CMD_POST_RECV, cmd, + cmd_size, &resp, sizeof(resp)); wr_count = resp.bad_wr; if (wr_count) { @@ -1493,7 +1487,7 @@ int ibv_cmd_post_srq_recv(struct ibv_srq *srq, struct ibv_recv_wr *wr, unsigned wr_count = 0; unsigned sge_count = 0; int cmd_size; - int ret = 0; + int ret; for (i = wr; i; i = i->next) { wr_count++; @@ -1503,7 +1497,6 @@ int ibv_cmd_post_srq_recv(struct ibv_srq *srq, struct ibv_recv_wr *wr, cmd_size = sizeof *cmd + wr_count * sizeof *n + sge_count * sizeof *s; cmd = alloca(cmd_size); - IBV_INIT_CMD_RESP(cmd, cmd_size, POST_SRQ_RECV, &resp, sizeof resp); cmd->srq_handle = srq->handle; cmd->wr_count = wr_count; cmd->sge_count = sge_count; @@ -1526,10 +1519,9 @@ int ibv_cmd_post_srq_recv(struct ibv_srq *srq, struct ibv_recv_wr *wr, } resp.bad_wr = 0; - if (write(srq->context->cmd_fd, cmd, cmd_size) != cmd_size) - ret = errno; - - (void) VALGRIND_MAKE_MEM_DEFINED(&resp, sizeof resp); + ret = execute_cmd_write_no_uhw(srq->context, + IB_USER_VERBS_CMD_POST_SRQ_RECV, cmd, + cmd_size, &resp, sizeof(resp)); wr_count = resp.bad_wr; if (wr_count) { @@ -1549,8 +1541,8 @@ int ibv_cmd_create_ah(struct ibv_pd *pd, struct ibv_ah *ah, size_t resp_size) { struct ibv_create_ah cmd; + int ret; - IBV_INIT_CMD_RESP(&cmd, sizeof cmd, CREATE_AH, resp, resp_size); cmd.user_handle = (uintptr_t) ah; cmd.pd_handle = pd->handle; cmd.attr.dlid = attr->dlid; @@ -1565,10 +1557,10 @@ int ibv_cmd_create_ah(struct ibv_pd *pd, struct ibv_ah *ah, cmd.attr.grh.traffic_class = attr->grh.traffic_class; memcpy(cmd.attr.grh.dgid, attr->grh.dgid.raw, 16); - if (write(pd->context->cmd_fd, &cmd, sizeof cmd) != sizeof cmd) - return errno; - - (void) VALGRIND_MAKE_MEM_DEFINED(resp, resp_size); + ret = execute_cmd_write(pd->context, IB_USER_VERBS_CMD_CREATE_AH, &cmd, + sizeof(cmd), resp, resp_size); + if (ret) + return ret; ah->handle = resp->ah_handle; ah->context = pd->context; diff --git a/libibverbs/cmd_fallback.c b/libibverbs/cmd_fallback.c index 2aac6cb9f24c92..3a43a0c569776f 100644 --- a/libibverbs/cmd_fallback.c +++ b/libibverbs/cmd_fallback.c @@ -244,6 +244,23 @@ int _execute_write_raw(struct ibv_context *ctx, struct ib_uverbs_cmd_hdr *hdr, return 0; } +int _execute_cmd_write(struct ibv_context *ctx, unsigned int write_method, + struct ib_uverbs_cmd_hdr *req, size_t core_req_size, + size_t req_size, void *resp, size_t core_resp_size, + size_t resp_size) +{ + req->command = write_method; + req->in_words = __check_divide(req_size, 4); + req->out_words = __check_divide(resp_size, 4); + + if (write(ctx->cmd_fd, req, req_size) != req_size) + return errno; + + if (resp) + VALGRIND_MAKE_MEM_DEFINED(resp, resp_size); + return 0; +} + int _execute_write_raw_ex(struct ibv_context *ctx, struct ex_hdr *hdr) { size_t write_bytes = diff --git a/libibverbs/cmd_write.h b/libibverbs/cmd_write.h index 98aa58a26d6728..c7a7ef0b50a22b 100644 --- a/libibverbs/cmd_write.h +++ b/libibverbs/cmd_write.h @@ -207,6 +207,44 @@ int _execute_write_raw_ex(struct ibv_context *ctx, struct ex_hdr *req); #define execute_write_ex(ctx, req) \ _execute_write_raw_ex(ctx, get_req_hdr_ex(req)) +/* + * For write() only commands that have fixed core structures and may take uhw + * driver data. The last arguments are the same ones passed into the typical + * ibv_cmd_* function. execute_cmd_write deduces the length of the core + * structure based on the KABI struct linked to the enum op code. + */ +int _execute_cmd_write(struct ibv_context *ctx, unsigned int write_method, + struct ib_uverbs_cmd_hdr *req, size_t core_req_size, + size_t req_size, void *resp, size_t core_resp_size, + size_t resp_size); +#define execute_cmd_write(ctx, enum, cmd, cmd_size, resp, resp_size) \ + ({ \ + (cmd)->core_payload.response = ioctl_ptr_to_u64(resp); \ + _execute_cmd_write( \ + ctx, enum, \ + &(cmd)->hdr + check_type(cmd, IBV_ABI_REQ(enum) *), \ + sizeof(*(cmd)), cmd_size, \ + resp + check_type(resp, IBV_KABI_RESP(enum) *), \ + sizeof(*(resp)), resp_size); \ + }) + +/* + * Execute a write command that does not have a uhw component. THe cmd_size + * and resp_size are the lengths of the core structure. This version is only + * needed if the core structure ends in a flex array, as the internal sizeof() + * in execute_cmd_write() will give the wrong size. + */ +#define execute_cmd_write_no_uhw(ctx, enum, cmd, cmd_size, resp, resp_size) \ + ({ \ + (cmd)->core_payload.response = ioctl_ptr_to_u64(resp); \ + _execute_cmd_write( \ + ctx, enum, \ + &(cmd)->hdr + check_type(cmd, IBV_ABI_REQ(enum) *), \ + cmd_size, cmd_size, \ + resp + check_type(resp, IBV_KABI_RESP(enum) *), \ + resp_size, resp_size); \ + }) + /* * These two macros are used only with execute_ioctl_fallback - they allow the * IOCTL code to be elided by the compiler when disabled. diff --git a/libibverbs/ibverbs.h b/libibverbs/ibverbs.h index 5954cea30986a5..c99d617eace611 100644 --- a/libibverbs/ibverbs.h +++ b/libibverbs/ibverbs.h @@ -93,14 +93,6 @@ static inline const struct verbs_context_ops *get_ops(struct ibv_context *ctx) (cmd)->hdr.out_words = 0; \ } while (0) -#define IBV_INIT_CMD_RESP(cmd, size, opcode, out, outsize) \ - do { \ - (cmd)->hdr.command = IB_USER_VERBS_CMD_##opcode; \ - (cmd)->hdr.in_words = (size) / 4; \ - (cmd)->hdr.out_words = (outsize) / 4; \ - (cmd)->response = (uintptr_t) (out); \ - } while (0) - static inline uint32_t _cmd_ex(uint32_t cmd) { return IB_USER_VERBS_CMD_FLAG_EXTENDED | cmd;