Blob Blame History Raw
From a7cc1c068ba979a35bad3f4a21e1161379725c61 Mon Sep 17 00:00:00 2001
Message-Id: <a7cc1c068ba979a35bad3f4a21e1161379725c61@dist-git>
From: Martin Kletzander <mkletzan@redhat.com>
Date: Wed, 31 Jan 2018 16:32:42 +0100
Subject: [PATCH] util: Don't overwrite mask in virResctrlAllocFindUnused

Due to confusing naming the pointer to the mask got copied which must not
happen, so use UpdateMask instead of SetMask.  That also means we can get
completely rid of SetMask.

Also don't clear the free bits since it is not used again (leftover from
previous versions).

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
(cherry picked from commit 859186091cc6348e48cdad37c635fb3342e68189)
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>

https://bugzilla.redhat.com/show_bug.cgi?id=1289368
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
---
 src/util/virresctrl.c | 41 ++++++++++++++---------------------------
 1 file changed, 14 insertions(+), 27 deletions(-)

diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c
index a0ea274871..fdd1ded6f7 100644
--- a/src/util/virresctrl.c
+++ b/src/util/virresctrl.c
@@ -1253,22 +1253,6 @@ virResctrlAllocGetUnused(virResctrlInfoPtr resctrl ATTRIBUTE_UNUSED)
 
 #endif /* ! __linux__ */
 
-static int
-virResctrlAllocSetMask(virResctrlAllocPerTypePtr a_type,
-                       unsigned int cache,
-                       virBitmapPtr mask)
-{
-    if (a_type->nmasks <= cache &&
-        VIR_EXPAND_N(a_type->masks, a_type->nmasks,
-                     cache - a_type->nmasks + 1) < 0)
-        return -1;
-
-    virBitmapFree(a_type->masks[cache]);
-    a_type->masks[cache] = mask;
-
-    return 0;
-}
-
 
 /*
  * Given the information about requested allocation type `a_type`, the host
@@ -1276,16 +1260,19 @@ virResctrlAllocSetMask(virResctrlAllocPerTypePtr a_type,
  * this function tries to find the smallest free space in which the allocation
  * for cache id `cache` would fit.  We're looking for the smallest place in
  * order to minimize fragmentation and maximize the possibility of succeeding.
+ *
+ * Per-cache allocation for the @level, @type and @cache must already be
+ * allocated for @alloc (does not have to exist though).
  */
 static int
-virResctrlAllocFindUnused(virResctrlAllocPerTypePtr a_type,
+virResctrlAllocFindUnused(virResctrlAllocPtr alloc,
                           virResctrlInfoPerTypePtr i_type,
                           virResctrlAllocPerTypePtr f_type,
                           unsigned int level,
                           unsigned int type,
                           unsigned int cache)
 {
-    unsigned long long *size = a_type->sizes[cache];
+    unsigned long long *size = alloc->levels[level]->types[type]->sizes[cache];
     virBitmapPtr a_mask = NULL;
     virBitmapPtr f_mask = NULL;
     unsigned long long need_bits;
@@ -1293,6 +1280,7 @@ virResctrlAllocFindUnused(virResctrlAllocPerTypePtr a_type,
     ssize_t pos = -1;
     ssize_t last_bits = 0;
     ssize_t last_pos = -1;
+    int ret = -1;
 
     if (!size)
         return 0;
@@ -1384,17 +1372,16 @@ virResctrlAllocFindUnused(virResctrlAllocPerTypePtr a_type,
     if (!a_mask)
         return -1;
 
-    for (i = last_pos; i < last_pos + need_bits; i++) {
+    for (i = last_pos; i < last_pos + need_bits; i++)
         ignore_value(virBitmapSetBit(a_mask, i));
-        ignore_value(virBitmapClearBit(f_mask, i));
-    }
 
-    if (virResctrlAllocSetMask(a_type, cache, a_mask) < 0) {
-        virBitmapFree(a_mask);
-        return -1;
-    }
+    if (virResctrlAllocUpdateMask(alloc, level, type, cache, a_mask) < 0)
+        goto cleanup;
 
-    return 0;
+    ret = 0;
+ cleanup:
+    virBitmapFree(a_mask);
+    return ret;
 }
 
 
@@ -1500,7 +1487,7 @@ virResctrlAllocMasksAssign(virResctrlInfoPtr resctrl,
                 virResctrlInfoPerLevelPtr i_level = resctrl->levels[level];
                 virResctrlInfoPerTypePtr i_type = i_level->types[type];
 
-                if (virResctrlAllocFindUnused(a_type, i_type, f_type, level, type, cache) < 0)
+                if (virResctrlAllocFindUnused(alloc, i_type, f_type, level, type, cache) < 0)
                     goto cleanup;
             }
         }
-- 
2.16.1