Skip to content
This repository was archived by the owner on Nov 12, 2025. It is now read-only.

Commit 9e55da3

Browse files
committed
common: limit the size of stack memory by replacing with Malloc
Signed-off-by: Tomasz Gromadzki <[email protected]>
1 parent aa42768 commit 9e55da3

File tree

8 files changed

+187
-102
lines changed

8 files changed

+187
-102
lines changed

ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
This release:
44
- Significantly reduces the libpmem's stack usage.
5+
- Decrease stack usage by allocating paths' buffers on heap
6+
57

68
Tue Aug 8 2023 Oksana Sałyk <[email protected]>
79

src/core/out.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "os_thread.h"
2121
#include "valgrind_internal.h"
2222
#include "util.h"
23+
#include "alloc.h"
2324

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

163164
/* reserve more than enough space for a PID + '\0' */
164-
char log_file_pid[PATH_MAX];
165+
char *log_file_pid;
166+
log_file_pid = Malloc(PATH_MAX);
167+
if (log_file_pid == NULL) {
168+
fprintf(stderr, "out_init !Malloc\n");
169+
abort();
170+
}
165171
size_t len = strlen(log_file);
166172
if (len > 0 && log_file[len - 1] == '-') {
167173
if (util_snprintf(log_file_pid, PATH_MAX, "%s%d",
168174
log_file, getpid()) < 0) {
169175
ERR("snprintf: %d", errno);
176+
Free(log_file_pid);
170177
abort();
171178
}
172179
log_file = log_file_pid;
@@ -178,8 +185,10 @@ out_init(const char *log_prefix, const char *log_level_var,
178185
fprintf(stderr, "Error (%s): %s=%s: %s\n",
179186
log_prefix, log_file_var,
180187
log_file, buff);
188+
Free(log_file_pid);
181189
abort();
182190
}
191+
Free(log_file_pid);
183192
}
184193
#endif /* DEBUG */
185194

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

198207
#ifdef DEBUG
199-
static char namepath[PATH_MAX];
208+
char *namepath;
209+
namepath = Malloc(PATH_MAX);
210+
if (namepath == NULL) {
211+
fprintf(stderr, "out_init !Malloc\n");
212+
abort();
213+
}
200214
LOG(1, "pid %d: program: %s", getpid(),
201215
util_getexecname(namepath, PATH_MAX));
202216
#endif

src/libpmem2/auto_flush_linux.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: BSD-3-Clause
2-
/* Copyright 2018-2020, Intel Corporation */
2+
/* Copyright 2018-2023, Intel Corporation */
33

44
/*
55
* auto_flush_linux.c -- Linux auto flush detection
@@ -16,6 +16,7 @@
1616
#include "os.h"
1717
#include "fs.h"
1818
#include "auto_flush.h"
19+
#include "alloc.h"
1920

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

8889
struct fs *reg = NULL;
8990
struct fs_entry *reg_entry;
90-
char domain_path[PATH_MAX];
91+
char *domain_path = NULL;
9192
int cpu_cache = 0;
9293

94+
domain_path = Malloc(PATH_MAX);
95+
if (domain_path == NULL) {
96+
ERR("!Malloc");
97+
cpu_cache = -1;
98+
goto end;
99+
}
100+
93101
reg = fs_new(region_path);
94102
if (reg == NULL) {
95103
ERR("!fs_new: \"%s\"", region_path);
@@ -122,6 +130,8 @@ check_domain_in_region(const char *region_path)
122130
end:
123131
if (reg)
124132
fs_delete(reg);
133+
if (domain_path)
134+
Free(domain_path);
125135
return cpu_cache;
126136
}
127137

src/libpmem2/deep_flush_linux.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: BSD-3-Clause
2-
/* Copyright 2020, Intel Corporation */
2+
/* Copyright 2020-2023, Intel Corporation */
33

