Skip to content

Commit ecba9a5

Browse files
Takuya Yoshikawamatosatti
authored andcommitted
KVM: x86: lapic: Clean up find_highest_vector() and count_vectors()
find_highest_vector() and count_vectors(): - Instead of using magic values, define and use proper macros. find_highest_vector(): - Remove likely() which is there only for historical reasons and not doing correct branch predictions anymore. Using such heuristics to optimize this function is not worth it now. Let CPUs predict things instead. - Stop checking word[0] separately. This was only needed for doing likely() optimization. - Use for loop, not while, to iterate over the register array to make the code clearer. Note that we actually confirmed that the likely() did wrong predictions by inserting debug code. Acked-by: Michael S. Tsirkin <[email protected]> Signed-off-by: Takuya Yoshikawa <[email protected]> Signed-off-by: Marcelo Tosatti <[email protected]>
1 parent 7de5bdc commit ecba9a5

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

arch/x86/kvm/lapic.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#define APIC_DEST_NOSHORT 0x0
6767
#define APIC_DEST_MASK 0x800
6868
#define MAX_APIC_VECTOR 256
69+
#define APIC_VECTORS_PER_REG 32
6970

7071
#define VEC_POS(v) ((v) & (32 - 1))
7172
#define REG_POS(v) (((v) >> 5) << 4)
@@ -208,25 +209,30 @@ static const unsigned int apic_lvt_mask[APIC_LVT_NUM] = {
208209

209210
static int find_highest_vector(void *bitmap)
210211
{
211-
u32 *word = bitmap;
212-
int word_offset = MAX_APIC_VECTOR >> 5;
212+
int vec;
213+
u32 *reg;
213214

214-
while ((word_offset != 0) && (word[(--word_offset) << 2] == 0))
215-
continue;
215+
for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG;
216+
vec >= 0; vec -= APIC_VECTORS_PER_REG) {
217+
reg = bitmap + REG_POS(vec);
218+
if (*reg)
219+
return fls(*reg) - 1 + vec;
220+
}
216221

217-
if (likely(!word_offset && !word[0]))
218-
return -1;
219-
else
220-
return fls(word[word_offset << 2]) - 1 + (word_offset << 5);
222+
return -1;
221223
}
222224

223225
static u8 count_vectors(void *bitmap)
224226
{
225-
u32 *word = bitmap;
226-
int word_offset;
227+
int vec;
228+
u32 *reg;
227229
u8 count = 0;
228-
for (word_offset = 0; word_offset < MAX_APIC_VECTOR >> 5; ++word_offset)
229-
count += hweight32(word[word_offset << 2]);
230+
231+
for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) {
232+
reg = bitmap + REG_POS(vec);
233+
count += hweight32(*reg);
234+
}
235+
230236
return count;
231237
}
232238

0 commit comments

Comments
 (0)