[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v20210601 34/38] tools: add API for expandable bitmaps


  • To: xen-devel@xxxxxxxxxxxxxxxxxxxx
  • From: Olaf Hering <olaf@xxxxxxxxx>
  • Date: Tue, 1 Jun 2021 18:11:14 +0200
  • Arc-authentication-results: i=1; strato.com; dkim=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1622563900; s=strato-dkim-0002; d=strato.com; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=aRD7fh9RvdtKI1U0FLi3WzkFwBE/k6ib8HdQNOs79bo=; b=CIv1T1c91pfhV264VbVU4AxCLfH/LvZWqdJQcBFiJCZHW5urMgZuWY1CLjYTyF1NQl O91kPoTN2t3D7GXTRt7aHLpCJ8odBdAuxINJfPGHJ/VlrinYWy6ySdGY9XhN2K0mZGBM dRlYR5Ag5RF1sBEVOSPEfvoXfU9EvRTIO84ARVie5QFWDVZpMhNBJMLsuob7IDFnvMMQ /zaM8SuQ9GjIPS8Cnl1ID47+N4tm3vG+ry3dy9lcW0rDGPkYUzqnGJRx7A1y5fOzlH3j rz56GRF2gcTq2u16zS2IftFX2rjYBhmXbxJpJ5atRMNchqD9OHGOPxoR2FByjaQlBkNY zRyQ==
  • Arc-seal: i=1; a=rsa-sha256; t=1622563900; cv=none; d=strato.com; s=strato-dkim-0002; b=K0Zx3LsjNuvmX6bb/7E8krJY0h3wiRmnd1Iea4Mn7/bvNh47ZNrEJFxkHe2izUzrXe +rwp5gHhr76tMqvWqJqJd0y99aZgsM1/xnRbTcSuzWUJ5v7wOW6hkW1DPvyBbnRHohOj Gytiyp7Ext008MkAWiOs9qNNV1ggeTwyfZfJ4AIIUU2Ui53tNvRSbSXvck/jX6UxB821 DFrU/IziJg6ICyDO8Mzq8XYVk654MmPq0I4es24ug7WoGeG4MHRfCSQLesL9fvbOR064 UyNcAiZhCOwxrVL8iNU5DyrRU6I0eIOBZq2uhCsR7jbJdT+SB1uzngSKYIrNM3zTjI4z kjSg==
  • Authentication-results: strato.com; dkim=none
  • Cc: Olaf Hering <olaf@xxxxxxxxx>, Ian Jackson <iwj@xxxxxxxxxxxxxx>, Wei Liu <wl@xxxxxxx>
  • Delivery-date: Tue, 01 Jun 2021 16:25:38 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

Since the incoming migration stream lacks info about what the highest pfn
will be, data structures can not be allocated upfront.

Add an API for expandable bitmaps, loosely based on pfn_set_populated.

Signed-off-by: Olaf Hering <olaf@xxxxxxxxx>
---
 tools/libs/saverestore/common.c | 40 ++++++++++++++++++++
 tools/libs/saverestore/common.h | 67 +++++++++++++++++++++++++++++++++
 2 files changed, 107 insertions(+)

diff --git a/tools/libs/saverestore/common.c b/tools/libs/saverestore/common.c
index 7da7fa4e2c..9f1af4e671 100644
--- a/tools/libs/saverestore/common.c
+++ b/tools/libs/saverestore/common.c
@@ -163,6 +163,46 @@ static void __attribute__((unused)) build_assertions(void)
     BUILD_BUG_ON(sizeof(struct xc_sr_rec_hvm_params)        != 8);
 }
 
