63535b
From 55f7ba830d40d438f0b0663a505e0c227fc68b6b Mon Sep 17 00:00:00 2001
63535b
From: Phillip Lougher <phillip@squashfs.org.uk>
63535b
Date: Tue, 10 Jun 2014 21:51:52 +0100
63535b
Subject: mksquashfs: fix phys mem calculation for 32-bit processes on
63535b
 PAE/64-bit kernels
63535b
63535b
When adding the code to base default memory usage on physical memory
63535b
(by default use 25% of physical memory), I made an oversight.  I assumed
63535b
the process would be able to address 25% of physical memory.
63535b
63535b
However, for 32-bit processes running on a PAE kernel or 64-bit kernel,
63535b
25% of physical memory can easily exceed the addressible memory for a
63535b
32-bit process, e.g. if a machine has 24 GB of physical memory, the
63535b
code would asume the process could easily use 6 GB.
63535b
63535b
A 32-bit process by definition can only address 4 GB (32-bit pointers).
63535b
But, due to the typical kernel/user-space split (1GB/3GB, or 2GB/2GB)
63535b
on PAE kernels, a 32-bit process may only be able to address 2 GB.
63535b
63535b
So, if Mksquashfs is a 32-bit application running on a PAE/64-bit kernel,
63535b
the code assumes it can address much more memory than it really can, which
63535b
means it runs out of memory.
63535b
63535b
The fix is to impose a maximum default limit on 32-bit kernels, or
63535b
otherwise to never use a value more than 25% of the address space.  If
63535b
we assume the maximum address space is 2 GB, then the maximum becomes
63535b
512 MB.  But, given most kernels used the 1GB/3GB split, that may be
63535b
unduely conservative, and 25% of 3 GB (756 MB) may be better.  This
63535b
patch compromises on 640 MB, which is mid-way between the 512 MB and 756 MB
63535b
values.  It is also the fixed default value previously used by Mksquashfs.
63535b
63535b
This patch also alters the code which imposes a maximum size.  Previously
63535b
it was believed limiting to the physical memory size was adequate.  But
63535b
obviously this needs to be updated to take into account a 32-bit process
63535b
may only be able to address 2 GB.  In the process I've also taken the
63535b
opportunity to limit all requests to no more than 75% of physical memory.
63535b
63535b
Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
63535b
63535b
diff --git a/squashfs-tools/mksquashfs.c b/squashfs-tools/mksquashfs.c
63535b
index 86f82bb..5370ecf 100644
63535b
--- a/squashfs-tools/mksquashfs.c
63535b
+++ b/squashfs-tools/mksquashfs.c
63535b
@@ -304,7 +304,7 @@ void restorefs();
63535b
 struct dir_info *scan1_opendir(char *pathname, char *subpath, int depth);
63535b
 void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad);
63535b
 unsigned short get_checksum_mem(char *buff, int bytes);
63535b
-int get_physical_memory();
63535b
+void check_usable_phys_mem(int total_mem);
63535b
 
63535b
 
63535b
 void prep_exit()
