1
2
3
4
5
6
7
8
9
10
11
12#include "qemu/osdep.h"
13#include "qemu/hbitmap.h"
14#include "qemu/host-utils.h"
15#include "trace.h"
16#include "crypto/hash.h"
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55struct HBitmap {
56
57 uint64_t size;
58
59
60 uint64_t count;
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 int granularity;
81
82
83 HBitmap *meta;
84
85
86
87
88
89
90
91
92
93 unsigned long *levels[HBITMAP_LEVELS];
94
95
96 uint64_t sizes[HBITMAP_LEVELS];
97};
98
99
100
101
102unsigned long hbitmap_iter_skip_words(HBitmapIter *hbi)
103{
104 size_t pos = hbi->pos;
105 const HBitmap *hb = hbi->hb;
106 unsigned i = HBITMAP_LEVELS - 1;
107
108 unsigned long cur;
109 do {
110 i--;
111 pos >>= BITS_PER_LEVEL;
112 cur = hbi->cur[i] & hb->levels[i][pos];
113 } while (cur == 0);
114
115
116
117
118
119
120
121 if (i == 0 && cur == (1UL << (BITS_PER_LONG - 1))) {
122 return 0;
123 }
124 for (; i < HBITMAP_LEVELS - 1; i++) {
125
126
127
128
129 assert(cur);
130 pos = (pos << BITS_PER_LEVEL) + ctzl(cur);
131 hbi->cur[i] = cur & (cur - 1);
132
133
134 cur = hb->levels[i + 1][pos];
135 }
136
137 hbi->pos = pos;
138 trace_hbitmap_iter_skip_words(hbi->hb, hbi, pos, cur);
139
140 assert(cur);
141 return cur;
142}
143
144int64_t hbitmap_iter_next(HBitmapIter *hbi)
145{
146 unsigned long cur = hbi->cur[HBITMAP_LEVELS - 1] &
147 hbi->hb->levels[HBITMAP_LEVELS - 1][hbi->pos];
148 int64_t item;
149
150 if (cur == 0) {
151 cur = hbitmap_iter_skip_words(hbi);
152 if (cur == 0) {
153 return -1;
154 }
155 }
156
157
158 hbi->cur[HBITMAP_LEVELS - 1] = cur & (cur - 1);
159 item = ((uint64_t)hbi->pos << BITS_PER_LEVEL) + ctzl(cur);
160
161 return item << hbi->granularity;
162}
163
164void hbitmap_iter_init(HBitmapIter *hbi, const HBitmap *hb, uint64_t first)
165{
166 unsigned i, bit;
167 uint64_t pos;
168
169 hbi->hb = hb;
170 pos = first >> hb->granularity;
171 assert(pos < hb->size);
172 hbi->pos = pos >> BITS_PER_LEVEL;
173 hbi->granularity = hb->granularity;
174
175 for (i = HBITMAP_LEVELS; i-- > 0; ) {
176 bit = pos & (BITS_PER_LONG - 1);
177 pos >>= BITS_PER_LEVEL;
178
179
180 hbi->cur[i] = hb->levels[i][pos] & ~((1UL << bit) - 1);
181
182
183
184
185 if (i != HBITMAP_LEVELS - 1) {
186 hbi->cur[i] &= ~(1UL << bit);
187 }
188 }
189}
190
191bool hbitmap_empty(const HBitmap *hb)
192{
193 return hb->count == 0;
194}
195
196int hbitmap_granularity(const HBitmap *hb)
197{
198 return hb->granularity;
199}
200
201uint64_t hbitmap_count(const HBitmap *hb)
202{
203 return hb->count << hb->granularity;
204}
205
206
207
208
209static uint64_t hb_count_between(HBitmap *hb, uint64_t start, uint64_t last)
210{
211 HBitmapIter hbi;
212 uint64_t count = 0;
213 uint64_t end = last + 1;
214 unsigned long cur;
215 size_t pos;
216
217 hbitmap_iter_init(&hbi, hb, start << hb->granularity);
218 for (;;) {
219 pos = hbitmap_iter_next_word(&hbi, &cur);
220 if (pos >= (end >> BITS_PER_LEVEL)) {
221 break;
222 }
223 count += ctpopl(cur);
224 }
225
226 if (pos == (end >> BITS_PER_LEVEL)) {
227
228 int bit = end & (BITS_PER_LONG - 1);
229 cur &= (1UL << bit) - 1;
230 count += ctpopl(cur);
231 }
232
233 return count;
234}
235
236
237
238
239static inline bool hb_set_elem(unsigned long *elem, uint64_t start, uint64_t last)
240{
241 unsigned long mask;
242 unsigned long old;
243
244 assert((last >> BITS_PER_LEVEL) == (start >> BITS_PER_LEVEL));
245 assert(start <= last);
246
247 mask = 2UL << (last & (BITS_PER_LONG - 1));
248 mask -= 1UL << (start & (BITS_PER_LONG - 1));
249 old = *elem;
250 *elem |= mask;
251 return old != *elem;
252}
253
254
255
256static bool hb_set_between(HBitmap *hb, int level, uint64_t start,
257 uint64_t last)
258{
259 size_t pos = start >> BITS_PER_LEVEL;
260 size_t lastpos = last >> BITS_PER_LEVEL;
261 bool changed = false;
262 size_t i;
263
264 i = pos;
265 if (i < lastpos) {
266 uint64_t next = (start | (BITS_PER_LONG - 1)) + 1;
267 changed |= hb_set_elem(&hb->levels[level][i], start, next - 1);
268 for (;;) {
269 start = next;
270 next += BITS_PER_LONG;
271 if (++i == lastpos) {
272 break;
273 }
274 changed |= (hb->levels[level][i] == 0);
275 hb->levels[level][i] = ~0UL;
276 }
277 }
278 changed |= hb_set_elem(&hb->levels[level][i], start, last);
279
280
281
282
283 if (level > 0 && changed) {
284 hb_set_between(hb, level - 1, pos, lastpos);
285 }
286 return changed;
287}
288
289void hbitmap_set(HBitmap *hb, uint64_t start, uint64_t count)
290{
291
292 uint64_t first, n;
293 uint64_t last = start + count - 1;
294
295 trace_hbitmap_set(hb, start, count,
296 start >> hb->granularity, last >> hb->granularity);
297
298 first = start >> hb->granularity;
299 last >>= hb->granularity;
300 assert(last < hb->size);
301 n = last - first + 1;
302
303 hb->count += n - hb_count_between(hb, first, last);
304 if (hb_set_between(hb, HBITMAP_LEVELS - 1, first, last) &&
305 hb->meta) {
306 hbitmap_set(hb->meta, start, count);
307 }
308}
309
310
311
312
313static inline bool hb_reset_elem(unsigned long *elem, uint64_t start, uint64_t last)
314{
315 unsigned long mask;
316 bool blanked;
317
318 assert((last >> BITS_PER_LEVEL) == (start >> BITS_PER_LEVEL));
319 assert(start <= last);
320
321 mask = 2UL << (last & (BITS_PER_LONG - 1));
322 mask -= 1UL << (start & (BITS_PER_LONG - 1));
323 blanked = *elem != 0 && ((*elem & ~mask) == 0);
324 *elem &= ~mask;
325 return blanked;
326}
327
328
329
330static bool hb_reset_between(HBitmap *hb, int level, uint64_t start,
331 uint64_t last)
332{
333 size_t pos = start >> BITS_PER_LEVEL;
334 size_t lastpos = last >> BITS_PER_LEVEL;
335 bool changed = false;
336 size_t i;
337
338 i = pos;
339 if (i < lastpos) {
340 uint64_t next = (start | (BITS_PER_LONG - 1)) + 1;
341
342
343
344
345
346
347 if (hb_reset_elem(&hb->levels[level][i], start, next - 1)) {
348 changed = true;
349 } else {
350 pos++;
351 }
352
353 for (;;) {
354 start = next;
355 next += BITS_PER_LONG;
356 if (++i == lastpos) {
357 break;
358 }
359 changed |= (hb->levels[level][i] != 0);
360 hb->levels[level][i] = 0UL;
361 }
362 }
363
364
365 if (hb_reset_elem(&hb->levels[level][i], start, last)) {
366 changed = true;
367 } else {
368 lastpos--;
369 }
370
371 if (level > 0 && changed) {
372 hb_reset_between(hb, level - 1, pos, lastpos);
373 }
374
375 return changed;
376
377}
378
379void hbitmap_reset(HBitmap *hb, uint64_t start, uint64_t count)
380{
381
382 uint64_t first;
383 uint64_t last = start + count - 1;
384
385 trace_hbitmap_reset(hb, start, count,
386 start >> hb->granularity, last >> hb->granularity);
387
388 first = start >> hb->granularity;
389 last >>= hb->granularity;
390 assert(last < hb->size);
391
392 hb->count -= hb_count_between(hb, first, last);
393 if (hb_reset_between(hb, HBITMAP_LEVELS - 1, first, last) &&
394 hb->meta) {
395 hbitmap_set(hb->meta, start, count);
396 }
397}
398
399void hbitmap_reset_all(HBitmap *hb)
400{
401 unsigned int i;
402
403
404 for (i = HBITMAP_LEVELS; --i >= 1; ) {
405 memset(hb->levels[i], 0, hb->sizes[i] * sizeof(unsigned long));
406 }
407
408 hb->levels[0][0] = 1UL << (BITS_PER_LONG - 1);
409 hb->count = 0;
410}
411
412bool hbitmap_is_serializable(const HBitmap *hb)
413{
414
415
416
417
418
419
420
421
422
423
424
425
426
427 return hb->granularity < 58;
428}
429
430bool hbitmap_get(const HBitmap *hb, uint64_t item)
431{
432
433 uint64_t pos = item >> hb->granularity;
434 unsigned long bit = 1UL << (pos & (BITS_PER_LONG - 1));
435 assert(pos < hb->size);
436
437 return (hb->levels[HBITMAP_LEVELS - 1][pos >> BITS_PER_LEVEL] & bit) != 0;
438}
439
440uint64_t hbitmap_serialization_align(const HBitmap *hb)
441{
442 assert(hbitmap_is_serializable(hb));
443
444
445
446 return UINT64_C(64) << hb->granularity;
447}
448
449
450
451
452static void serialization_chunk(const HBitmap *hb,
453 uint64_t start, uint64_t count,
454 unsigned long **first_el, uint64_t *el_count)
455{
456 uint64_t last = start + count - 1;
457 uint64_t gran = hbitmap_serialization_align(hb);
458
459 assert((start & (gran - 1)) == 0);
460 assert((last >> hb->granularity) < hb->size);
461 if ((last >> hb->granularity) != hb->size - 1) {
462 assert((count & (gran - 1)) == 0);
463 }
464
465 start = (start >> hb->granularity) >> BITS_PER_LEVEL;
466 last = (last >> hb->granularity) >> BITS_PER_LEVEL;
467
468 *first_el = &hb->levels[HBITMAP_LEVELS - 1][start];
469 *el_count = last - start + 1;
470}
471
472uint64_t hbitmap_serialization_size(const HBitmap *hb,
473 uint64_t start, uint64_t count)
474{
475 uint64_t el_count;
476 unsigned long *cur;
477
478 if (!count) {
479 return 0;
480 }
481 serialization_chunk(hb, start, count, &cur, &el_count);
482
483 return el_count * sizeof(unsigned long);
484}
485
486void hbitmap_serialize_part(const HBitmap *hb, uint8_t *buf,
487 uint64_t start, uint64_t count)
488{
489 uint64_t el_count;
490 unsigned long *cur, *end;
491
492 if (!count) {
493 return;
494 }
495 serialization_chunk(hb, start, count, &cur, &el_count);
496 end = cur + el_count;
497
498 while (cur != end) {
499 unsigned long el =
500 (BITS_PER_LONG == 32 ? cpu_to_le32(*cur) : cpu_to_le64(*cur));
501
502 memcpy(buf, &el, sizeof(el));
503 buf += sizeof(el);
504 cur++;
505 }
506}
507
508void hbitmap_deserialize_part(HBitmap *hb, uint8_t *buf,
509 uint64_t start, uint64_t count,
510 bool finish)
511{
512 uint64_t el_count;
513 unsigned long *cur, *end;
514
515 if (!count) {
516 return;
517 }
518 serialization_chunk(hb, start, count, &cur, &el_count);
519 end = cur + el_count;
520
521 while (cur != end) {
522 memcpy(cur, buf, sizeof(*cur));
523
524 if (BITS_PER_LONG == 32) {
525 le32_to_cpus((uint32_t *)cur);
526 } else {
527 le64_to_cpus((uint64_t *)cur);
528 }
529
530 buf += sizeof(unsigned long);
531 cur++;
532 }
533 if (finish) {
534 hbitmap_deserialize_finish(hb);
535 }
536}
537
538void hbitmap_deserialize_zeroes(HBitmap *hb, uint64_t start, uint64_t count,
539 bool finish)
540{
541 uint64_t el_count;
542 unsigned long *first;
543
544 if (!count) {
545 return;
546 }
547 serialization_chunk(hb, start, count, &first, &el_count);
548
549 memset(first, 0, el_count * sizeof(unsigned long));
550 if (finish) {
551 hbitmap_deserialize_finish(hb);
552 }
553}
554
555void hbitmap_deserialize_ones(HBitmap *hb, uint64_t start, uint64_t count,
556 bool finish)
557{
558 uint64_t el_count;
559 unsigned long *first;
560
561 if (!count) {
562 return;
563 }
564 serialization_chunk(hb, start, count, &first, &el_count);
565
566 memset(first, 0xff, el_count * sizeof(unsigned long));
567 if (finish) {
568 hbitmap_deserialize_finish(hb);
569 }
570}
571
572void hbitmap_deserialize_finish(HBitmap *bitmap)
573{
574 int64_t i, size, prev_size;
575 int lev;
576
577
578
579 size = MAX((bitmap->size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1);
580 for (lev = HBITMAP_LEVELS - 1; lev-- > 0; ) {
581 prev_size = size;
582 size = MAX((size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1);
583 memset(bitmap->levels[lev], 0, size * sizeof(unsigned long));
584
585 for (i = 0; i < prev_size; ++i) {
586 if (bitmap->levels[lev + 1][i]) {
587 bitmap->levels[lev][i >> BITS_PER_LEVEL] |=
588 1UL << (i & (BITS_PER_LONG - 1));
589 }
590 }
591 }
592
593 bitmap->levels[0][0] |= 1UL << (BITS_PER_LONG - 1);
594}
595
596void hbitmap_free(HBitmap *hb)
597{
598 unsigned i;
599 assert(!hb->meta);
600 for (i = HBITMAP_LEVELS; i-- > 0; ) {
601 g_free(hb->levels[i]);
602 }
603 g_free(hb);
604}
605
606HBitmap *hbitmap_alloc(uint64_t size, int granularity)
607{
608 HBitmap *hb = g_new0(struct HBitmap, 1);
609 unsigned i;
610
611 assert(granularity >= 0 && granularity < 64);
612 size = (size + (1ULL << granularity) - 1) >> granularity;
613 assert(size <= ((uint64_t)1 << HBITMAP_LOG_MAX_SIZE));
614
615 hb->size = size;
616 hb->granularity = granularity;
617 for (i = HBITMAP_LEVELS; i-- > 0; ) {
618 size = MAX((size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1);
619 hb->sizes[i] = size;
620 hb->levels[i] = g_new0(unsigned long, size);
621 }
622
623
624
625
626
627 assert(size == 1);
628 hb->levels[0][0] |= 1UL << (BITS_PER_LONG - 1);
629 return hb;
630}
631
632void hbitmap_truncate(HBitmap *hb, uint64_t size)
633{
634 bool shrink;
635 unsigned i;
636 uint64_t num_elements = size;
637 uint64_t old;
638
639
640 size = (size + (1ULL << hb->granularity) - 1) >> hb->granularity;
641 assert(size <= ((uint64_t)1 << HBITMAP_LOG_MAX_SIZE));
642 shrink = size < hb->size;
643
644
645 if (size == hb->size) {
646 return;
647 }
648
649
650
651
652
653 if (shrink) {
654
655
656 uint64_t start = ROUND_UP(num_elements, UINT64_C(1) << hb->granularity);
657 uint64_t fix_count = (hb->size << hb->granularity) - start;
658
659 assert(fix_count);
660 hbitmap_reset(hb, start, fix_count);
661 }
662
663 hb->size = size;
664 for (i = HBITMAP_LEVELS; i-- > 0; ) {
665 size = MAX(BITS_TO_LONGS(size), 1);
666 if (hb->sizes[i] == size) {
667 break;
668 }
669 old = hb->sizes[i];
670 hb->sizes[i] = size;
671 hb->levels[i] = g_realloc(hb->levels[i], size * sizeof(unsigned long));
672 if (!shrink) {
673 memset(&hb->levels[i][old], 0x00,
674 (size - old) * sizeof(*hb->levels[i]));
675 }
676 }
677 if (hb->meta) {
678 hbitmap_truncate(hb->meta, hb->size << hb->granularity);
679 }
680}
681
682
683
684
685
686
687
688
689
690bool hbitmap_merge(HBitmap *a, const HBitmap *b)
691{
692 int i;
693 uint64_t j;
694
695 if ((a->size != b->size) || (a->granularity != b->granularity)) {
696 return false;
697 }
698
699 if (hbitmap_count(b) == 0) {
700 return true;
701 }
702
703
704
705
706
707 for (i = HBITMAP_LEVELS - 1; i >= 0; i--) {
708 for (j = 0; j < a->sizes[i]; j++) {
709 a->levels[i][j] |= b->levels[i][j];
710 }
711 }
712
713 return true;
714}
715
716HBitmap *hbitmap_create_meta(HBitmap *hb, int chunk_size)
717{
718 assert(!(chunk_size & (chunk_size - 1)));
719 assert(!hb->meta);
720 hb->meta = hbitmap_alloc(hb->size << hb->granularity,
721 hb->granularity + ctz32(chunk_size));
722 return hb->meta;
723}
724
725void hbitmap_free_meta(HBitmap *hb)
726{
727 assert(hb->meta);
728 hbitmap_free(hb->meta);
729 hb->meta = NULL;
730}
731
732char *hbitmap_sha256(const HBitmap *bitmap, Error **errp)
733{
734 size_t size = bitmap->sizes[HBITMAP_LEVELS - 1] * sizeof(unsigned long);
735 char *data = (char *)bitmap->levels[HBITMAP_LEVELS - 1];
736 char *hash = NULL;
737 qcrypto_hash_digest(QCRYPTO_HASH_ALG_SHA256, data, size, &hash, errp);
738
739 return hash;
740}
741