c1c534
From a7cc1c068ba979a35bad3f4a21e1161379725c61 Mon Sep 17 00:00:00 2001
c1c534
Message-Id: <a7cc1c068ba979a35bad3f4a21e1161379725c61@dist-git>
c1c534
From: Martin Kletzander <mkletzan@redhat.com>
c1c534
Date: Wed, 31 Jan 2018 16:32:42 +0100
c1c534
Subject: [PATCH] util: Don't overwrite mask in virResctrlAllocFindUnused
c1c534
c1c534
Due to confusing naming the pointer to the mask got copied which must not
c1c534
happen, so use UpdateMask instead of SetMask.  That also means we can get
c1c534
completely rid of SetMask.
c1c534
c1c534
Also don't clear the free bits since it is not used again (leftover from
c1c534
previous versions).
c1c534
c1c534
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
c1c534
(cherry picked from commit 859186091cc6348e48cdad37c635fb3342e68189)
c1c534
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
c1c534
c1c534
https://bugzilla.redhat.com/show_bug.cgi?id=1289368
c1c534
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
c1c534
---
c1c534
 src/util/virresctrl.c | 41 ++++++++++++++---------------------------
c1c534
 1 file changed, 14 insertions(+), 27 deletions(-)
c1c534
c1c534
diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c
c1c534
index a0ea274871..fdd1ded6f7 100644
c1c534
--- a/src/util/virresctrl.c
c1c534
+++ b/src/util/virresctrl.c
c1c534
@@ -1253,22 +1253,6 @@ virResctrlAllocGetUnused(virResctrlInfoPtr resctrl ATTRIBUTE_UNUSED)
c1c534
 
c1c534
 #endif /* ! __linux__ */
c1c534
 
c1c534
-static int
c1c534
-virResctrlAllocSetMask(virResctrlAllocPerTypePtr a_type,
c1c534
-                       unsigned int cache,
c1c534
-                       virBitmapPtr mask)
c1c534
-{
c1c534
-    if (a_type->nmasks <= cache &&
c1c534
-        VIR_EXPAND_N(a_type->masks, a_type->nmasks,
c1c534
-                     cache - a_type->nmasks + 1) < 0)
c1c534
-        return -1;
c1c534
-
c1c534
-    virBitmapFree(a_type->masks[cache]);
c1c534
-    a_type->masks[cache] = mask;
c1c534
-
c1c534
-    return 0;
c1c534
-}
c1c534
-
c1c534
 
c1c534
 /*
c1c534
  * Given the information about requested allocation type `a_type`, the host
c1c534
@@ -1276,16 +1260,19 @@ virResctrlAllocSetMask(virResctrlAllocPerTypePtr a_type,
c1c534
  * this function tries to find the smallest free space in which the allocation
c1c534
  * for cache id `cache` would fit.  We're looking for the smallest place in
c1c534
  * order to minimize fragmentation and maximize the possibility of succeeding.
c1c534
+ *
c1c534
+ * Per-cache allocation for the @level, @type and @cache must already be
c1c534
+ * allocated for @alloc (does not have to exist though).
c1c534
  */
c1c534
 static int
c1c534
-virResctrlAllocFindUnused(virResctrlAllocPerTypePtr a_type,
c1c534
+virResctrlAllocFindUnused(virResctrlAllocPtr alloc,
c1c534
                           virResctrlInfoPerTypePtr i_type,
c1c534
                           virResctrlAllocPerTypePtr f_type,
c1c534
                           unsigned int level,
c1c534
                           unsigned int type,
c1c534
                           unsigned int cache)
c1c534
 {
c1c534
-    unsigned long long *size = a_type->sizes[cache];
c1c534
+    unsigned long long *size = alloc->levels[level]->types[type]->sizes[cache];
c1c534
     virBitmapPtr a_mask = NULL;
c1c534
     virBitmapPtr f_mask = NULL;
c1c534
     unsigned long long need_bits;
c1c534
@@ -1293,6 +1280,7 @@ virResctrlAllocFindUnused(virResctrlAllocPerTypePtr a_type,
c1c534
     ssize_t pos = -1;
c1c534
     ssize_t last_bits = 0;
c1c534
     ssize_t last_pos = -1;
c1c534
+    int ret = -1;
c1c534
 
c1c534
     if (!size)
c1c534
         return 0;
c1c534
@@ -1384,17 +1372,16 @@ virResctrlAllocFindUnused(virResctrlAllocPerTypePtr a_type,
c1c534
     if (!a_mask)
c1c534
         return -1;
c1c534
 
c1c534
-    for (i = last_pos; i < last_pos + need_bits; i++) {
c1c534
+    for (i = last_pos; i < last_pos + need_bits; i++)
c1c534
         ignore_value(virBitmapSetBit(a_mask, i));
c1c534
-        ignore_value(virBitmapClearBit(f_mask, i));
c1c534
-    }
c1c534
 
c1c534
-    if (virResctrlAllocSetMask(a_type, cache, a_mask) < 0) {
c1c534
-        virBitmapFree(a_mask);
c1c534
-        return -1;
c1c534
-    }
c1c534
+    if (virResctrlAllocUpdateMask(alloc, level, type, cache, a_mask) < 0)
c1c534
+        goto cleanup;
c1c534
 
c1c534
-    return 0;
c1c534
+    ret = 0;
c1c534
+ cleanup:
c1c534
+    virBitmapFree(a_mask);
c1c534
+    return ret;
c1c534
 }
c1c534
 
c1c534
 
c1c534
@@ -1500,7 +1487,7 @@ virResctrlAllocMasksAssign(virResctrlInfoPtr resctrl,
c1c534
                 virResctrlInfoPerLevelPtr i_level = resctrl->levels[level];
c1c534
                 virResctrlInfoPerTypePtr i_type = i_level->types[type];
c1c534
 
c1c534
-                if (virResctrlAllocFindUnused(a_type, i_type, f_type, level, type, cache) < 0)
c1c534
+                if (virResctrlAllocFindUnused(alloc, i_type, f_type, level, type, cache) < 0)
c1c534
                     goto cleanup;
c1c534
             }
c1c534
         }
c1c534
-- 
c1c534
2.16.1
c1c534