63535b
@@ -4053,11 +4053,7 @@ void initialise_threads(int readq, int fragq, int bwriteq, int fwriteq,
63535b
 		BAD_ERROR("Queue sizes rediculously too large\n");
63535b
 	total_mem += fwriteq;
63535b
 
63535b
-	if(total_mem > get_physical_memory()) {
63535b
-		ERROR("Total queue sizes larger than physical memory.\n");
63535b
-		ERROR("Mksquashfs will exhaust physical memory and thrash.\n");
63535b
-		BAD_ERROR("Queues too large\n");
63535b
-	}
63535b
+	check_usable_phys_mem(total_mem);
63535b
 
63535b
 	/*
63535b
 	 * convert from queue size in Mbytes to queue size in
63535b
@@ -4879,6 +4875,72 @@ int get_physical_memory()
63535b
 }
63535b
 
63535b
 
63535b
+void check_usable_phys_mem(int total_mem)
63535b
+{
63535b
+	/*
63535b
+	 * We want to allow users to use as much of their physical
63535b
+	 * memory as they wish.  However, for practical reasons there are
63535b
+	 * limits which need to be imposed, to protect users from themselves
63535b
+	 * and to prevent people from using Mksquashfs as a DOS attack by using
63535b
+	 * all physical memory.   Mksquashfs uses memory to cache data from disk
63535b
+	 * to optimise performance.  It is pointless to ask it to use more
63535b
+	 * than 75% of physical memory, as this causes thrashing and it is thus
63535b
+	 * self-defeating.
63535b
+	 */
63535b
+	int mem = get_physical_memory();
63535b
+
63535b
+	mem = (mem >> 1) + (mem >> 2); /* 75% */
63535b
+						
63535b
+	if(total_mem > mem) {
63535b
+		ERROR("Total memory requested is more than 75%% of physical "
63535b
+						"memory.\n");
63535b
+		ERROR("Mksquashfs uses memory to cache data from disk to "
63535b
+						"optimise performance.\n");
63535b
+		ERROR("It is pointless to ask it to use more than this amount "
63535b
+						"of memory, as this\n");
63535b
+		ERROR("causes thrashing and it is thus self-defeating.\n");
63535b
+		BAD_ERROR("Requested memory size too large\n");
63535b
+	}
63535b
+
63535b
+	if(sizeof(void *) == 4 && total_mem > 2048) {
63535b
+		/*
63535b
+		 * If we're running on a kernel with PAE or on a 64-bit kernel,
63535b
+		 * then the 75% physical memory limit can still easily exceed
63535b
+		 * the addressable memory by this process.
63535b
+		 *
63535b
+		 * Due to the typical kernel/user-space split (1GB/3GB, or
63535b
+		 * 2GB/2GB), we have to conservatively assume the 32-bit
63535b
+		 * processes can only address 2-3GB.  So refuse if the user
63535b
+		 * tries to allocate more than 2GB.
63535b
+		 */
63535b
+		ERROR("Total memory requested may exceed maximum "
63535b
+				"addressable memory by this process\n");
63535b
+		BAD_ERROR("Requested memory size too large\n");
63535b
+	}
63535b
+}
63535b
+
63535b
+
63535b
+int get_default_phys_mem()
63535b
+{
63535b
+	int mem = get_physical_memory() / SQUASHFS_TAKE;
63535b
+
63535b
+	if(sizeof(void *) == 4 && mem > 640) {
63535b
+		/*
63535b
+		 * If we're running on a kernel with PAE or on a 64-bit kernel,
63535b
+		 * the default memory usage can exceed the addressable
63535b
+		 * memory by this process.
63535b
+		 * Due to the typical kernel/user-space split (1GB/3GB, or
63535b
+		 * 2GB/2GB), we have to conservatively assume the 32-bit
63535b
+		 * processes can only address 2-3GB.  So limit the  default
63535b
+		 * usage to 640M, which gives room for other data.
63535b
+		 */
63535b
+		mem = 640;
63535b
+	}
63535b
+
63535b
+	return mem;
63535b
+}
63535b
+
63535b
+
63535b
 void calculate_queue_sizes(int mem, int *readq, int *fragq, int *bwriteq,
63535b
 							int *fwriteq)
63535b
 {
63535b
@@ -4890,7 +4952,7 @@ void calculate_queue_sizes(int mem, int *readq, int *fragq, int *bwriteq,
63535b
 
63535b
 
63535b
 #define VERSION() \
63535b
-	printf("mksquashfs version 4.3 (2014/05/12)\n");\
63535b
+	printf("mksquashfs version 4.3-git (2014/06/09)\n");\
63535b
 	printf("copyright (C) 2014 Phillip Lougher "\
63535b
 		"<phillip@squashfs.org.uk>\n\n"); \
63535b
 	printf("This program is free software; you can redistribute it and/or"\
63535b
@@ -4918,7 +4980,7 @@ int main(int argc, char *argv[])
63535b
 	int fragq;
63535b
 	int bwriteq;
63535b
 	int fwriteq;
63535b
-	int total_mem = get_physical_memory() / SQUASHFS_TAKE;
63535b
+	int total_mem = get_default_phys_mem();
63535b
 	int progress = TRUE;
63535b
 	int force_progress = FALSE;
63535b
 	struct file_buffer **fragment = NULL;
63535b
-- 
63535b
cgit v0.10.1
63535b