Skip to content

[DRAFT] IPC: open shm from ipc_data #775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/proxy_lib.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:

- name: Run "ctest --output-on-failure" with proxy library
working-directory: ${{env.BUILD_DIR}}
run: LD_PRELOAD=./lib/libumf_proxy.so ctest --output-on-failure
run: LD_PRELOAD=./lib/libumf_proxy.so ctest --output-on-failure -E "proxy_lib_memoryPool"

- name: Run "./test/umf_test-memoryPool" with proxy library
working-directory: ${{env.BUILD_DIR}}
Expand Down
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,16 @@ if(WINDOWS)
)
endif()
endif()

# set UMF_PROXY_LIB_ENABLED
if(UMF_PROXY_LIB_BASED_ON_POOL STREQUAL SCALABLE)
if(UMF_LINK_HWLOC_STATICALLY)
message(
STATUS
"Disabling the proxy library, because HWLOC is set to link statically which is not supported"
)
elseif(UMF_DISABLE_HWLOC)
message(STATUS "Disabling the proxy library, because HWLOC is disabled")
elseif(UMF_PROXY_LIB_BASED_ON_POOL STREQUAL SCALABLE)
if(UMF_POOL_SCALABLE_ENABLED)
set(UMF_PROXY_LIB_ENABLED ON)
set(PROXY_LIB_USES_SCALABLE_POOL ON)
Expand Down
4 changes: 1 addition & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,6 @@ install(TARGETS umf EXPORT ${PROJECT_NAME}-targets)

add_subdirectory(pool)

