slurmctld - Prevent reserved gres from being used by the wrong jobs

Before it was possible for the gres in a reservation to be used by a job
not requesting the reservation. This was because reservations reserve a
specific gres bit, but sock_gres->bits_any_sock's bitmap still included all
the gres bits, even those allocated to reservations. Additionally, before
it was possible for a job requesting the reservation to be allocated a gres
bit that was not reserved due to the same reason.

This modifies _handle_gres_exc_bit_and_not(), now renamed to
_handle_gres_exc_bit_restrict(), so that now in the case of
resv_exc_ptr->gres_js_inc the bits_by_sock bitmap is restricted to only
gres bits in the reservation. This function is now also called on
sock_gres->bits_any_sock in the case no core topology is defined, which
can be the case for gres with a type defined.

Ticket: 21862
Changelog: slurmctld - Fix how gres with cores or a type defined are
 selected to prevent jobs not using reservations from being allocated
 reserved gres and vice versa.
diff --git a/src/plugins/select/cons_tres/gres_sock_list.c b/src/plugins/select/cons_tres/gres_sock_list.c
index 9502a8e..ebdc42f 100644
--- a/src/plugins/select/cons_tres/gres_sock_list.c
+++ b/src/plugins/select/cons_tres/gres_sock_list.c
@@ -181,21 +181,25 @@
 	return;
 }
 
-static void _handle_gres_exc_bit_and_not(resv_exc_t *resv_exc_ptr,
-					 bitstr_t *bits_by_sock, int node_inx)
+static void _handle_gres_exc_bit_restrict(resv_exc_t *resv_exc_ptr,
+					  bitstr_t *bits_by_sock, int node_inx)
 {
 	gres_job_state_t *gres_js;
 
 	if (!resv_exc_ptr)
 		return;
 
-	gres_js = resv_exc_ptr->gres_js_exc;
+	gres_js = resv_exc_ptr->gres_js_exc ? resv_exc_ptr->gres_js_exc :
+					      resv_exc_ptr->gres_js_inc;
 
 	if (!gres_js || !gres_js->gres_bit_alloc ||
 	    !gres_js->gres_bit_alloc[node_inx])
 		return;
 
-	bit_and_not(bits_by_sock, gres_js->gres_bit_alloc[node_inx]);
+	if (resv_exc_ptr->gres_js_exc) /* use only not reserved bits */
+		bit_and_not(bits_by_sock, gres_js->gres_bit_alloc[node_inx]);
+	else /* resv_exc_ptr->gres_js_inc - use only reserved bits */
+		bit_and(bits_by_sock, gres_js->gres_bit_alloc[node_inx]);
 
 	return;
 }
@@ -374,6 +378,11 @@
 				bit_or(sock_gres->bits_any_sock,
 				       gres_ns->topo_gres_bitmap[i]);
 			}
+
+			_handle_gres_exc_bit_restrict(resv_exc_ptr,
+						      sock_gres->bits_any_sock,
+						      create_args->node_inx);
+
 			match = true;
 			continue;
 		}
@@ -411,7 +420,7 @@
 					       gres_ns->topo_gres_bitmap[i]);
 				}
 
-				_handle_gres_exc_bit_and_not(
+				_handle_gres_exc_bit_restrict(
 					resv_exc_ptr,
 					sock_gres->bits_by_sock[s],
 					create_args->node_inx);