xenfb.c: avoid expensive loops when prod <= out_cons
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
@@ -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;
Quoted 3 lines · Stefano Stabellini
If the frontend sets
out_consto a value higher thanout_prod, it will causexenfb_handle_eventsto 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
Quoted 9 lines · David Vrabel
Quoted 3 lines · Stefano Stabellini
If the frontend sets
out_consto a value higher thanout_prod, it will causexenfb_handle_eventsto 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
@@ -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);
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_consto a value higher thanout_prod, it will causexenfb_handle_eventsto 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
Signed-off-by Stefano Stabellini <[email protected]>hw/display/xenfb.c +4
-3@@ -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;