mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] fbdev: Fix incorrect page mapping clearance at fb_deferred_io_release()
@ 2023-03-08 10:50 Takashi Iwai
  2023-03-10 12:18 ` Patrik Jakobsson
  2023-03-10 14:23 ` Thomas Zimmermann
  0 siblings, 2 replies; 3+ messages in thread
From: Takashi Iwai @ 2023-03-08 10:50 UTC (permalink / raw)
  To: Thomas Zimmermann
  Cc: linux-fbdev, dri-devel, linux-kernel, Patrik Jakobsson,
	Helge Deller, Miko Larsson

The recent fix for the deferred I/O by the commit
  3efc61d95259 ("fbdev: Fix invalid page access after closing deferred I/O devices")
caused a regression when the same fb device is opened/closed while
it's being used.  It resulted in a frozen screen even if something
is redrawn there after the close.  The breakage is because the patch
was made under a wrong assumption of a single open; in the current
code, fb_deferred_io_release() cleans up the page mapping of the
pageref list and it calls cancel_delayed_work_sync() unconditionally,
where both are no correct behavior for multiple opens.

This patch adds a refcount for the opens of the device, and applies
the cleanup only when all files get closed.

As both fb_deferred_io_open() and _close() are called always in the
fb_info lock (mutex), it's safe to use the normal int for the
refcounting.

Also, a useless BUG_ON() is dropped.

Fixes: 3efc61d95259 ("fbdev: Fix invalid page access after closing deferred I/O devices")
Cc: <stable@vger.kernel.org>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
v1->v2:
* Rename to fb_deferred_io_lastclose()
* Rename the new field from opens to open_count
* Removed unused variable
* More comments about fb_info locking

 drivers/video/fbdev/core/fb_defio.c | 17 +++++++++++++----
 include/linux/fb.h                  |  1 +
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/drivers/video/fbdev/core/fb_defio.c b/drivers/video/fbdev/core/fb_defio.c
index aa5f059d0222..274f5d0fa247 100644
--- a/drivers/video/fbdev/core/fb_defio.c
+++ b/drivers/video/fbdev/core/fb_defio.c
@@ -305,17 +305,18 @@ void fb_deferred_io_open(struct fb_info *info,
 			 struct inode *inode,
 			 struct file *file)
 {
+	struct fb_deferred_io *fbdefio = info->fbdefio;
+
 	file->f_mapping->a_ops = &fb_deferred_io_aops;
+	fbdefio->open_count++;
 }
 EXPORT_SYMBOL_GPL(fb_deferred_io_open);
 
-void fb_deferred_io_release(struct fb_info *info)
+static void fb_deferred_io_lastclose(struct fb_info *info)
 {
-	struct fb_deferred_io *fbdefio = info->fbdefio;
 	struct page *page;
 	int i;
 
-	BUG_ON(!fbdefio);
 	cancel_delayed_work_sync(&info->deferred_work);
 
 	/* clear out the mapping that we setup */
@@ -324,13 +325,21 @@ void fb_deferred_io_release(struct fb_info *info)
 		page->mapping = NULL;
 	}
 }
