Skip to content

Commit eb76d0f

Browse files
committed
drm, fbcon, vga_switcheroo: Avoid race condition in fbcon setup
Protect vga_switcheroo_client_fb_set() with console lock. Avoids OOB access in fbcon_remap_all(). Without holding the console lock the call races with switching outputs. VGA switcheroo calls fbcon_remap_all() when switching clients. The fbcon function uses struct fb_info.node, which is set by register_framebuffer(). As the fb-helper code currently sets up VGA switcheroo before registering the framebuffer, the value of node is -1 and therefore not a legal value. For example, fbcon uses the value within set_con2fb_map() [1] as an index into an array. Moving vga_switcheroo_client_fb_set() after register_framebuffer() can result in VGA switching that does not switch fbcon correctly. Therefore move vga_switcheroo_client_fb_set() under fbcon_fb_registered(), which already holds the console lock. Fbdev calls fbcon_fb_registered() from within register_framebuffer(). Serializes the helper with VGA switcheroo's call to fbcon_remap_all(). Although vga_switcheroo_client_fb_set() takes an instance of struct fb_info as parameter, it really only needs the contained fbcon state. Moving the call to fbcon initialization is therefore cleaner than before. Only amdgpu, i915, nouveau and radeon support vga_switcheroo. For all other drivers, this change does nothing. Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de> Link: https://elixir.bootlin.com/linux/v6.17/source/drivers/video/fbdev/core/fbcon.c#L2942 # [1] Fixes: 6a9ee8a ("vga_switcheroo: initial implementation (v15)") Acked-by: Javier Martinez Canillas <javierm@redhat.com> Acked-by: Alex Deucher <alexander.deucher@amd.com> Cc: dri-devel@lists.freedesktop.org Cc: nouveau@lists.freedesktop.org Cc: amd-gfx@lists.freedesktop.org Cc: linux-fbdev@vger.kernel.org Cc: <stable@vger.kernel.org> # v2.6.34+ Link: https://patch.msgid.link/20251105161549.98836-1-tzimmermann@suse.de
1 parent 620a8f1 commit eb76d0f

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

drivers/gpu/drm/drm_fb_helper.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131

3232
#include <linux/console.h>
3333
#include <linux/export.h>
34-
#include <linux/pci.h>
3534
#include <linux/sysrq.h>
36-
#include <linux/vga_switcheroo.h>
3735

3836
#include <drm/drm_atomic.h>
3937
#include <drm/drm_drv.h>
@@ -566,11 +564,6 @@ EXPORT_SYMBOL(drm_fb_helper_release_info);
566564
*/
567565
void drm_fb_helper_unregister_info(struct drm_fb_helper *fb_helper)
568566
{
569-
struct fb_info *info = fb_helper->info;
570-
struct device *dev = info->device;
571-
572-
if (dev_is_pci(dev))
573-
vga_switcheroo_client_fb_set(to_pci_dev(dev), NULL);
574567
unregister_framebuffer(fb_helper->info);
575568
}
576569
EXPORT_SYMBOL(drm_fb_helper_unregister_info);
@@ -1632,7 +1625,6 @@ static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper)
16321625
struct drm_client_dev *client = &fb_helper->client;
16331626
struct drm_device *dev = fb_helper->dev;
16341627
struct drm_fb_helper_surface_size sizes;
1635-
struct fb_info *info;
16361628
int ret;
16371629

16381630
if (drm_WARN_ON(dev, !dev->driver->fbdev_probe))
@@ -1653,12 +1645,6 @@ static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper)
16531645

16541646
strcpy(fb_helper->fb->comm, "[fbcon]");
16551647

1656-
info = fb_helper->info;
1657-
1658-
/* Set the fb info for vgaswitcheroo clients. Does nothing otherwise. */
1659-
if (dev_is_pci(info->device))
1660-
vga_switcheroo_client_fb_set(to_pci_dev(info->device), info);
1661-
16621648
return 0;
16631649
}
16641650

drivers/video/fbdev/core/fbcon.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#include <linux/string.h>
6767
#include <linux/kd.h>
6868
#include <linux/panic.h>
69+
#include <linux/pci.h>
6970
#include <linux/printk.h>
7071
#include <linux/slab.h>
7172
#include <linux/fb.h>
@@ -78,6 +79,7 @@
7879
#include <linux/interrupt.h>
7980
#include <linux/crc32.h> /* For counting font checksums */
8081
#include <linux/uaccess.h>
82+
#include <linux/vga_switcheroo.h>
8183
#include <asm/irq.h>
8284

8385
#include "fbcon.h"
@@ -2899,6 +2901,9 @@ void fbcon_fb_unregistered(struct fb_info *info)
28992901

29002902
console_lock();
29012903

2904+
if (info->device && dev_is_pci(info->device))
2905+
vga_switcheroo_client_fb_set(to_pci_dev(info->device), NULL);
2906+
29022907
fbcon_registered_fb[info->node] = NULL;
29032908
fbcon_num_registered_fb--;
29042909

@@ -3032,6 +3037,10 @@ static int do_fb_registered(struct fb_info *info)
30323037
}
30333038
}
30343039

3040+
/* Set the fb info for vga_switcheroo clients. Does nothing otherwise. */
3041+
if (info->device && dev_is_pci(info->device))
3042+
vga_switcheroo_client_fb_set(to_pci_dev(info->device), info);
3043+
30353044
return ret;
30363045
}
30373046

0 commit comments

Comments
 (0)