Thread (10 messages) 10 messages, 3 authors, 2023-02-10

Re: [PATCH v2] fbdev: Fix invalid page access after closing deferred I/O devices

From: Thomas Zimmermann <tzimmermann@suse.de>
Date: 2023-01-30 08:28:46
Also in: dri-devel, lkml

Hi

Am 29.01.23 um 09:28 schrieb Takashi Iwai:
When a fbdev with deferred I/O is once opened and closed, the dirty
pages still remain queued in the pageref list, and eventually later
those may be processed in the delayed work.  This may lead to a
corruption of pages, hitting an Oops.
Do you have more information on this problem?

The mmap'ed buffer of the fbdev device comes from a vmalloc call. That 
memory's location never changes; even across pairs of open/close on the 
device file. I'm surprised that a page entry becomes invalid.

In drm_fbdev_cleanup(), we first remove the fbdefio at [1] and then 
vfree() the shadow buffer. So the memory should still be around until 
fbdevio is gone.

[1] 
https://elixir.bootlin.com/linux/latest/source/drivers/gpu/drm/drm_fb_helper.c#L2146
This patch makes sure to cancel the delayed work and clean up the
pageref list at closing the device for addressing the bug.  A part of
the cleanup code is factored out as a new helper function that is
called from the common fb_release().
The delayed work is required to copy the framebuffer to the device 
output. So if it's just canceled, could this result in missing updates?

There's a call to cancel_delayed_work_sync() in the new helper 
fb_deferred_io_release(). Is this the right function? Maybe 
flush_delayed_work() is a better choice.
Reviewed-by: Patrik Jakobsson <patrik.r.jakobsson@gmail.com>
Cc: <redacted>
Signed-off-by: Takashi Iwai <redacted>
This could use a Fixes tag. It's not exactly clear to me when this 
problem got originally introduced, but the recent refactoring seems a 
candidate.

Fixes: 56c134f7f1b5 ("fbdev: Track deferred-I/O pages in pageref struct")
Cc: Thomas Zimmermann <tzimmermann@suse.de>
Cc: Javier Martinez Canillas <javierm@redhat.com>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Maxime Ripard <mripard@kernel.org>
Cc: Zack Rusin <redacted>
Cc: VMware Graphics Reviewers <redacted>
Cc: Jaya Kumar <jayalk@intworks.biz>
Cc: Daniel Vetter <redacted>
Cc: "K. Y. Srinivasan" <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Cc: Wei Liu <wei.liu@kernel.org>
Cc: Dexuan Cui <decui@microsoft.com>
Cc: Steve Glendinning <steve.glendinning@shawell.net>
Cc: Bernie Thompson <bernie@plugable.com>
Cc: Helge Deller <deller@gmx.de>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Stephen Kitt <redacted>
Cc: Peter Suti <redacted>
Cc: Sam Ravnborg <redacted>
Cc: Geert Uytterhoeven <geert+renesas@glider.be>
Cc: ye xingchen <redacted>
Cc: Petr Mladek <pmladek@suse.com>
Cc: John Ogness <john.ogness@linutronix.de>
Cc: Tom Rix <trix@redhat.com>
Cc: dri-devel@lists.freedesktop.org
Cc: linux-fbdev@vger.kernel.org
Cc: linux-hyperv@vger.kernel.org
Cc: <redacted> # v5.19+
quoted hunk ↗ jump to hunk
---
v1->v2: Fix build error without CONFIG_FB_DEFERRED_IO

  drivers/video/fbdev/core/fb_defio.c | 10 +++++++++-
  drivers/video/fbdev/core/fbmem.c    |  4 ++++
  include/linux/fb.h                  |  1 +
  3 files changed, 14 insertions(+), 1 deletion(-)
diff --git a/drivers/video/fbdev/core/fb_defio.c b/drivers/video/fbdev/core/fb_defio.c
index c730253ab85c..583cbcf09446 100644
--- a/drivers/video/fbdev/core/fb_defio.c
+++ b/drivers/video/fbdev/core/fb_defio.c
@@ -313,7 +313,7 @@ void fb_deferred_io_open(struct fb_info *info,
  }
  EXPORT_SYMBOL_GPL(fb_deferred_io_open);
  
-void fb_deferred_io_cleanup(struct fb_info *info)
+void fb_deferred_io_release(struct fb_info *info)
  {
  	struct fb_deferred_io *fbdefio = info->fbdefio;
  	struct page *page;
@@ -327,6 +327,14 @@ void fb_deferred_io_cleanup(struct fb_info *info)
  		page = fb_deferred_io_page(info, i);
  		page->mapping = NULL;
  	}
+}
+EXPORT_SYMBOL_GPL(fb_deferred_io_release);
It's all in the same module. No need to export this symbol.

Best regards
Thomas
quoted hunk ↗ jump to hunk
+
+void fb_deferred_io_cleanup(struct fb_info *info)
+{
+	struct fb_deferred_io *fbdefio = info->fbdefio;
+
+	fb_deferred_io_release(info);
  
  	kvfree(info->pagerefs);
  	mutex_destroy(&fbdefio->lock);
diff --git a/drivers/video/fbdev/core/fbmem.c b/drivers/video/fbdev/core/fbmem.c
index 3a6c8458eb8d..ab3545a00abc 100644
--- a/drivers/video/fbdev/core/fbmem.c
+++ b/drivers/video/fbdev/core/fbmem.c
@@ -1454,6 +1454,10 @@ __releases(&info->lock)
  	struct fb_info * const info = file->private_data;
  
  	lock_fb_info(info);
+#if IS_ENABLED(CONFIG_FB_DEFERRED_IO)
+	if (info->fbdefio)
+		fb_deferred_io_release(info);
+#endif
  	if (info->fbops->fb_release)
  		info->fbops->fb_release(info,1);
quoted hunk ↗ jump to hunk
  	module_put(info->fbops->owner);
diff --git a/include/linux/fb.h b/include/linux/fb.h
index 96b96323e9cb..73eb1f85ea8e 100644
--- a/include/linux/fb.h
+++ b/include/linux/fb.h
@@ -662,6 +662,7 @@ extern int  fb_deferred_io_init(struct fb_info *info);
  extern void fb_deferred_io_open(struct fb_info *info,
  				struct inode *inode,
  				struct file *file);
+extern void fb_deferred_io_release(struct fb_info *info);
  extern void fb_deferred_io_cleanup(struct fb_info *info);
  extern int fb_deferred_io_fsync(struct file *file, loff_t start,
  				loff_t end, int datasync);
-- 
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

Attachments

Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help