+
+void fb_deferred_io_release(struct fb_info *info)
+{
+	struct fb_deferred_io *fbdefio = info->fbdefio;
+
+	if (!--fbdefio->open_count)
+		fb_deferred_io_lastclose(info);
+}
 EXPORT_SYMBOL_GPL(fb_deferred_io_release);
 
 void fb_deferred_io_cleanup(struct fb_info *info)
 {
 	struct fb_deferred_io *fbdefio = info->fbdefio;
 
-	fb_deferred_io_release(info);
+	fb_deferred_io_lastclose(info);
 
 	kvfree(info->pagerefs);
 	mutex_destroy(&fbdefio->lock);
diff --git a/include/linux/fb.h b/include/linux/fb.h
index d8d20514ea05..02d09cb57f6c 100644
--- a/include/linux/fb.h
+++ b/include/linux/fb.h
@@ -212,6 +212,7 @@ struct fb_deferred_io {
 	/* delay between mkwrite and deferred handler */
 	unsigned long delay;
 	bool sort_pagereflist; /* sort pagelist by offset */
+	int open_count; /* number of opened files; protected by fb_info lock */
 	struct mutex lock; /* mutex that protects the pageref list */
 	struct list_head pagereflist; /* list of pagerefs for touched pages */
 	/* callback */
-- 
2.35.3


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

* Re: [PATCH v2] fbdev: Fix incorrect page mapping clearance at fb_deferred_io_release()
  2023-03-08 10:50 [PATCH v2] fbdev: Fix incorrect page mapping clearance at fb_deferred_io_release() Takashi Iwai
@ 2023-03-10 12:18 ` Patrik Jakobsson
  2023-03-10 14:23 ` Thomas Zimmermann
  1 sibling, 0 replies; 3+ messages in thread
From: Patrik Jakobsson @ 2023-03-10 12:18 UTC (permalink / raw)
  To: Takashi Iwai
  Cc: Thomas Zimmermann, linux-fbdev, Miko Larsson, Helge Deller,
	linux-kernel, dri-devel, Patrik Jakobsson

On Wed, Mar 8, 2023 at 11:50 AM Takashi Iwai <tiwai@suse.de> wrote:
>
> The recent fix for the deferred I/O by the commit
>   3efc61d95259 ("fbdev: Fix invalid page access after closing deferred I/O devices")
> caused a regression when the same fb device is opened/closed while
> it's being used.  It resulted in a frozen screen even if something
> is redrawn there after the close.  The breakage is because the patch
> was made under a wrong assumption of a single open; in the current
> code, fb_deferred_io_release() cleans up the page mapping of the
> pageref list and it calls cancel_delayed_work_sync() unconditionally,
> where both are no correct behavior for multiple opens.
>
> This patch adds a refcount for the opens of the device, and applies
> the cleanup only when all files get closed.
>
> As both fb_deferred_io_open() and _close() are called always in the
> fb_info lock (mutex), it's safe to use the normal int for the
> refcounting.
>
> Also, a useless BUG_ON() is dropped.
>
> Fixes: 3efc61d95259 ("fbdev: Fix invalid page access after closing deferred I/O devices")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Takashi Iwai <tiwai@suse.de>

Looks good to me

Reviewed-by: Patrik Jakobsson <patrik.r.jakobsson@gmail.com>

> ---
> v1->v2:
> * Rename to fb_deferred_io_lastclose()
> * Rename the new field from opens to open_count
> * Removed unused variable
> * More comments about fb_info locking
>
>  drivers/video/fbdev/core/fb_defio.c | 17 +++++++++++++----
>  include/linux/fb.h                  |  1 +
>  2 files changed, 14 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/video/fbdev/core/fb_defio.c b/drivers/video/fbdev/core/fb_defio.c
> index aa5f059d0222..274f5d0fa247 100644
> --- a/drivers/video/fbdev/core/fb_defio.c
> +++ b/drivers/video/fbdev/core/fb_defio.c
> @@ -305,17 +305,18 @@ void fb_deferred_io_open(struct fb_info *info,
>                          struct inode *inode,
>                          struct file *file)
>  {
> +       struct fb_deferred_io *fbdefio = info->fbdefio;
> +
>         file->f_mapping->a_ops = &fb_deferred_io_aops;
> +       fbdefio->open_count++;
>  }
>  EXPORT_SYMBOL_GPL(fb_deferred_io_open);
>
> -void fb_deferred_io_release(struct fb_info *info)
> +static void fb_deferred_io_lastclose(struct fb_info *info)
>  {
> -       struct fb_deferred_io *fbdefio = info->fbdefio;
>         struct page *page;
>         int i;
>
> -       BUG_ON(!fbdefio);
>         cancel_delayed_work_sync(&info->deferred_work);
>
>         /* clear out the mapping that we setup */
> @@ -324,13 +325,21 @@ void fb_deferred_io_release(struct fb_info *info)
>                 page->mapping = NULL;
>         }
>  }
> +
> +void fb_deferred_io_release(struct fb_info *info)
> +{
> +       struct fb_deferred_io *fbdefio = info->fbdefio;
> +
> +       if (!--fbdefio->open_count)
> +               fb_deferred_io_lastclose(info);
> +}
>  EXPORT_SYMBOL_GPL(fb_deferred_io_release);
>
>  void fb_deferred_io_cleanup(struct fb_info *info)
>  {
>         struct fb_deferred_io *fbdefio = info->fbdefio;
>
> -       fb_deferred_io_release(info);
> +       fb_deferred_io_lastclose(info);
>
>         kvfree(info->pagerefs);
>         mutex_destroy(&fbdefio->lock);
> diff --git a/include/linux/fb.h b/include/linux/fb.h
> index d8d20514ea05..02d09cb57f6c 100644
> --- a/include/linux/fb.h
> +++ b/include/linux/fb.h
> @@ -212,6 +212,7 @@ struct fb_deferred_io {
>         /* delay between mkwrite and deferred handler */
>         unsigned long delay;
>         bool sort_pagereflist; /* sort pagelist by offset */
> +       int open_count; /* number of opened files; protected by fb_info lock */
>         struct mutex lock; /* mutex that protects the pageref list */
>         struct list_head pagereflist; /* list of pagerefs for touched pages */
>         /* callback */
> --
> 2.35.3
>

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

* Re: [PATCH v2] fbdev: Fix incorrect page mapping clearance at fb_deferred_io_release()
  2023-03-08 10:50 [PATCH v2] fbdev: Fix incorrect page mapping clearance at fb_deferred_io_release() Takashi Iwai
  2023-03-10 12:18 ` Patrik Jakobsson
@ 2023-03-10 14:23 ` Thomas Zimmermann
  1 sibling, 0 replies; 3+ messages in thread
From: Thomas Zimmermann @ 2023-03-10 14:23 UTC (permalink / raw)
  To: Takashi Iwai
  Cc: linux-fbdev, dri-devel, linux-kernel, Patrik Jakobsson,
	Helge Deller, Miko Larsson


[-- Attachment #1.1: Type: text/plain, Size: 3901 bytes --]

Added to drm-misc-fixes. Thanks a lot for the patch.

Am 08.03.23 um 11:50 schrieb Takashi Iwai:
> The recent fix for the deferred I/O by the commit
>    3efc61d95259 ("fbdev: Fix invalid page access after closing deferred I/O devices")
> caused a regression when the same fb device is opened/closed while
> it's being used.  It resulted in a frozen screen even if something
> is redrawn there after the close.  The breakage is because the patch
> was made under a wrong assumption of a single open; in the current
> code, fb_deferred_io_release() cleans up the page mapping of the
> pageref list and it calls cancel_delayed_work_sync() unconditionally,
> where both are no correct behavior for multiple opens.
> 
> This patch adds a refcount for the opens of the device, and applies
> the cleanup only when all files get closed.
> 
> As both fb_deferred_io_open() and _close() are called always in the
> fb_info lock (mutex), it's safe to use the normal int for the
> refcounting.
> 
> Also, a useless BUG_ON() is dropped.
> 
> Fixes: 3efc61d95259 ("fbdev: Fix invalid page access after closing deferred I/O devices")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Takashi Iwai <tiwai@suse.de>
> ---
> v1->v2:
> * Rename to fb_deferred_io_lastclose()
> * Rename the new field from opens to open_count
> * Removed unused variable
> * More comments about fb_info locking
> 
>   drivers/video/fbdev/core/fb_defio.c | 17 +++++++++++++----
>   include/linux/fb.h                  |  1 +
>   2 files changed, 14 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/video/fbdev/core/fb_defio.c b/drivers/video/fbdev/core/fb_defio.c
> index aa5f059d0222..274f5d0fa247 100644
> --- a/drivers/video/fbdev/core/fb_defio.c
> +++ b/drivers/video/fbdev/core/fb_defio.c
> @@ -305,17 +305,18 @@ void fb_deferred_io_open(struct fb_info *info,
>   			 struct inode *inode,
>   			 struct file *file)
>   {
> +	struct fb_deferred_io *fbdefio = info->fbdefio;
> +
>   	file->f_mapping->a_ops = &fb_deferred_io_aops;
> +	fbdefio->open_count++;
>   }
>   EXPORT_SYMBOL_GPL(fb_deferred_io_open);
>   
> -void fb_deferred_io_release(struct fb_info *info)
> +static void fb_deferred_io_lastclose(struct fb_info *info)
>   {
> -	struct fb_deferred_io *fbdefio = info->fbdefio;
>   	struct page *page;
>   	int i;
>   
> -	BUG_ON(!fbdefio);
>   	cancel_delayed_work_sync(&info->deferred_work);
>   
>   	/* clear out the mapping that we setup */
> @@ -324,13 +325,21 @@ void fb_deferred_io_release(struct fb_info *info)
>   		page->mapping = NULL;
>   	}
>   }
> +
> +void fb_deferred_io_release(struct fb_info *info)
> +{
> +	struct fb_deferred_io *fbdefio = info->fbdefio;
> +
> +	if (!--fbdefio->open_count)
> +		fb_deferred_io_lastclose(info);
> +}
>   EXPORT_SYMBOL_GPL(fb_deferred_io_release);
>   
>   void fb_deferred_io_cleanup(struct fb_info *info)
>   {
>   	struct fb_deferred_io *fbdefio = info->fbdefio;
>   
> -	fb_deferred_io_release(info);
> +	fb_deferred_io_lastclose(info);
>   
>   	kvfree(info->pagerefs);
>   	mutex_destroy(&fbdefio->lock);
> diff --git a/include/linux/fb.h b/include/linux/fb.h
> index d8d20514ea05..02d09cb57f6c 100644
> --- a/include/linux/fb.h
> +++ b/include/linux/fb.h
> @@ -212,6 +212,7 @@ struct fb_deferred_io {
>   	/* delay between mkwrite and deferred handler */
>   	unsigned long delay;
>   	bool sort_pagereflist; /* sort pagelist by offset */
> +	int open_count; /* number of opened files; protected by fb_info lock */
>   	struct mutex lock; /* mutex that protects the pageref list */
>   	struct list_head pagereflist; /* list of pagerefs for touched pages */
>   	/* callback */

-- 
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 840 bytes --]

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

end of thread, other threads:[~2023-03-10 14:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-08 10:50 [PATCH v2] fbdev: Fix incorrect page mapping clearance at fb_deferred_io_release() Takashi Iwai
2023-03-10 12:18 ` Patrik Jakobsson
2023-03-10 14:23 ` Thomas Zimmermann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®