RE: [PATCH] video: hyperv: hyperv_fb: Support deferred IO for Hyper-V frame buffer driver

2019-08-24 Thread Michael Kelley
From: Wei Hu  Sent: Wednesday, August 21, 2019 4:59 AM
>
> > From: Michael Kelley 
> > Sent: Monday, August 19, 2019 6:41 AM
> > To: Wei Hu ; rdun...@infradead.org; shc_w...@mail.ru;
> 
> > > - msg.dirt.rect[0].x1 = 0;
> > > - msg.dirt.rect[0].y1 = 0;
> > > - msg.dirt.rect[0].x2 = info->var.xres;
> > > - msg.dirt.rect[0].y2 = info->var.yres;
> > > + msg.dirt.rect[0].x1 = (x1 < 0 || x1 > x2) ? 0 : x1;
> > > + msg.dirt.rect[0].y1 = (y2 < 0 || y1 > y2) ? 0 : y1;
> >
> > This should be:
> >
> > msg.dirt.rect[0].y1 = (y1 < 0 || y1 > y2) ? 0 : y1;
> >
> > Also, throughout the code, I don't think there are any places where
> > x or y coordinate values are ever negative.  INT_MAX or 0 is used as the
> > sentinel value indicating "not set".  So can all the tests for less than 0
> > now be eliminated, both in this function and in other functions?
> >
> > > + msg.dirt.rect[0].x2 =
> > > + (x2 < x1 || x2 > info->var.xres) ? info->var.xres : x2;
> > > + msg.dirt.rect[0].y2 =
> > > + (y2 < y1 || y2 > info->var.yres) ? info->var.yres : y2;
> >
> > How exactly is the dirty rectangle specified to Hyper-V?  Suppose the frame
> > buffer resolution is 100x200.  If you want to specify the entire rectangle, 
> > the
> > first coordinate is (0, 0).  But what is the second coordinate?  Should it 
> > be
> > (99, 199) or (100, 200)?  The above code (and original code) implies it
> > should specified as (100, 200), which is actually a point outside the
> > maximum resolution, which is counter-intuitive and makes me wonder
> > if the code is correct.
> >
> [Wei Hu]
> The current code treat the entire framebuffer rectangle as (0,0) -> 
> (var.xres, var.yres).
> Every time it sends refresh request, these are two points sent to host and 
> host
> seems accept it. See the above (x1, y1) and (x2, y2)  in the deleted lines.
> 
> So in your example the second coordinate is (100, 200).

OK, agreed.  I ran some experiments and confirmed that this is indeed the
Hyper-V behavior.

