[Ocfs2-tools-devel] [PATCH] mkfs.ocfs2: allocate the least memory in do_malloc

Xue jiufei xuejiufei at huawei.com
Fri Dec 21 02:45:02 PST 2012


  Currently in mkfs.ocfs2 we use do_malloc() to alloc memory. No matter
how many size required, it will be OCFS2_MAX_BLOCKSIZE(4KB) aligned
using posix_memalign(). And there is a big loop in initialize_bitmap()
which will require (nr_cluster_groups * 8KB) at a time. This may cause
OOM when formatting a big device such as 16T with block size 512B and
cluster size 4KB.
  Changing do_malloc() to allocating the least memory can do help in
this kind of situation.

Signed-off-by: xuejiufei <xuejiufei at huawei.com>
---
diff --git a/mkfs.ocfs2/mkfs.c b/mkfs.ocfs2/mkfs.c
index 16e2eab..1268c27 100644
--- a/mkfs.ocfs2/mkfs.c
+++ b/mkfs.ocfs2/mkfs.c
@@ -1674,8 +1674,20 @@ do_malloc(State *s, size_t size)
 {
 	void *buf;
 	int ret;
+	int order = -1;
+	unsigned int tmp_size = size;
+	unsigned int aligned_size = 1;
 
-	ret = posix_memalign(&buf, OCFS2_MAX_BLOCKSIZE, size);
+	do {
+		tmp_size >>= 1;
+		order++;
+	} while (tmp_size);
+
+	aligned_size <<= order;
+	if (aligned_size > OCFS2_MAX_BLOCKSIZE)
+		aligned_size = OCFS2_MAX_BLOCKSIZE;
+
+	ret = posix_memalign(&buf, aligned_size, size);
 
 	if (ret != 0) {
 		com_err(s->progname, 0,



More information about the Ocfs2-tools-devel mailing list