From cc8edc07fae6130d4057e3a5f376e88f67995dea Mon Sep 17 00:00:00 2001
From: Morris Jette <jette@schedmd.com>
Date: Thu, 13 Apr 2017 11:55:20 -0600
Subject: [PATCH] When running the "scontrol top" command, make sure that all
 of the user's jobs have a priority that is lower than the selected job.
 Previous logic would permit other jobs with equal priority (no jobs with
 higher priority).

Bug 3650
---
 NEWS                    | 3 +++
 src/slurmctld/job_mgr.c | 7 +++++--
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/NEWS b/NEWS
index 1385e9055f0..fe5f6757033 100644
--- a/NEWS
+++ b/NEWS
@@ -10,6 +10,9 @@ documents those changes that are of interest to users and administrators.
     array element per line.
  -- Reset backfill timers correctly without skipping over them in certain
     circumstances.
+ -- When running the "scontrol top" command, make sure that all of the user's
+    jobs have a priority that is lower than the selected job. Previous logic
+    would permit other jobs with equal priority (no jobs with higher priority).
 
 * Changes in Slurm 17.02.2
 ==========================
diff --git a/src/slurmctld/job_mgr.c b/src/slurmctld/job_mgr.c
index 4e11338d276..edeb4dce01b 100644
--- a/src/slurmctld/job_mgr.c
+++ b/src/slurmctld/job_mgr.c
@@ -14833,7 +14833,7 @@ static int _set_top(struct job_record *job_ptr, uid_t uid)
 	list_iterator_destroy(job_iterator);
 
 	/* Now adjust nice values and priorities of effected jobs */
-	if (high_prio > job_ptr->priority) {
+	if (high_prio >= job_ptr->priority) {
 		delta_nice = high_prio - job_ptr->priority;
 		delta_nice = MIN(job_ptr->details->nice, delta_nice);
 		job_ptr->priority += delta_nice;
@@ -14841,13 +14841,16 @@ static int _set_top(struct job_record *job_ptr, uid_t uid)
 		for (i = 0; i < high_prio_job_cnt; i++) {
 			adj_prio = delta_nice / (high_prio_job_cnt - i);
 			job_test_ptr = job_adj_list[i];
+			if (job_test_ptr->priority == high_prio)
+				adj_prio = MAX(adj_prio, 1);  /* ensure lower */
 			adj_prio = MIN((job_test_ptr->priority - 1), adj_prio);
 			max_delta = (uint32_t) 0xffffffff -
 				    job_test_ptr->details->nice;
 			adj_prio = MIN(max_delta, adj_prio);
 			job_test_ptr->priority -= adj_prio;
 			job_test_ptr->details->nice += adj_prio;
-			delta_nice -= adj_prio;
+			if (delta_nice >= adj_prio)
+				delta_nice -= adj_prio;
 		}
 		last_job_update = time(NULL);
 	}
-- 
GitLab