All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] KVM: x86: lapic: Fix the misuse of likely() in find_highest_vector()
@ 2012-08-24  9:15 Takuya Yoshikawa
  2012-08-27 20:25 ` Marcelo Tosatti
  0 siblings, 1 reply; 22+ messages in thread
From: Takuya Yoshikawa @ 2012-08-24  9:15 UTC (permalink / raw)
  To: avi, mtosatti; +Cc: kvm

Although returning -1 should be likely according to the likely(),
the ASSERT in apic_find_highest_irr() will be triggered in such a case.
It seems that this optimization is not working as expected.

This patch simplifies the logic to mitigate this issue: search for the
first non-zero word in a for loop and then use __fls() if found.  When
nothing found, we are out of the loop, so we can just return -1.

Signed-off-by: Takuya Yoshikawa <yoshikawa.takuya@oss.ntt.co.jp>
---
 arch/x86/kvm/lapic.c |   18 ++++++++++--------
 1 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index 18d149d..5eb4dde 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -208,16 +208,18 @@ static unsigned int apic_lvt_mask[APIC_LVT_NUM] = {
 
 static int find_highest_vector(void *bitmap)
 {
-	u32 *word = bitmap;
-	int word_offset = MAX_APIC_VECTOR >> 5;
+	u32 *p = bitmap;
+	u32 word;
+	int word_offset;
 
-	while ((word_offset != 0) && (word[(--word_offset) << 2] == 0))
-		continue;
+	for (word_offset = (MAX_APIC_VECTOR >> 5) - 1;
+	     word_offset >= 0; --word_offset) {
+		word = p[word_offset << 2];
+		if (word)
+			return __fls(word) + (word_offset << 5);
+	}
 
-	if (likely(!word_offset && !word[0]))
-		return -1;
-	else
-		return fls(word[word_offset << 2]) - 1 + (word_offset << 5);
+	return -1;
 }
 
 static u8 count_vectors(void *bitmap)
-- 
1.7.5.4


^ permalink raw reply related	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2012-09-12 17:05 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-24  9:15 [PATCH] KVM: x86: lapic: Fix the misuse of likely() in find_highest_vector() Takuya Yoshikawa
2012-08-27 20:25 ` Marcelo Tosatti
2012-08-28  9:57   ` Takuya Yoshikawa
2012-08-29 19:10     ` Marcelo Tosatti
2012-08-29 22:51       ` Michael S. Tsirkin
2012-08-30  1:09         ` Takuya Yoshikawa
2012-08-30  6:37           ` Michael S. Tsirkin
2012-08-30  9:50             ` Takuya Yoshikawa
2012-08-30 10:10               ` Michael S. Tsirkin
2012-08-30 10:24                 ` Takuya Yoshikawa
2012-08-30 10:44                   ` Michael S. Tsirkin
2012-08-30 12:30                     ` [PATCH -v3] KVM: x86: lapic: Clean up find_highest_vector() and count_vectors() Takuya Yoshikawa
2012-08-30 13:21                       ` Michael S. Tsirkin
2012-08-30 16:09                         ` Takuya Yoshikawa
2012-08-30 16:49                           ` Michael S. Tsirkin
2012-09-05  8:30                             ` Takuya Yoshikawa
2012-09-05  9:26                               ` Michael S. Tsirkin
2012-09-05  9:40                                 ` Takuya Yoshikawa
2012-09-05  9:51                                   ` Michael S. Tsirkin
2012-09-05 10:30                                     ` [PATCH -v4] " Takuya Yoshikawa
2012-09-05 10:58                                       ` Michael S. Tsirkin
2012-09-12 16:39                                       ` Marcelo Tosatti

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.