Skip to contentSkip to conversation

xenfb.c: avoid expensive loops when prod <= out_cons

PATCH Superseded Stefano Stabellini 3 participants 4 messages Jan 6, 2016

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xenfb.c: avoid expensive loops when prod <= out_cons

If the frontend sets out_cons to a value higher than out_prod, it will cause xenfb_handle_events to loop about 2^32 times. Avoid that by using better checks at the beginning of the function.

hw/display/xenfb.c +4 -3
index 4e2a27a..f963cf2 100644
@@ -789,10 +789,11 @@ static void xenfb_handle_events(struct XenFB *xenfb)
 
     prod = page->out_prod;
     out_cons = page->out_cons;
-    if (prod == out_cons)
-	return;
+    if (prod <= out_cons) {
+        return;
+    }
     xen_rmb();		/* ensure we see ring contents up to prod */
-    for (cons = out_cons; cons != prod; cons++) {
+    for (cons = out_cons; cons < prod; cons++) {
 	union xenfb_out_event *event = &XENFB_OUT_RING_REF(page, cons);
         uint8_t type = event->type;
 	int x, y, w, h;
#1 lore raw
Quoted 3 lines · Stefano Stabellini

If the frontend sets out_cons to a value higher than out_prod, it will cause xenfb_handle_events to loop about 2^32 times. Avoid that by using better checks at the beginning of the function.

You can't use less than to compare prod and cons because they wrap.

You need to compare (prod - cons) against ring size (or similar) to check for overflow. See RING_REQUEST_PROD_OVERFLOW() etc.

David

#2 lore raw
Quoted 9 lines · David Vrabel
Quoted 3 lines · Stefano Stabellini

If the frontend sets out_cons to a value higher than out_prod, it will cause xenfb_handle_events to loop about 2^32 times. Avoid that by using better checks at the beginning of the function.

You can't use less than to compare prod and cons because they wrap.

You need to compare (prod - cons) against ring size (or similar) to check for overflow. See RING_REQUEST_PROD_OVERFLOW() etc.

Yes, you are right. I think that the right fix should be:

hw/display/xenfb.c +3 -2
index 4e2a27a..594baff 100644
@@ -789,8 +789,9 @@ static void xenfb_handle_events(struct XenFB *xenfb)
 
     prod = page->out_prod;
     out_cons = page->out_cons;
-    if (prod == out_cons)
-	return;
+    if (prod - out_cons >= XENFB_OUT_RING_LEL) {
+        return;
+    }
     xen_rmb();		/* ensure we see ring contents up to prod */
     for (cons = out_cons; cons != prod; cons++) {
 	union xenfb_out_event *event = &XENFB_OUT_RING_REF(page, cons);
#3 lore raw
In reply to Stefano Stabellini
Quoted 10 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Stefano Stabellini
Sent: 06 January 2016 12:08
To: [email protected]
Cc: [email protected]; [email protected]; Stefano Stabellini
Subject: [Qemu-devel] [PATCH] xenfb.c: avoid expensive loops when prod <= out_cons

Quoted 4 lines

If the frontend sets out_cons to a value higher than out_prod, it will cause xenfb_handle_events to loop about 2^32 times. Avoid that by using better checks at the beginning of the function.

What happens when out_prod wraps?

Paul

Quoted 23 lines
hw/display/xenfb.c +4 -3
index 4e2a27a..f963cf2 100644
@@ -789,10 +789,11 @@ static void xenfb_handle_events(struct XenFB
 *xenfb)
 
     prod = page->out_prod;
     out_cons = page->out_cons;
-    if (prod == out_cons)
-	return;
+    if (prod <= out_cons) {
+        return;
+    }
     xen_rmb();		/* ensure we see ring contents up to prod */
-    for (cons = out_cons; cons != prod; cons++) {
+    for (cons = out_cons; cons < prod; cons++) {
 	union xenfb_out_event *event = &XENFB_OUT_RING_REF(page,
 cons);
         uint8_t type = event->type;

int x, y, w, h;