xref: /openbmc/qemu/util/hbitmap.c (revision 299ea9ff01a8452dd14a042d700d8651370f5314)
1e7c033c3SPaolo Bonzini /*
2e7c033c3SPaolo Bonzini  * Hierarchical Bitmap Data Type
3e7c033c3SPaolo Bonzini  *
4e7c033c3SPaolo Bonzini  * Copyright Red Hat, Inc., 2012
5e7c033c3SPaolo Bonzini  *
6e7c033c3SPaolo Bonzini  * Author: Paolo Bonzini <pbonzini@redhat.com>
7e7c033c3SPaolo Bonzini  *
8e7c033c3SPaolo Bonzini  * This work is licensed under the terms of the GNU GPL, version 2 or
9e7c033c3SPaolo Bonzini  * later.  See the COPYING file in the top-level directory.
10e7c033c3SPaolo Bonzini  */
11e7c033c3SPaolo Bonzini 
12e7c033c3SPaolo Bonzini #include "qemu/osdep.h"
13e7c033c3SPaolo Bonzini #include "qemu/hbitmap.h"
14e7c033c3SPaolo Bonzini #include "qemu/host-utils.h"
15e7c033c3SPaolo Bonzini #include "trace.h"
16a3b52535SVladimir Sementsov-Ogievskiy #include "crypto/hash.h"
17e7c033c3SPaolo Bonzini 
18e7c033c3SPaolo Bonzini /* HBitmaps provides an array of bits.  The bits are stored as usual in an
19e7c033c3SPaolo Bonzini  * array of unsigned longs, but HBitmap is also optimized to provide fast
20e7c033c3SPaolo Bonzini  * iteration over set bits; going from one bit to the next is O(logB n)
21e7c033c3SPaolo Bonzini  * worst case, with B = sizeof(long) * CHAR_BIT: the result is low enough
22e7c033c3SPaolo Bonzini  * that the number of levels is in fact fixed.
23e7c033c3SPaolo Bonzini  *
24e7c033c3SPaolo Bonzini  * In order to do this, it stacks multiple bitmaps with progressively coarser
25e7c033c3SPaolo Bonzini  * granularity; in all levels except the last, bit N is set iff the N-th
26e7c033c3SPaolo Bonzini  * unsigned long is nonzero in the immediately next level.  When iteration
27e7c033c3SPaolo Bonzini  * completes on the last level it can examine the 2nd-last level to quickly
28e7c033c3SPaolo Bonzini  * skip entire words, and even do so recursively to skip blocks of 64 words or
29e7c033c3SPaolo Bonzini  * powers thereof (32 on 32-bit machines).
30e7c033c3SPaolo Bonzini  *
31e7c033c3SPaolo Bonzini  * Given an index in the bitmap, it can be split in group of bits like
32e7c033c3SPaolo Bonzini  * this (for the 64-bit case):
33e7c033c3SPaolo Bonzini  *
34e7c033c3SPaolo Bonzini  *   bits 0-57 => word in the last bitmap     | bits 58-63 => bit in the word
35e7c033c3SPaolo Bonzini  *   bits 0-51 => word in the 2nd-last bitmap | bits 52-57 => bit in the word
36e7c033c3SPaolo Bonzini  *   bits 0-45 => word in the 3rd-last bitmap | bits 46-51 => bit in the word
37e7c033c3SPaolo Bonzini  *
38e7c033c3SPaolo Bonzini  * So it is easy to move up simply by shifting the index right by
39e7c033c3SPaolo Bonzini  * log2(BITS_PER_LONG) bits.  To move down, you shift the index left
40e7c033c3SPaolo Bonzini  * similarly, and add the word index within the group.  Iteration uses
41e7c033c3SPaolo Bonzini  * ffs (find first set bit) to find the next word to examine; this
42e7c033c3SPaolo Bonzini  * operation can be done in constant time in most current architectures.
43e7c033c3SPaolo Bonzini  *
44e7c033c3SPaolo Bonzini  * Setting or clearing a range of m bits on all levels, the work to perform
45e7c033c3SPaolo Bonzini  * is O(m + m/W + m/W^2 + ...), which is O(m) like on a regular bitmap.
46e7c033c3SPaolo Bonzini  *
47e7c033c3SPaolo Bonzini  * When iterating on a bitmap, each bit (on any level) is only visited
48e7c033c3SPaolo Bonzini  * once.  Hence, The total cost of visiting a bitmap with m bits in it is
49e7c033c3SPaolo Bonzini  * the number of bits that are set in all bitmaps.  Unless the bitmap is
50e7c033c3SPaolo Bonzini  * extremely sparse, this is also O(m + m/W + m/W^2 + ...), so the amortized
51e7c033c3SPaolo Bonzini  * cost of advancing from one bit to the next is usually constant (worst case
52e7c033c3SPaolo Bonzini  * O(logB n) as in the non-amortized complexity).
53e7c033c3SPaolo Bonzini  */
54e7c033c3SPaolo Bonzini 
55e7c033c3SPaolo Bonzini struct HBitmap {
564e4de222SVladimir Sementsov-Ogievskiy     /*
574e4de222SVladimir Sementsov-Ogievskiy      * Size of the bitmap, as requested in hbitmap_alloc or in hbitmap_truncate.
584e4de222SVladimir Sementsov-Ogievskiy      */
5976d570dcSVladimir Sementsov-Ogievskiy     uint64_t orig_size;
6076d570dcSVladimir Sementsov-Ogievskiy 
61e7c033c3SPaolo Bonzini     /* Number of total bits in the bottom level.  */
62e7c033c3SPaolo Bonzini     uint64_t size;
63e7c033c3SPaolo Bonzini 
64e7c033c3SPaolo Bonzini     /* Number of set bits in the bottom level.  */
65e7c033c3SPaolo Bonzini     uint64_t count;
66e7c033c3SPaolo Bonzini 
67e7c033c3SPaolo Bonzini     /* A scaling factor.  Given a granularity of G, each bit in the bitmap will
68e7c033c3SPaolo Bonzini      * will actually represent a group of 2^G elements.  Each operation on a
69e7c033c3SPaolo Bonzini      * range of bits first rounds the bits to determine which group they land
70e7c033c3SPaolo Bonzini      * in, and then affect the entire page; iteration will only visit the first
71e7c033c3SPaolo Bonzini      * bit of each group.  Here is an example of operations in a size-16,
72e7c033c3SPaolo Bonzini      * granularity-1 HBitmap:
73e7c033c3SPaolo Bonzini      *
74e7c033c3SPaolo Bonzini      *    initial state            00000000
75e7c033c3SPaolo Bonzini      *    set(start=0, count=9)    11111000 (iter: 0, 2, 4, 6, 8)
76e7c033c3SPaolo Bonzini      *    reset(start=1, count=3)  00111000 (iter: 4, 6, 8)
77e7c033c3SPaolo Bonzini      *    set(start=9, count=2)    00111100 (iter: 4, 6, 8, 10)
78e7c033c3SPaolo Bonzini      *    reset(start=5, count=5)  00000000
79e7c033c3SPaolo Bonzini      *
80e7c033c3SPaolo Bonzini      * From an implementation point of view, when setting or resetting bits,
81e7c033c3SPaolo Bonzini      * the bitmap will scale bit numbers right by this amount of bits.  When
82e7c033c3SPaolo Bonzini      * iterating, the bitmap will scale bit numbers left by this amount of
83e7c033c3SPaolo Bonzini      * bits.
84e7c033c3SPaolo Bonzini      */
85e7c033c3SPaolo Bonzini     int granularity;
86e7c033c3SPaolo Bonzini 
8707ac4cdbSFam Zheng     /* A meta dirty bitmap to track the dirtiness of bits in this HBitmap. */
8807ac4cdbSFam Zheng     HBitmap *meta;
8907ac4cdbSFam Zheng 
90e7c033c3SPaolo Bonzini     /* A number of progressively less coarse bitmaps (i.e. level 0 is the
91e7c033c3SPaolo Bonzini      * coarsest).  Each bit in level N represents a word in level N+1 that
92e7c033c3SPaolo Bonzini      * has a set bit, except the last level where each bit represents the
93e7c033c3SPaolo Bonzini      * actual bitmap.
94e7c033c3SPaolo Bonzini      *
95e7c033c3SPaolo Bonzini      * Note that all bitmaps have the same number of levels.  Even a 1-bit
96e7c033c3SPaolo Bonzini      * bitmap will still allocate HBITMAP_LEVELS arrays.
97e7c033c3SPaolo Bonzini      */
98e7c033c3SPaolo Bonzini     unsigned long *levels[HBITMAP_LEVELS];
998515efbeSJohn Snow 
1008515efbeSJohn Snow     /* The length of each levels[] array. */
1018515efbeSJohn Snow     uint64_t sizes[HBITMAP_LEVELS];
102e7c033c3SPaolo Bonzini };
103e7c033c3SPaolo Bonzini 
104e7c033c3SPaolo Bonzini /* Advance hbi to the next nonzero word and return it.  hbi->pos
105e7c033c3SPaolo Bonzini  * is updated.  Returns zero if we reach the end of the bitmap.
106e7c033c3SPaolo Bonzini  */
10730b8346cSVladimir Sementsov-Ogievskiy static unsigned long hbitmap_iter_skip_words(HBitmapIter *hbi)
108e7c033c3SPaolo Bonzini {
109e7c033c3SPaolo Bonzini     size_t pos = hbi->pos;
110e7c033c3SPaolo Bonzini     const HBitmap *hb = hbi->hb;
111e7c033c3SPaolo Bonzini     unsigned i = HBITMAP_LEVELS - 1;
112e7c033c3SPaolo Bonzini 
113e7c033c3SPaolo Bonzini     unsigned long cur;
114e7c033c3SPaolo Bonzini     do {
115f63ea4e9SVladimir Sementsov-Ogievskiy         i--;
116e7c033c3SPaolo Bonzini         pos >>= BITS_PER_LEVEL;
117f63ea4e9SVladimir Sementsov-Ogievskiy         cur = hbi->cur[i] & hb->levels[i][pos];
118e7c033c3SPaolo Bonzini     } while (cur == 0);
119e7c033c3SPaolo Bonzini 
120e7c033c3SPaolo Bonzini     /* Check for end of iteration.  We always use fewer than BITS_PER_LONG
121e7c033c3SPaolo Bonzini      * bits in the level 0 bitmap; thus we can repurpose the most significant
122e7c033c3SPaolo Bonzini      * bit as a sentinel.  The sentinel is set in hbitmap_alloc and ensures
123e7c033c3SPaolo Bonzini      * that the above loop ends even without an explicit check on i.
124e7c033c3SPaolo Bonzini      */
125e7c033c3SPaolo Bonzini 
126e7c033c3SPaolo Bonzini     if (i == 0 && cur == (1UL << (BITS_PER_LONG - 1))) {
127e7c033c3SPaolo Bonzini         return 0;
128e7c033c3SPaolo Bonzini     }
129e7c033c3SPaolo Bonzini     for (; i < HBITMAP_LEVELS - 1; i++) {
130e7c033c3SPaolo Bonzini         /* Shift back pos to the left, matching the right shifts above.
131e7c033c3SPaolo Bonzini          * The index of this word's least significant set bit provides
132e7c033c3SPaolo Bonzini          * the low-order bits.
133e7c033c3SPaolo Bonzini          */
13418331e7cSRichard Henderson         assert(cur);
13518331e7cSRichard Henderson         pos = (pos << BITS_PER_LEVEL) + ctzl(cur);
136e7c033c3SPaolo Bonzini         hbi->cur[i] = cur & (cur - 1);
137e7c033c3SPaolo Bonzini 
138e7c033c3SPaolo Bonzini         /* Set up next level for iteration.  */
139e7c033c3SPaolo Bonzini         cur = hb->levels[i + 1][pos];
140e7c033c3SPaolo Bonzini     }
141e7c033c3SPaolo Bonzini 
142e7c033c3SPaolo Bonzini     hbi->pos = pos;
143e7c033c3SPaolo Bonzini     trace_hbitmap_iter_skip_words(hbi->hb, hbi, pos, cur);
144e7c033c3SPaolo Bonzini 
145e7c033c3SPaolo Bonzini     assert(cur);
146e7c033c3SPaolo Bonzini     return cur;
147e7c033c3SPaolo Bonzini }
148e7c033c3SPaolo Bonzini 
14919c021e1SVladimir Sementsov-Ogievskiy int64_t hbitmap_iter_next(HBitmapIter *hbi)
150f63ea4e9SVladimir Sementsov-Ogievskiy {
151f63ea4e9SVladimir Sementsov-Ogievskiy     unsigned long cur = hbi->cur[HBITMAP_LEVELS - 1] &
152f63ea4e9SVladimir Sementsov-Ogievskiy             hbi->hb->levels[HBITMAP_LEVELS - 1][hbi->pos];
153f63ea4e9SVladimir Sementsov-Ogievskiy     int64_t item;
154f63ea4e9SVladimir Sementsov-Ogievskiy 
155f63ea4e9SVladimir Sementsov-Ogievskiy     if (cur == 0) {
156f63ea4e9SVladimir Sementsov-Ogievskiy         cur = hbitmap_iter_skip_words(hbi);
157f63ea4e9SVladimir Sementsov-Ogievskiy         if (cur == 0) {
158f63ea4e9SVladimir Sementsov-Ogievskiy             return -1;
159f63ea4e9SVladimir Sementsov-Ogievskiy         }
160f63ea4e9SVladimir Sementsov-Ogievskiy     }
161f63ea4e9SVladimir Sementsov-Ogievskiy 
162f63ea4e9SVladimir Sementsov-Ogievskiy     /* The next call will resume work from the next bit.  */
163f63ea4e9SVladimir Sementsov-Ogievskiy     hbi->cur[HBITMAP_LEVELS - 1] = cur & (cur - 1);
164f63ea4e9SVladimir Sementsov-Ogievskiy     item = ((uint64_t)hbi->pos << BITS_PER_LEVEL) + ctzl(cur);
165f63ea4e9SVladimir Sementsov-Ogievskiy 
166f63ea4e9SVladimir Sementsov-Ogievskiy     return item << hbi->granularity;
167f63ea4e9SVladimir Sementsov-Ogievskiy }
168f63ea4e9SVladimir Sementsov-Ogievskiy 
169e7c033c3SPaolo Bonzini void hbitmap_iter_init(HBitmapIter *hbi, const HBitmap *hb, uint64_t first)
170e7c033c3SPaolo Bonzini {
171e7c033c3SPaolo Bonzini     unsigned i, bit;
172e7c033c3SPaolo Bonzini     uint64_t pos;
173e7c033c3SPaolo Bonzini 
174e7c033c3SPaolo Bonzini     hbi->hb = hb;
175e7c033c3SPaolo Bonzini     pos = first >> hb->granularity;
1761b095244SPaolo Bonzini     assert(pos < hb->size);
177e7c033c3SPaolo Bonzini     hbi->pos = pos >> BITS_PER_LEVEL;
178e7c033c3SPaolo Bonzini     hbi->granularity = hb->granularity;
179e7c033c3SPaolo Bonzini 
180e7c033c3SPaolo Bonzini     for (i = HBITMAP_LEVELS; i-- > 0; ) {
181e7c033c3SPaolo Bonzini         bit = pos & (BITS_PER_LONG - 1);
182e7c033c3SPaolo Bonzini         pos >>= BITS_PER_LEVEL;
183e7c033c3SPaolo Bonzini 
184e7c033c3SPaolo Bonzini         /* Drop bits representing items before first.  */
185e7c033c3SPaolo Bonzini         hbi->cur[i] = hb->levels[i][pos] & ~((1UL << bit) - 1);
186e7c033c3SPaolo Bonzini 
187e7c033c3SPaolo Bonzini         /* We have already added level i+1, so the lowest set bit has
188e7c033c3SPaolo Bonzini          * been processed.  Clear it.
189e7c033c3SPaolo Bonzini          */
190e7c033c3SPaolo Bonzini         if (i != HBITMAP_LEVELS - 1) {
191e7c033c3SPaolo Bonzini             hbi->cur[i] &= ~(1UL << bit);
192e7c033c3SPaolo Bonzini         }
193e7c033c3SPaolo Bonzini     }
194e7c033c3SPaolo Bonzini }
195e7c033c3SPaolo Bonzini 
1969399c54bSVladimir Sementsov-Ogievskiy int64_t hbitmap_next_dirty(const HBitmap *hb, int64_t start, int64_t count)
1979399c54bSVladimir Sementsov-Ogievskiy {
1989399c54bSVladimir Sementsov-Ogievskiy     HBitmapIter hbi;
1999399c54bSVladimir Sementsov-Ogievskiy     int64_t first_dirty_off;
2009399c54bSVladimir Sementsov-Ogievskiy     uint64_t end;
2019399c54bSVladimir Sementsov-Ogievskiy 
2029399c54bSVladimir Sementsov-Ogievskiy     assert(start >= 0 && count >= 0);
2039399c54bSVladimir Sementsov-Ogievskiy 
2049399c54bSVladimir Sementsov-Ogievskiy     if (start >= hb->orig_size || count == 0) {
2059399c54bSVladimir Sementsov-Ogievskiy         return -1;
2069399c54bSVladimir Sementsov-Ogievskiy     }
2079399c54bSVladimir Sementsov-Ogievskiy 
2089399c54bSVladimir Sementsov-Ogievskiy     end = count > hb->orig_size - start ? hb->orig_size : start + count;
2099399c54bSVladimir Sementsov-Ogievskiy 
2109399c54bSVladimir Sementsov-Ogievskiy     hbitmap_iter_init(&hbi, hb, start);
2119399c54bSVladimir Sementsov-Ogievskiy     first_dirty_off = hbitmap_iter_next(&hbi);
2129399c54bSVladimir Sementsov-Ogievskiy 
2139399c54bSVladimir Sementsov-Ogievskiy     if (first_dirty_off < 0 || first_dirty_off >= end) {
2149399c54bSVladimir Sementsov-Ogievskiy         return -1;
2159399c54bSVladimir Sementsov-Ogievskiy     }
2169399c54bSVladimir Sementsov-Ogievskiy 
2179399c54bSVladimir Sementsov-Ogievskiy     return MAX(start, first_dirty_off);
2189399c54bSVladimir Sementsov-Ogievskiy }
2199399c54bSVladimir Sementsov-Ogievskiy 
220642700fdSVladimir Sementsov-Ogievskiy int64_t hbitmap_next_zero(const HBitmap *hb, int64_t start, int64_t count)
22156207df5SVladimir Sementsov-Ogievskiy {
22256207df5SVladimir Sementsov-Ogievskiy     size_t pos = (start >> hb->granularity) >> BITS_PER_LEVEL;
22356207df5SVladimir Sementsov-Ogievskiy     unsigned long *last_lev = hb->levels[HBITMAP_LEVELS - 1];
22456207df5SVladimir Sementsov-Ogievskiy     unsigned long cur = last_lev[pos];
22576d570dcSVladimir Sementsov-Ogievskiy     unsigned start_bit_offset;
22676d570dcSVladimir Sementsov-Ogievskiy     uint64_t end_bit, sz;
22756207df5SVladimir Sementsov-Ogievskiy     int64_t res;
22856207df5SVladimir Sementsov-Ogievskiy 
229642700fdSVladimir Sementsov-Ogievskiy     assert(start >= 0 && count >= 0);
230642700fdSVladimir Sementsov-Ogievskiy 
23176d570dcSVladimir Sementsov-Ogievskiy     if (start >= hb->orig_size || count == 0) {
23276d570dcSVladimir Sementsov-Ogievskiy         return -1;
23376d570dcSVladimir Sementsov-Ogievskiy     }
23476d570dcSVladimir Sementsov-Ogievskiy 
23576d570dcSVladimir Sementsov-Ogievskiy     end_bit = count > hb->orig_size - start ?
23676d570dcSVladimir Sementsov-Ogievskiy                 hb->size :
23776d570dcSVladimir Sementsov-Ogievskiy                 ((start + count - 1) >> hb->granularity) + 1;
23876d570dcSVladimir Sementsov-Ogievskiy     sz = (end_bit + BITS_PER_LONG - 1) >> BITS_PER_LEVEL;
23976d570dcSVladimir Sementsov-Ogievskiy 
24076d570dcSVladimir Sementsov-Ogievskiy     /* There may be some zero bits in @cur before @start. We are not interested
24176d570dcSVladimir Sementsov-Ogievskiy      * in them, let's set them.
24276d570dcSVladimir Sementsov-Ogievskiy      */
24376d570dcSVladimir Sementsov-Ogievskiy     start_bit_offset = (start >> hb->granularity) & (BITS_PER_LONG - 1);
24456207df5SVladimir Sementsov-Ogievskiy     cur |= (1UL << start_bit_offset) - 1;
24556207df5SVladimir Sementsov-Ogievskiy     assert((start >> hb->granularity) < hb->size);
24656207df5SVladimir Sementsov-Ogievskiy 
24756207df5SVladimir Sementsov-Ogievskiy     if (cur == (unsigned long)-1) {
24856207df5SVladimir Sementsov-Ogievskiy         do {
24956207df5SVladimir Sementsov-Ogievskiy             pos++;
25056207df5SVladimir Sementsov-Ogievskiy         } while (pos < sz && last_lev[pos] == (unsigned long)-1);
25156207df5SVladimir Sementsov-Ogievskiy 
25256207df5SVladimir Sementsov-Ogievskiy         if (pos >= sz) {
25356207df5SVladimir Sementsov-Ogievskiy             return -1;
25456207df5SVladimir Sementsov-Ogievskiy         }
25556207df5SVladimir Sementsov-Ogievskiy 
25656207df5SVladimir Sementsov-Ogievskiy         cur = last_lev[pos];
25756207df5SVladimir Sementsov-Ogievskiy     }
25856207df5SVladimir Sementsov-Ogievskiy 
25956207df5SVladimir Sementsov-Ogievskiy     res = (pos << BITS_PER_LEVEL) + ctol(cur);
26076d570dcSVladimir Sementsov-Ogievskiy     if (res >= end_bit) {
26156207df5SVladimir Sementsov-Ogievskiy         return -1;
26256207df5SVladimir Sementsov-Ogievskiy     }
26356207df5SVladimir Sementsov-Ogievskiy 
26456207df5SVladimir Sementsov-Ogievskiy     res = res << hb->granularity;
26556207df5SVladimir Sementsov-Ogievskiy     if (res < start) {
26656207df5SVladimir Sementsov-Ogievskiy         assert(((start - res) >> hb->granularity) == 0);
26756207df5SVladimir Sementsov-Ogievskiy         return start;
26856207df5SVladimir Sementsov-Ogievskiy     }
26956207df5SVladimir Sementsov-Ogievskiy 
27056207df5SVladimir Sementsov-Ogievskiy     return res;
27156207df5SVladimir Sementsov-Ogievskiy }
27256207df5SVladimir Sementsov-Ogievskiy 
273*299ea9ffSVladimir Sementsov-Ogievskiy bool hbitmap_next_dirty_area(const HBitmap *hb, int64_t start, int64_t end,
274*299ea9ffSVladimir Sementsov-Ogievskiy                              int64_t max_dirty_count,
275*299ea9ffSVladimir Sementsov-Ogievskiy                              int64_t *dirty_start, int64_t *dirty_count)
276a78a1a48SVladimir Sementsov-Ogievskiy {
277*299ea9ffSVladimir Sementsov-Ogievskiy     int64_t next_zero;
278a78a1a48SVladimir Sementsov-Ogievskiy 
279*299ea9ffSVladimir Sementsov-Ogievskiy     assert(start >= 0 && end >= 0 && max_dirty_count > 0);
280*299ea9ffSVladimir Sementsov-Ogievskiy 
281*299ea9ffSVladimir Sementsov-Ogievskiy     end = MIN(end, hb->orig_size);
282*299ea9ffSVladimir Sementsov-Ogievskiy     if (start >= end) {
283a78a1a48SVladimir Sementsov-Ogievskiy         return false;
284a78a1a48SVladimir Sementsov-Ogievskiy     }
285a78a1a48SVladimir Sementsov-Ogievskiy 
286*299ea9ffSVladimir Sementsov-Ogievskiy     start = hbitmap_next_dirty(hb, start, end - start);
287*299ea9ffSVladimir Sementsov-Ogievskiy     if (start < 0) {
288*299ea9ffSVladimir Sementsov-Ogievskiy         return false;
289a78a1a48SVladimir Sementsov-Ogievskiy     }
290a78a1a48SVladimir Sementsov-Ogievskiy 
291*299ea9ffSVladimir Sementsov-Ogievskiy     end = start + MIN(end - start, max_dirty_count);
292*299ea9ffSVladimir Sementsov-Ogievskiy 
293*299ea9ffSVladimir Sementsov-Ogievskiy     next_zero = hbitmap_next_zero(hb, start, end - start);
294*299ea9ffSVladimir Sementsov-Ogievskiy     if (next_zero >= 0) {
295*299ea9ffSVladimir Sementsov-Ogievskiy         end = next_zero;
296*299ea9ffSVladimir Sementsov-Ogievskiy     }
297*299ea9ffSVladimir Sementsov-Ogievskiy 
298*299ea9ffSVladimir Sementsov-Ogievskiy     *dirty_start = start;
299*299ea9ffSVladimir Sementsov-Ogievskiy     *dirty_count = end - start;
300a78a1a48SVladimir Sementsov-Ogievskiy 
301a78a1a48SVladimir Sementsov-Ogievskiy     return true;
302a78a1a48SVladimir Sementsov-Ogievskiy }
303a78a1a48SVladimir Sementsov-Ogievskiy 
304e7c033c3SPaolo Bonzini bool hbitmap_empty(const HBitmap *hb)
305e7c033c3SPaolo Bonzini {
306e7c033c3SPaolo Bonzini     return hb->count == 0;
307e7c033c3SPaolo Bonzini }
308e7c033c3SPaolo Bonzini 
309e7c033c3SPaolo Bonzini int hbitmap_granularity(const HBitmap *hb)
310e7c033c3SPaolo Bonzini {
311e7c033c3SPaolo Bonzini     return hb->granularity;
312e7c033c3SPaolo Bonzini }
313e7c033c3SPaolo Bonzini 
314e7c033c3SPaolo Bonzini uint64_t hbitmap_count(const HBitmap *hb)
315e7c033c3SPaolo Bonzini {
316e7c033c3SPaolo Bonzini     return hb->count << hb->granularity;
317e7c033c3SPaolo Bonzini }
318e7c033c3SPaolo Bonzini 
319be24c714SVladimir Sementsov-Ogievskiy /**
320be24c714SVladimir Sementsov-Ogievskiy  * hbitmap_iter_next_word:
321be24c714SVladimir Sementsov-Ogievskiy  * @hbi: HBitmapIter to operate on.
322be24c714SVladimir Sementsov-Ogievskiy  * @p_cur: Location where to store the next non-zero word.
323be24c714SVladimir Sementsov-Ogievskiy  *
324be24c714SVladimir Sementsov-Ogievskiy  * Return the index of the next nonzero word that is set in @hbi's
325be24c714SVladimir Sementsov-Ogievskiy  * associated HBitmap, and set *p_cur to the content of that word
326be24c714SVladimir Sementsov-Ogievskiy  * (bits before the index that was passed to hbitmap_iter_init are
327be24c714SVladimir Sementsov-Ogievskiy  * trimmed on the first call).  Return -1, and set *p_cur to zero,
328be24c714SVladimir Sementsov-Ogievskiy  * if all remaining words are zero.
329be24c714SVladimir Sementsov-Ogievskiy  */
330be24c714SVladimir Sementsov-Ogievskiy static size_t hbitmap_iter_next_word(HBitmapIter *hbi, unsigned long *p_cur)
331be24c714SVladimir Sementsov-Ogievskiy {
332be24c714SVladimir Sementsov-Ogievskiy     unsigned long cur = hbi->cur[HBITMAP_LEVELS - 1];
333be24c714SVladimir Sementsov-Ogievskiy 
334be24c714SVladimir Sementsov-Ogievskiy     if (cur == 0) {
335be24c714SVladimir Sementsov-Ogievskiy         cur = hbitmap_iter_skip_words(hbi);
336be24c714SVladimir Sementsov-Ogievskiy         if (cur == 0) {
337be24c714SVladimir Sementsov-Ogievskiy             *p_cur = 0;
338be24c714SVladimir Sementsov-Ogievskiy             return -1;
339be24c714SVladimir Sementsov-Ogievskiy         }
340be24c714SVladimir Sementsov-Ogievskiy     }
341be24c714SVladimir Sementsov-Ogievskiy 
342be24c714SVladimir Sementsov-Ogievskiy     /* The next call will resume work from the next word.  */
343be24c714SVladimir Sementsov-Ogievskiy     hbi->cur[HBITMAP_LEVELS - 1] = 0;
344be24c714SVladimir Sementsov-Ogievskiy     *p_cur = cur;
345be24c714SVladimir Sementsov-Ogievskiy     return hbi->pos;
346be24c714SVladimir Sementsov-Ogievskiy }
347be24c714SVladimir Sementsov-Ogievskiy 
348e7c033c3SPaolo Bonzini /* Count the number of set bits between start and end, not accounting for
349e7c033c3SPaolo Bonzini  * the granularity.  Also an example of how to use hbitmap_iter_next_word.
350e7c033c3SPaolo Bonzini  */
351e7c033c3SPaolo Bonzini static uint64_t hb_count_between(HBitmap *hb, uint64_t start, uint64_t last)
352e7c033c3SPaolo Bonzini {
353e7c033c3SPaolo Bonzini     HBitmapIter hbi;
354e7c033c3SPaolo Bonzini     uint64_t count = 0;
355e7c033c3SPaolo Bonzini     uint64_t end = last + 1;
356e7c033c3SPaolo Bonzini     unsigned long cur;
357e7c033c3SPaolo Bonzini     size_t pos;
358e7c033c3SPaolo Bonzini 
359e7c033c3SPaolo Bonzini     hbitmap_iter_init(&hbi, hb, start << hb->granularity);
360e7c033c3SPaolo Bonzini     for (;;) {
361e7c033c3SPaolo Bonzini         pos = hbitmap_iter_next_word(&hbi, &cur);
362e7c033c3SPaolo Bonzini         if (pos >= (end >> BITS_PER_LEVEL)) {
363e7c033c3SPaolo Bonzini             break;
364e7c033c3SPaolo Bonzini         }
365591b320aSPeter Maydell         count += ctpopl(cur);
366e7c033c3SPaolo Bonzini     }
367e7c033c3SPaolo Bonzini 
368e7c033c3SPaolo Bonzini     if (pos == (end >> BITS_PER_LEVEL)) {
369e7c033c3SPaolo Bonzini         /* Drop bits representing the END-th and subsequent items.  */
370e7c033c3SPaolo Bonzini         int bit = end & (BITS_PER_LONG - 1);
371e7c033c3SPaolo Bonzini         cur &= (1UL << bit) - 1;
372591b320aSPeter Maydell         count += ctpopl(cur);
373e7c033c3SPaolo Bonzini     }
374e7c033c3SPaolo Bonzini 
375e7c033c3SPaolo Bonzini     return count;
376e7c033c3SPaolo Bonzini }
377e7c033c3SPaolo Bonzini 
378e7c033c3SPaolo Bonzini /* Setting starts at the last layer and propagates up if an element
37907ac4cdbSFam Zheng  * changes.
380e7c033c3SPaolo Bonzini  */
381e7c033c3SPaolo Bonzini static inline bool hb_set_elem(unsigned long *elem, uint64_t start, uint64_t last)
382e7c033c3SPaolo Bonzini {
383e7c033c3SPaolo Bonzini     unsigned long mask;
38407ac4cdbSFam Zheng     unsigned long old;
385e7c033c3SPaolo Bonzini 
386e7c033c3SPaolo Bonzini     assert((last >> BITS_PER_LEVEL) == (start >> BITS_PER_LEVEL));
387e7c033c3SPaolo Bonzini     assert(start <= last);
388e7c033c3SPaolo Bonzini 
389e7c033c3SPaolo Bonzini     mask = 2UL << (last & (BITS_PER_LONG - 1));
390e7c033c3SPaolo Bonzini     mask -= 1UL << (start & (BITS_PER_LONG - 1));
39107ac4cdbSFam Zheng     old = *elem;
392e7c033c3SPaolo Bonzini     *elem |= mask;
39307ac4cdbSFam Zheng     return old != *elem;
394e7c033c3SPaolo Bonzini }
395e7c033c3SPaolo Bonzini 
39607ac4cdbSFam Zheng /* The recursive workhorse (the depth is limited to HBITMAP_LEVELS)...
39707ac4cdbSFam Zheng  * Returns true if at least one bit is changed. */
39807ac4cdbSFam Zheng static bool hb_set_between(HBitmap *hb, int level, uint64_t start,
39907ac4cdbSFam Zheng                            uint64_t last)
400e7c033c3SPaolo Bonzini {
401e7c033c3SPaolo Bonzini     size_t pos = start >> BITS_PER_LEVEL;
402e7c033c3SPaolo Bonzini     size_t lastpos = last >> BITS_PER_LEVEL;
403e7c033c3SPaolo Bonzini     bool changed = false;
404e7c033c3SPaolo Bonzini     size_t i;
405e7c033c3SPaolo Bonzini 
406e7c033c3SPaolo Bonzini     i = pos;
407e7c033c3SPaolo Bonzini     if (i < lastpos) {
408e7c033c3SPaolo Bonzini         uint64_t next = (start | (BITS_PER_LONG - 1)) + 1;
409e7c033c3SPaolo Bonzini         changed |= hb_set_elem(&hb->levels[level][i], start, next - 1);
410e7c033c3SPaolo Bonzini         for (;;) {
411e7c033c3SPaolo Bonzini             start = next;
412e7c033c3SPaolo Bonzini             next += BITS_PER_LONG;
413e7c033c3SPaolo Bonzini             if (++i == lastpos) {
414e7c033c3SPaolo Bonzini                 break;
415e7c033c3SPaolo Bonzini             }
416e7c033c3SPaolo Bonzini             changed |= (hb->levels[level][i] == 0);
417e7c033c3SPaolo Bonzini             hb->levels[level][i] = ~0UL;
418e7c033c3SPaolo Bonzini         }
419e7c033c3SPaolo Bonzini     }
420e7c033c3SPaolo Bonzini     changed |= hb_set_elem(&hb->levels[level][i], start, last);
421e7c033c3SPaolo Bonzini 
422e7c033c3SPaolo Bonzini     /* If there was any change in this layer, we may have to update
423e7c033c3SPaolo Bonzini      * the one above.
424e7c033c3SPaolo Bonzini      */
425e7c033c3SPaolo Bonzini     if (level > 0 && changed) {
426e7c033c3SPaolo Bonzini         hb_set_between(hb, level - 1, pos, lastpos);
427e7c033c3SPaolo Bonzini     }
42807ac4cdbSFam Zheng     return changed;
429e7c033c3SPaolo Bonzini }
430e7c033c3SPaolo Bonzini 
431e7c033c3SPaolo Bonzini void hbitmap_set(HBitmap *hb, uint64_t start, uint64_t count)
432e7c033c3SPaolo Bonzini {
433e7c033c3SPaolo Bonzini     /* Compute range in the last layer.  */
43407ac4cdbSFam Zheng     uint64_t first, n;
435e7c033c3SPaolo Bonzini     uint64_t last = start + count - 1;
436e7c033c3SPaolo Bonzini 
437fed33bd1SVladimir Sementsov-Ogievskiy     if (count == 0) {
438fed33bd1SVladimir Sementsov-Ogievskiy         return;
439fed33bd1SVladimir Sementsov-Ogievskiy     }
440fed33bd1SVladimir Sementsov-Ogievskiy 
441e7c033c3SPaolo Bonzini     trace_hbitmap_set(hb, start, count,
442e7c033c3SPaolo Bonzini                       start >> hb->granularity, last >> hb->granularity);
443e7c033c3SPaolo Bonzini 
44407ac4cdbSFam Zheng     first = start >> hb->granularity;
445e7c033c3SPaolo Bonzini     last >>= hb->granularity;
4460e321191SVladimir Sementsov-Ogievskiy     assert(last < hb->size);
44707ac4cdbSFam Zheng     n = last - first + 1;
448e7c033c3SPaolo Bonzini 
44907ac4cdbSFam Zheng     hb->count += n - hb_count_between(hb, first, last);
45007ac4cdbSFam Zheng     if (hb_set_between(hb, HBITMAP_LEVELS - 1, first, last) &&
45107ac4cdbSFam Zheng         hb->meta) {
45207ac4cdbSFam Zheng         hbitmap_set(hb->meta, start, count);
45307ac4cdbSFam Zheng     }
454e7c033c3SPaolo Bonzini }
455e7c033c3SPaolo Bonzini 
456e7c033c3SPaolo Bonzini /* Resetting works the other way round: propagate up if the new
457e7c033c3SPaolo Bonzini  * value is zero.
458e7c033c3SPaolo Bonzini  */
459e7c033c3SPaolo Bonzini static inline bool hb_reset_elem(unsigned long *elem, uint64_t start, uint64_t last)
460e7c033c3SPaolo Bonzini {
461e7c033c3SPaolo Bonzini     unsigned long mask;
462e7c033c3SPaolo Bonzini     bool blanked;
463e7c033c3SPaolo Bonzini 
464e7c033c3SPaolo Bonzini     assert((last >> BITS_PER_LEVEL) == (start >> BITS_PER_LEVEL));
465e7c033c3SPaolo Bonzini     assert(start <= last);
466e7c033c3SPaolo Bonzini 
467e7c033c3SPaolo Bonzini     mask = 2UL << (last & (BITS_PER_LONG - 1));
468e7c033c3SPaolo Bonzini     mask -= 1UL << (start & (BITS_PER_LONG - 1));
469e7c033c3SPaolo Bonzini     blanked = *elem != 0 && ((*elem & ~mask) == 0);
470e7c033c3SPaolo Bonzini     *elem &= ~mask;
471e7c033c3SPaolo Bonzini     return blanked;
472e7c033c3SPaolo Bonzini }
473e7c033c3SPaolo Bonzini 
47407ac4cdbSFam Zheng /* The recursive workhorse (the depth is limited to HBITMAP_LEVELS)...
47507ac4cdbSFam Zheng  * Returns true if at least one bit is changed. */
47607ac4cdbSFam Zheng static bool hb_reset_between(HBitmap *hb, int level, uint64_t start,
47707ac4cdbSFam Zheng                              uint64_t last)
478e7c033c3SPaolo Bonzini {
479e7c033c3SPaolo Bonzini     size_t pos = start >> BITS_PER_LEVEL;
480e7c033c3SPaolo Bonzini     size_t lastpos = last >> BITS_PER_LEVEL;
481e7c033c3SPaolo Bonzini     bool changed = false;
482e7c033c3SPaolo Bonzini     size_t i;
483e7c033c3SPaolo Bonzini 
484e7c033c3SPaolo Bonzini     i = pos;
485e7c033c3SPaolo Bonzini     if (i < lastpos) {
486e7c033c3SPaolo Bonzini         uint64_t next = (start | (BITS_PER_LONG - 1)) + 1;
487e7c033c3SPaolo Bonzini 
488e7c033c3SPaolo Bonzini         /* Here we need a more complex test than when setting bits.  Even if
489e7c033c3SPaolo Bonzini          * something was changed, we must not blank bits in the upper level
490e7c033c3SPaolo Bonzini          * unless the lower-level word became entirely zero.  So, remove pos
491e7c033c3SPaolo Bonzini          * from the upper-level range if bits remain set.
492e7c033c3SPaolo Bonzini          */
493e7c033c3SPaolo Bonzini         if (hb_reset_elem(&hb->levels[level][i], start, next - 1)) {
494e7c033c3SPaolo Bonzini             changed = true;
495e7c033c3SPaolo Bonzini         } else {
496e7c033c3SPaolo Bonzini             pos++;
497e7c033c3SPaolo Bonzini         }
498e7c033c3SPaolo Bonzini 
499e7c033c3SPaolo Bonzini         for (;;) {
500e7c033c3SPaolo Bonzini             start = next;
501e7c033c3SPaolo Bonzini             next += BITS_PER_LONG;
502e7c033c3SPaolo Bonzini             if (++i == lastpos) {
503e7c033c3SPaolo Bonzini                 break;
504e7c033c3SPaolo Bonzini             }
505e7c033c3SPaolo Bonzini             changed |= (hb->levels[level][i] != 0);
506e7c033c3SPaolo Bonzini             hb->levels[level][i] = 0UL;
507e7c033c3SPaolo Bonzini         }
508e7c033c3SPaolo Bonzini     }
509e7c033c3SPaolo Bonzini 
510e7c033c3SPaolo Bonzini     /* Same as above, this time for lastpos.  */
511e7c033c3SPaolo Bonzini     if (hb_reset_elem(&hb->levels[level][i], start, last)) {
512e7c033c3SPaolo Bonzini         changed = true;
513e7c033c3SPaolo Bonzini     } else {
514e7c033c3SPaolo Bonzini         lastpos--;
515e7c033c3SPaolo Bonzini     }
516e7c033c3SPaolo Bonzini 
517e7c033c3SPaolo Bonzini     if (level > 0 && changed) {
518e7c033c3SPaolo Bonzini         hb_reset_between(hb, level - 1, pos, lastpos);
519e7c033c3SPaolo Bonzini     }
52007ac4cdbSFam Zheng 
52107ac4cdbSFam Zheng     return changed;
52207ac4cdbSFam Zheng 
523e7c033c3SPaolo Bonzini }
524e7c033c3SPaolo Bonzini 
525e7c033c3SPaolo Bonzini void hbitmap_reset(HBitmap *hb, uint64_t start, uint64_t count)
526e7c033c3SPaolo Bonzini {
527e7c033c3SPaolo Bonzini     /* Compute range in the last layer.  */
52807ac4cdbSFam Zheng     uint64_t first;
529e7c033c3SPaolo Bonzini     uint64_t last = start + count - 1;
53048557b13SVladimir Sementsov-Ogievskiy     uint64_t gran = 1ULL << hb->granularity;
53148557b13SVladimir Sementsov-Ogievskiy 
532fed33bd1SVladimir Sementsov-Ogievskiy     if (count == 0) {
533fed33bd1SVladimir Sementsov-Ogievskiy         return;
534fed33bd1SVladimir Sementsov-Ogievskiy     }
535fed33bd1SVladimir Sementsov-Ogievskiy 
53648557b13SVladimir Sementsov-Ogievskiy     assert(QEMU_IS_ALIGNED(start, gran));
53748557b13SVladimir Sementsov-Ogievskiy     assert(QEMU_IS_ALIGNED(count, gran) || (start + count == hb->orig_size));
538e7c033c3SPaolo Bonzini 
539e7c033c3SPaolo Bonzini     trace_hbitmap_reset(hb, start, count,
540e7c033c3SPaolo Bonzini                         start >> hb->granularity, last >> hb->granularity);
541e7c033c3SPaolo Bonzini 
54207ac4cdbSFam Zheng     first = start >> hb->granularity;
543e7c033c3SPaolo Bonzini     last >>= hb->granularity;
5440e321191SVladimir Sementsov-Ogievskiy     assert(last < hb->size);
545e7c033c3SPaolo Bonzini 
54607ac4cdbSFam Zheng     hb->count -= hb_count_between(hb, first, last);
54707ac4cdbSFam Zheng     if (hb_reset_between(hb, HBITMAP_LEVELS - 1, first, last) &&
54807ac4cdbSFam Zheng         hb->meta) {
54907ac4cdbSFam Zheng         hbitmap_set(hb->meta, start, count);
55007ac4cdbSFam Zheng     }
551e7c033c3SPaolo Bonzini }
552e7c033c3SPaolo Bonzini 
553c6a8c328SWen Congyang void hbitmap_reset_all(HBitmap *hb)
554c6a8c328SWen Congyang {
555c6a8c328SWen Congyang     unsigned int i;
556c6a8c328SWen Congyang 
557c6a8c328SWen Congyang     /* Same as hbitmap_alloc() except for memset() instead of malloc() */
558c6a8c328SWen Congyang     for (i = HBITMAP_LEVELS; --i >= 1; ) {
559c6a8c328SWen Congyang         memset(hb->levels[i], 0, hb->sizes[i] * sizeof(unsigned long));
560c6a8c328SWen Congyang     }
561c6a8c328SWen Congyang 
562c6a8c328SWen Congyang     hb->levels[0][0] = 1UL << (BITS_PER_LONG - 1);
563c6a8c328SWen Congyang     hb->count = 0;
564c6a8c328SWen Congyang }
565c6a8c328SWen Congyang 
56620a579deSMax Reitz bool hbitmap_is_serializable(const HBitmap *hb)
56720a579deSMax Reitz {
56820a579deSMax Reitz     /* Every serialized chunk must be aligned to 64 bits so that endianness
56920a579deSMax Reitz      * requirements can be fulfilled on both 64 bit and 32 bit hosts.
570ecbfa281SEric Blake      * We have hbitmap_serialization_align() which converts this
57120a579deSMax Reitz      * alignment requirement from bitmap bits to items covered (e.g. sectors).
57220a579deSMax Reitz      * That value is:
57320a579deSMax Reitz      *    64 << hb->granularity
57420a579deSMax Reitz      * Since this value must not exceed UINT64_MAX, hb->granularity must be
57520a579deSMax Reitz      * less than 58 (== 64 - 6, where 6 is ld(64), i.e. 1 << 6 == 64).
57620a579deSMax Reitz      *
577ecbfa281SEric Blake      * In order for hbitmap_serialization_align() to always return a
57820a579deSMax Reitz      * meaningful value, bitmaps that are to be serialized must have a
57920a579deSMax Reitz      * granularity of less than 58. */
58020a579deSMax Reitz 
58120a579deSMax Reitz     return hb->granularity < 58;
58220a579deSMax Reitz }
58320a579deSMax Reitz 
584e7c033c3SPaolo Bonzini bool hbitmap_get(const HBitmap *hb, uint64_t item)
585e7c033c3SPaolo Bonzini {
586e7c033c3SPaolo Bonzini     /* Compute position and bit in the last layer.  */
587e7c033c3SPaolo Bonzini     uint64_t pos = item >> hb->granularity;
588e7c033c3SPaolo Bonzini     unsigned long bit = 1UL << (pos & (BITS_PER_LONG - 1));
5890e321191SVladimir Sementsov-Ogievskiy     assert(pos < hb->size);
590e7c033c3SPaolo Bonzini 
591e7c033c3SPaolo Bonzini     return (hb->levels[HBITMAP_LEVELS - 1][pos >> BITS_PER_LEVEL] & bit) != 0;
592e7c033c3SPaolo Bonzini }
593e7c033c3SPaolo Bonzini 
594ecbfa281SEric Blake uint64_t hbitmap_serialization_align(const HBitmap *hb)
5958258888eSVladimir Sementsov-Ogievskiy {
59620a579deSMax Reitz     assert(hbitmap_is_serializable(hb));
5976725f887SMax Reitz 
5988258888eSVladimir Sementsov-Ogievskiy     /* Require at least 64 bit granularity to be safe on both 64 bit and 32 bit
5998258888eSVladimir Sementsov-Ogievskiy      * hosts. */
6006725f887SMax Reitz     return UINT64_C(64) << hb->granularity;
6018258888eSVladimir Sementsov-Ogievskiy }
6028258888eSVladimir Sementsov-Ogievskiy 
6038258888eSVladimir Sementsov-Ogievskiy /* Start should be aligned to serialization granularity, chunk size should be
6048258888eSVladimir Sementsov-Ogievskiy  * aligned to serialization granularity too, except for last chunk.
6058258888eSVladimir Sementsov-Ogievskiy  */
6068258888eSVladimir Sementsov-Ogievskiy static void serialization_chunk(const HBitmap *hb,
6078258888eSVladimir Sementsov-Ogievskiy                                 uint64_t start, uint64_t count,
6088258888eSVladimir Sementsov-Ogievskiy                                 unsigned long **first_el, uint64_t *el_count)
6098258888eSVladimir Sementsov-Ogievskiy {
6108258888eSVladimir Sementsov-Ogievskiy     uint64_t last = start + count - 1;
611ecbfa281SEric Blake     uint64_t gran = hbitmap_serialization_align(hb);
6128258888eSVladimir Sementsov-Ogievskiy 
6138258888eSVladimir Sementsov-Ogievskiy     assert((start & (gran - 1)) == 0);
6148258888eSVladimir Sementsov-Ogievskiy     assert((last >> hb->granularity) < hb->size);
6158258888eSVladimir Sementsov-Ogievskiy     if ((last >> hb->granularity) != hb->size - 1) {
6168258888eSVladimir Sementsov-Ogievskiy         assert((count & (gran - 1)) == 0);
6178258888eSVladimir Sementsov-Ogievskiy     }
6188258888eSVladimir Sementsov-Ogievskiy 
6198258888eSVladimir Sementsov-Ogievskiy     start = (start >> hb->granularity) >> BITS_PER_LEVEL;
6208258888eSVladimir Sementsov-Ogievskiy     last = (last >> hb->granularity) >> BITS_PER_LEVEL;
6218258888eSVladimir Sementsov-Ogievskiy 
6228258888eSVladimir Sementsov-Ogievskiy     *first_el = &hb->levels[HBITMAP_LEVELS - 1][start];
6238258888eSVladimir Sementsov-Ogievskiy     *el_count = last - start + 1;
6248258888eSVladimir Sementsov-Ogievskiy }
6258258888eSVladimir Sementsov-Ogievskiy 
6268258888eSVladimir Sementsov-Ogievskiy uint64_t hbitmap_serialization_size(const HBitmap *hb,
6278258888eSVladimir Sementsov-Ogievskiy                                     uint64_t start, uint64_t count)
6288258888eSVladimir Sementsov-Ogievskiy {
6298258888eSVladimir Sementsov-Ogievskiy     uint64_t el_count;
6308258888eSVladimir Sementsov-Ogievskiy     unsigned long *cur;
6318258888eSVladimir Sementsov-Ogievskiy 
6328258888eSVladimir Sementsov-Ogievskiy     if (!count) {
6338258888eSVladimir Sementsov-Ogievskiy         return 0;
6348258888eSVladimir Sementsov-Ogievskiy     }
6358258888eSVladimir Sementsov-Ogievskiy     serialization_chunk(hb, start, count, &cur, &el_count);
6368258888eSVladimir Sementsov-Ogievskiy 
6378258888eSVladimir Sementsov-Ogievskiy     return el_count * sizeof(unsigned long);
6388258888eSVladimir Sementsov-Ogievskiy }
6398258888eSVladimir Sementsov-Ogievskiy 
6408258888eSVladimir Sementsov-Ogievskiy void hbitmap_serialize_part(const HBitmap *hb, uint8_t *buf,
6418258888eSVladimir Sementsov-Ogievskiy                             uint64_t start, uint64_t count)
6428258888eSVladimir Sementsov-Ogievskiy {
6438258888eSVladimir Sementsov-Ogievskiy     uint64_t el_count;
6448258888eSVladimir Sementsov-Ogievskiy     unsigned long *cur, *end;
6458258888eSVladimir Sementsov-Ogievskiy 
6468258888eSVladimir Sementsov-Ogievskiy     if (!count) {
6478258888eSVladimir Sementsov-Ogievskiy         return;
6488258888eSVladimir Sementsov-Ogievskiy     }
6498258888eSVladimir Sementsov-Ogievskiy     serialization_chunk(hb, start, count, &cur, &el_count);
6508258888eSVladimir Sementsov-Ogievskiy     end = cur + el_count;
6518258888eSVladimir Sementsov-Ogievskiy 
6528258888eSVladimir Sementsov-Ogievskiy     while (cur != end) {
6538258888eSVladimir Sementsov-Ogievskiy         unsigned long el =
6548258888eSVladimir Sementsov-Ogievskiy             (BITS_PER_LONG == 32 ? cpu_to_le32(*cur) : cpu_to_le64(*cur));
6558258888eSVladimir Sementsov-Ogievskiy 
6568258888eSVladimir Sementsov-Ogievskiy         memcpy(buf, &el, sizeof(el));
6578258888eSVladimir Sementsov-Ogievskiy         buf += sizeof(el);
6588258888eSVladimir Sementsov-Ogievskiy         cur++;
6598258888eSVladimir Sementsov-Ogievskiy     }
6608258888eSVladimir Sementsov-Ogievskiy }
6618258888eSVladimir Sementsov-Ogievskiy 
6628258888eSVladimir Sementsov-Ogievskiy void hbitmap_deserialize_part(HBitmap *hb, uint8_t *buf,
6638258888eSVladimir Sementsov-Ogievskiy                               uint64_t start, uint64_t count,
6648258888eSVladimir Sementsov-Ogievskiy                               bool finish)
6658258888eSVladimir Sementsov-Ogievskiy {
6668258888eSVladimir Sementsov-Ogievskiy     uint64_t el_count;
6678258888eSVladimir Sementsov-Ogievskiy     unsigned long *cur, *end;
6688258888eSVladimir Sementsov-Ogievskiy 
6698258888eSVladimir Sementsov-Ogievskiy     if (!count) {
6708258888eSVladimir Sementsov-Ogievskiy         return;
6718258888eSVladimir Sementsov-Ogievskiy     }
6728258888eSVladimir Sementsov-Ogievskiy     serialization_chunk(hb, start, count, &cur, &el_count);
6738258888eSVladimir Sementsov-Ogievskiy     end = cur + el_count;
6748258888eSVladimir Sementsov-Ogievskiy 
6758258888eSVladimir Sementsov-Ogievskiy     while (cur != end) {
6768258888eSVladimir Sementsov-Ogievskiy         memcpy(cur, buf, sizeof(*cur));
6778258888eSVladimir Sementsov-Ogievskiy 
6788258888eSVladimir Sementsov-Ogievskiy         if (BITS_PER_LONG == 32) {
6798258888eSVladimir Sementsov-Ogievskiy             le32_to_cpus((uint32_t *)cur);
6808258888eSVladimir Sementsov-Ogievskiy         } else {
6818258888eSVladimir Sementsov-Ogievskiy             le64_to_cpus((uint64_t *)cur);
6828258888eSVladimir Sementsov-Ogievskiy         }
6838258888eSVladimir Sementsov-Ogievskiy 
6848258888eSVladimir Sementsov-Ogievskiy         buf += sizeof(unsigned long);
6858258888eSVladimir Sementsov-Ogievskiy         cur++;
6868258888eSVladimir Sementsov-Ogievskiy     }
6878258888eSVladimir Sementsov-Ogievskiy     if (finish) {
6888258888eSVladimir Sementsov-Ogievskiy         hbitmap_deserialize_finish(hb);
6898258888eSVladimir Sementsov-Ogievskiy     }
6908258888eSVladimir Sementsov-Ogievskiy }
6918258888eSVladimir Sementsov-Ogievskiy 
6928258888eSVladimir Sementsov-Ogievskiy void hbitmap_deserialize_zeroes(HBitmap *hb, uint64_t start, uint64_t count,
6938258888eSVladimir Sementsov-Ogievskiy                                 bool finish)
6948258888eSVladimir Sementsov-Ogievskiy {
6958258888eSVladimir Sementsov-Ogievskiy     uint64_t el_count;
6968258888eSVladimir Sementsov-Ogievskiy     unsigned long *first;
6978258888eSVladimir Sementsov-Ogievskiy 
6988258888eSVladimir Sementsov-Ogievskiy     if (!count) {
6998258888eSVladimir Sementsov-Ogievskiy         return;
7008258888eSVladimir Sementsov-Ogievskiy     }
7018258888eSVladimir Sementsov-Ogievskiy     serialization_chunk(hb, start, count, &first, &el_count);
7028258888eSVladimir Sementsov-Ogievskiy 
7038258888eSVladimir Sementsov-Ogievskiy     memset(first, 0, el_count * sizeof(unsigned long));
7048258888eSVladimir Sementsov-Ogievskiy     if (finish) {
7058258888eSVladimir Sementsov-Ogievskiy         hbitmap_deserialize_finish(hb);
7068258888eSVladimir Sementsov-Ogievskiy     }
7078258888eSVladimir Sementsov-Ogievskiy }
7088258888eSVladimir Sementsov-Ogievskiy 
7096bdc8b71SVladimir Sementsov-Ogievskiy void hbitmap_deserialize_ones(HBitmap *hb, uint64_t start, uint64_t count,
7106bdc8b71SVladimir Sementsov-Ogievskiy                               bool finish)
7116bdc8b71SVladimir Sementsov-Ogievskiy {
7126bdc8b71SVladimir Sementsov-Ogievskiy     uint64_t el_count;
7136bdc8b71SVladimir Sementsov-Ogievskiy     unsigned long *first;
7146bdc8b71SVladimir Sementsov-Ogievskiy 
7156bdc8b71SVladimir Sementsov-Ogievskiy     if (!count) {
7166bdc8b71SVladimir Sementsov-Ogievskiy         return;
7176bdc8b71SVladimir Sementsov-Ogievskiy     }
7186bdc8b71SVladimir Sementsov-Ogievskiy     serialization_chunk(hb, start, count, &first, &el_count);
7196bdc8b71SVladimir Sementsov-Ogievskiy 
7206bdc8b71SVladimir Sementsov-Ogievskiy     memset(first, 0xff, el_count * sizeof(unsigned long));
7216bdc8b71SVladimir Sementsov-Ogievskiy     if (finish) {
7226bdc8b71SVladimir Sementsov-Ogievskiy         hbitmap_deserialize_finish(hb);
7236bdc8b71SVladimir Sementsov-Ogievskiy     }
7246bdc8b71SVladimir Sementsov-Ogievskiy }
7256bdc8b71SVladimir Sementsov-Ogievskiy 
7268258888eSVladimir Sementsov-Ogievskiy void hbitmap_deserialize_finish(HBitmap *bitmap)
7278258888eSVladimir Sementsov-Ogievskiy {
7288258888eSVladimir Sementsov-Ogievskiy     int64_t i, size, prev_size;
7298258888eSVladimir Sementsov-Ogievskiy     int lev;
7308258888eSVladimir Sementsov-Ogievskiy 
7318258888eSVladimir Sementsov-Ogievskiy     /* restore levels starting from penultimate to zero level, assuming
7328258888eSVladimir Sementsov-Ogievskiy      * that the last level is ok */
7338258888eSVladimir Sementsov-Ogievskiy     size = MAX((bitmap->size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1);
7348258888eSVladimir Sementsov-Ogievskiy     for (lev = HBITMAP_LEVELS - 1; lev-- > 0; ) {
7358258888eSVladimir Sementsov-Ogievskiy         prev_size = size;
7368258888eSVladimir Sementsov-Ogievskiy         size = MAX((size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1);
7378258888eSVladimir Sementsov-Ogievskiy         memset(bitmap->levels[lev], 0, size * sizeof(unsigned long));
7388258888eSVladimir Sementsov-Ogievskiy 
7398258888eSVladimir Sementsov-Ogievskiy         for (i = 0; i < prev_size; ++i) {
7408258888eSVladimir Sementsov-Ogievskiy             if (bitmap->levels[lev + 1][i]) {
7418258888eSVladimir Sementsov-Ogievskiy                 bitmap->levels[lev][i >> BITS_PER_LEVEL] |=
7428258888eSVladimir Sementsov-Ogievskiy                     1UL << (i & (BITS_PER_LONG - 1));
7438258888eSVladimir Sementsov-Ogievskiy             }
7448258888eSVladimir Sementsov-Ogievskiy         }
7458258888eSVladimir Sementsov-Ogievskiy     }
7468258888eSVladimir Sementsov-Ogievskiy 
7478258888eSVladimir Sementsov-Ogievskiy     bitmap->levels[0][0] |= 1UL << (BITS_PER_LONG - 1);
7483260cdffSLiang Li     bitmap->count = hb_count_between(bitmap, 0, bitmap->size - 1);
7498258888eSVladimir Sementsov-Ogievskiy }
7508258888eSVladimir Sementsov-Ogievskiy 
751e7c033c3SPaolo Bonzini void hbitmap_free(HBitmap *hb)
752e7c033c3SPaolo Bonzini {
753e7c033c3SPaolo Bonzini     unsigned i;
75407ac4cdbSFam Zheng     assert(!hb->meta);
755e7c033c3SPaolo Bonzini     for (i = HBITMAP_LEVELS; i-- > 0; ) {
756e7c033c3SPaolo Bonzini         g_free(hb->levels[i]);
757e7c033c3SPaolo Bonzini     }
758e7c033c3SPaolo Bonzini     g_free(hb);
759e7c033c3SPaolo Bonzini }
760e7c033c3SPaolo Bonzini 
761e7c033c3SPaolo Bonzini HBitmap *hbitmap_alloc(uint64_t size, int granularity)
762e7c033c3SPaolo Bonzini {
763e1cf5582SMarkus Armbruster     HBitmap *hb = g_new0(struct HBitmap, 1);
764e7c033c3SPaolo Bonzini     unsigned i;
765e7c033c3SPaolo Bonzini 
7666a150995SVladimir Sementsov-Ogievskiy     assert(size <= INT64_MAX);
76776d570dcSVladimir Sementsov-Ogievskiy     hb->orig_size = size;
76876d570dcSVladimir Sementsov-Ogievskiy 
769e7c033c3SPaolo Bonzini     assert(granularity >= 0 && granularity < 64);
770e7c033c3SPaolo Bonzini     size = (size + (1ULL << granularity) - 1) >> granularity;
771e7c033c3SPaolo Bonzini     assert(size <= ((uint64_t)1 << HBITMAP_LOG_MAX_SIZE));
772e7c033c3SPaolo Bonzini 
773e7c033c3SPaolo Bonzini     hb->size = size;
774e7c033c3SPaolo Bonzini     hb->granularity = granularity;
775e7c033c3SPaolo Bonzini     for (i = HBITMAP_LEVELS; i-- > 0; ) {
776e7c033c3SPaolo Bonzini         size = MAX((size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1);
7778515efbeSJohn Snow         hb->sizes[i] = size;
778e1cf5582SMarkus Armbruster         hb->levels[i] = g_new0(unsigned long, size);
779e7c033c3SPaolo Bonzini     }
780e7c033c3SPaolo Bonzini 
781e7c033c3SPaolo Bonzini     /* We necessarily have free bits in level 0 due to the definition
782e7c033c3SPaolo Bonzini      * of HBITMAP_LEVELS, so use one for a sentinel.  This speeds up
783e7c033c3SPaolo Bonzini      * hbitmap_iter_skip_words.
784e7c033c3SPaolo Bonzini      */
785e7c033c3SPaolo Bonzini     assert(size == 1);
786e7c033c3SPaolo Bonzini     hb->levels[0][0] |= 1UL << (BITS_PER_LONG - 1);
787e7c033c3SPaolo Bonzini     return hb;
788e7c033c3SPaolo Bonzini }
789be58721dSJohn Snow 
790ce1ffea8SJohn Snow void hbitmap_truncate(HBitmap *hb, uint64_t size)
791ce1ffea8SJohn Snow {
792ce1ffea8SJohn Snow     bool shrink;
793ce1ffea8SJohn Snow     unsigned i;
794ce1ffea8SJohn Snow     uint64_t num_elements = size;
795ce1ffea8SJohn Snow     uint64_t old;
796ce1ffea8SJohn Snow 
7976a150995SVladimir Sementsov-Ogievskiy     assert(size <= INT64_MAX);
7984e4de222SVladimir Sementsov-Ogievskiy     hb->orig_size = size;
7994e4de222SVladimir Sementsov-Ogievskiy 
800ce1ffea8SJohn Snow     /* Size comes in as logical elements, adjust for granularity. */
801ce1ffea8SJohn Snow     size = (size + (1ULL << hb->granularity) - 1) >> hb->granularity;
802ce1ffea8SJohn Snow     assert(size <= ((uint64_t)1 << HBITMAP_LOG_MAX_SIZE));
803ce1ffea8SJohn Snow     shrink = size < hb->size;
804ce1ffea8SJohn Snow 
805ce1ffea8SJohn Snow     /* bit sizes are identical; nothing to do. */
806ce1ffea8SJohn Snow     if (size == hb->size) {
807ce1ffea8SJohn Snow         return;
808ce1ffea8SJohn Snow     }
809ce1ffea8SJohn Snow 
810ce1ffea8SJohn Snow     /* If we're losing bits, let's clear those bits before we invalidate all of
811ce1ffea8SJohn Snow      * our invariants. This helps keep the bitcount consistent, and will prevent
812ce1ffea8SJohn Snow      * us from carrying around garbage bits beyond the end of the map.
813ce1ffea8SJohn Snow      */
814ce1ffea8SJohn Snow     if (shrink) {
815ce1ffea8SJohn Snow         /* Don't clear partial granularity groups;
816ce1ffea8SJohn Snow          * start at the first full one. */
8176725f887SMax Reitz         uint64_t start = ROUND_UP(num_elements, UINT64_C(1) << hb->granularity);
818ce1ffea8SJohn Snow         uint64_t fix_count = (hb->size << hb->granularity) - start;
819ce1ffea8SJohn Snow 
820ce1ffea8SJohn Snow         assert(fix_count);
821ce1ffea8SJohn Snow         hbitmap_reset(hb, start, fix_count);
822ce1ffea8SJohn Snow     }
823ce1ffea8SJohn Snow 
824ce1ffea8SJohn Snow     hb->size = size;
825ce1ffea8SJohn Snow     for (i = HBITMAP_LEVELS; i-- > 0; ) {
826ce1ffea8SJohn Snow         size = MAX(BITS_TO_LONGS(size), 1);
827ce1ffea8SJohn Snow         if (hb->sizes[i] == size) {
828ce1ffea8SJohn Snow             break;
829ce1ffea8SJohn Snow         }
830ce1ffea8SJohn Snow         old = hb->sizes[i];
831ce1ffea8SJohn Snow         hb->sizes[i] = size;
832ce1ffea8SJohn Snow         hb->levels[i] = g_realloc(hb->levels[i], size * sizeof(unsigned long));
833ce1ffea8SJohn Snow         if (!shrink) {
834ce1ffea8SJohn Snow             memset(&hb->levels[i][old], 0x00,
835ce1ffea8SJohn Snow                    (size - old) * sizeof(*hb->levels[i]));
836ce1ffea8SJohn Snow         }
837ce1ffea8SJohn Snow     }
83807ac4cdbSFam Zheng     if (hb->meta) {
83907ac4cdbSFam Zheng         hbitmap_truncate(hb->meta, hb->size << hb->granularity);
84007ac4cdbSFam Zheng     }
841ce1ffea8SJohn Snow }
842ce1ffea8SJohn Snow 
843fa000f2fSVladimir Sementsov-Ogievskiy bool hbitmap_can_merge(const HBitmap *a, const HBitmap *b)
844fa000f2fSVladimir Sementsov-Ogievskiy {
845c5b40c1fSJohn Snow     return (a->orig_size == b->orig_size);
846c5b40c1fSJohn Snow }
847c5b40c1fSJohn Snow 
848c5b40c1fSJohn Snow /**
849c5b40c1fSJohn Snow  * hbitmap_sparse_merge: performs dst = dst | src
850c5b40c1fSJohn Snow  * works with differing granularities.
851c5b40c1fSJohn Snow  * best used when src is sparsely populated.
852c5b40c1fSJohn Snow  */
853c5b40c1fSJohn Snow static void hbitmap_sparse_merge(HBitmap *dst, const HBitmap *src)
854c5b40c1fSJohn Snow {
855*299ea9ffSVladimir Sementsov-Ogievskiy     int64_t offset;
856*299ea9ffSVladimir Sementsov-Ogievskiy     int64_t count;
857c5b40c1fSJohn Snow 
858*299ea9ffSVladimir Sementsov-Ogievskiy     for (offset = 0;
859*299ea9ffSVladimir Sementsov-Ogievskiy          hbitmap_next_dirty_area(src, offset, src->orig_size, INT64_MAX,
860*299ea9ffSVladimir Sementsov-Ogievskiy                                  &offset, &count);
861*299ea9ffSVladimir Sementsov-Ogievskiy          offset += count)
862*299ea9ffSVladimir Sementsov-Ogievskiy     {
863c5b40c1fSJohn Snow         hbitmap_set(dst, offset, count);
864c5b40c1fSJohn Snow     }
865fa000f2fSVladimir Sementsov-Ogievskiy }
866ce1ffea8SJohn Snow 
867be58721dSJohn Snow /**
8683bde4b01SJohn Snow  * Given HBitmaps A and B, let R := A (BITOR) B.
8693bde4b01SJohn Snow  * Bitmaps A and B will not be modified,
8703bde4b01SJohn Snow  *     except when bitmap R is an alias of A or B.
871be58721dSJohn Snow  *
872be58721dSJohn Snow  * @return true if the merge was successful,
873be58721dSJohn Snow  *         false if it was not attempted.
874be58721dSJohn Snow  */
875fa000f2fSVladimir Sementsov-Ogievskiy bool hbitmap_merge(const HBitmap *a, const HBitmap *b, HBitmap *result)
876be58721dSJohn Snow {
877be58721dSJohn Snow     int i;
878be58721dSJohn Snow     uint64_t j;
879be58721dSJohn Snow 
880fa000f2fSVladimir Sementsov-Ogievskiy     if (!hbitmap_can_merge(a, b) || !hbitmap_can_merge(a, result)) {
881be58721dSJohn Snow         return false;
882be58721dSJohn Snow     }
883fa000f2fSVladimir Sementsov-Ogievskiy     assert(hbitmap_can_merge(b, result));
884be58721dSJohn Snow 
8853bde4b01SJohn Snow     if ((!hbitmap_count(a) && result == b) ||
8863bde4b01SJohn Snow         (!hbitmap_count(b) && result == a)) {
8873bde4b01SJohn Snow         return true;
8883bde4b01SJohn Snow     }
8893bde4b01SJohn Snow 
8903bde4b01SJohn Snow     if (!hbitmap_count(a) && !hbitmap_count(b)) {
8913bde4b01SJohn Snow         hbitmap_reset_all(result);
892be58721dSJohn Snow         return true;
893be58721dSJohn Snow     }
894be58721dSJohn Snow 
895c5b40c1fSJohn Snow     if (a->granularity != b->granularity) {
896c5b40c1fSJohn Snow         if ((a != result) && (b != result)) {
897c5b40c1fSJohn Snow             hbitmap_reset_all(result);
898c5b40c1fSJohn Snow         }
899c5b40c1fSJohn Snow         if (a != result) {
900c5b40c1fSJohn Snow             hbitmap_sparse_merge(result, a);
901c5b40c1fSJohn Snow         }
902c5b40c1fSJohn Snow         if (b != result) {
903c5b40c1fSJohn Snow             hbitmap_sparse_merge(result, b);
904c5b40c1fSJohn Snow         }
905c5b40c1fSJohn Snow         return true;
906c5b40c1fSJohn Snow     }
907c5b40c1fSJohn Snow 
908be58721dSJohn Snow     /* This merge is O(size), as BITS_PER_LONG and HBITMAP_LEVELS are constant.
909be58721dSJohn Snow      * It may be possible to improve running times for sparsely populated maps
910be58721dSJohn Snow      * by using hbitmap_iter_next, but this is suboptimal for dense maps.
911be58721dSJohn Snow      */
912c5b40c1fSJohn Snow     assert(a->size == b->size);
913be58721dSJohn Snow     for (i = HBITMAP_LEVELS - 1; i >= 0; i--) {
914be58721dSJohn Snow         for (j = 0; j < a->sizes[i]; j++) {
915fa000f2fSVladimir Sementsov-Ogievskiy             result->levels[i][j] = a->levels[i][j] | b->levels[i][j];
916be58721dSJohn Snow         }
917be58721dSJohn Snow     }
918be58721dSJohn Snow 
919d1dde714SEric Blake     /* Recompute the dirty count */
920d1dde714SEric Blake     result->count = hb_count_between(result, 0, result->size - 1);
921d1dde714SEric Blake 
922be58721dSJohn Snow     return true;
923be58721dSJohn Snow }
92407ac4cdbSFam Zheng 
925a3b52535SVladimir Sementsov-Ogievskiy char *hbitmap_sha256(const HBitmap *bitmap, Error **errp)
926a3b52535SVladimir Sementsov-Ogievskiy {
927a3b52535SVladimir Sementsov-Ogievskiy     size_t size = bitmap->sizes[HBITMAP_LEVELS - 1] * sizeof(unsigned long);
928a3b52535SVladimir Sementsov-Ogievskiy     char *data = (char *)bitmap->levels[HBITMAP_LEVELS - 1];
929a3b52535SVladimir Sementsov-Ogievskiy     char *hash = NULL;
930a3b52535SVladimir Sementsov-Ogievskiy     qcrypto_hash_digest(QCRYPTO_HASH_ALG_SHA256, data, size, &hash, errp);
931a3b52535SVladimir Sementsov-Ogievskiy 
932a3b52535SVladimir Sementsov-Ogievskiy     return hash;
933a3b52535SVladimir Sementsov-Ogievskiy }
934