> 
> 
> > > +/* Deferred IO callback */
> > > +static void synthvid_deferred_io(struct fb_info *p,
> > > +  struct list_head *pagelist)
> > > +{
> > > + struct hvfb_par *par = p->par;
> > > + struct page *page;
> > > + unsigned long start, end;
> > > + int y1, y2, miny, maxy;
> > > + unsigned long flags;
> > > +
> > > + miny = INT_MAX;
> > > + maxy = 0;
> > > +
> > > + list_for_each_entry(page, pagelist, lru) {
> > > + start = page->index << PAGE_SHIFT;
> > > + end = start + PAGE_SIZE - 1;
> > > + y1 = start / p->fix.line_length;
> > > + y2 = end / p->fix.line_length;
> >
> > The above division rounds down because any remainder is discarded.  I
> > wondered whether rounding down is correct, which got me to thinking
> > about how the dirty rectangle is specified.  Is y2 the index of the last
> > dirty row?  If so, that's not consistent with the code in synthvid_update(),
> > which might choose var.yres as y2, and that's the index of a row outside
> > of the frame buffer.
> >
> [Wei Hu]
> In this place we try to figure out and merge all the faulted pages into one
> big dirty rectangle. A page in memory represents one or multiple lines in
> frame buffer. For example, one faulted page could represent all the linear
> pixels from (x, y) to (x-1, y+1). In this case we just form the dirty 
> rectangle
> as (0, y) -> (var.xres, y+1). Also keep in mind we need to merge multiple
> pages. That's why in the end the dirty rectangle is (0, miny) -> (var.xres, 
> maxy).

Let me give an example of where I think the new code doesn't work.  Suppose
the frame buffer resolution is 1024x768.  With 4 bytes per pixel, each row
is 4096 bytes, or exactly one page.  So each page contains exactly one row of
pixels. For simplicity in my example, let's look at the case when this function
is called with only one dirty page.   The calculation of y1 will identify the 
row
that is dirty.   The calculation of y2 will identify the same row.  So y1 will
equal y2, and miny will equal maxy.  Then when synthvid_update() is called,
Hyper-V will interpret the parameters as no rows needing to be updated.  In
a more complex case where the pagelist contains multiple dirty pages, maxy
also ends up one less than it needs to be.

I think passing 'maxy + 1' instead of 'maxy' to synthvid_update() will solve
the problem.  It certainly warrants a comment that the calculation of maxy
is "inclusive", while synthvid_update() expects its parameters to be "exclusive"
per Hyper-V's expectations.

There's also another interesting situation.  Suppose the resolution and page 
size
is such that a page contains multiple rows.  If the last page of the frame 
buffer
is dirty, this routine could calculate a y2 value identifying a "phantom" row
that is off the end of the frame buffer -- i.e., that is bigger than yres.  You
have synthvid_send() handling that case by clamping the y2 value to yres, but
it might be worth a comment here acknowledging the 

RE: [PATCH] video: hyperv: hyperv_fb: Support deferred IO for Hyper-V frame buffer driver

2019-08-22 Thread Wei Hu
Thanks Michael. See my reply inline to some of your comments.

> -Original Message-
> From: Michael Kelley 
> Sent: Monday, August 19, 2019 6:41 AM
> To: Wei Hu ; rdun...@infradead.org; shc_w...@mail.ru;

> > -   msg.dirt.rect[0].x1 = 0;
> > -   msg.dirt.rect[0].y1 = 0;
> > -   msg.dirt.rect[0].x2 = info->var.xres;
> > -   msg.dirt.rect[0].y2 = info->var.yres;
> > +   msg.dirt.rect[0].x1 = (x1 < 0 || x1 > x2) ? 0 : x1;
> > +   msg.dirt.rect[0].y1 = (y2 < 0 || y1 > y2) ? 0 : y1;
> 
> This should be:
> 
>   msg.dirt.rect[0].y1 = (y1 < 0 || y1 > y2) ? 0 : y1;
> 
> Also, throughout the code, I don't think there are any places where
> x or y coordinate values are ever negative.  INT_MAX or 0 is used as the
> sentinel value indicating "not set".  So can all the tests for less than 0
> now be eliminated, both in this function and in other functions?
> 
> > +   msg.dirt.rect[0].x2 =
> > +   (x2 < x1 || x2 > info->var.xres) ? info->var.xres : x2;
> > +   msg.dirt.rect[0].y2 =
> > +   (y2 < y1 || y2 > info->var.yres) ? info->var.yres : y2;
> 
> How exactly is the dirty rectangle specified to Hyper-V?  Suppose the frame
> buffer resolution is 100x200.  If you want to specify the entire rectangle, 
> the
> first coordinate is (0, 0).  But what is the second coordinate?  Should it be
> (99, 199) or (100, 200)?  The above code (and original code) implies it
> should specified as (100, 200), which is actually a point outside the
> maximum resolution, which is counter-intuitive and makes me wonder
> if the code is correct.
> 
[Wei Hu] 
The current code treat the entire framebuffer rectangle as (0,0) -> (var.xres, 
var.yres).
Every time it sends refresh request, these are two points sent to host and host
seems accept it. See the above (x1, y1) and (x2, y2)  in the deleted lines.

So in your example the second coordinate is (100, 200). 


> > +/* Deferred IO callback */
> > +static void synthvid_deferred_io(struct fb_info *p,
> > +struct list_head *pagelist)
> > +{
> > +   struct hvfb_par *par = p->par;
> > +   struct page *page;
> > +   unsigned long start, end;
> > +   int y1, y2, miny, maxy;
> > +   unsigned long flags;
> > +
> > +   miny = INT_MAX;
> > +   maxy = 0;
> > +
> > +   list_for_each_entry(page, pagelist, lru) {
> > +   start = page->index << PAGE_SHIFT;
> > +   end = start + PAGE_SIZE - 1;
> > +   y1 = start / p->fix.line_length;
> > +   y2 = end / p->fix.line_length;
> 
> The above division rounds down because any remainder is discarded.  I
> wondered whether rounding down is correct, which got me to thinking
> about how the dirty rectangle is specified.  Is y2 the index of the last
> dirty row?  If so, that's not consistent with the code in synthvid_update(),
> which might choose var.yres as y2, and that's the index of a row outside
> of the frame buffer.
> 
[Wei Hu] 
In this place we try to figure out and merge all the faulted pages into one
big dirty rectangle. A page in memory represents one or multiple lines in
frame buffer. For example, one faulted page could represent all the linear 
pixels from (x, y) to (x-1, y+1). In this case we just form the dirty rectangle
as (0, y) -> (var.xres, y+1). Also keep in mind we need to merge multiple
pages. That's why in the end the dirty rectangle is (0, miny) -> (var.xres, 
maxy).


> > +   if (y2 > p->var.yres)
> > +   y2 = p->var.yres;
> > +   miny = min_t(int, miny, y1);
> > +   maxy = max_t(int, maxy, y2);
> > +
> > +   /* Copy from dio space to mmio address */
> > +   if (par->fb_ready) {
> > +   spin_lock_irqsave(>docopy_lock, flags);
> > +   hvfb_docopy(par, start, PAGE_SIZE);
> > +   spin_unlock_irqrestore(>docopy_lock, flags);
> > +   }
> > +   }
> > +
> > +   if (par->fb_ready)
> > +   synthvid_update(p, 0, miny, p->var.xres, maxy);
> > +}




