Skip to content

Commit 482330f

Browse files
tdzgregkh
authored andcommitted
drm, fbcon, vga_switcheroo: Avoid race condition in fbcon setup
[ Upstream commit eb76d0f ] 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 <[email protected]> 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 <[email protected]> Acked-by: Alex Deucher <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: <[email protected]> # v2.6.34+ Link: https://patch.msgid.link/[email protected] Signed-off-by: Sasha Levin <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 1ba6da6 commit 482330f

File tree

4 files changed

+9
-17
lines changed

4 files changed

+9
-17
lines changed

drivers/gpu/drm/drm_fb_helper.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@
3030
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3131

3232
#include <linux/console.h>
33-
#include <linux/pci.h>
3433
#include <linux/sysrq.h>
35-
#include <linux/vga_switcheroo.h>
3634

3735
#include <drm/drm_atomic.h>
3836
#include <drm/drm_drv.h>
@@ -1637,10 +1635,6 @@ static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper)
16371635

16381636
strcpy(fb_helper->fb->comm, "[fbcon]");
16391637

1640-
/* Set the fb info for vgaswitcheroo clients. Does nothing otherwise. */
1641-
if (dev_is_pci(dev->dev))
1642-
vga_switcheroo_client_fb_set(to_pci_dev(dev->dev), fb_helper->info);
1643-
16441638
return 0;
16451639
}
16461640

drivers/gpu/drm/i915/display/intel_fbdev.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -589,11 +589,8 @@ static int intel_fbdev_restore_mode(struct drm_i915_private *dev_priv)
589589
static void intel_fbdev_client_unregister(struct drm_client_dev *client)
590590
{
591591
struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
592-
struct drm_device *dev = fb_helper->dev;
593-
struct pci_dev *pdev = to_pci_dev(dev->dev);
594592

595593
if (fb_helper->info) {
596-
vga_switcheroo_client_fb_set(pdev, NULL);
597594
drm_fb_helper_unregister_info(fb_helper);
598595
} else {
599596
drm_fb_helper_unprepare(fb_helper);
@@ -620,7 +617,6 @@ static int intel_fbdev_client_hotplug(struct drm_client_dev *client)
620617
{
621618
struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
622619
struct drm_device *dev = client->dev;
623-
struct pci_dev *pdev = to_pci_dev(dev->dev);
624620
int ret;
625621

626622
if (dev->fb_helper)
@@ -634,8 +630,6 @@ static int intel_fbdev_client_hotplug(struct drm_client_dev *client)
634630
if (ret)
635631
goto err_drm_fb_helper_fini;
636632

637-
vga_switcheroo_client_fb_set(pdev, fb_helper->info);
638-
639633
return 0;
640634

641635
err_drm_fb_helper_fini:

drivers/gpu/drm/radeon/radeon_fbdev.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,8 @@ static void radeon_fbdev_client_unregister(struct drm_client_dev *client)
300300
{
301301
struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
302302
struct drm_device *dev = fb_helper->dev;
303-
struct radeon_device *rdev = dev->dev_private;
304303

305304
if (fb_helper->info) {
306-
vga_switcheroo_client_fb_set(rdev->pdev, NULL);
307305
drm_helper_force_disable_all(dev);
308306
drm_fb_helper_unregister_info(fb_helper);
309307
} else {
@@ -325,7 +323,6 @@ static int radeon_fbdev_client_hotplug(struct drm_client_dev *client)
325323
{
326324
struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
327325
struct drm_device *dev = client->dev;
328-
struct radeon_device *rdev = dev->dev_private;
329326
int ret;
330327

331328
if (dev->fb_helper)
@@ -342,8 +339,6 @@ static int radeon_fbdev_client_hotplug(struct drm_client_dev *client)
342339
if (ret)
343340
goto err_drm_fb_helper_fini;
344341

345-
vga_switcheroo_client_fb_set(rdev->pdev, fb_helper->info);
346-
347342
return 0;
348343

349344
err_drm_fb_helper_fini:

drivers/video/fbdev/core/fbcon.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include <linux/string.h>
6666
#include <linux/kd.h>
6767
#include <linux/panic.h>
68+
#include <linux/pci.h>
6869
#include <linux/printk.h>
6970
#include <linux/slab.h>
7071
#include <linux/fb.h>
@@ -77,6 +78,7 @@
7778
#include <linux/interrupt.h>
7879
#include <linux/crc32.h> /* For counting font checksums */
7980
#include <linux/uaccess.h>
81+
#include <linux/vga_switcheroo.h>
8082
#include <asm/irq.h>
8183

8284
#include "fbcon.h"
@@ -2894,6 +2896,9 @@ void fbcon_fb_unregistered(struct fb_info *info)
28942896

28952897
console_lock();
28962898

2899+
if (info->device && dev_is_pci(info->device))
2900+
vga_switcheroo_client_fb_set(to_pci_dev(info->device), NULL);
2901+
28972902
fbcon_registered_fb[info->node] = NULL;
28982903
fbcon_num_registered_fb--;
28992904

@@ -3027,6 +3032,10 @@ static int do_fb_registered(struct fb_info *info)
30273032
}
30283033
}
30293034

3035+
/* Set the fb info for vga_switcheroo clients. Does nothing otherwise. */
3036+
if (info->device && dev_is_pci(info->device))
3037+
vga_switcheroo_client_fb_set(to_pci_dev(info->device), info);
3038+
30303039
return ret;
30313040
}
30323041

0 commit comments

Comments
 (0)