Skip to content

Commit

Permalink
common: limit the size of stack memory by replacing with Malloc
Browse files Browse the repository at this point in the history
Signed-off-by: Tomasz Gromadzki <tomasz.gromadzki@intel.com>
  • Loading branch information
grom72 committed Oct 24, 2023
1 parent aa42768 commit 9e55da3
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 102 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

This release:
- Significantly reduces the libpmem's stack usage.
- Decrease stack usage by allocating paths' buffers on heap


Tue Aug 8 2023 Oksana Sałyk <oksana.salyk@intel.com>

Expand Down
18 changes: 16 additions & 2 deletions src/core/out.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "os_thread.h"
#include "valgrind_internal.h"
#include "util.h"
#include "alloc.h"

static const char *Log_prefix;
static int Log_level;
Expand Down Expand Up @@ -161,12 +162,18 @@ out_init(const char *log_prefix, const char *log_level_var,
log_file[0] != '\0') {

/* reserve more than enough space for a PID + '\0' */
char log_file_pid[PATH_MAX];
char *log_file_pid;
log_file_pid = Malloc(PATH_MAX);
if (log_file_pid == NULL) {
fprintf(stderr, "out_init !Malloc\n");
abort();
}
size_t len = strlen(log_file);
if (len > 0 && log_file[len - 1] == '-') {
if (util_snprintf(log_file_pid, PATH_MAX, "%s%d",
log_file, getpid()) < 0) {
ERR("snprintf: %d", errno);
Free(log_file_pid);
abort();
}
log_file = log_file_pid;
Expand All @@ -178,8 +185,10 @@ out_init(const char *log_prefix, const char *log_level_var,
fprintf(stderr, "Error (%s): %s=%s: %s\n",
log_prefix, log_file_var,
log_file, buff);
Free(log_file_pid);
abort();
}
Free(log_file_pid);
}
#endif /* DEBUG */

Expand All @@ -196,7 +205,12 @@ out_init(const char *log_prefix, const char *log_level_var,
setlinebuf(Out_fp);

#ifdef DEBUG
static char namepath[PATH_MAX];
char *namepath;
namepath = Malloc(PATH_MAX);
if (namepath == NULL) {
fprintf(stderr, "out_init !Malloc\n");
abort();
}
LOG(1, "pid %d: program: %s", getpid(),
util_getexecname(namepath, PATH_MAX));
#endif
Expand Down
14 changes: 12 additions & 2 deletions src/libpmem2/auto_flush_linux.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2018-2020, Intel Corporation */
/* Copyright 2018-2023, Intel Corporation */

/*
* auto_flush_linux.c -- Linux auto flush detection
Expand All @@ -16,6 +16,7 @@
#include "os.h"
#include "fs.h"
#include "auto_flush.h"
#include "alloc.h"

#define BUS_DEVICE_PATH "/sys/bus/nd/devices"
#define PERSISTENCE_DOMAIN "persistence_domain"
Expand Down Expand Up @@ -87,9 +88,16 @@ check_domain_in_region(const char *region_path)

struct fs *reg = NULL;
struct fs_entry *reg_entry;
char domain_path[PATH_MAX];
char *domain_path = NULL;
int cpu_cache = 0;

domain_path = Malloc(PATH_MAX);
if (domain_path == NULL) {
ERR("!Malloc");
cpu_cache = -1;
goto end;
}

reg = fs_new(region_path);
if (reg == NULL) {
ERR("!fs_new: \"%s\"", region_path);
Expand Down Expand Up @@ -122,6 +130,8 @@ check_domain_in_region(const char *region_path)
end:
if (reg)
fs_delete(reg);
if (domain_path)
Free(domain_path);
return cpu_cache;
}

Expand Down
30 changes: 22 additions & 8 deletions src/libpmem2/deep_flush_linux.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2020, Intel Corporation */
/* Copyright 2020-2023, Intel Corporation */

/*
* deep_flush_linux.c -- deep_flush functionality
Expand All @@ -18,6 +18,7 @@
#include "persist.h"
#include "pmem2_utils.h"
#include "region_namespace.h"
#include "alloc.h"

/*
* pmem2_deep_flush_write -- perform write to deep_flush file
Expand All @@ -28,19 +29,28 @@ pmem2_deep_flush_write(unsigned region_id)
{
LOG(3, "region_id %d", region_id);

char deep_flush_path[PATH_MAX];
int deep_flush_fd;
int ret = 0;
char *deep_flush_path = NULL;
int deep_flush_fd = -1;
char rbuf[2];

deep_flush_path = Malloc(PATH_MAX);
if (deep_flush_path == NULL) {
ERR("!Malloc");
ret = -1;
goto end;
}

if (util_snprintf(deep_flush_path, PATH_MAX,
"/sys/bus/nd/devices/region%u/deep_flush", region_id) < 0) {
ERR("!snprintf");
return PMEM2_E_ERRNO;
ret = PMEM2_E_ERRNO;
goto end;
}

if ((deep_flush_fd = os_open(deep_flush_path, O_RDONLY)) < 0) {
LOG(1, "!os_open(\"%s\", O_RDONLY)", deep_flush_path);
return 0;
goto end;
}

if (read(deep_flush_fd, rbuf, sizeof(rbuf)) != 2) {
Expand All @@ -54,11 +64,12 @@ pmem2_deep_flush_write(unsigned region_id)
}

os_close(deep_flush_fd);
deep_flush_fd = -1;

if ((deep_flush_fd = os_open(deep_flush_path, O_WRONLY)) < 0) {
LOG(1, "Cannot open deep_flush file %s to write",
deep_flush_path);
return 0;
goto end;
}

if (write(deep_flush_fd, "1", 1) != 1) {
Expand All @@ -67,8 +78,11 @@ pmem2_deep_flush_write(unsigned region_id)
}

end:
os_close(deep_flush_fd);
return 0;
if (deep_flush_fd > -1)
os_close(deep_flush_fd);
if (deep_flush_path)
Free(deep_flush_path);
return ret;
}

/*
Expand Down
43 changes: 35 additions & 8 deletions src/libpmem2/pmem2_utils_linux.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2014-2020, Intel Corporation */
/* Copyright 2014-2023, Intel Corporation */

#include <errno.h>
#include <fcntl.h>
Expand All @@ -14,6 +14,7 @@
#include "pmem2_utils.h"
#include "region_namespace.h"
#include "source.h"
#include "alloc.h"

/*
* pmem2_get_type_from_stat -- determine type of file based on output of stat
Expand All @@ -37,34 +38,60 @@ pmem2_get_type_from_stat(const os_stat_t *st, enum pmem2_file_type *type)
return PMEM2_E_INVALID_FILE_TYPE;
}

char spath[PATH_MAX];
int ret = util_snprintf(spath, PATH_MAX,
int ret = 0;
char *spath = NULL;
char *npath = NULL;

spath = Malloc(PATH_MAX);
if (spath == NULL) {
errno = ENOMEM;
ERR("!Malloc");
return PMEM2_E_ERRNO;
}

ret = util_snprintf(spath, PATH_MAX,
"/sys/dev/char/%u:%u/subsystem",
os_major(st->st_rdev), os_minor(st->st_rdev));

if (ret < 0) {
/* impossible */
ERR("!snprintf");
ASSERTinfo(0, "snprintf failed");
return PMEM2_E_ERRNO;
ret = PMEM2_E_ERRNO;
goto end;
}

LOG(4, "device subsystem path \"%s\"", spath);

char npath[PATH_MAX];
npath = Malloc(PATH_MAX);
if (npath == NULL) {
errno = ENOMEM;
ERR("!Malloc");
ret = PMEM2_E_ERRNO;
goto end;
}

char *rpath = realpath(spath, npath);
if (rpath == NULL) {
ERR("!realpath \"%s\"", spath);
return PMEM2_E_ERRNO;
ret = PMEM2_E_ERRNO;
goto end;
}

char *basename = strrchr(rpath, '/');
if (!basename || strcmp("dax", basename + 1) != 0) {
LOG(3, "%s path does not match device dax prefix path", rpath);
return PMEM2_E_INVALID_FILE_TYPE;
ret = PMEM2_E_INVALID_FILE_TYPE;
goto end;
}

*type = PMEM2_FTYPE_DEVDAX;

return 0;
end:
if (spath)
Free(spath);
if (npath)
Free(npath);

return ret;
}

0 comments on commit 9e55da3

Please sign in to comment.