From 49a7d7f9fb9d554c3f51a33bc5de3bb3e9249a35 Mon Sep 17 00:00:00 2001
From: Danny Auble <da@schedmd.com>
Date: Thu, 5 Nov 2020 13:09:42 -0700
Subject: [PATCH] Prevent a job from requesting too much memory if it requests
 MEM_PER_CPUS and --threads-per-core < the number of threads on a core.

Bug 9724
---
 NEWS                                      |  3 +++
 src/plugins/select/cons_common/job_test.c | 17 ++++++++++++++++-
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index 0890a62c803..6f356557f2d 100644
--- a/NEWS
+++ b/NEWS
@@ -13,6 +13,9 @@ documents those changes that are of interest to users and administrators.
  -- Enforce invalid argument combinations with --ntasks-per-gpu
  -- slurmrestd/auth_local - Verify username on slurm_rest_auth_p_apply()
  -- Fix requeue of job on node failure.
+ -- Prevent a job from requesting too much memory if it
+    requests MEM_PER_CPUS and --threads-per-core < the number of threads
+    on a core.
 
 * Changes in Slurm 20.11.0rc2
 ==============================
diff --git a/src/plugins/select/cons_common/job_test.c b/src/plugins/select/cons_common/job_test.c
index 6879c244748..3d990ef22e7 100644
--- a/src/plugins/select/cons_common/job_test.c
+++ b/src/plugins/select/cons_common/job_test.c
@@ -1607,7 +1607,22 @@ alloc_job:
 			avail_mem = select_node_record[i].real_memory -
 				select_node_record[i].mem_spec_limit;
 			if (save_mem & MEM_PER_CPU) {	/* Memory per CPU */
-				needed_mem = job_res->cpus[j] *
+				uint16_t cpu_count = job_res->cpus[j];
+				/*
+				 * If the job requested less threads that we
+				 * allocated but requested memory based on cpu
+				 * count we would need to adjust that to avoid
+				 * getting more memory than we are actually
+				 * expecting.
+				 */
+				if (job_ptr->details->mc_ptr->threads_per_core <
+				    select_node_record[i].vpus) {
+					cpu_count /= select_node_record[i].vpus;
+					cpu_count *= job_ptr->details->
+						mc_ptr->threads_per_core;
+				}
+
+				needed_mem = cpu_count *
 					(save_mem & (~MEM_PER_CPU));
 			} else if (save_mem) {		/* Memory per node */
 				needed_mem = save_mem;
-- 
GitLab