44
/*
55
* deep_flush_linux.c -- deep_flush functionality
@@ -18,6 +18,7 @@
1818
#include "persist.h"
1919
#include "pmem2_utils.h"
2020
#include "region_namespace.h"
21+
#include "alloc.h"
2122

2223
/*
2324
* pmem2_deep_flush_write -- perform write to deep_flush file
@@ -28,19 +29,28 @@ pmem2_deep_flush_write(unsigned region_id)
2829
{
2930
LOG(3, "region_id %d", region_id);
3031

31-
char deep_flush_path[PATH_MAX];
32-
int deep_flush_fd;
32+
int ret = 0;
33+
char *deep_flush_path = NULL;
34+
int deep_flush_fd = -1;
3335
char rbuf[2];
3436

37+
deep_flush_path = Malloc(PATH_MAX);
38+
if (deep_flush_path == NULL) {
39+
ERR("!Malloc");
40+
ret = -1;
41+
goto end;
42+
}
43+
3544
if (util_snprintf(deep_flush_path, PATH_MAX,
3645
"/sys/bus/nd/devices/region%u/deep_flush", region_id) < 0) {
3746
ERR("!snprintf");
38-
return PMEM2_E_ERRNO;
47+
ret = PMEM2_E_ERRNO;
48+
goto end;
3949
}
4050

4151
if ((deep_flush_fd = os_open(deep_flush_path, O_RDONLY)) < 0) {
4252
LOG(1, "!os_open(\"%s\", O_RDONLY)", deep_flush_path);
43-
return 0;
53+
goto end;
4454
}
4555

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

5666
os_close(deep_flush_fd);
67+
deep_flush_fd = -1;
5768

5869
if ((deep_flush_fd = os_open(deep_flush_path, O_WRONLY)) < 0) {
5970
LOG(1, "Cannot open deep_flush file %s to write",
6071
deep_flush_path);
61-
return 0;
72+
goto end;
6273
}
6374

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

6980
end:
70-
os_close(deep_flush_fd);
71-
return 0;
81+
if (deep_flush_fd > -1)
82+
os_close(deep_flush_fd);
83+
if (deep_flush_path)
84+
Free(deep_flush_path);
85+
return ret;
7286
}
7387

7488
/*

src/libpmem2/pmem2_utils_linux.c

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: BSD-3-Clause
2-
/* Copyright 2014-2020, Intel Corporation */
2+
/* Copyright 2014-2023, Intel Corporation */
33

44
#include <errno.h>
55
#include <fcntl.h>
@@ -14,6 +14,7 @@
1414
#include "pmem2_utils.h"
1515
#include "region_namespace.h"
1616
#include "source.h"
17+
#include "alloc.h"
1718

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

40-
char spath[PATH_MAX];
41-
int ret = util_snprintf(spath, PATH_MAX,
41+
int ret = 0;
42+
char *spath = NULL;
43+
char *npath = NULL;
44+
45+
spath = Malloc(PATH_MAX);
46+
if (spath == NULL) {
47+
errno = ENOMEM;
48+
ERR("!Malloc");
49+
return PMEM2_E_ERRNO;
50+
}
51+
52+
ret = util_snprintf(spath, PATH_MAX,
4253
"/sys/dev/char/%u:%u/subsystem",
4354
os_major(st->st_rdev), os_minor(st->st_rdev));
4455

4556
if (ret < 0) {
4657
/* impossible */
4758
ERR("!snprintf");
4859
ASSERTinfo(0, "snprintf failed");
49-
return PMEM2_E_ERRNO;
60+
ret = PMEM2_E_ERRNO;
61+
goto end;
5062
}
5163

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

54-
char npath[PATH_MAX];
66+
npath = Malloc(PATH_MAX);
67+
if (npath == NULL) {
68+
errno = ENOMEM;
69+
ERR("!Malloc");
70+
ret = PMEM2_E_ERRNO;
71+
goto end;
72+
}
73+
5574
char *rpath = realpath(spath, npath);
5675
if (rpath == NULL) {
5776
ERR("!realpath \"%s\"", spath);
58-
return PMEM2_E_ERRNO;
77+
ret = PMEM2_E_ERRNO;
78+
goto end;
5979
}
6080

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

6788
*type = PMEM2_FTYPE_DEVDAX;
6889

69-
return 0;
90+
end:
91+
if (spath)
92+
Free(spath);
93+
if (npath)
94+
Free(npath);
95+
96+
return ret;
7097
}

0 commit comments

Comments
 (0)