> > +
> > +   if (j == info->var.yres)
> > +   break;
> > +   hvfb_docopy(par,
> > +   j * info->fix.line_length +
> > +   (x1 * screen_depth / 8),
> > +   (x2 - x1 + 1) * screen_depth / 8);
> 
> Whether the +1 is needed above gets back to the question I
> raised earlier about how to interpret the coordinates -- whether
> the (x2, y2) coordinate is just outside the dirty rectangle or
> just inside the dirty rectangle.  Most of the code seems to treat
> it as being just outside the dirty rectangle, in which case the +1
> should not be used.
> 
[Wei Hu] 
This dirty rectangle is not from page fault, but rather from frame buffer
framework when the screen is in text mode. I am not 100% sure if the dirty
rectangle given from kernel includes on extra line outside or not.  Here I 
just play it safe by copying one extra line in the worst case.

Suppose dirty rectangle only contain one pixel, for example (0,0) is the only
pixel 

RE: [PATCH] video: hyperv: hyperv_fb: Support deferred IO for Hyper-V frame buffer driver

2019-08-18 Thread Michael Kelley
From: Wei Hu  Sent: Tuesday, August 13, 2019 3:37 AM
> 
> Without deferred IO support, hyperv_fb driver informs the host to refresh
> the entire guest frame buffer at fixed rate, e.g. at 20Hz, no matter there
> is screen update or not. This patch supports defered IO for screens in

s/defered/deferred/

> graphic mode and also enables the framme buffer on-demand refresh. The

s/graphic/graphics/
s/framme/frame/

> highest refresh rate is still set at 20Hz.
> 
> Due to limitation on Hyper-V host, we keep a shadow copy of frame buffer

I think it might be worthwhile to explain exactly what the issue is so that
there's a record kept.

