Skip to content

Commit

Permalink
Add wrapper function to sys calls ioctl, mmap, mremap and munmap
Browse files Browse the repository at this point in the history
Change-Id: I0a77042eb9a000b5b9ce0162c4fdb050cbfbc1b5
Signed-off-by: Steve Linsell <[email protected]>
  • Loading branch information
Yogaraj-Alamenda authored and stevelinsell committed Jun 11, 2019
1 parent 19f6c1a commit a38ab5f
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 43 deletions.
6 changes: 4 additions & 2 deletions .tools/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ ACLOCAL_AMFLAGS = -I m4
lib_LTLIBRARIES = libqat.la

if QAT_CONTIG_MEM
MEM_LIB_SRC = qae_mem_utils.c
MEM_LIB_HEADER = qae_mem_utils.h
MEM_LIB_SRC = qae_mem_utils.c \
qat_sys_call.c
MEM_LIB_HEADER = qae_mem_utils.h \
qat_sys_call.h
endif

if QAT_MULTI_THREAD
Expand Down
41 changes: 20 additions & 21 deletions multi_thread_qaememutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* BSD LICENSE
*
* Copyright(c) 2016-2018 Intel Corporation.
* Copyright(c) 2016-2019 Intel Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -45,16 +45,15 @@
*****************************************************************************/
#define _GNU_SOURCE

#include "qat_sys_call.h"
#include "qae_mem_utils.h"
#ifdef USE_QAT_CONTIG_MEM
#include "qat_contig_mem.h"
#endif
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <assert.h>
#include <string.h>
#include <limits.h>
#include <pthread.h>
Expand Down Expand Up @@ -359,7 +358,7 @@ static qae_slab *crypto_create_slab(int size, int pool_index, int memfd)

