Skip to content

Commit

Permalink
Fix the handling of memory allocation for tmpfs.
Browse files Browse the repository at this point in the history
If the total memory on the system is less then the max memory in cgroup
then do not set memory limit on tmpfs, allow the kernel to set the default.
  • Loading branch information
rhatdan committed Dec 20, 2016
1 parent 45455fe commit e15e812
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/systemdhook.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <sys/mount.h>
#include <syslog.h>
#include <sys/stat.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sched.h>
Expand All @@ -21,6 +22,14 @@

#include <libmount/libmount.h>

static unsigned long get_mem_total() {
struct sysinfo info;
int ret = sysinfo(&info);
if (ret < 0) {
return ret;
}
return info.totalram;
}

#define _cleanup_(x) __attribute__((cleanup(x)))

Expand Down Expand Up @@ -487,15 +496,23 @@ static int prestart(const char *rootfs,

pr_pdebug("LIMIT: %s\n", memory_limit_str);

char memory_str[PATH_MAX];
uint64_t total_memory = 0;
uint64_t memory_limit_in_bytes = 0;
char *ptr = NULL;

memory_limit_in_bytes = strtoull(memory_limit_str, &ptr, 10);

pr_pdebug("Limit in bytes: ""%" PRIu64 "\n", memory_limit_in_bytes);

/* Set it to half of limit in kb */
uint64_t memory_limit_in_kb = memory_limit_in_bytes / 2048;
total_memory = get_mem_total();
if (memory_limit_in_bytes < total_memory) {
/* Set it to half of limit in kb */
uint64_t memory_limit_in_kb = memory_limit_in_bytes / 2048;
snprintf(memory_str, sizeof(memory_str)-1 , "size=%" PRIu64 "k", memory_limit_in_kb);
} else {
strcpy(memory_str, "");
}

char tmp_dir[PATH_MAX];
snprintf(tmp_dir, PATH_MAX, "%s/tmp", rootfs);
Expand All @@ -510,9 +527,9 @@ static int prestart(const char *rootfs,
}

if (!strcmp("", mount_label)) {
rc = asprintf(&options, "mode=1777,size=%" PRIu64 "k", memory_limit_in_kb);
rc = asprintf(&options, "mode=1777%s", memory_str);
} else {
rc = asprintf(&options, "mode=1777,size=%" PRIu64 "k,context=\"%s\"", memory_limit_in_kb, mount_label);
rc = asprintf(&options, "mode=1777%s,context=\"%s\"", memory_str, mount_label);
}
if (rc < 0) {
pr_perror("Failed to allocate memory for context");
Expand Down

0 comments on commit e15e812

Please sign in to comment.