Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: selftests: Make sparsebit structs const where appropriate

Make all sparsebit struct pointers "const" where appropriate. This will
allow adding a bitmap to track protected/encrypted physical memory that
tests can access in a read-only fashion.

Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Vishal Annapurve <vannapurve@google.com>
Cc: Ackerley Tng <ackerleytng@google.com>
Cc: Andrew Jones <andrew.jones@linux.dev>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Michael Roth <michael.roth@amd.com>
Tested-by: Carlos Bilbao <carlos.bilbao@amd.com>
Signed-off-by: Michael Roth <michael.roth@amd.com>
Signed-off-by: Peter Gonda <pgonda@google.com>
[sean: massage changelog]
Link: https://lore.kernel.org/r/20240223004258.3104051-3-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>

authored by

Michael Roth and committed by
Sean Christopherson
35f50c91 12619037

+42 -42
+18 -18
tools/testing/selftests/kvm/include/sparsebit.h
··· 30 30 31 31 struct sparsebit *sparsebit_alloc(void); 32 32 void sparsebit_free(struct sparsebit **sbitp); 33 - void sparsebit_copy(struct sparsebit *dstp, struct sparsebit *src); 33 + void sparsebit_copy(struct sparsebit *dstp, const struct sparsebit *src); 34 34 35 - bool sparsebit_is_set(struct sparsebit *sbit, sparsebit_idx_t idx); 36 - bool sparsebit_is_set_num(struct sparsebit *sbit, 35 + bool sparsebit_is_set(const struct sparsebit *sbit, sparsebit_idx_t idx); 36 + bool sparsebit_is_set_num(const struct sparsebit *sbit, 37 37 sparsebit_idx_t idx, sparsebit_num_t num); 38 - bool sparsebit_is_clear(struct sparsebit *sbit, sparsebit_idx_t idx); 39 - bool sparsebit_is_clear_num(struct sparsebit *sbit, 38 + bool sparsebit_is_clear(const struct sparsebit *sbit, sparsebit_idx_t idx); 39 + bool sparsebit_is_clear_num(const struct sparsebit *sbit, 40 40 sparsebit_idx_t idx, sparsebit_num_t num); 41 - sparsebit_num_t sparsebit_num_set(struct sparsebit *sbit); 42 - bool sparsebit_any_set(struct sparsebit *sbit); 43 - bool sparsebit_any_clear(struct sparsebit *sbit); 44 - bool sparsebit_all_set(struct sparsebit *sbit); 45 - bool sparsebit_all_clear(struct sparsebit *sbit); 46 - sparsebit_idx_t sparsebit_first_set(struct sparsebit *sbit); 47 - sparsebit_idx_t sparsebit_first_clear(struct sparsebit *sbit); 48 - sparsebit_idx_t sparsebit_next_set(struct sparsebit *sbit, sparsebit_idx_t prev); 49 - sparsebit_idx_t sparsebit_next_clear(struct sparsebit *sbit, sparsebit_idx_t prev); 50 - sparsebit_idx_t sparsebit_next_set_num(struct sparsebit *sbit, 41 + sparsebit_num_t sparsebit_num_set(const struct sparsebit *sbit); 42 + bool sparsebit_any_set(const struct sparsebit *sbit); 43 + bool sparsebit_any_clear(const struct sparsebit *sbit); 44 + bool sparsebit_all_set(const struct sparsebit *sbit); 45 + bool sparsebit_all_clear(const struct sparsebit *sbit); 46 + sparsebit_idx_t sparsebit_first_set(const struct sparsebit *sbit); 47 + sparsebit_idx_t sparsebit_first_clear(const struct sparsebit *sbit); 48 + sparsebit_idx_t sparsebit_next_set(const struct sparsebit *sbit, sparsebit_idx_t prev); 49 + sparsebit_idx_t sparsebit_next_clear(const struct sparsebit *sbit, sparsebit_idx_t prev); 50 + sparsebit_idx_t sparsebit_next_set_num(const struct sparsebit *sbit, 51 51 sparsebit_idx_t start, sparsebit_num_t num); 52 - sparsebit_idx_t sparsebit_next_clear_num(struct sparsebit *sbit, 52 + sparsebit_idx_t sparsebit_next_clear_num(const struct sparsebit *sbit, 53 53 sparsebit_idx_t start, sparsebit_num_t num); 54 54 55 55 void sparsebit_set(struct sparsebit *sbitp, sparsebit_idx_t idx); ··· 62 62 sparsebit_idx_t start, sparsebit_num_t num); 63 63 void sparsebit_clear_all(struct sparsebit *sbitp); 64 64 65 - void sparsebit_dump(FILE *stream, struct sparsebit *sbit, 65 + void sparsebit_dump(FILE *stream, const struct sparsebit *sbit, 66 66 unsigned int indent); 67 - void sparsebit_validate_internal(struct sparsebit *sbit); 67 + void sparsebit_validate_internal(const struct sparsebit *sbit); 68 68 69 69 #ifdef __cplusplus 70 70 }
+24 -24
tools/testing/selftests/kvm/lib/sparsebit.c
··· 202 202 /* Returns a pointer to the node that describes the 203 203 * lowest bit index. 204 204 */ 205 - static struct node *node_first(struct sparsebit *s) 205 + static struct node *node_first(const struct sparsebit *s) 206 206 { 207 207 struct node *nodep; 208 208 ··· 216 216 * lowest bit index > the index of the node pointed to by np. 217 217 * Returns NULL if no node with a higher index exists. 218 218 */ 219 - static struct node *node_next(struct sparsebit *s, struct node *np) 219 + static struct node *node_next(const struct sparsebit *s, struct node *np) 220 220 { 221 221 struct node *nodep = np; 222 222 ··· 244 244 * highest index < the index of the node pointed to by np. 245 245 * Returns NULL if no node with a lower index exists. 246 246 */ 247 - static struct node *node_prev(struct sparsebit *s, struct node *np) 247 + static struct node *node_prev(const struct sparsebit *s, struct node *np) 248 248 { 249 249 struct node *nodep = np; 250 250 ··· 273 273 * subtree and duplicates the bit settings to the newly allocated nodes. 274 274 * Returns the newly allocated copy of subtree. 275 275 */ 276 - static struct node *node_copy_subtree(struct node *subtree) 276 + static struct node *node_copy_subtree(const struct node *subtree) 277 277 { 278 278 struct node *root; 279 279 ··· 307 307 * index is within the bits described by the mask bits or the number of 308 308 * contiguous bits set after the mask. Returns NULL if there is no such node. 309 309 */ 310 - static struct node *node_find(struct sparsebit *s, sparsebit_idx_t idx) 310 + static struct node *node_find(const struct sparsebit *s, sparsebit_idx_t idx) 311 311 { 312 312 struct node *nodep; 313 313 ··· 393 393 } 394 394 395 395 /* Returns whether all the bits in the sparsebit array are set. */ 396 - bool sparsebit_all_set(struct sparsebit *s) 396 + bool sparsebit_all_set(const struct sparsebit *s) 397 397 { 398 398 /* 399 399 * If any nodes there must be at least one bit set. Only case ··· 775 775 /* Returns whether the bit at the index given by idx, within the 776 776 * sparsebit array is set or not. 777 777 */ 778 - bool sparsebit_is_set(struct sparsebit *s, sparsebit_idx_t idx) 778 + bool sparsebit_is_set(const struct sparsebit *s, sparsebit_idx_t idx) 779 779 { 780 780 struct node *nodep; 781 781 ··· 921 921 * used by test cases after they detect an unexpected condition, as a means 922 922 * to capture diagnostic information. 923 923 */ 924 - static void sparsebit_dump_internal(FILE *stream, struct sparsebit *s, 924 + static void sparsebit_dump_internal(FILE *stream, const struct sparsebit *s, 925 925 unsigned int indent) 926 926 { 927 927 /* Dump the contents of s */ ··· 969 969 * sparsebit_alloc(). It can though already have bits set, which 970 970 * if different from src will be cleared. 971 971 */ 972 - void sparsebit_copy(struct sparsebit *d, struct sparsebit *s) 972 + void sparsebit_copy(struct sparsebit *d, const struct sparsebit *s) 973 973 { 974 974 /* First clear any bits already set in the destination */ 975 975 sparsebit_clear_all(d); ··· 981 981 } 982 982 983 983 /* Returns whether num consecutive bits starting at idx are all set. */ 984 - bool sparsebit_is_set_num(struct sparsebit *s, 984 + bool sparsebit_is_set_num(const struct sparsebit *s, 985 985 sparsebit_idx_t idx, sparsebit_num_t num) 986 986 { 987 987 sparsebit_idx_t next_cleared; ··· 1005 1005 } 1006 1006 1007 1007 /* Returns whether the bit at the index given by idx. */ 1008 - bool sparsebit_is_clear(struct sparsebit *s, 1008 + bool sparsebit_is_clear(const struct sparsebit *s, 1009 1009 sparsebit_idx_t idx) 1010 1010 { 1011 1011 return !sparsebit_is_set(s, idx); 1012 1012 } 1013 1013 1014 1014 /* Returns whether num consecutive bits starting at idx are all cleared. */ 1015 - bool sparsebit_is_clear_num(struct sparsebit *s, 1015 + bool sparsebit_is_clear_num(const struct sparsebit *s, 1016 1016 sparsebit_idx_t idx, sparsebit_num_t num) 1017 1017 { 1018 1018 sparsebit_idx_t next_set; ··· 1041 1041 * value. Use sparsebit_any_set(), instead of sparsebit_num_set() > 0, 1042 1042 * to determine if the sparsebit array has any bits set. 1043 1043 */ 1044 - sparsebit_num_t sparsebit_num_set(struct sparsebit *s) 1044 + sparsebit_num_t sparsebit_num_set(const struct sparsebit *s) 1045 1045 { 1046 1046 return s->num_set; 1047 1047 } 1048 1048 1049 1049 /* Returns whether any bit is set in the sparsebit array. */ 1050 - bool sparsebit_any_set(struct sparsebit *s) 1050 + bool sparsebit_any_set(const struct sparsebit *s) 1051 1051 { 1052 1052 /* 1053 1053 * Nodes only describe set bits. If any nodes then there ··· 1070 1070 } 1071 1071 1072 1072 /* Returns whether all the bits in the sparsebit array are cleared. */ 1073 - bool sparsebit_all_clear(struct sparsebit *s) 1073 + bool sparsebit_all_clear(const struct sparsebit *s) 1074 1074 { 1075 1075 return !sparsebit_any_set(s); 1076 1076 } 1077 1077 1078 1078 /* Returns whether all the bits in the sparsebit array are set. */ 1079 - bool sparsebit_any_clear(struct sparsebit *s) 1079 + bool sparsebit_any_clear(const struct sparsebit *s) 1080 1080 { 1081 1081 return !sparsebit_all_set(s); 1082 1082 } 1083 1083 1084 1084 /* Returns the index of the first set bit. Abort if no bits are set. 1085 1085 */ 1086 - sparsebit_idx_t sparsebit_first_set(struct sparsebit *s) 1086 + sparsebit_idx_t sparsebit_first_set(const struct sparsebit *s) 1087 1087 { 1088 1088 struct node *nodep; 1089 1089 ··· 1097 1097 /* Returns the index of the first cleared bit. Abort if 1098 1098 * no bits are cleared. 1099 1099 */ 1100 - sparsebit_idx_t sparsebit_first_clear(struct sparsebit *s) 1100 + sparsebit_idx_t sparsebit_first_clear(const struct sparsebit *s) 1101 1101 { 1102 1102 struct node *nodep1, *nodep2; 1103 1103 ··· 1151 1151 /* Returns index of next bit set within s after the index given by prev. 1152 1152 * Returns 0 if there are no bits after prev that are set. 1153 1153 */ 1154 - sparsebit_idx_t sparsebit_next_set(struct sparsebit *s, 1154 + sparsebit_idx_t sparsebit_next_set(const struct sparsebit *s, 1155 1155 sparsebit_idx_t prev) 1156 1156 { 1157 1157 sparsebit_idx_t lowest_possible = prev + 1; ··· 1244 1244 /* Returns index of next bit cleared within s after the index given by prev. 1245 1245 * Returns 0 if there are no bits after prev that are cleared. 1246 1246 */ 1247 - sparsebit_idx_t sparsebit_next_clear(struct sparsebit *s, 1247 + sparsebit_idx_t sparsebit_next_clear(const struct sparsebit *s, 1248 1248 sparsebit_idx_t prev) 1249 1249 { 1250 1250 sparsebit_idx_t lowest_possible = prev + 1; ··· 1300 1300 * and returns the index of the first sequence of num consecutively set 1301 1301 * bits. Returns a value of 0 of no such sequence exists. 1302 1302 */ 1303 - sparsebit_idx_t sparsebit_next_set_num(struct sparsebit *s, 1303 + sparsebit_idx_t sparsebit_next_set_num(const struct sparsebit *s, 1304 1304 sparsebit_idx_t start, sparsebit_num_t num) 1305 1305 { 1306 1306 sparsebit_idx_t idx; ··· 1335 1335 * and returns the index of the first sequence of num consecutively cleared 1336 1336 * bits. Returns a value of 0 of no such sequence exists. 1337 1337 */ 1338 - sparsebit_idx_t sparsebit_next_clear_num(struct sparsebit *s, 1338 + sparsebit_idx_t sparsebit_next_clear_num(const struct sparsebit *s, 1339 1339 sparsebit_idx_t start, sparsebit_num_t num) 1340 1340 { 1341 1341 sparsebit_idx_t idx; ··· 1583 1583 * contiguous bits. This is done because '-' is used to specify command-line 1584 1584 * options, and sometimes ranges are specified as command-line arguments. 1585 1585 */ 1586 - void sparsebit_dump(FILE *stream, struct sparsebit *s, 1586 + void sparsebit_dump(FILE *stream, const struct sparsebit *s, 1587 1587 unsigned int indent) 1588 1588 { 1589 1589 size_t current_line_len = 0; ··· 1681 1681 * s. On error, diagnostic information is printed to stderr and 1682 1682 * abort is called. 1683 1683 */ 1684 - void sparsebit_validate_internal(struct sparsebit *s) 1684 + void sparsebit_validate_internal(const struct sparsebit *s) 1685 1685 { 1686 1686 bool error_detected = false; 1687 1687 struct node *nodep, *prev = NULL;