Skip to content

Commit 5e632fb

Browse files
committed
common: limit the size of stack memory by replacing with Malloc
Signed-off-by: Tomasz Gromadzki <tomasz.gromadzki@intel.com>
1 parent 87108b8 commit 5e632fb

8 files changed

+166
-101
lines changed

ChangeLog

+2
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 <oksana.salyk@intel.com>
79

src/core/out.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ out_init(const char *log_prefix, const char *log_level_var,
161161
log_file[0] != '\0') {
162162

163163
/* reserve more than enough space for a PID + '\0' */
164-
char log_file_pid[PATH_MAX];
164+
static char log_file_pid[PATH_MAX];
165165
size_t len = strlen(log_file);
166166
if (len > 0 && log_file[len - 1] == '-') {
167167
if (util_snprintf(log_file_pid, PATH_MAX, "%s%d",

src/libpmem2/auto_flush_linux.c

+12-2
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 * sizeof(char));
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

+22-8
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 * sizeof(char));
38+
if (deep_flush_path == NULL) {
39+
ERR("!Malloc");
40+
ret = PMEM2_E_ERRNO;
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

+33-8
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,58 @@ 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 * sizeof(char));
46+
if (spath == NULL) {
47+
ERR("!Malloc");
48+
return PMEM2_E_ERRNO;
49+
}
50+
51+
ret = util_snprintf(spath, PATH_MAX,
4252
"/sys/dev/char/%u:%u/subsystem",
4353
os_major(st->st_rdev), os_minor(st->st_rdev));
4454

4555
if (ret < 0) {
4656
/* impossible */
4757
ERR("!snprintf");
4858
ASSERTinfo(0, "snprintf failed");
49-
return PMEM2_E_ERRNO;
59+
ret = PMEM2_E_ERRNO;
60+
goto end;
5061
}
5162

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

54-
char npath[PATH_MAX];
65+
npath = Malloc(PATH_MAX * sizeof(char));
66+
if (npath == NULL) {
67+
ERR("!Malloc");
68+
ret = PMEM2_E_ERRNO;
69+
goto end;
70+
}
71+
5572
char *rpath = realpath(spath, npath);
5673
if (rpath == NULL) {
5774
ERR("!realpath \"%s\"", spath);
58-
return PMEM2_E_ERRNO;
75+
ret = PMEM2_E_ERRNO;
76+
goto end;
5977
}
6078

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

6786
*type = PMEM2_FTYPE_DEVDAX;
6887

69-
return 0;
88+
end:
89+
if (npath)
90+
Free(npath);
91+
if (spath)
92+
Free(spath);
93+
94+
return ret;
7095
}

src/libpmem2/region_namespace_ndctl.c

+46-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: BSD-3-Clause
2-
/* Copyright 2020-2022, Intel Corporation */
2+
/* Copyright 2020-2023, Intel Corporation */
33

44
/*
55
* region_namespace_ndctl.c -- common ndctl functions
@@ -16,6 +16,7 @@
1616
#include "region_namespace_ndctl.h"
1717
#include "region_namespace.h"
1818
#include "out.h"
19+
#include "alloc.h"
1920

2021
/*
2122
* ndctl_match_devdax -- (internal) returns 0 if the devdax matches
@@ -27,30 +28,42 @@ ndctl_match_devdax(dev_t st_rdev, const char *devname)
2728
{
2829
LOG(3, "st_rdev %lu devname %s", st_rdev, devname);
2930

31+
int ret = 0;
32+
char *path;
33+
os_stat_t stat;
34+
3035
if (*devname == '\0')
3136
return 1;
3237

33-
char path[PATH_MAX];
34-
os_stat_t stat;
38+
path = Malloc(PATH_MAX * sizeof(char));
39+
if (path == NULL) {
40+
ERR("!Malloc");
41+
return PMEM2_E_ERRNO;
42+
}
3543

3644
if (util_snprintf(path, PATH_MAX, "/dev/%s", devname) < 0) {
3745
ERR("!snprintf");
38-
return PMEM2_E_ERRNO;
46+
ret = PMEM2_E_ERRNO;
47+
goto end;
3948
}
4049

4150
if (os_stat(path, &stat)) {
4251
ERR("!stat %s", path);
43-
return PMEM2_E_ERRNO;
52+
ret = PMEM2_E_ERRNO;
53+
goto end;
4454
}
4555

4656
if (st_rdev != stat.st_rdev) {
4757
LOG(10, "skipping not matching device: %s", path);
48-
return 1;
58+
ret = 1;
59+
goto end;
4960
}
5061

5162
LOG(4, "found matching device: %s", path);
63+
end:
64+
Free(path);
5265

53-
return 0;
66+
return ret;
5467
}
5568

5669
#define BUFF_LENGTH 64
@@ -65,27 +78,37 @@ ndctl_match_fsdax(dev_t st_dev, const char *devname)
6578
{
6679
LOG(3, "st_dev %lu devname %s", st_dev, devname);
6780

81+
int ret = 0;
82+
char *path;
83+
char dev_id[BUFF_LENGTH];
84+
6885
if (*devname == '\0')
6986
return 1;
7087

71-
char path[PATH_MAX];
72-
char dev_id[BUFF_LENGTH];
88+
path = Malloc(PATH_MAX * sizeof(char));
89+
if (path == NULL) {
90+
ERR("!Malloc");
91+
return PMEM2_E_ERRNO;
92+
}
7393

7494
if (util_snprintf(path, PATH_MAX, "/sys/block/%s/dev", devname) < 0) {
7595
ERR("!snprintf");
76-
return PMEM2_E_ERRNO;
96+
ret = PMEM2_E_ERRNO;
97+
goto end;
7798
}
7899

79100
if (util_snprintf(dev_id, BUFF_LENGTH, "%d:%d",
80101
major(st_dev), minor(st_dev)) < 0) {
81102
ERR("!snprintf");
82-
return PMEM2_E_ERRNO;
103+
ret = PMEM2_E_ERRNO;
104+
goto end;
83105
}
84106

85107
int fd = os_open(path, O_RDONLY);
86108
if (fd < 0) {
87109
ERR("!open \"%s\"", path);
88-
return PMEM2_E_ERRNO;
110+
ret = PMEM2_E_ERRNO;
111+
goto end;
89112
}
90113

91114
char buff[BUFF_LENGTH];
@@ -95,31 +118,37 @@ ndctl_match_fsdax(dev_t st_dev, const char *devname)
95118
int oerrno = errno; /* save the errno */
96119
os_close(fd);
97120
errno = oerrno;
98-
return PMEM2_E_ERRNO;
121+
ret = PMEM2_E_ERRNO;
122+
goto end;
99123
}
100124

101125
os_close(fd);
102126

103127
if (nread == 0) {
104128
ERR("%s is empty", path);
105-
return PMEM2_E_INVALID_DEV_FORMAT;
129+
ret = PMEM2_E_INVALID_DEV_FORMAT;
130+
goto end;
106131
}
107132

108133
if (buff[nread - 1] != '\n') {
109134
ERR("%s doesn't end with new line", path);
110-
return PMEM2_E_INVALID_DEV_FORMAT;
135+
ret = PMEM2_E_INVALID_DEV_FORMAT;
136+
goto end;
111137
}
112138

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

115141
if (strcmp(buff, dev_id) != 0) {
116142
LOG(10, "skipping not matching device: %s", path);
117-
return 1;
143+
ret = 1;
144+
goto end;
118145
}
119146

120147
LOG(4, "found matching device: %s", path);
148+
end:
149+
Free(path);
121150

122-
return 0;
151+
return ret;
123152
}
124153

125154
/*

0 commit comments

Comments
 (0)