if(UMF_PROXY_LIB_ENABLED
AND NOT UMF_LINK_HWLOC_STATICALLY
AND NOT UMF_DISABLE_HWLOC)
if(UMF_PROXY_LIB_ENABLED)
add_subdirectory(proxy_lib)
endif()
8 changes: 4 additions & 4 deletions src/provider/provider_os_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -1251,14 +1251,14 @@ static umf_result_t os_open_ipc_handle(void *provider, void *providerIpcData,
umf_result_t ret = UMF_RESULT_SUCCESS;
int fd;

if (os_provider->shm_name[0]) {
fd = utils_shm_open(os_provider->shm_name);
if (os_ipc_data->shm_name[0]) {
fd = utils_shm_open(os_ipc_data->shm_name);
if (fd <= 0) {
LOG_PERR("opening a shared memory file (%s) failed",
os_provider->shm_name);
os_ipc_data->shm_name);
return UMF_RESULT_ERROR_UNKNOWN;
}
(void)utils_shm_unlink(os_provider->shm_name);
(void)utils_shm_unlink(os_ipc_data->shm_name);
} else {
umf_result_t umf_result =
utils_duplicate_fd(os_ipc_data->pid, os_ipc_data->fd, &fd);
Expand Down
7 changes: 2 additions & 5 deletions src/proxy_lib/proxy_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ void proxy_lib_create_common(void) {

} else if (utils_env_var_has_str("UMF_PROXY",
"page.disposition=shared-shm")) {
LOG_DEBUG("proxy_lib: using the MAP_SHARED visibility mode with the "
"named shared memory");
os_params.visibility = UMF_MEM_MAP_SHARED;

memset(shm_name, 0, NAME_MAX);
Expand All @@ -145,9 +143,8 @@ void proxy_lib_create_common(void) {
exit(-1);
}

umf_result =
umfPoolCreate(umfPoolManagerOps(), OS_memory_provider, NULL,
UMF_POOL_CREATE_FLAG_DISABLE_TRACKING, &Proxy_pool);
umf_result = umfPoolCreate(umfPoolManagerOps(), OS_memory_provider, NULL, 0,
&Proxy_pool);
if (umf_result != UMF_RESULT_SUCCESS) {
LOG_ERR("creating UMF pool manager failed");
exit(-1);
Expand Down
16 changes: 12 additions & 4 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,7 @@ add_umf_test(
LIBS ${UMF_UTILS_FOR_TEST})

# tests for the proxy library
if(UMF_PROXY_LIB_ENABLED
AND UMF_BUILD_SHARED_LIBRARY
AND NOT UMF_DISABLE_HWLOC
AND NOT UMF_LINK_HWLOC_STATICALLY)
if(UMF_PROXY_LIB_ENABLED AND UMF_BUILD_SHARED_LIBRARY)
add_umf_test(
NAME proxy_lib_basic
SRCS ${BA_SOURCES_FOR_TEST} test_proxy_lib.cpp
Expand Down Expand Up @@ -402,6 +399,17 @@ if(LINUX)
add_umf_ipc_test(TEST ipc_os_prov_anon_fd)
add_umf_ipc_test(TEST ipc_os_prov_shm)

if(UMF_PROXY_LIB_ENABLED AND UMF_BUILD_SHARED_LIBRARY)
build_umf_test(
NAME
ipc_os_prov_proxy
SRCS
ipc_os_prov_proxy.c
LIBS
${UMF_UTILS_FOR_TEST})
add_umf_ipc_test(TEST ipc_os_prov_proxy)
endif()

build_umf_test(
NAME
ipc_devdax_prov_consumer
Expand Down
113 changes: 113 additions & 0 deletions test/ipc_os_prov_proxy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (C) 2024 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/

#include <dlfcn.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <umf/ipc.h>

#include "utils_load_library.h"

umf_result_t (*pfnGetIPCHandle)(const void *ptr, umf_ipc_handle_t *umfIPCHandle,
size_t *size);

// This is a test for a scenario where a user process is started using the
// LD_PRELOAD with the UMF Proxy Lib and this process uses UMF by loading
// libumf.so at runtime.
// In this test, we expect that all allocations made by the process will be
// handled by UMF in the Proxy Lib and added to the UMF tracker so that they
// can be used later in the UMF IPC API.
int main(void) {
int ret = 0;

int fd = open("/proc/self/maps", O_RDONLY);
if (fd == -1) {
return -1;
}

// read the "/proc/self/maps" file until the "libumf_proxy.so" of the maps
// is found or EOF is reached.
const size_t size_buf = 8192;
char buf[size_buf];
size_t nbytes = 1;
char *found = NULL;
while (nbytes > 0 && found == NULL) {
memset(buf, 0, nbytes); // erase previous data
nbytes = read(fd, buf, size_buf);
found = strstr(buf, "libumf_proxy.so");
}
(void)close(fd);

if (found == NULL) {
fprintf(
stderr,
"test binary not run under LD_PRELOAD with \"libumf_proxy.so\"\n");
return -1;
}

// open the UMF library and get umfGetIPCHandle() function
const char *umf_lib_name = "libumf.so";
void *umf_lib_handle = utils_open_library(umf_lib_name, 0);
if (umf_lib_handle == NULL) {
fprintf(stderr, "utils_open_library: UMF library not found (%s)\n",
umf_lib_name);
return -1;
}

*(void **)&pfnGetIPCHandle =
utils_get_symbol_addr(umf_lib_handle, "umfGetIPCHandle", umf_lib_name);
if (pfnGetIPCHandle == NULL) {
ret = -1;
goto err_close_lib;
}

// create simple allocation - it should be added to the UMF tracker if the
// process was launched under UMF Proxy Lib
size_t size = 2137;
void *ptr = malloc(size);
if (ptr == NULL) {
ret = -1;
goto err_close_lib;
}

fprintf(stderr, "Allocated memory - %zu\n", size);
*(int *)ptr = 0x23;

// get IPC handle of the allocation
umf_ipc_handle_t ipc_handle = NULL;
size_t ipc_handle_size = 0;
umf_result_t res = pfnGetIPCHandle(ptr, &ipc_handle, &ipc_handle_size);
if (res != UMF_RESULT_SUCCESS) {
fprintf(stderr, "pfnGetIPCHandle() failed!\n");
ret = -1;
goto err_free_mem;
}

// check if we got valid data
if (ipc_handle == NULL || ipc_handle_size == 0) {
fprintf(stderr, "pfnGetIPCHandle() couldn't find the handle data!\n");
ret = -1;
goto err_free_mem;
}

fprintf(stderr, "Got IPCHandle for memory - %p | size - %zu\n",
(void *)ipc_handle, ipc_handle_size);

err_free_mem:
free(ptr);

err_close_lib:
utils_close_library(umf_lib_handle);

return ret;
}
16 changes: 16 additions & 0 deletions test/ipc_os_prov_proxy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Copyright (C) 2024 Intel Corporation
#
# Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#

#!/bin/bash

set -e

UMF_LOG_VAL="level:debug;flush:debug;output:stderr;pid:yes"
UMF_PROXY_VAL="page.disposition=shared-shm"
LD_PRELOAD_VAL="../lib/libumf_proxy.so"

LD_PRELOAD=$LD_PRELOAD_VAL UMF_LOG=$UMF_LOG_VAL UMF_PROXY=$UMF_PROXY_VAL ./umf_test-ipc_os_prov_proxy
Loading