Skip to content

Commit e568ea3

Browse files
committed
Add support for the named shared memory to the proxy library
Add support for the named shared memory to the proxy library. It adds the UMF_PROXY="page.disposition=shared-shm" environment variable for the MAP_SHARED visibility mode with the named shared memory using a generated SHM name. Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 2d74808 commit e568ea3

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,9 @@ $ LD_PRELOAD=/usr/lib/libumf_proxy.so myprogram
256256

257257
The memory used by the proxy memory allocator is mmap'ed:
258258
1) with the `MAP_PRIVATE` flag by default or
259-
2) with the `MAP_SHARED` flag only if the `UMF_PROXY` environment variable contains the `page.disposition=shared` string.
259+
2) with the `MAP_SHARED` flag if the `UMF_PROXY` environment variable contains one of two following strings: `page.disposition=shared-shm` or `page.disposition=shared-fd`. These two options differ in a mechanism used during IPC:
260+
- `page.disposition=shared-shm` - IPC uses the named shared memory. An SHM name is generated using the `umf_proxy_lib_shm_pid_$PID` pattern, where `$PID` is the PID of the process. It creates the `/dev/shm/umf_proxy_lib_shm_pid_$PID` file.
261+
- `page.disposition=shared-fd` - IPC uses the file descriptor duplication. It requires using `pidfd_getfd(2)` to obtain a duplicate of another process's file descriptor. Permission to duplicate another process's file descriptor is governed by a ptrace access mode `PTRACE_MODE_ATTACH_REALCREDS` check (see `ptrace(2)`) that can be changed using the `/proc/sys/kernel/yama/ptrace_scope` interface. `pidfd_getfd(2)` is supported since Linux 5.6.
260262

261263
#### Windows
262264

src/proxy_lib/proxy_lib.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,30 @@ void proxy_lib_create_common(void) {
112112
umf_result_t umf_result;
113113

114114
#ifndef _WIN32
115-
if (util_env_var_has_str("UMF_PROXY", "page.disposition=shared")) {
116-
LOG_DEBUG("proxy_lib: using the MAP_SHARED visibility mode");
115+
#define NAME_MAX 255
116+
char shm_name[NAME_MAX];
117+
118+
if (util_env_var_has_str("UMF_PROXY", "page.disposition=shared-fd")) {
119+
LOG_DEBUG("proxy_lib: using the MAP_SHARED visibility mode with the "
120+
"file descriptor duplication");
121+
os_params.visibility = UMF_MEM_MAP_SHARED;
122+
os_params.shm_name = NULL;
123+
124+
} else if (util_env_var_has_str("UMF_PROXY",
125+
"page.disposition=shared-shm")) {
126+
LOG_DEBUG("proxy_lib: using the MAP_SHARED visibility mode with the "
127+
"named shared memory");
117128
os_params.visibility = UMF_MEM_MAP_SHARED;
129+
130+
memset(shm_name, 0, NAME_MAX);
131+
sprintf(shm_name, "umf_proxy_lib_shm_pid_%i", utils_getpid());
132+
os_params.shm_name = shm_name;
133+
134+
LOG_DEBUG("proxy_lib: using the MAP_SHARED visibility mode with the "
135+
"named shared memory: %s",
136+
os_params.shm_name);
118137
}
138+
#undef NAME_MAX
119139
#endif
120140

121141
umf_result = umfMemoryProviderCreate(umfOsMemoryProviderOps(), &os_params,

0 commit comments

Comments
 (0)