+/*
+ * Expand the tracking structures as needed.
+ * To avoid realloc()ing too excessively, the size increased to the nearest
+ * power of two large enough to contain the required number of bits.
+ */
+bool _xg_sr_bitmap_expand(struct xg_sr_bitmap *bm, unsigned long bits)
+{
+    size_t new_max;
+    size_t old_sz, new_sz;
+    void *p;
+
+    if (bits <= bm->bits)
+        return true;
+
+    /* Round up to the nearest power of two larger than bit, less 1. */
+    new_max = bits;
+    new_max |= new_max >> 1;
+    new_max |= new_max >> 2;
+    new_max |= new_max >> 4;
+    new_max |= new_max >> 8;
+    new_max |= new_max >> 16;
+    if ( sizeof(unsigned long) > 4 )
+        new_max |= new_max >> 32;
+
+    /* Allocate units of unsigned long */
+    new_max = (new_max + BITS_PER_LONG - 1) & ~(BITS_PER_LONG - 1);
+
+    old_sz = bitmap_size(bm->bits);
+    new_sz = bitmap_size(new_max);
+    p = realloc(bm->p, new_sz);
+    if (!p)
+        return false;
+
+    memset(p + old_sz, 0, new_sz - old_sz);
+    bm->p = p;
+    bm->bits = new_max;
+
+    return true;
+}
+
 /*
  * Local variables:
  * mode: C
diff --git a/tools/libs/saverestore/common.h b/tools/libs/saverestore/common.h
index cf8d6545e2..5241e50f5e 100644
--- a/tools/libs/saverestore/common.h
+++ b/tools/libs/saverestore/common.h
@@ -30,6 +30,73 @@ const char *rec_type_to_str(uint32_t type);
 struct xc_sr_context;
 struct xc_sr_record;
 
+struct xg_sr_bitmap
+{
+    void *p;
+    unsigned long bits;
+};
+
+extern bool _xg_sr_bitmap_expand(struct xg_sr_bitmap *bm, unsigned long bits);
+
+static inline bool xg_sr_bitmap_expand(struct xg_sr_bitmap *bm, unsigned long 
bits)
+{
+    if (bits > bm->bits)
+        return _xg_sr_bitmap_expand(bm, bits);
+    return true;
+}
+
+static inline void xg_sr_bitmap_free(struct xg_sr_bitmap *bm)
+{
+    free(bm->p);
+    bm->p = NULL;
+}
+
+static inline bool xg_sr_set_bit(unsigned long bit, struct xg_sr_bitmap *bm)
+{
+    if (xg_sr_bitmap_expand(bm, bit) == false)
+        return false;
+
+    set_bit(bit, bm->p);
+    return true;
+}
+
+static inline bool xg_sr_test_bit(unsigned long bit, struct xg_sr_bitmap *bm)
+{
+    if (bit > bm->bits)
+        return false;
+    return !!test_bit(bit, bm->p);
+}
+
+static inline void xg_sr_clear_bit(unsigned long bit, struct xg_sr_bitmap *bm)
+{
+    if (bit <= bm->bits)
+        clear_bit(bit, bm->p);
+}
+
+static inline bool xg_sr_test_and_clear_bit(unsigned long bit, struct 
xg_sr_bitmap *bm)
+{
+    if (bit > bm->bits)
+        return false;
+    return !!test_and_clear_bit(bit, bm->p);
+}
+
+/* No way to report potential allocation error, bitmap must be expanded prior 
usage */
+static inline bool xg_sr_test_and_set_bit(unsigned long bit, struct 
xg_sr_bitmap *bm)
+{
+    if (bit > bm->bits)
+        return false;
+    return !!test_and_set_bit(bit, bm->p);
+}
+
+static inline bool xg_sr_set_long_bit(unsigned long base_bit, struct 
xg_sr_bitmap *bm)
+{
+    if (xg_sr_bitmap_expand(bm, base_bit + BITS_PER_LONG) == false)
+        return false;
+
+    set_bit_long(base_bit, bm->p);
+    return true;
+}
+
 /**
  * Save operations.  To be implemented for each type of guest, for use by the
  * common save algorithm.



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.