qmcfg.length = SLAB_SIZE;
#ifdef USE_QAT_CONTIG_MEM
if (ioctl(memfd, QAT_CONTIG_MEM_MALLOC, &qmcfg) == -1) {
if (qat_ioctl(memfd, QAT_CONTIG_MEM_MALLOC, &qmcfg) == -1) {
static char errmsg[LINE_MAX];

snprintf(errmsg, LINE_MAX, "ioctl QAT_CONTIG_MEM_MALLOC(%d)",
Expand All @@ -368,10 +367,10 @@ static qae_slab *crypto_create_slab(int size, int pool_index, int memfd)
goto exit;
}
if ((slb =
mmap(NULL, qmcfg.length*QAT_CONTIG_MEM_MMAP_ADJUSTMENT,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_LOCKED, memfd,
qmcfg.virtualAddress)) == MAP_FAILED) {
qat_mmap(NULL, qmcfg.length*QAT_CONTIG_MEM_MMAP_ADJUSTMENT,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_LOCKED, memfd,
qmcfg.virtualAddress)) == MAP_FAILED) {
static char errmsg[LINE_MAX];
snprintf(errmsg, LINE_MAX, "mmap: %d %s", errno, strerror(errno));
perror(errmsg);
Expand Down Expand Up @@ -559,12 +558,12 @@ static void crypto_free_slab(qae_slab *slb, void *thread_key)
MEM_DEBUG("do munmap of %p\n", slb);
qmcfg = *((qat_contig_mem_config *) slb);

if (munmap(slb, SLAB_SIZE) == -1) {
if (qat_munmap(slb, SLAB_SIZE) == -1) {
perror("munmap");
exit(EXIT_FAILURE);
}
MEM_DEBUG("ioctl free of %p\n", slb);
if (ioctl(tls_ptr->crypto_qat_contig_memfd, QAT_CONTIG_MEM_FREE, &qmcfg)
if (qat_ioctl(tls_ptr->crypto_qat_contig_memfd, QAT_CONTIG_MEM_FREE, &qmcfg)
== -1) {
perror("ioctl QAT_CONTIG_MEM_FREE");
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -699,7 +698,7 @@ void fork_slab_list(qae_slab_pool * list, int memfd)

while (count < list->slot_size) {
#ifdef USE_QAT_CONTIG_MEM
if (ioctl(memfd, QAT_CONTIG_MEM_MALLOC, &qmcfg) == -1) {
if (qat_ioctl(memfd, QAT_CONTIG_MEM_MALLOC, &qmcfg) == -1) {
static char errmsg[LINE_MAX];

snprintf(errmsg, LINE_MAX, "ioctl QAT_CONTIG_MEM_MALLOC(%d)",
Expand All @@ -709,10 +708,10 @@ void fork_slab_list(qae_slab_pool * list, int memfd)
}

if ((new_slb =
mmap(NULL, qmcfg.length*QAT_CONTIG_MEM_MMAP_ADJUSTMENT,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_LOCKED, memfd,
qmcfg.virtualAddress)) == MAP_FAILED) {
qat_mmap(NULL, qmcfg.length*QAT_CONTIG_MEM_MMAP_ADJUSTMENT,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_LOCKED, memfd,
qmcfg.virtualAddress)) == MAP_FAILED) {
static char errmsg[LINE_MAX];
snprintf(errmsg, LINE_MAX, "mmap: %d %s", errno, strerror(errno));
perror(errmsg);
Expand All @@ -725,12 +724,12 @@ void fork_slab_list(qae_slab_pool * list, int memfd)
#endif
qae_slab *to_unmap = old_slb;
old_slb = old_slb->next;
if (munmap(to_unmap, SLAB_SIZE) == -1) {
if (qat_munmap(to_unmap, SLAB_SIZE) == -1) {
perror("munmap");
exit(EXIT_FAILURE);
}
qae_slab *remap = mremap(new_slb, SLAB_SIZE, SLAB_SIZE,
MREMAP_FIXED | MREMAP_MAYMOVE, to_unmap);
qae_slab *remap = qat_mremap(new_slb, SLAB_SIZE, SLAB_SIZE,
MREMAP_FIXED | MREMAP_MAYMOVE, to_unmap);
if ((remap == MAP_FAILED) || (remap != to_unmap)) {
perror("mremap");
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -767,12 +766,12 @@ static void crypto_free_slab_list(qae_slab_pool *list, int memfd)
MEM_DEBUG("do munmap of %p\n", slb);
qmcfg = *((qat_contig_mem_config *) slb);

if (munmap(slb, SLAB_SIZE) == -1) {
if (qat_munmap(slb, SLAB_SIZE) == -1) {
perror("munmap");
exit(EXIT_FAILURE);
}
MEM_DEBUG("ioctl free of %p\n", slb);
if (ioctl(memfd, QAT_CONTIG_MEM_FREE, &qmcfg) == -1) {
if (qat_ioctl(memfd, QAT_CONTIG_MEM_FREE, &qmcfg) == -1) {
perror("ioctl QAT_CONTIG_MEM_FREE");
exit(EXIT_FAILURE);
}
Expand Down Expand Up @@ -846,7 +845,7 @@ void slab_list_stat(qae_slab_pool * list)
void crypto_cleanup_slabs(void *thread_key)
{
qae_slab_pools_local *tls_ptr = (qae_slab_pools_local *)thread_key;
crypto_free_empty_slab_list(thread_key);
crypto_free_empty_slab_list(tls_ptr);
#ifdef QAT_MEM_DEBUG
int i;
/* statistics of available slab lists */
Expand Down
40 changes: 20 additions & 20 deletions qae_mem_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* BSD LICENSE
*
* Copyright(c) 2016-2018 Intel Corporation.
* Copyright(c) 2016-2019 Intel Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -47,6 +47,7 @@
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include "qat_sys_call.h"
#include "qae_mem_utils.h"
#ifdef USE_QAT_CONTIG_MEM
# include "qat_contig_mem.h"
Expand All @@ -56,7 +57,6 @@
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <assert.h>
#include <string.h>
#include <limits.h>
#include <pthread.h>
Expand Down Expand Up @@ -371,7 +371,7 @@ static qae_slab *crypto_create_slab(int size, int pool_index)

qmcfg.length = SLAB_SIZE;
#ifdef USE_QAT_CONTIG_MEM
if (ioctl(crypto_qat_contig_memfd, QAT_CONTIG_MEM_MALLOC, &qmcfg) == -1) {
if (qat_ioctl(crypto_qat_contig_memfd, QAT_CONTIG_MEM_MALLOC, &qmcfg) == -1) {
static char errmsg[LINE_MAX];

snprintf(errmsg, LINE_MAX, "ioctl QAT_CONTIG_MEM_MALLOC(%d)",
Expand All @@ -380,10 +380,10 @@ static qae_slab *crypto_create_slab(int size, int pool_index)
goto exit;
}
if ((slb =
mmap(NULL, qmcfg.length*QAT_CONTIG_MEM_MMAP_ADJUSTMENT,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_LOCKED, crypto_qat_contig_memfd,
qmcfg.virtualAddress)) == MAP_FAILED) {
qat_mmap(NULL, qmcfg.length*QAT_CONTIG_MEM_MMAP_ADJUSTMENT,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_LOCKED, crypto_qat_contig_memfd,
qmcfg.virtualAddress)) == MAP_FAILED) {
static char errmsg[LINE_MAX];
snprintf(errmsg, LINE_MAX, "mmap: %d %s", errno, strerror(errno));
perror(errmsg);
Expand Down Expand Up @@ -575,12 +575,12 @@ static void crypto_free_slab(qae_slab *slb)
MEM_DEBUG("do munmap of %p\n", slb);
qmcfg = *((qat_contig_mem_config *) slb);

if (munmap(slb, SLAB_SIZE) == -1) {
if (qat_munmap(slb, SLAB_SIZE) == -1) {
perror("munmap");
exit(EXIT_FAILURE);
}
MEM_DEBUG("ioctl free of %p\n", slb);
if (ioctl(crypto_qat_contig_memfd, QAT_CONTIG_MEM_FREE, &qmcfg) == -1) {
if (qat_ioctl(crypto_qat_contig_memfd, QAT_CONTIG_MEM_FREE, &qmcfg) == -1) {
perror("ioctl QAT_CONTIG_MEM_FREE");
exit(EXIT_FAILURE);
}
Expand Down Expand Up @@ -729,7 +729,7 @@ void fork_slab_list(qae_slab_pool * list)

while (count < list->slot_size) {
#ifdef USE_QAT_CONTIG_MEM
if (ioctl(crypto_qat_contig_memfd, QAT_CONTIG_MEM_MALLOC, &qmcfg)
if (qat_ioctl(crypto_qat_contig_memfd, QAT_CONTIG_MEM_MALLOC, &qmcfg)
== -1) {
static char errmsg[LINE_MAX];

Expand All @@ -740,10 +740,10 @@ void fork_slab_list(qae_slab_pool * list)
}

if ((new_slb =
mmap(NULL, qmcfg.length*QAT_CONTIG_MEM_MMAP_ADJUSTMENT,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_LOCKED, crypto_qat_contig_memfd,
qmcfg.virtualAddress)) == MAP_FAILED) {
qat_mmap(NULL, qmcfg.length*QAT_CONTIG_MEM_MMAP_ADJUSTMENT,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_LOCKED, crypto_qat_contig_memfd,
qmcfg.virtualAddress)) == MAP_FAILED) {
static char errmsg[LINE_MAX];
snprintf(errmsg, LINE_MAX, "mmap: %d %s", errno, strerror(errno));
perror(errmsg);
Expand All @@ -756,12 +756,12 @@ void fork_slab_list(qae_slab_pool * list)
#endif
qae_slab *to_unmap = old_slb;
old_slb = old_slb->next;
if (munmap(to_unmap, SLAB_SIZE) == -1) {
if (qat_munmap(to_unmap, SLAB_SIZE) == -1) {
perror("munmap");
exit(EXIT_FAILURE);
}
qae_slab *remap = mremap(new_slb, SLAB_SIZE, SLAB_SIZE,
MREMAP_FIXED | MREMAP_MAYMOVE, to_unmap);
qae_slab *remap = qat_mremap(new_slb, SLAB_SIZE, SLAB_SIZE,
MREMAP_FIXED | MREMAP_MAYMOVE, to_unmap);
if ((remap == MAP_FAILED) || (remap != to_unmap)) {
perror("mremap");
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -807,12 +807,12 @@ static void crypto_free_slab_list(qae_slab_pool *list)
MEM_DEBUG("do munmap of %p\n", slb);
qmcfg = *((qat_contig_mem_config *) slb);

if (munmap(slb, SLAB_SIZE) == -1) {
if (qat_munmap(slb, SLAB_SIZE) == -1) {
perror("munmap");
exit(EXIT_FAILURE);
}
MEM_DEBUG("ioctl free of %p\n", slb);
if (ioctl(crypto_qat_contig_memfd, QAT_CONTIG_MEM_FREE, &qmcfg)
if (qat_ioctl(crypto_qat_contig_memfd, QAT_CONTIG_MEM_FREE, &qmcfg)
== -1) {
perror("ioctl QAT_CONTIG_MEM_FREE");
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -922,7 +922,7 @@ static void crypto_init(void)
}
init_pool(&full_slab_list);
#ifdef USE_QAT_CONTIG_MEM
if ((crypto_qat_contig_memfd = open("/dev/qat_contig_mem", O_RDWR)) == FD_ERROR) {
if ((crypto_qat_contig_memfd = qat_open("/dev/qat_contig_mem", O_RDWR)) == FD_ERROR) {
perror("open qat_contig_mem");
exit(EXIT_FAILURE);
}
Expand Down
81 changes: 81 additions & 0 deletions qat_sys_call.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* ====================================================================
*
*
* BSD LICENSE
*
* Copyright(c) 2016-2019 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.
*
*
* ====================================================================
*/

/*****************************************************************************
* * @file qat_sys_call.c
*
* This file provides additional layer to system calls
*
*****************************************************************************/


#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include "qat_sys_call.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

int qat_open(const char *pathname, int flags)
{
return open(pathname, flags);
}

int qat_ioctl(int fd, unsigned long request, qat_contig_mem_config *qat_mem_cfg)
{
return ioctl(fd, request, qat_mem_cfg);
}

void *qat_mmap(void *addr, size_t length, int prot, int flags,
int fd, off_t offset)
{
return mmap(addr, length, prot, flags, fd, offset);
}

void *qat_mremap(void *old_address, size_t old_size,
size_t new_size, int flags, void *new_address)
{
return mremap(old_address, old_size, new_size, flags, new_address);
}

int qat_munmap(void *addr, size_t length)
{
return munmap(addr, length);
}
Loading

0 comments on commit a38ab5f

Please sign in to comment.