Skip to content

Commit f9bf272

Browse files
committed
Fix: avoid using deprecated valloc & frequent aligned alloc
switch to using a single aligned buffer that stays allocated. - free to use allocation mechanism that doesn't allow to use free() to hand back (not needed for current posix_memalign) - avoid memory fragmentation risk (and thus risk to run out of hogged memory) due to frequent alloc/free of aligned memory
1 parent f4ca41f commit f9bf272

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

src/sbd-md.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ static void
9090
close_device(struct sbd_context *st)
9191
{
9292
close(st->devfd);
93+
free(st->buffer);
9394
free(st);
9495
}
9596

@@ -108,6 +109,7 @@ open_device(const char* devname, int loglevel)
108109

109110
if (io_setup(1, &st->ioctx) != 0) {
110111
cl_perror("io_setup failed");
112+
free(st->buffer);
111113
free(st);
112114
return NULL;
113115
}
@@ -120,6 +122,7 @@ open_device(const char* devname, int loglevel)
120122
} else {
121123
cl_log(loglevel, "Opening device %s failed.", devname);
122124
}
125+
free(st->buffer);
123126
free(st);
124127
return NULL;
125128
}
@@ -132,6 +135,12 @@ open_device(const char* devname, int loglevel)
132135
return NULL;
133136
}
134137

138+
if (posix_memalign(&st->buffer, sector_size, sector_size)) {
139+
cl_perror("Couldn't allocate sector-buffer.");
140+
close_device(st);
141+
return NULL;
142+
}
143+
135144
return st;
136145
}
137146

@@ -140,11 +149,10 @@ sector_alloc(void)
140149
{
141150
void *x;
142151

143-
x = valloc(sector_size);
152+
x = calloc(1, sector_size);
144153
if (!x) {
145154
exit(1);
146155
}
147-
memset(x, 0, sector_size);
148156

149157
return x;
150158
}
@@ -162,9 +170,11 @@ sector_io(struct sbd_context *st, int sector, void *data, int rw)
162170

163171
memset(&st->io, 0, sizeof(struct iocb));
164172
if (rw) {
165-
io_prep_pwrite(&st->io, st->devfd, data, sector_size, (long long) sector_size * sector);
173+
memcpy(st->buffer, data, sector_size);
174+
io_prep_pwrite(&st->io, st->devfd, st->buffer, sector_size, (long long) sector_size * sector);
166175
} else {
167-
io_prep_pread(&st->io, st->devfd, data, sector_size, (long long) sector_size * sector);
176+
memset(st->buffer, 0, sector_size);
177+
io_prep_pread(&st->io, st->devfd, st->buffer, sector_size, (long long) sector_size * sector);
168178
}
169179

170180
if (io_submit(st->ioctx, 1, ios) != 1) {
@@ -179,7 +189,7 @@ sector_io(struct sbd_context *st, int sector, void *data, int rw)
179189
cl_log(LOG_ERR, "Failed to retrieve IO events (rw=%d)", rw);
180190
return -1;
181191
} else if (r < 1L) {
182-
cl_log(LOG_INFO, "Cancelling IO request due to timeout (rw=%d)", rw);
192+
cl_log(LOG_INFO, "Cancelling IO request due to timeout (rw=%d, r=%ld)", rw, r);
183193
r = io_cancel(st->ioctx, ios[0], &event);
184194
if (r) {
185195
DBGLOG(LOG_INFO, "Could not cancel IO request (rw=%d)", rw);
@@ -195,6 +205,9 @@ sector_io(struct sbd_context *st, int sector, void *data, int rw)
195205

196206
/* IO is happy */
197207
if (event.res == sector_size) {
208+
if (!rw) {
209+
memcpy(data, st->buffer, sector_size);
210+
}
198211
return 0;
199212
} else {
200213
cl_log(LOG_ERR, "Short IO (rw=%d, res=%lu, sector_size=%d)",

src/sbd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ struct sbd_context {
108108
int devfd;
109109
io_context_t ioctx;
110110
struct iocb io;
111+
void *buffer;
111112
};
112113

113114
enum pcmk_health

0 commit comments

Comments
 (0)