> in the guest. This means one more copy of the dirty rectangle inside
> guest when doing the on-demand refresh. This can be optimized in the
> future with help from host. For now the host performance gain from deferred
> IO outweighs the shadow copy impact in the guest.
> 
> Signed-off-by: Wei Hu 
> ---
>  drivers/video/fbdev/Kconfig |   1 +
>  drivers/video/fbdev/hyperv_fb.c | 217 +---
>  2 files changed, 198 insertions(+), 20 deletions(-)
> 
> diff --git a/drivers/video/fbdev/Kconfig b/drivers/video/fbdev/Kconfig
> index 1b2f5f31fb6f..e781f89a1824 100644
> --- a/drivers/video/fbdev/Kconfig
> +++ b/drivers/video/fbdev/Kconfig
> @@ -2241,6 +2241,7 @@ config FB_HYPERV
>   select FB_CFB_FILLRECT
>   select FB_CFB_COPYAREA
>   select FB_CFB_IMAGEBLIT
> + select FB_DEFERRED_IO
>   help
> This framebuffer driver supports Microsoft Hyper-V Synthetic Video.
> 
> diff --git a/drivers/video/fbdev/hyperv_fb.c b/drivers/video/fbdev/hyperv_fb.c
> index 1042f3311fa2..85198a6ea8e7 100644
> --- a/drivers/video/fbdev/hyperv_fb.c
> +++ b/drivers/video/fbdev/hyperv_fb.c
> @@ -233,6 +233,7 @@ struct synthvid_msg {
>  #define RING_BUFSIZE (256 * 1024)
>  #define VSP_TIMEOUT (10 * HZ)
>  #define HVFB_UPDATE_DELAY (HZ / 20)
> +#define HVFB_ONDEMAND_THROTTLE (HZ / 20)
> 
>  struct hvfb_par {
>   struct fb_info *info;
> @@ -252,6 +253,17 @@ struct hvfb_par {
>   bool synchronous_fb;
> 
>   struct notifier_block hvfb_panic_nb;
> +
> + /* Memory for deferred IO and frame buffer itself */
> + unsigned char *dio_vp;
> + unsigned char *mmio_vp;
> + unsigned long mmio_pp;
> + spinlock_t docopy_lock; /* Lock to protect memory copy */
> +
> + /* Dirty rectangle, protected by delayed_refresh_lock */
> + int x1, y1, x2, y2;
> + bool delayed_refresh;
> + spinlock_t delayed_refresh_lock;
>  };
> 
>  static uint screen_width = HVFB_WIDTH;
> @@ -260,6 +272,7 @@ static uint screen_width_max = HVFB_WIDTH;
>  static uint screen_height_max = HVFB_HEIGHT;
>  static uint screen_depth;
>  static uint screen_fb_size;
> +static uint dio_fb_size; /* FB size for deferred IO */
> 
>  /* Send message to Hyper-V host */
>  static inline int synthvid_send(struct hv_device *hdev,
> @@ -346,28 +359,88 @@ static int synthvid_send_ptr(struct hv_device *hdev)
>  }
> 
>  /* Send updated screen area (dirty rectangle) location to host */
> -static int synthvid_update(struct fb_info *info)
> +static int
> +synthvid_update(struct fb_info *info, int x1, int y1, int x2, int y2)
>  {
>   struct hv_device *hdev = device_to_hv_device(info->device);
>   struct synthvid_msg msg;
> 
>   memset(, 0, sizeof(struct synthvid_msg));
> + if (x2 == INT_MAX)
> + x2 = info->var.xres;
> + if (y2 == INT_MAX)
> + y2 = info->var.yres;
> 
>   msg.vid_hdr.type = SYNTHVID_DIRT;
>   msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
>   sizeof(struct synthvid_dirt);
>   msg.dirt.video_output = 0;
>   msg.dirt.dirt_count = 1;
> - msg.dirt.rect[0].x1 = 0;
> - msg.dirt.rect[0].y1 = 0;
> - msg.dirt.rect[0].x2 = info->var.xres;
> - msg.dirt.rect[0].y2 = info->var.yres;
> + msg.dirt.rect[0].x1 = (x1 < 0 || x1 > x2) ? 0 : x1;
> + msg.dirt.rect[0].y1 = (y2 < 0 || y1 > y2) ? 0 : y1;

This should be:

msg.dirt.rect[0].y1 = (y1 < 0 || y1 > y2) ? 0 : y1;

Also, throughout the code, I don't think there are any places where
x or y coordinate values are ever negative.  INT_MAX or 0 is used as the
sentinel value indicating "not set".  So can all the tests for less than 0
now be eliminated, both in this function and in other functions?

> + msg.dirt.rect[0].x2 =
> + (x2 < x1 || x2 > info->var.xres) ? info->var.xres : x2;
> + msg.dirt.rect[0].y2 =
> + (y2 < y1 || y2 > info->var.yres) ? info->var.yres : y2;

How exactly is the dirty rectangle specified to Hyper-V?  Suppose the frame
buffer resolution is 100x200.  If you want to specify the entire rectangle, the
first coordinate is (0, 0).  But what is the second coordinate?  Should it be
(99, 199) or (100, 200)?  The above code (and original code) implies it
should specified as (100, 200), which is actually a point outside the
maximum resolution, which is counter-intuitive and makes me