Skip to content

Commit 655a06f

Browse files
committed
IB fork
The key change was in btl_openib_connect_udcm.c where a buffer was being pinned with size 65664 (whether openib was being used or not). The start of the buffer was page aligned, but because of the size the end wasn't. That makes it too easy for a forked child to accidentally touch pinned memory on the same page as the end of that buffer. So this change increases the size of the allocated buffer to use the rest of the page. I inspected the rest of the ibv_reg_mr() calls and changed one other place to page align its buffer too, although I think the above is the one that really matters. Signed-off-by: Mark Allen <[email protected]>
1 parent bf668ad commit 655a06f

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

opal/mca/btl/openib/connect/btl_openib_connect_base.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
#include "opal/util/proc.h"
3333
#include "opal/util/show_help.h"
3434

35+
#include "opal/util/sys_limits.h"
36+
#include "opal/align.h"
37+
3538
/*
3639
* Array of all possible connection functions
3740
*/
@@ -421,10 +424,27 @@ int opal_btl_openib_connect_base_alloc_cts(mca_btl_base_endpoint_t *endpoint)
421424
sizeof(mca_btl_openib_footer_t) +
422425
mca_btl_openib_component.qp_infos[mca_btl_openib_component.credits_qp].size;
423426

427+
int align_it = 0;
428+
int page_size;
429+
430+
page_size = opal_getpagesize();
431+
if (length >= page_size / 2) { align_it = 1; }
432+
if (align_it) {
433+
// I think this is only active for ~64k+ buffers anyway, but I'm not
434+
// positive, so I'm only increasing the buffer size and alignment if
435+
// it's not too small. That way we'd avoid wasting excessive memory
436+
// in case this code was active for tiny buffers.
437+
length = OPAL_ALIGN(length, page_size, int);
438+
}
439+
424440
/* Explicitly don't use the mpool registration */
425441
fli = &(endpoint->endpoint_cts_frag.super.super.base.super);
426442
fli->registration = NULL;
427-
fli->ptr = malloc(length);
443+
if (!align_it) {
444+
fli->ptr = malloc(length);
445+
} else {
446+
posix_memalign((void**)&(fli->ptr), page_size, length);
447+
}
428448
if (NULL == fli->ptr) {
429449
BTL_ERROR(("malloc failed"));
430450
return OPAL_ERR_OUT_OF_RESOURCE;

opal/mca/btl/openib/connect/btl_openib_connect_udcm.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#include "connect/connect.h"
7676

7777
#include "opal/util/sys_limits.h"
78+
#include "opal/align.h"
7879

7980
#if (ENABLE_DYNAMIC_SL)
8081
#include "connect/btl_openib_connect_sl.h"
@@ -1030,16 +1031,19 @@ static void udcm_module_destroy_listen_qp (udcm_module_t *m)
10301031

10311032
static int udcm_module_allocate_buffers (udcm_module_t *m)
10321033
{
1033-
size_t total_size;
1034+
size_t total_size, page_size;
10341035

10351036
m->msg_length = sizeof (udcm_msg_hdr_t) +
10361037
mca_btl_openib_component.num_qps * sizeof (udcm_qp_t);
10371038

10381039
total_size = (udcm_recv_count + 1) * (m->msg_length +
10391040
UDCM_GRH_SIZE);
10401041

1042+
page_size = opal_getpagesize();
1043+
total_size = OPAL_ALIGN(total_size, page_size, size_t);
1044+
10411045
m->cm_buffer = NULL;
1042-
posix_memalign ((void **)&m->cm_buffer, (size_t)opal_getpagesize(),
1046+
posix_memalign ((void **)&m->cm_buffer, (size_t)page_size,
10431047
total_size);
10441048
if (NULL == m->cm_buffer) {
10451049
BTL_ERROR(("malloc failed! errno = %d", errno));

opal/util/sys_limits.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,20 @@ int opal_util_init_sys_limits(char **errmsg)
235235

236236
int opal_getpagesize(void)
237237
{
238+
static int page_size = -1;
239+
240+
if (page_size != -1) {
241+
// testing in a loop showed sysconf() took ~5 usec vs ~0.3 usec with it cached
242+
return page_size;
243+
}
244+
238245
#ifdef HAVE_GETPAGESIZE
239-
return getpagesize();
246+
return page_size = getpagesize();
240247
#elif defined(_SC_PAGESIZE )
241-
return sysconf(_SC_PAGESIZE);
248+
return page_size = sysconf(_SC_PAGESIZE);
242249
#elif defined(_SC_PAGE_SIZE)
243-
return sysconf(_SC_PAGE_SIZE);
250+
return page_size = sysconf(_SC_PAGE_SIZE);
244251
#else
245-
return 65536; /* safer to overestimate than under */
252+
return page_size = 65536; /* safer to overestimate than under */
246253
#endif
247254
}

0 commit comments

Comments
 (0)