From b9fbdd189a30d2e265b427f8b5945fbd604a79ed Mon Sep 17 00:00:00 2001 From: Ben Walker Date: Wed, 10 Aug 2016 10:37:12 -0700 Subject: [PATCH] env: Move malloc/free wrappers into env Change-Id: Ief591f5e23c4ae06cb77fab647a7afd082450a73 Signed-off-by: Ben Walker --- include/spdk/env.h | 15 +++++++++ lib/env/Makefile | 2 +- lib/env/env.c | 56 ++++++++++++++++++++++++++++++++++ lib/ioat/ioat.c | 4 +-- lib/ioat/ioat_impl.h | 14 ++------- lib/nvme/nvme.c | 4 +-- lib/nvme/nvme_ctrlr.c | 9 ++---- lib/nvme/nvme_impl.h | 13 ++------ lib/nvme/nvme_qpair.c | 8 ++--- test/lib/ioat/unit/ioat_impl.h | 2 +- test/lib/nvme/unit/nvme_impl.h | 2 +- 11 files changed, 88 insertions(+), 41 deletions(-) create mode 100644 lib/env/env.c diff --git a/include/spdk/env.h b/include/spdk/env.h index 3bc6723de..aedad786a 100644 --- a/include/spdk/env.h +++ b/include/spdk/env.h @@ -42,8 +42,23 @@ extern "C" { #endif +#include #include +/** + * Allocate a pinned, physically contiguous memory buffer with the + * given size and alignment. The buffer will be zeroed. + */ +void * +spdk_zmalloc(size_t size, size_t align, uint64_t *phys_addr); + +/** + * Free a memory buffer previously allocated with spdk_malloc. + * This call is never made from the performance path. + */ +void +spdk_free(void *buf); + #define SPDK_VTOPHYS_ERROR (0xFFFFFFFFFFFFFFFFULL) uint64_t spdk_vtophys(void *buf); diff --git a/lib/env/Makefile b/lib/env/Makefile index 146f02b87..ee00c75ff 100644 --- a/lib/env/Makefile +++ b/lib/env/Makefile @@ -35,7 +35,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..) include $(SPDK_ROOT_DIR)/mk/spdk.common.mk CFLAGS += $(DPDK_INC) -C_SRCS = vtophys.c +C_SRCS = env.c vtophys.c LIBNAME = env include $(SPDK_ROOT_DIR)/mk/spdk.lib.mk diff --git a/lib/env/env.c b/lib/env/env.c new file mode 100644 index 000000000..834f7da69 --- /dev/null +++ b/lib/env/env.c @@ -0,0 +1,56 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk/env.h" + +#include + +#include +#include + +void * +spdk_zmalloc(size_t size, size_t align, uint64_t *phys_addr) +{ + void *buf = rte_malloc(NULL, size, align); + if (buf) { + memset(buf, 0, size); + *phys_addr = rte_malloc_virt2phy(buf); + } + return buf; +} + +void +spdk_free(void *buf) +{ + return rte_free(buf); +} diff --git a/lib/ioat/ioat.c b/lib/ioat/ioat.c index c79eae7eb..5ec5d0731 100644 --- a/lib/ioat/ioat.c +++ b/lib/ioat/ioat.c @@ -402,7 +402,7 @@ ioat_channel_start(struct spdk_ioat_chan *ioat) ioat->max_xfer_size = 1U << xfercap; } - ioat->comp_update = ioat_zmalloc(NULL, sizeof(*ioat->comp_update), SPDK_IOAT_CHANCMP_ALIGN, + ioat->comp_update = ioat_zmalloc(sizeof(*ioat->comp_update), SPDK_IOAT_CHANCMP_ALIGN, &comp_update_bus_addr); if (ioat->comp_update == NULL) { return -1; @@ -417,7 +417,7 @@ ioat_channel_start(struct spdk_ioat_chan *ioat) return -1; } - ioat->hw_ring = ioat_zmalloc(NULL, num_descriptors * sizeof(union spdk_ioat_hw_desc), 64, + ioat->hw_ring = ioat_zmalloc(num_descriptors * sizeof(union spdk_ioat_hw_desc), 64, &ioat->hw_ring_phys_addr); if (!ioat->hw_ring) { return -1; diff --git a/lib/ioat/ioat_impl.h b/lib/ioat/ioat_impl.h index efb822810..30f659c9e 100644 --- a/lib/ioat/ioat_impl.h +++ b/lib/ioat/ioat_impl.h @@ -30,22 +30,12 @@ * Allocate a pinned, physically contiguous memory buffer with the * given size and alignment. */ -static inline void * -ioat_zmalloc(const char *tag, size_t size, unsigned align, uint64_t *phys_addr) -{ - void *buf = rte_malloc(tag, size, align); - - if (buf) { - memset(buf, 0, size); - *phys_addr = rte_malloc_virt2phy(buf); - } - return buf; -} +#define ioat_zmalloc spdk_zmalloc /** * Free a memory buffer previously allocated with ioat_zmalloc. */ -#define ioat_free(buf) rte_free(buf) +#define ioat_free spdk_free /** * Return the physical address for the specified virtual address. diff --git a/lib/nvme/nvme.c b/lib/nvme/nvme.c index d25ccb46b..900bcf92d 100644 --- a/lib/nvme/nvme.c +++ b/lib/nvme/nvme.c @@ -51,7 +51,7 @@ nvme_attach(void *devhandle) int status; uint64_t phys_addr = 0; - ctrlr = nvme_malloc("nvme_ctrlr", sizeof(struct spdk_nvme_ctrlr), + ctrlr = nvme_malloc(sizeof(struct spdk_nvme_ctrlr), 64, &phys_addr); if (ctrlr == NULL) { SPDK_ERRLOG("could not allocate ctrlr\n"); @@ -178,7 +178,7 @@ nvme_allocate_request_user_copy(void *buffer, uint32_t payload_size, spdk_nvme_c uint64_t phys_addr; if (buffer && payload_size) { - contig_buffer = nvme_malloc("nvme_user_copy", payload_size, 4096, &phys_addr); + contig_buffer = nvme_malloc(payload_size, 4096, &phys_addr); if (!contig_buffer) { return NULL; } diff --git a/lib/nvme/nvme_ctrlr.c b/lib/nvme/nvme_ctrlr.c index 8422ef994..2f6645b22 100644 --- a/lib/nvme/nvme_ctrlr.c +++ b/lib/nvme/nvme_ctrlr.c @@ -247,8 +247,7 @@ static int nvme_ctrlr_set_intel_support_log_pages(struct spdk_nvme_ctrlr *ctrlr) struct nvme_completion_poll_status status; struct spdk_nvme_intel_log_page_directory *log_page_directory; - log_page_directory = nvme_malloc("nvme_log_page_directory", - sizeof(struct spdk_nvme_intel_log_page_directory), + log_page_directory = nvme_malloc(sizeof(struct spdk_nvme_intel_log_page_directory), 64, &phys_addr); if (log_page_directory == NULL) { SPDK_ERRLOG("could not allocate log_page_directory\n"); @@ -375,8 +374,7 @@ nvme_ctrlr_construct_io_qpairs(struct spdk_nvme_ctrlr *ctrlr) */ num_trackers = nvme_min(NVME_IO_TRACKERS, (num_entries - 1)); - ctrlr->ioq = nvme_malloc("nvme_ioq", - ctrlr->opts.num_io_queues * sizeof(struct spdk_nvme_qpair), + ctrlr->ioq = nvme_malloc(ctrlr->opts.num_io_queues * sizeof(struct spdk_nvme_qpair), 64, &phys_addr); if (ctrlr->ioq == NULL) @@ -695,8 +693,7 @@ nvme_ctrlr_construct_namespaces(struct spdk_nvme_ctrlr *ctrlr) goto fail; } - ctrlr->nsdata = nvme_malloc("nvme_namespaces", - nn * sizeof(struct spdk_nvme_ns_data), 64, + ctrlr->nsdata = nvme_malloc(nn * sizeof(struct spdk_nvme_ns_data), 64, &phys_addr); if (ctrlr->nsdata == NULL) { goto fail; diff --git a/lib/nvme/nvme_impl.h b/lib/nvme/nvme_impl.h index ccb839443..b5b261b14 100644 --- a/lib/nvme/nvme_impl.h +++ b/lib/nvme/nvme_impl.h @@ -81,21 +81,12 @@ * given size and alignment. * Note: these calls are only made during driver initialization. */ -static inline void * -nvme_malloc(const char *tag, size_t size, unsigned align, uint64_t *phys_addr) -{ - void *buf = rte_malloc(tag, size, align); - if (buf) { - memset(buf, 0, size); - *phys_addr = rte_malloc_virt2phy(buf); - } - return buf; -} +#define nvme_malloc spdk_zmalloc /** * Free a memory buffer previously allocated with nvme_malloc. */ -#define nvme_free(buf) rte_free(buf) +#define nvme_free spdk_free /** * Reserve a named, process shared memory zone with the given size, diff --git a/lib/nvme/nvme_qpair.c b/lib/nvme/nvme_qpair.c index 96a2e4668..01b40024f 100644 --- a/lib/nvme/nvme_qpair.c +++ b/lib/nvme/nvme_qpair.c @@ -556,8 +556,7 @@ nvme_qpair_construct(struct spdk_nvme_qpair *qpair, uint16_t id, } } if (qpair->sq_in_cmb == false) { - qpair->cmd = nvme_malloc("qpair_cmd", - qpair->num_entries * sizeof(struct spdk_nvme_cmd), + qpair->cmd = nvme_malloc(qpair->num_entries * sizeof(struct spdk_nvme_cmd), 0x1000, &qpair->cmd_bus_addr); if (qpair->cmd == NULL) { @@ -566,8 +565,7 @@ nvme_qpair_construct(struct spdk_nvme_qpair *qpair, uint16_t id, } } - qpair->cpl = nvme_malloc("qpair_cpl", - qpair->num_entries * sizeof(struct spdk_nvme_cpl), + qpair->cpl = nvme_malloc(qpair->num_entries * sizeof(struct spdk_nvme_cpl), 0x1000, &qpair->cpl_bus_addr); if (qpair->cpl == NULL) { @@ -589,7 +587,7 @@ nvme_qpair_construct(struct spdk_nvme_qpair *qpair, uint16_t id, * This ensures the PRP list embedded in the nvme_tracker object will not span a * 4KB boundary, while allowing access to trackers in tr[] via normal array indexing. */ - qpair->tr = nvme_malloc("nvme_tr", num_trackers * sizeof(*tr), sizeof(*tr), &phys_addr); + qpair->tr = nvme_malloc(num_trackers * sizeof(*tr), sizeof(*tr), &phys_addr); if (qpair->tr == NULL) { SPDK_ERRLOG("nvme_tr failed\n"); goto fail; diff --git a/test/lib/ioat/unit/ioat_impl.h b/test/lib/ioat/unit/ioat_impl.h index ddfa43c13..f5e777b4f 100644 --- a/test/lib/ioat/unit/ioat_impl.h +++ b/test/lib/ioat/unit/ioat_impl.h @@ -12,7 +12,7 @@ struct spdk_pci_device; static inline void * -ioat_zmalloc(const char *tag, size_t size, unsigned align, uint64_t *phys_addr) +ioat_zmalloc(size_t size, unsigned align, uint64_t *phys_addr) { return calloc(1, size); } diff --git a/test/lib/nvme/unit/nvme_impl.h b/test/lib/nvme/unit/nvme_impl.h index 0bf6e9698..3a3d0ae56 100644 --- a/test/lib/nvme/unit/nvme_impl.h +++ b/test/lib/nvme/unit/nvme_impl.h @@ -46,7 +46,7 @@ struct spdk_pci_device; static inline void * -nvme_malloc(const char *tag, size_t size, unsigned align, uint64_t *phys_addr) +nvme_malloc(size_t size, unsigned align, uint64_t *phys_addr) { void *buf = NULL; if (posix_memalign(&buf, align, size)) {