Skip to content

Commit

Permalink
Merge 9dfd9c0 into b336e0e
Browse files Browse the repository at this point in the history
  • Loading branch information
grom72 authored Oct 25, 2023
2 parents b336e0e + 9dfd9c0 commit 04a984a
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 101 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
2 changes: 1 addition & 1 deletion src/core/out.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ 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];
static char log_file_pid[PATH_MAX];
size_t len = strlen(log_file);
if (len > 0 && log_file[len - 1] == '-') {
if (util_snprintf(log_file_pid, PATH_MAX, "%s%d",
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 * sizeof(char));
if (domain_path == NULL) {
ERR("!Malloc");
cpu_cache = -1;
goto end;

Check warning on line 98 in src/libpmem2/auto_flush_linux.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/auto_flush_linux.c#L96-L98

Added lines #L96 - L98 were not covered by tests
}

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 * sizeof(char));
if (deep_flush_path == NULL) {
ERR("!Malloc");
ret = PMEM2_E_ERRNO;
goto end;

Check warning on line 41 in src/libpmem2/deep_flush_linux.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/deep_flush_linux.c#L39-L41

Added lines #L39 - L41 were not covered by tests
}

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;

Check warning on line 48 in src/libpmem2/deep_flush_linux.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/deep_flush_linux.c#L47-L48

Added lines #L47 - L48 were not covered by tests
}

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;

Check warning on line 53 in src/libpmem2/deep_flush_linux.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/deep_flush_linux.c#L53

Added line #L53 was not covered by tests
}

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;

Check warning on line 72 in src/libpmem2/deep_flush_linux.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/deep_flush_linux.c#L72

Added line #L72 was not covered by tests
}

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
41 changes: 33 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,58 @@ 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 * sizeof(char));
if (spath == NULL) {
ERR("!Malloc");
return PMEM2_E_ERRNO;

Check warning on line 48 in src/libpmem2/pmem2_utils_linux.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/pmem2_utils_linux.c#L47-L48

Added lines #L47 - L48 were not covered by tests
}

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;

Check warning on line 60 in src/libpmem2/pmem2_utils_linux.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/pmem2_utils_linux.c#L59-L60

Added lines #L59 - L60 were not covered by tests
}

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

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

Check warning on line 69 in src/libpmem2/pmem2_utils_linux.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/pmem2_utils_linux.c#L67-L69

Added lines #L67 - L69 were not covered by tests
}

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

Check warning on line 76 in src/libpmem2/pmem2_utils_linux.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/pmem2_utils_linux.c#L75-L76

Added lines #L75 - L76 were not covered by tests
}

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 (npath)
Free(npath);
if (spath)
Free(spath);

return ret;
}
65 changes: 48 additions & 17 deletions src/libpmem2/region_namespace_ndctl.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2020-2022, Intel Corporation */
/* Copyright 2020-2023, Intel Corporation */

