diff mbox

[RFC] libibverbs: ibv_fork_init() and libhugetlbfs

Message ID 20100506093949.55916ab0@alex-laptop (mailing list archive)
State Superseded, archived
Headers show

Commit Message

Alexander Schmidt May 6, 2010, 7:39 a.m. UTC
None
diff mbox

Patch

--- libibverbs-1.1.2.orig/src/memory.c
+++ libibverbs-1.1.2/src/memory.c
@@ -40,6 +40,8 @@ 
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdint.h>
+#include <stdio.h>
+#include <string.h>
 
 #include "ibverbs.h"
 
@@ -54,6 +56,8 @@ 
 #define MADV_DOFORK	11
 #endif
 
+#define HUGE_PAGE_SIZE	(16 * 1024 * 1024)
+
 struct ibv_mem_node {
 	enum {
 		IBV_RED,
@@ -446,6 +450,48 @@  static struct ibv_mem_node *__mm_find_st
 	return node;
 }
 
+static void get_range_address(uintptr_t *start, uintptr_t *end, void *base, size_t size)
+{
+	pid_t pid;
+	FILE *file;
+	char buf[1024], lib[128];
+	int range_page_size = page_size;
+
+	pid = getpid();
+	snprintf(buf, sizeof(buf), "/proc/%d/maps", pid);
+
+	file = fopen(buf, "r");
+	if (!file)
+		goto out;
+
+	while (fgets(buf, sizeof(buf), file) != NULL) {
+		int n;
+		char *substr;
+		uintptr_t range_start, range_end;
+
+		n = sscanf(buf, "%lx-%lx %*s %*x %*s %*u %127s",
+				&range_start, &range_end, &lib);
+
+		if (n < 3)
+			continue;
+
+		substr = strstr(lib, "libhugetlbfs");
+		if (substr) {
+			if ((uintptr_t) base >= range_start &&
+					(uintptr_t) base < range_end) {
+				range_page_size = HUGE_PAGE_SIZE;
+				break;
+			}
+		}
+	}
+	fclose(file);
+
+out:
+	*start = (uintptr_t) base & ~(range_page_size - 1);
+	*end   = ((uintptr_t) (base + size + range_page_size - 1) &
+		 ~(range_page_size - 1)) - 1;
+}
+
 static int ibv_madvise_range(void *base, size_t size, int advice)
 {
 	uintptr_t start, end;
@@ -458,9 +504,7 @@  static int ibv_madvise_range(void *base,
 
 	inc = advice == MADV_DONTFORK ? 1 : -1;
 
-	start = (uintptr_t) base & ~(page_size - 1);
-	end   = ((uintptr_t) (base + size + page_size - 1) &
-		 ~(page_size - 1)) - 1;
+	get_range_address(&start, &end, base, size);
 
 	pthread_mutex_lock(&mm_mutex);