From e1bb16897caa75857b130e25e8b088148178dfb4 Mon Sep 17 00:00:00 2001
From: "Mark A. Grondona" <mgrondona@llnl.gov>
Date: Sat, 1 Oct 2011 13:15:47 -0700
Subject: [PATCH] task/cgroup: Don't create memory cgroups with limit of 0
 bytes

Treat a 0 byte memory limit from SLURM as unlimited and instead use
MaxRAMPercent and MaxSwapPercent as RAM and Swap limits for the job/job
step. This avoids creating a memory cgroup with limit_in_bytes = 0,
which would end up causing the cgroup to OOM before slurmstepd could
even be started.

This also allows systems in which SLURM isn't explicitly allocating
memory to use the task/cgroup plugin with ConstrainRAMSpace=yes.
---
 src/plugins/task/cgroup/task_cgroup_memory.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/src/plugins/task/cgroup/task_cgroup_memory.c b/src/plugins/task/cgroup/task_cgroup_memory.c
index a9fe3a16611..1db40e636c9 100644
--- a/src/plugins/task/cgroup/task_cgroup_memory.c
+++ b/src/plugins/task/cgroup/task_cgroup_memory.c
@@ -193,7 +193,15 @@ extern int task_cgroup_memory_fini(slurm_cgroup_conf_t *slurm_cgroup_conf)
  */
 static uint64_t mem_limit_in_bytes (uint64_t mem)
 {
-	mem = percent_in_bytes (mem, allowed_ram_space);
+	/* 
+	 *  If mem == 0 then assume there was no SLURM limit imposed
+	 *   on the amount of memory for job or step. Use the total
+	 *   amount of available RAM instead.
+	 */
+	if (mem == 0)
+		mem = totalram * 1024 * 1024;
+	else
+		mem = percent_in_bytes (mem, allowed_ram_space);
 	return ((mem < max_ram) ? mem : max_ram);
 }
 
@@ -203,12 +211,15 @@ static uint64_t mem_limit_in_bytes (uint64_t mem)
  *   Swap limit is calculated as:
  *
  *     mem_limit_in_bytes + (configured_swap_percent * allocated_mem_in_bytes)
- *       or equivalently:
- *     available_mem * ((configured_swap_percent + configured_mem_percent)/100)
  */
 static uint64_t swap_limit_in_bytes (uint64_t mem)
 {
-	mem *= ((allowed_ram_space + allowed_swap_space)/100.0) * 1024 * 1024;
+	uint64_t swap;
+	/*
+	 *  If mem == 0 assume "unlimited" and use totalram.
+	 */
+	swap = percent_in_bytes (mem ? mem : totalram, allowed_swap_space);
+	mem = mem_limit_in_bytes (mem) + swap;
 	return ((mem < max_swap) ? mem : max_swap);
 }
 
-- 
GitLab