1
2
3
4
5
6
7
8
9
10
11
12#include "qemu/osdep.h"
13#include <glib.h>
14#include "qemu/hbitmap.h"
15#include "qemu/host-utils.h"
16#include "trace.h"
17#include "crypto/hash.h"
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
55
56struct HBitmap {
57
58
59
60 uint64_t orig_size;
61
62
63 uint64_t size;
64
65
66 uint64_t count;
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 int granularity;
87
88
89 HBitmap *meta;
90
91
92
93
94
95
96
97
98
99 unsigned long *levels[HBITMAP_LEVELS];
100
101
102 uint64_t sizes[HBITMAP_LEVELS];
103};
104
105
106
107
108static unsigned long hbitmap_iter_skip_words(HBitmapIter *hbi)
109{
110 size_t pos = hbi->pos;
111 const HBitmap *hb = hbi->hb;
112 unsigned i = HBITMAP_LEVELS - 1;
113
114 unsigned long cur;
115 do {
116 i--;
117 pos >>= BITS_PER_LEVEL;
118 cur = hbi->cur[i] & hb->levels[i][pos];
119 } while (cur == 0);
120
121
122
123
124
125
126
127 if (i == 0 && cur == (1UL << (BITS_PER_LONG - 1))) {
128 return 0;
129 }
130 for (; i < HBITMAP_LEVELS - 1; i++) {
131
132
133
134
135 assert(cur);
136 pos = (pos << BITS_PER_LEVEL) + ctzl(cur);
137 hbi->cur[i] = cur & (cur - 1);
138
139
140 cur = hb->levels[i + 1][pos];
141 }
142
143 hbi->pos = pos;
144 trace_hbitmap_iter_skip_words(hbi->hb, hbi, pos, cur);
145
146 assert(cur);
147 return cur;
148}
149
150int64_t hbitmap_iter_next(HBitmapIter *hbi)
151{
152 unsigned long cur = hbi->cur[HBITMAP_LEVELS - 1] &
153 hbi->hb->levels[HBITMAP_LEVELS - 1][hbi->pos];
154 int64_t item;
155
156 if (cur == 0) {
157 cur = hbitmap_iter_skip_words(hbi);
158 if (cur == 0) {
159 return -1;
160 }
161 }
162
163
164 hbi->cur[HBITMAP_LEVELS - 1] = cur & (cur - 1);
165 item = ((uint64_t)hbi->pos << BITS_PER_LEVEL) + ctzl(cur);
166
167 return item << hbi->granularity;
168}
169
170void hbitmap_iter_init(HBitmapIter *hbi, const HBitmap *hb, uint64_t first)
171{
172 unsigned i, bit;
173 uint64_t pos;
174
175 hbi->hb = hb;
176 pos = first >> hb->granularity;
177 assert(pos < hb->size);
178 hbi->pos = pos >> BITS_PER_LEVEL;
179 hbi->granularity = hb->granularity;
180
181 for (i = HBITMAP_LEVELS; i-- > 0; ) {
182 bit = pos & (BITS_PER_LONG - 1);
183 pos >>= BITS_PER_LEVEL;
184
185
186 hbi->cur[i] = hb->levels[i][pos] & ~((1UL << bit) - 1);
187
188
189
190
191 if (i != HBITMAP_LEVELS - 1) {
192 hbi->cur[i] &= ~(1UL << bit);
193 }
194 }
195}
196
197int64_t hbitmap_next_dirty(const HBitmap *hb, int64_t start, int64_t count)
198{
199 HBitmapIter hbi;
200 int64_t first_dirty_off;
201 uint64_t end;
202
203 assert(start >= 0 && count >= 0);
204
205 if (start >= hb->orig_size || count == 0) {
206 return -1;
207 }
208
209 end = count > hb->orig_size - start ? hb->orig_size : start + count;
210
211 hbitmap_iter_init(&hbi, hb, start);
212 first_dirty_off = hbitmap_iter_next(&hbi);
213
214 if (first_dirty_off < 0 || first_dirty_off >= end) {
215 return -1;
216 }
217
218 return MAX(start, first_dirty_off);
219}
220
221int64_t hbitmap_next_zero(const HBitmap *hb, int64_t start, int64_t count)
222{
223 size_t pos = (start >> hb->granularity) >> BITS_PER_LEVEL;
224 unsigned long *last_lev = hb->levels[HBITMAP_LEVELS - 1];
225 unsigned long cur = last_lev[pos];
226 unsigned start_bit_offset;
227 uint64_t end_bit, sz;
228 int64_t res;
229
230 assert(start >= 0 && count >= 0);
231
232 if (start >= hb->orig_size || count == 0) {
233 return -1;
234 }
235
236 end_bit = count > hb->orig_size - start ?
237 hb->size :
238 ((start + count - 1) >> hb->granularity) + 1;
239 sz = (end_bit + BITS_PER_LONG - 1) >> BITS_PER_LEVEL;
240
241
242
243
244 start_bit_offset = (start >> hb->granularity) & (BITS_PER_LONG - 1);
245 cur |= (1UL << start_bit_offset) - 1;
246 assert((start >> hb->granularity) < hb->size);
247
248 if (cur == (unsigned long)-1) {
249 do {
250 pos++;
251 } while (pos < sz && last_lev[pos] == (unsigned long)-1);
252
253 if (pos >= sz) {
254 return -1;
255 }
256
257 cur = last_lev[pos];
258 }
259
260 res = (pos << BITS_PER_LEVEL) + ctol(cur);
261 if (res >= end_bit) {
262 return -1;
263 }
264
265 res = res << hb->granularity;
266 if (res < start) {
267 assert(((start - res) >> hb->granularity) == 0);
268 return start;
269 }
270
271 return res;
272}
273
274bool hbitmap_next_dirty_area(const HBitmap *hb, int64_t start, int64_t end,
275 int64_t max_dirty_count,
276 int64_t *dirty_start, int64_t *dirty_count)
277{
278 int64_t next_zero;
279
280 assert(start >= 0 && end >= 0 && max_dirty_count > 0);
281
282 end = MIN(end, hb->orig_size);
283 if (start >= end) {
284 return false;
285 }
286
287 start = hbitmap_next_dirty(hb, start, end - start);
288 if (start < 0) {
289 return false;
290 }
291
292 end = start + MIN(end - start, max_dirty_count);
293
294 next_zero = hbitmap_next_zero(hb, start, end - start);
295 if (next_zero >= 0) {
296 end = next_zero;
297 }
298
299 *dirty_start = start;
300 *dirty_count = end - start;
301
302 return true;
303}
304
305bool hbitmap_empty(const HBitmap *hb)
306{
307 return hb->count == 0;
308}
309
310int hbitmap_granularity(const HBitmap *hb)
311{
312 return hb->granularity;
313}
314
315uint64_t hbitmap_count(const HBitmap *hb)
316{
317 return hb->count << hb->granularity;
318}
319
320
321
322
323
324
325
326
327
328
329
330
331static size_t hbitmap_iter_next_word(HBitmapIter *hbi, unsigned long *p_cur)
332{
333 unsigned long cur = hbi->cur[HBITMAP_LEVELS - 1];
334
335 if (cur == 0) {
336 cur = hbitmap_iter_skip_words(hbi);
337 if (cur == 0) {
338 *p_cur = 0;
339 return -1;
340 }
341 }
342
343
344 hbi->cur[HBITMAP_LEVELS - 1] = 0;
345 *p_cur = cur;
346 return hbi->pos;
347}
348
349
350
351
352static uint64_t hb_count_between(HBitmap *hb, uint64_t start, uint64_t last)
353{
354 HBitmapIter hbi;
355 uint64_t count = 0;
356 uint64_t end = last + 1;
357 unsigned long cur;
358 size_t pos;
359
360 hbitmap_iter_init(&hbi, hb, start << hb->granularity);
361 for (;;) {
362 pos = hbitmap_iter_next_word(&hbi, &cur);
363 if (pos >= (end >> BITS_PER_LEVEL)) {
364 break;
365 }
366 count += ctpopl(cur);
367 }
368
369 if (pos == (end >> BITS_PER_LEVEL)) {
370
371 int bit = end & (BITS_PER_LONG - 1);
372 cur &= (1UL << bit) - 1;
373 count += ctpopl(cur);
374 }
375
376 return count;
377}
378
379
380
381
382static inline bool hb_set_elem(unsigned long *elem, uint64_t start, uint64_t last)
383{
384 unsigned long mask;
385 unsigned long old;
386
387 assert((last >> BITS_PER_LEVEL) == (start >> BITS_PER_LEVEL));
388 assert(start <= last);
389
390 mask = 2UL << (last & (BITS_PER_LONG - 1));
391 mask -= 1UL << (start & (BITS_PER_LONG - 1));
392 old = *elem;
393 *elem |= mask;
394 return old != *elem;
395}
396
397
398
399static bool hb_set_between(HBitmap *hb, int level, uint64_t start,
400 uint64_t last)
401{
402 size_t pos = start >> BITS_PER_LEVEL;
403 size_t lastpos = last >> BITS_PER_LEVEL;
404 bool changed = false;
405 size_t i;
406
407 i = pos;
408 if (i < lastpos) {
409 uint64_t next = (start | (BITS_PER_LONG - 1)) + 1;
410 changed |= hb_set_elem(&hb->levels[level][i], start, next - 1);
411 for (;;) {
412 start = next;
413 next += BITS_PER_LONG;
414 if (++i == lastpos) {
415 break;
416 }
417 changed |= (hb->levels[level][i] == 0);
418 hb->levels[level][i] = ~0UL;
419 }
420 }
421 changed |= hb_set_elem(&hb->levels[level][i], start, last);
422
423
424
425
426 if (level > 0 && changed) {
427 hb_set_between(hb, level - 1, pos, lastpos);
428 }
429 return changed;
430}
431
432void hbitmap_set(HBitmap *hb, uint64_t start, uint64_t count)
433{
434
435 uint64_t first, n;
436 uint64_t last = start + count - 1;
437
438 if (count == 0) {
439 return;
440 }
441
442 trace_hbitmap_set(hb, start, count,
443 start >> hb->granularity, last >> hb->granularity);
444
445 first = start >> hb->granularity;
446 last >>= hb->granularity;
447 assert(last < hb->size);
448 n = last - first + 1;
449
450 hb->count += n - hb_count_between(hb, first, last);
451 if (hb_set_between(hb, HBITMAP_LEVELS - 1, first, last) &&
452 hb->meta) {
453 hbitmap_set(hb->meta, start, count);
454 }
455}
456
457
458
459
460static inline bool hb_reset_elem(unsigned long *elem, uint64_t start, uint64_t last)
461{
462 unsigned long mask;
463 bool blanked;
464
465 assert((last >> BITS_PER_LEVEL) == (start >> BITS_PER_LEVEL));
466 assert(start <= last);
467
468 mask = 2UL << (last & (BITS_PER_LONG - 1));
469 mask -= 1UL << (start & (BITS_PER_LONG - 1));
470 blanked = *elem != 0 && ((*elem & ~mask) == 0);
471 *elem &= ~mask;
472 return blanked;
473}
474
475
476
477static bool hb_reset_between(HBitmap *hb, int level, uint64_t start,
478 uint64_t last)
479{
480 size_t pos = start >> BITS_PER_LEVEL;
481 size_t lastpos = last >> BITS_PER_LEVEL;
482 bool changed = false;
483 size_t i;
484
485 i = pos;
486 if (i < lastpos) {
487 uint64_t next = (start | (BITS_PER_LONG - 1)) + 1;
488
489
490
491
492
493
494 if (hb_reset_elem(&hb->levels[level][i], start, next - 1)) {
495 changed = true;
496 } else {
497 pos++;
498 }
499
500 for (;;) {
501 start = next;
502 next += BITS_PER_LONG;
503 if (++i == lastpos) {
504 break;
505 }
506 changed |= (hb->levels[level][i] != 0);
507 hb->levels[level][i] = 0UL;
508 }
509 }
510
511
512 if (hb_reset_elem(&hb->levels[level][i], start, last)) {
513 changed = true;
514 } else {
515 lastpos--;
516 }
517
518 if (level > 0 && changed) {
519 hb_reset_between(hb, level - 1, pos, lastpos);
520 }
521
522 return changed;
523
524}
525
526void hbitmap_reset(HBitmap *hb, uint64_t start, uint64_t count)
527{
528
529 uint64_t first;
530 uint64_t last = start + count - 1;
531 uint64_t gran = 1ULL << hb->granularity;
532
533 if (count == 0) {
534 return;
535 }
536
537 assert(QEMU_IS_ALIGNED(start, gran));
538 assert(QEMU_IS_ALIGNED(count, gran) || (start + count == hb->orig_size));
539
540 trace_hbitmap_reset(hb, start, count,
541 start >> hb->granularity, last >> hb->granularity);
542
543 first = start >> hb->granularity;
544 last >>= hb->granularity;
545 assert(last < hb->size);
546
547 hb->count -= hb_count_between(hb, first, last);
548 if (hb_reset_between(hb, HBITMAP_LEVELS - 1, first, last) &&
549 hb->meta) {
550 hbitmap_set(hb->meta, start, count);
551 }
552}
553
554void hbitmap_reset_all(HBitmap *hb)
555{
556 unsigned int i;
557
558
559 for (i = HBITMAP_LEVELS; --i >= 1; ) {
560 memset(hb->levels[i], 0, hb->sizes[i] * sizeof(unsigned long));
561 }
562
563 hb->levels[0][0] = 1UL << (BITS_PER_LONG - 1);
564 hb->count = 0;
565}
566
567bool hbitmap_is_serializable(const HBitmap *hb)
568{
569
570
571
572
573
574
575
576
577
578
579
580
581
582 return hb->granularity < 58;
583}
584
585bool hbitmap_get(const HBitmap *hb, uint64_t item)
586{
587
588 uint64_t pos = item >> hb->granularity;
589 unsigned long bit = 1UL << (pos & (BITS_PER_LONG - 1));
590 assert(pos < hb->size);
591
592 return (hb->levels[HBITMAP_LEVELS - 1][pos >> BITS_PER_LEVEL] & bit) != 0;
593}
594
595uint64_t hbitmap_serialization_align(const HBitmap *hb)
596{
597 assert(hbitmap_is_serializable(hb));
598
599
600
601 return UINT64_C(64) << hb->granularity;
602}
603
604
605
606
607static void serialization_chunk(const HBitmap *hb,
608 uint64_t start, uint64_t count,
609 unsigned long **first_el, uint64_t *el_count)
610{
611 uint64_t last = start + count - 1;
612 uint64_t gran = hbitmap_serialization_align(hb);
613
614 assert((start & (gran - 1)) == 0);
615 assert((last >> hb->granularity) < hb->size);
616 if ((last >> hb->granularity) != hb->size - 1) {
617 assert((count & (gran - 1)) == 0);
618 }
619
620 start = (start >> hb->granularity) >> BITS_PER_LEVEL;
621 last = (last >> hb->granularity) >> BITS_PER_LEVEL;
622
623 *first_el = &hb->levels[HBITMAP_LEVELS - 1][start];
624 *el_count = last - start + 1;
625}
626
627uint64_t hbitmap_serialization_size(const HBitmap *hb,
628 uint64_t start, uint64_t count)
629{
630 uint64_t el_count;
631 unsigned long *cur;
632
633 if (!count) {
634 return 0;
635 }
636 serialization_chunk(hb, start, count, &cur, &el_count);
637
638 return el_count * sizeof(unsigned long);
639}
640
641void hbitmap_serialize_part(const HBitmap *hb, uint8_t *buf,
642 uint64_t start, uint64_t count)
643{
644 uint64_t el_count;
645 unsigned long *cur, *end;
646
647 if (!count) {
648 return;
649 }
650 serialization_chunk(hb, start, count, &cur, &el_count);
651 end = cur + el_count;
652
653 while (cur != end) {
654 unsigned long el =
655 (BITS_PER_LONG == 32 ? cpu_to_le32(*cur) : cpu_to_le64(*cur));
656
657 memcpy(buf, &el, sizeof(el));
658 buf += sizeof(el);
659 cur++;
660 }
661}
662
663void hbitmap_deserialize_part(HBitmap *hb, uint8_t *buf,
664 uint64_t start, uint64_t count,
665 bool finish)
666{
667 uint64_t el_count;
668 unsigned long *cur, *end;
669
670 if (!count) {
671 return;
672 }
673 serialization_chunk(hb, start, count, &cur, &el_count);
674 end = cur + el_count;
675
676 while (cur != end) {
677 memcpy(cur, buf, sizeof(*cur));
678
679 if (BITS_PER_LONG == 32) {
680 le32_to_cpus((uint32_t *)cur);
681 } else {
682 le64_to_cpus((uint64_t *)cur);
683 }
684
685 buf += sizeof(unsigned long);
686 cur++;
687 }
688 if (finish) {
689 hbitmap_deserialize_finish(hb);
690 }
691}
692
693void hbitmap_deserialize_zeroes(HBitmap *hb, uint64_t start, uint64_t count,
694 bool finish)
695{
696 uint64_t el_count;
697 unsigned long *first;
698
699 if (!count) {
700 return;
701 }
702 serialization_chunk(hb, start, count, &first, &el_count);
703
704 memset(first, 0, el_count * sizeof(unsigned long));
705 if (finish) {
706 hbitmap_deserialize_finish(hb);
707 }
708}
709
710void hbitmap_deserialize_ones(HBitmap *hb, uint64_t start, uint64_t count,
711 bool finish)
712{
713 uint64_t el_count;
714 unsigned long *first;
715
716 if (!count) {
717 return;
718 }
719 serialization_chunk(hb, start, count, &first, &el_count);
720
721 memset(first, 0xff, el_count * sizeof(unsigned long));
722 if (finish) {
723 hbitmap_deserialize_finish(hb);
724 }
725}
726
727void hbitmap_deserialize_finish(HBitmap *bitmap)
728{
729 int64_t i, size, prev_size;
730 int lev;
731
732
733
734 size = MAX((bitmap->size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1);
735 for (lev = HBITMAP_LEVELS - 1; lev-- > 0; ) {
736 prev_size = size;
737 size = MAX((size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1);
738 memset(bitmap->levels[lev], 0, size * sizeof(unsigned long));
739
740 for (i = 0; i < prev_size; ++i) {
741 if (bitmap->levels[lev + 1][i]) {
742 bitmap->levels[lev][i >> BITS_PER_LEVEL] |=
743 1UL << (i & (BITS_PER_LONG - 1));
744 }
745 }
746 }
747
748 bitmap->levels[0][0] |= 1UL << (BITS_PER_LONG - 1);
749 bitmap->count = hb_count_between(bitmap, 0, bitmap->size - 1);
750}
751
752void hbitmap_free(HBitmap *hb)
753{
754 unsigned i;
755 assert(!hb->meta);
756 for (i = HBITMAP_LEVELS; i-- > 0; ) {
757 g_free(hb->levels[i]);
758 }
759 g_free(hb);
760}
761
762HBitmap *hbitmap_alloc(uint64_t size, int granularity)
763{
764 HBitmap *hb = g_new0(struct HBitmap, 1);
765 unsigned i;
766
767 assert(size <= INT64_MAX);
768 hb->orig_size = size;
769
770 assert(granularity >= 0 && granularity < 64);
771 size = (size + (1ULL << granularity) - 1) >> granularity;
772 assert(size <= ((uint64_t)1 << HBITMAP_LOG_MAX_SIZE));
773
774 hb->size = size;
775 hb->granularity = granularity;
776 for (i = HBITMAP_LEVELS; i-- > 0; ) {
777 size = MAX((size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1);
778 hb->sizes[i] = size;
779 hb->levels[i] = g_new0(unsigned long, size);
780 }
781
782
783
784
785
786 assert(size == 1);
787 hb->levels[0][0] |= 1UL << (BITS_PER_LONG - 1);
788 return hb;
789}
790
791void hbitmap_truncate(HBitmap *hb, uint64_t size)
792{
793 bool shrink;
794 unsigned i;
795 uint64_t num_elements = size;
796 uint64_t old;
797
798 assert(size <= INT64_MAX);
799 hb->orig_size = size;
800
801
802 size = (size + (1ULL << hb->granularity) - 1) >> hb->granularity;
803 assert(size <= ((uint64_t)1 << HBITMAP_LOG_MAX_SIZE));
804 shrink = size < hb->size;
805
806
807 if (size == hb->size) {
808 return;
809 }
810
811
812
813
814
815 if (shrink) {
816
817
818 uint64_t start = ROUND_UP(num_elements, UINT64_C(1) << hb->granularity);
819 uint64_t fix_count = (hb->size << hb->granularity) - start;
820
821 assert(fix_count);
822 hbitmap_reset(hb, start, fix_count);
823 }
824
825 hb->size = size;
826 for (i = HBITMAP_LEVELS; i-- > 0; ) {
827 size = MAX(BITS_TO_LONGS(size), 1);
828 if (hb->sizes[i] == size) {
829 break;
830 }
831 old = hb->sizes[i];
832 hb->sizes[i] = size;
833 hb->levels[i] = g_realloc(hb->levels[i], size * sizeof(unsigned long));
834 if (!shrink) {
835 memset(&hb->levels[i][old], 0x00,
836 (size - old) * sizeof(*hb->levels[i]));
837 }
838 }
839 if (hb->meta) {
840 hbitmap_truncate(hb->meta, hb->size << hb->granularity);
841 }
842}
843
844bool hbitmap_can_merge(const HBitmap *a, const HBitmap *b)
845{
846 return (a->orig_size == b->orig_size);
847}
848
849
850
851
852
853
854static void hbitmap_sparse_merge(HBitmap *dst, const HBitmap *src)
855{
856 int64_t offset;
857 int64_t count;
858
859 for (offset = 0;
860 hbitmap_next_dirty_area(src, offset, src->orig_size, INT64_MAX,
861 &offset, &count);
862 offset += count)
863 {
864 hbitmap_set(dst, offset, count);
865 }
866}
867
868
869
870
871
872
873
874
875
876bool hbitmap_merge(const HBitmap *a, const HBitmap *b, HBitmap *result)
877{
878 int i;
879 uint64_t j;
880
881 if (!hbitmap_can_merge(a, b) || !hbitmap_can_merge(a, result)) {
882 return false;
883 }
884 assert(hbitmap_can_merge(b, result));
885
886 if ((!hbitmap_count(a) && result == b) ||
887 (!hbitmap_count(b) && result == a)) {
888 return true;
889 }
890
891 if (!hbitmap_count(a) && !hbitmap_count(b)) {
892 hbitmap_reset_all(result);
893 return true;
894 }
895
896 if (a->granularity != b->granularity) {
897 if ((a != result) && (b != result)) {
898 hbitmap_reset_all(result);
899 }
900 if (a != result) {
901 hbitmap_sparse_merge(result, a);
902 }
903 if (b != result) {
904 hbitmap_sparse_merge(result, b);
905 }
906 return true;
907 }
908
909
910
911
912
913 assert(a->size == b->size);
914 for (i = HBITMAP_LEVELS - 1; i >= 0; i--) {
915 for (j = 0; j < a->sizes[i]; j++) {
916 result->levels[i][j] = a->levels[i][j] | b->levels[i][j];
917 }
918 }
919
920
921 result->count = hb_count_between(result, 0, result->size - 1);
922
923 return true;
924}
925
926char *hbitmap_sha256(const HBitmap *bitmap, Error **errp)
927{
928 size_t size = bitmap->sizes[HBITMAP_LEVELS - 1] * sizeof(unsigned long);
929 char *data = (char *)bitmap->levels[HBITMAP_LEVELS - 1];
930 char *hash = NULL;
931 qcrypto_hash_digest(QCRYPTO_HASH_ALG_SHA256, data, size, &hash, errp);
932
933 return hash;
934}
935