Skip to content

Commit 3c7a550

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 4ce0b68 commit 3c7a550

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/sbd-md.c

Lines changed: 16 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

@@ -132,6 +133,12 @@ open_device(const char* devname, int loglevel)
132133
return NULL;
133134
}
134135

136+
if (posix_memalign(&st->buffer, sector_size, sector_size)) {
137+
cl_perror("Couldn't allocate sector-buffer.");
138+
close_device(st);
139+
return NULL;
140+
}
141+
135142
return st;
136143
}
137144

@@ -140,11 +147,10 @@ sector_alloc(void)
140147
{
141148
void *x;
142149

143-
x = valloc(sector_size);
150+
x = calloc(1, sector_size);
144151
if (!x) {
145152
exit(1);
146153
}
147-
memset(x, 0, sector_size);
148154

149155
return x;
150156
}
@@ -162,9 +168,11 @@ sector_io(struct sbd_context *st, int sector, void *data, int rw)
162168

163169
memset(&st->io, 0, sizeof(struct iocb));
164170
if (rw) {
165-
io_prep_pwrite(&st->io, st->devfd, data, sector_size, (long long) sector_size * sector);
171+
memcpy(st->buffer, data, sector_size);
172+
io_prep_pwrite(&st->io, st->devfd, st->buffer, sector_size, (long long) sector_size * sector);
166173
} else {
167-
io_prep_pread(&st->io, st->devfd, data, sector_size, (long long) sector_size * sector);
174+
memset(st->buffer, 0, sector_size);
175+
io_prep_pread(&st->io, st->devfd, st->buffer, sector_size, (long long) sector_size * sector);
168176
}
169177

170178
if (io_submit(st->ioctx, 1, ios) != 1) {
@@ -179,7 +187,7 @@ sector_io(struct sbd_context *st, int sector, void *data, int rw)
179187
cl_log(LOG_ERR, "Failed to retrieve IO events (rw=%d)", rw);
180188
return -1;
181189
} else if (r < 1L) {
182-
cl_log(LOG_INFO, "Cancelling IO request due to timeout (rw=%d)", rw);
190+
cl_log(LOG_INFO, "Cancelling IO request due to timeout (rw=%d, r=%ld)", rw, r);
183191
r = io_cancel(st->ioctx, ios[0], &event);
184192
if (r) {
185193
DBGLOG(LOG_INFO, "Could not cancel IO request (rw=%d)", rw);
@@ -195,6 +203,9 @@ sector_io(struct sbd_context *st, int sector, void *data, int rw)
195203

196204
/* IO is happy */
197205
if (event.res == sector_size) {
206+
if (!rw) {
207+
memcpy(data, st->buffer, sector_size);
208+
}
198209
return 0;
199210
} else {
200211
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)