From patchwork Thu Oct 12 12:19:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrien Mazarguil X-Patchwork-Id: 30260 X-Patchwork-Delegate: ferruh.yigit@amd.com Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 18DB31B2D9; Thu, 12 Oct 2017 14:20:26 +0200 (CEST) Received: from mail-wm0-f53.google.com (mail-wm0-f53.google.com [74.125.82.53]) by dpdk.org (Postfix) with ESMTP id C4DC31B2A4 for ; Thu, 12 Oct 2017 14:20:19 +0200 (CEST) Received: by mail-wm0-f53.google.com with SMTP id m72so12690504wmc.1 for ; Thu, 12 Oct 2017 05:20:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=6wind-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=wEmvSH+2hlgBNS/zIHBoEjA3TVoSR89Q4IGDjhgRpvQ=; b=ddsgDeYS1qX6Ls7X+tz2dlR4IzF1Tmgh8iw9uqBgzSC+SJ1BTkmQ8aE5ZSdexINI1G OEfiRB3rwLkP+ojgDIfHy9vGWZjmFGQ9QNvsjoKwQW0+oYtErtZr4c3dRAvhl488ozCO odh2FM5ZoPePXbhZs5kTEVIiPoRojzWP8TXQA+l8Im8NekUxc3/xvk1SQpaHSdMjWFim ci2lwEofN9TYmlctFA2yEZMuBT8Se1c/u8G15m2kNATl++vDnXQ/phFPqLLGOgrg5Lga SiJ3F9C3K0oCIcKAUphp2u7LtiDJEReHfcGEa7S0/Hf7uhxRChFlko4zV2vfBYizWGng knQA== 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; bh=wEmvSH+2hlgBNS/zIHBoEjA3TVoSR89Q4IGDjhgRpvQ=; b=eFGOl7QPYUbafIUX9Aj61KAM/PyddJZK7SFSRvIuo/CBA+e7Ejdz830wDK0y5p2BuN AKsxn8YGndwOu/6FWuJ2BPr2KXWPxDO0PQURwQkWoUWXDm0D7xqtRR4vX8mrCDlW1sFQ kPdIRRFei9EYG6roYTr+ps68OKia55dA189Hf6DSDOJJQb/cSX8Di5OZE9l/tYFnQdBL uBTywD9Ok+EtIr1Mio8SMHL3XjvQpajxlNNHbT3qDIOb3kajBsCKJEo4t0G7qSVaXyXe KRp/qeS/Suy8Lap5J3QKHOHC9llqAgBpsVZvVkn9TrCqmLhH+1FFi0L/qPZhheaTuyLm EiAg== X-Gm-Message-State: AMCzsaU3Fn7/6hAck2k0DASm6x582wLUi8MOaKwciHx1sVjEbkJ4LsYF MQp+L5PUS/bMDGdZ1DJ6C7oBNsIE X-Google-Smtp-Source: AOwi7QAwDIDXbb0XQU0BK4eDFj8txBYdrJQWTOYTlK2IutLXYML7HeW/toB3pNQe+P579yUR67jGdA== X-Received: by 10.28.113.196 with SMTP id d65mr1547052wmi.151.1507810819451; Thu, 12 Oct 2017 05:20:19 -0700 (PDT) Received: from 6wind.com (host.78.145.23.62.rev.coltfrance.com. [62.23.145.78]) by smtp.gmail.com with ESMTPSA id s196sm153007wmb.6.2017.10.12.05.20.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 Oct 2017 05:20:18 -0700 (PDT) From: Adrien Mazarguil To: Ferruh Yigit Cc: Nelio Laranjeiro , dev@dpdk.org Date: Thu, 12 Oct 2017 14:19:23 +0200 Message-Id: <8f23cb231bc7b01f7bf5211fde154aa9f6af9054.1507809961.git.adrien.mazarguil@6wind.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: References: Subject: [dpdk-dev] [PATCH v2 09/29] net/mlx4: add iovec-like allocation wrappers X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" These wrappers implement the ability to allocate room for several disparate objects as a single contiguous allocation while complying with their respective alignment constraints. This is usually more efficient than allocating and freeing them individually if they are not expected to be reallocated with rte_realloc(). A typical use case is when several objects that cannot be dissociated must be allocated together, as shown in the following example: struct b { ... struct d *d; } struct a { ... struct b *b; struct c *c; } struct mlx4_malloc_vec vec[] = { { .size = sizeof(struct a), .addr = &ptr_a, }, { .size = sizeof(struct b), .addr = &ptr_b, }, { .size = sizeof(struct c), .addr = &ptr_c, }, { .size = sizeof(struct d), .addr = &ptr_d, }, }; if (!mlx4_mallocv(NULL, vec, RTE_DIM(vec))) goto error; struct a *a = ptr_a; a->b = ptr_b; a->c = ptr_c; a->b->d = ptr_d; ... rte_free(a); Signed-off-by: Adrien Mazarguil Acked-by: Nelio Laranjeiro --- drivers/net/mlx4/mlx4_utils.c | 151 +++++++++++++++++++++++++++++++++++++ drivers/net/mlx4/mlx4_utils.h | 15 ++++ 2 files changed, 166 insertions(+) diff --git a/drivers/net/mlx4/mlx4_utils.c b/drivers/net/mlx4/mlx4_utils.c index fcf76c9..f18c714 100644 --- a/drivers/net/mlx4/mlx4_utils.c +++ b/drivers/net/mlx4/mlx4_utils.c @@ -39,8 +39,12 @@ #include #include #include +#include +#include #include +#include +#include #include "mlx4_utils.h" @@ -64,3 +68,150 @@ mlx4_fd_set_non_blocking(int fd) rte_errno = errno; return -rte_errno; } + +/** + * Internal helper to allocate memory once for several disparate objects. + * + * The most restrictive alignment constraint for standard objects is assumed + * to be sizeof(double) and is used as a default value. + * + * C11 code would include stdalign.h and use alignof(max_align_t) however + * we'll stick with C99 for the time being. + */ +static inline size_t +mlx4_mallocv_inline(const char *type, const struct mlx4_malloc_vec *vec, + unsigned int cnt, int zero, int socket) +{ + unsigned int i; + size_t size; + size_t least; + uint8_t *data = NULL; + int fill = !vec[0].addr; + +fill: + size = 0; + least = 0; + for (i = 0; i < cnt; ++i) { + size_t align = (uintptr_t)vec[i].align; + + if (!align) { + align = sizeof(double); + } else if (!rte_is_power_of_2(align)) { + rte_errno = EINVAL; + goto error; + } + if (least < align) + least = align; + align = RTE_ALIGN_CEIL(size, align); + size = align + vec[i].size; + if (fill && vec[i].addr) + *vec[i].addr = data + align; + } + if (fill) + return size; + if (!zero) + data = rte_malloc_socket(type, size, least, socket); + else + data = rte_zmalloc_socket(type, size, least, socket); + if (data) { + fill = 1; + goto fill; + } + rte_errno = ENOMEM; +error: + for (i = 0; i != cnt; ++i) + if (vec[i].addr) + *vec[i].addr = NULL; + return 0; +} + +/** + * Allocate memory once for several disparate objects. + * + * This function adds iovec-like semantics (e.g. readv()) to rte_malloc(). + * Memory is allocated once for several contiguous objects of nonuniform + * sizes and alignment constraints. + * + * Each entry of @p vec describes the size, alignment constraint and + * provides a buffer address where the resulting object pointer must be + * stored. + * + * The buffer of the first entry is guaranteed to point to the beginning of + * the allocated region and is safe to use with rte_free(). + * + * NULL buffers are silently ignored. + * + * Providing a NULL buffer in the first entry prevents this function from + * allocating any memory but has otherwise no effect on its behavior. In + * this case, the contents of remaining non-NULL buffers are updated with + * addresses relative to zero (i.e. offsets that would have been used during + * the allocation). + * + * @param[in] type + * A string identifying the type of allocated objects (useful for debug + * purposes, such as identifying the cause of a memory leak). Can be NULL. + * @param[in, out] vec + * Description of objects to allocate memory for. + * @param cnt + * Number of entries in @p vec. + * + * @return + * Size in bytes of the allocated region including any padding. In case of + * error, rte_errno is set, 0 is returned and NULL is stored in the + * non-NULL buffers pointed by @p vec. + * + * @see struct mlx4_malloc_vec + * @see rte_malloc() + */ +size_t +mlx4_mallocv(const char *type, const struct mlx4_malloc_vec *vec, + unsigned int cnt) +{ + return mlx4_mallocv_inline(type, vec, cnt, 0, SOCKET_ID_ANY); +} + +/** + * Combines the semantics of mlx4_mallocv() with those of rte_zmalloc(). + * + * @see mlx4_mallocv() + * @see rte_zmalloc() + */ +size_t +mlx4_zmallocv(const char *type, const struct mlx4_malloc_vec *vec, + unsigned int cnt) +{ + return mlx4_mallocv_inline(type, vec, cnt, 1, SOCKET_ID_ANY); +} + +/** + * Socket-aware version of mlx4_mallocv(). + * + * This function takes one additional parameter. + * + * @param socket + * NUMA socket to allocate memory on. If SOCKET_ID_ANY is used, this + * function will behave the same as mlx4_mallocv(). + * + * @see mlx4_mallocv() + * @see rte_malloc_socket() + */ +size_t +mlx4_mallocv_socket(const char *type, const struct mlx4_malloc_vec *vec, + unsigned int cnt, int socket) +{ + return mlx4_mallocv_inline(type, vec, cnt, 0, socket); +} + +/** + * Combines the semantics of mlx4_mallocv_socket() with those of + * mlx4_zmalloc_socket(). + * + * @see mlx4_mallocv_socket() + * @see rte_zmalloc_socket() + */ +size_t +mlx4_zmallocv_socket(const char *type, const struct mlx4_malloc_vec *vec, + unsigned int cnt, int socket) +{ + return mlx4_mallocv_inline(type, vec, cnt, 1, socket); +} diff --git a/drivers/net/mlx4/mlx4_utils.h b/drivers/net/mlx4/mlx4_utils.h index 13f731a..bebd4ae 100644 --- a/drivers/net/mlx4/mlx4_utils.h +++ b/drivers/net/mlx4/mlx4_utils.h @@ -110,8 +110,23 @@ pmd_drv_log_basename(const char *s) /** Similar to MLX4_STR() with enclosed macros expanded first. */ #define MLX4_STR_EXPAND(...) MLX4_STR(__VA_ARGS__) +/** Object description used with mlx4_mallocv() and similar functions. */ +struct mlx4_malloc_vec { + size_t align; /**< Alignment constraint (power of 2), 0 if unknown. */ + size_t size; /**< Object size. */ + void **addr; /**< Storage for allocation address. */ +}; + /* mlx4_utils.c */ int mlx4_fd_set_non_blocking(int fd); +size_t mlx4_mallocv(const char *type, const struct mlx4_malloc_vec *vec, + unsigned int cnt); +size_t mlx4_zmallocv(const char *type, const struct mlx4_malloc_vec *vec, + unsigned int cnt); +size_t mlx4_mallocv_socket(const char *type, const struct mlx4_malloc_vec *vec, + unsigned int cnt, int socket); +size_t mlx4_zmallocv_socket(const char *type, const struct mlx4_malloc_vec *vec, + unsigned int cnt, int socket); #endif /* MLX4_UTILS_H_ */