/*
* region_namespace_ndctl.c -- common ndctl functions
Expand All @@ -16,6 +16,7 @@
#include "region_namespace_ndctl.h"
#include "region_namespace.h"
#include "out.h"
#include "alloc.h"

/*
* ndctl_match_devdax -- (internal) returns 0 if the devdax matches
Expand All @@ -27,30 +28,43 @@ ndctl_match_devdax(dev_t st_rdev, const char *devname)
{
LOG(3, "st_rdev %lu devname %s", st_rdev, devname);

int ret = 0;

Check warning on line 31 in src/libpmem2/region_namespace_ndctl.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/region_namespace_ndctl.c#L31

Added line #L31 was not covered by tests
char *path;
os_stat_t stat;

if (*devname == '\0')
return 1;

char path[PATH_MAX];
os_stat_t stat;
path = Malloc(PATH_MAX * sizeof(char));
if (path == NULL) {
ERR("!Malloc");
return PMEM2_E_ERRNO;

Check warning on line 41 in src/libpmem2/region_namespace_ndctl.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/region_namespace_ndctl.c#L38-L41

Added lines #L38 - L41 were not covered by tests
}

if (util_snprintf(path, PATH_MAX, "/dev/%s", devname) < 0) {
ERR("!snprintf");
return PMEM2_E_ERRNO;
ret = PMEM2_E_ERRNO;
goto end;

Check warning on line 47 in src/libpmem2/region_namespace_ndctl.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/region_namespace_ndctl.c#L46-L47

Added lines #L46 - L47 were not covered by tests
}

if (os_stat(path, &stat)) {
ERR("!stat %s", path);
return PMEM2_E_ERRNO;
ret = PMEM2_E_ERRNO;
goto end;

Check warning on line 53 in src/libpmem2/region_namespace_ndctl.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/region_namespace_ndctl.c#L52-L53

Added lines #L52 - L53 were not covered by tests
}

if (st_rdev != stat.st_rdev) {
LOG(10, "skipping not matching device: %s", path);
return 1;
ret = 1;
goto end;

Check warning on line 59 in src/libpmem2/region_namespace_ndctl.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/region_namespace_ndctl.c#L58-L59

Added lines #L58 - L59 were not covered by tests
}

LOG(4, "found matching device: %s", path);

return 0;
end:
Free(path);

Check warning on line 65 in src/libpmem2/region_namespace_ndctl.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/region_namespace_ndctl.c#L64-L65

Added lines #L64 - L65 were not covered by tests

return ret;

Check warning on line 67 in src/libpmem2/region_namespace_ndctl.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/region_namespace_ndctl.c#L67

Added line #L67 was not covered by tests
}

#define BUFF_LENGTH 64
Expand All @@ -65,27 +79,37 @@ ndctl_match_fsdax(dev_t st_dev, const char *devname)
{
LOG(3, "st_dev %lu devname %s", st_dev, devname);

int ret = 0;

Check warning on line 82 in src/libpmem2/region_namespace_ndctl.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/region_namespace_ndctl.c#L82

Added line #L82 was not covered by tests
char *path;
char dev_id[BUFF_LENGTH];

if (*devname == '\0')
return 1;

char path[PATH_MAX];
char dev_id[BUFF_LENGTH];
path = Malloc(PATH_MAX * sizeof(char));
if (path == NULL) {
ERR("!Malloc");
return PMEM2_E_ERRNO;

Check warning on line 92 in src/libpmem2/region_namespace_ndctl.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/region_namespace_ndctl.c#L89-L92

Added lines #L89 - L92 were not covered by tests
}

if (util_snprintf(path, PATH_MAX, "/sys/block/%s/dev", devname) < 0) {
ERR("!snprintf");
return PMEM2_E_ERRNO;
ret = PMEM2_E_ERRNO;
goto end;

Check warning on line 98 in src/libpmem2/region_namespace_ndctl.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/region_namespace_ndctl.c#L97-L98

Added lines #L97 - L98 were not covered by tests
}

if (util_snprintf(dev_id, BUFF_LENGTH, "%d:%d",
major(st_dev), minor(st_dev)) < 0) {
ERR("!snprintf");
return PMEM2_E_ERRNO;
ret = PMEM2_E_ERRNO;
goto end;

Check warning on line 105 in src/libpmem2/region_namespace_ndctl.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/region_namespace_ndctl.c#L104-L105

Added lines #L104 - L105 were not covered by tests
}

int fd = os_open(path, O_RDONLY);
if (fd < 0) {
ERR("!open \"%s\"", path);
return PMEM2_E_ERRNO;
ret = PMEM2_E_ERRNO;
goto end;

Check warning on line 112 in src/libpmem2/region_namespace_ndctl.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/region_namespace_ndctl.c#L111-L112

Added lines #L111 - L112 were not covered by tests
}

char buff[BUFF_LENGTH];
Expand All @@ -95,31 +119,38 @@ ndctl_match_fsdax(dev_t st_dev, const char *devname)
int oerrno = errno; /* save the errno */
os_close(fd);
errno = oerrno;
return PMEM2_E_ERRNO;
ret = PMEM2_E_ERRNO;
goto end;

Check warning on line 123 in src/libpmem2/region_namespace_ndctl.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/region_namespace_ndctl.c#L122-L123

Added lines #L122 - L123 were not covered by tests
}

os_close(fd);

if (nread == 0) {
ERR("%s is empty", path);
return PMEM2_E_INVALID_DEV_FORMAT;
ret = PMEM2_E_INVALID_DEV_FORMAT;
goto end;

Check warning on line 131 in src/libpmem2/region_namespace_ndctl.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/region_namespace_ndctl.c#L130-L131

Added lines #L130 - L131 were not covered by tests
}

if (buff[nread - 1] != '\n') {
ERR("%s doesn't end with new line", path);
return PMEM2_E_INVALID_DEV_FORMAT;
ret = PMEM2_E_INVALID_DEV_FORMAT;
goto end;

Check warning on line 137 in src/libpmem2/region_namespace_ndctl.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/region_namespace_ndctl.c#L136-L137

Added lines #L136 - L137 were not covered by tests
}

buff[nread - 1] = '\0';

if (strcmp(buff, dev_id) != 0) {
LOG(10, "skipping not matching device: %s", path);
return 1;
ret = 1;
goto end;

Check warning on line 145 in src/libpmem2/region_namespace_ndctl.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/region_namespace_ndctl.c#L144-L145

Added lines #L144 - L145 were not covered by tests
}

LOG(4, "found matching device: %s", path);

return 0;
end:
Free(path);

Check warning on line 151 in src/libpmem2/region_namespace_ndctl.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/region_namespace_ndctl.c#L150-L151

Added lines #L150 - L151 were not covered by tests

return ret;

Check warning on line 153 in src/libpmem2/region_namespace_ndctl.c

View check run for this annotation

Codecov / codecov/patch

src/libpmem2/region_namespace_ndctl.c#L153

Added line #L153 was not covered by tests
}

/*
Expand Down
Loading

0 comments on commit 04a984a

Please sign in to comment.