Skip to contentSkip to conversation

fbdev: Prevent probing generic drivers if a FB is already registered

PATCH In review Javier Martinez Canillas 4 participants 8 messages Nov 11, 2021 – Nov 16, 2021 Framebuffer layer

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 fbdev: Prevent probing generic drivers if a FB is already registered

The efifb and simplefb drivers just render to a pre-allocated frame buffer and rely on the display hardware being initialized before the kernel boots.

But if another driver already probed correctly and registered a fbdev, the generic drivers shouldn't be probed since an actual driver for the display hardware is already present.

This is more likely to occur after commit d391c5827107 ("drivers/firmware:
move x86 Generic System Framebuffers support") since the "efi-framebuffer" and "simple-framebuffer" platform devices are registered at a later time.

Fixes d391c5827107 ("drivers/firmware: move x86 Generic System Framebuffers support")

---

Changes in v2:
- Add a Link: tag with a reference to the bug report (Thorsten Leemhuis).
- Add a comment explaining why the probe fails earlier (Daniel Vetter).
- Add a Fixes: tag for stable to pick the fix (Daniel Vetter).
- Add Daniel Vetter's Reviewed-by: tag.
- Improve the commit message and mention the culprit commit

 drivers/video/fbdev/efifb.c    | 11 +++++++++++
 drivers/video/fbdev/simplefb.c | 11 +++++++++++
 2 files changed, 22 insertions(+)
video/fbdev/efifb.c +11 -0
index edca3703b964..ea42ba6445b2 100644
@@ -351,6 +351,17 @@ static int efifb_probe(struct platform_device *dev)
 	char *option = NULL;
 	efi_memory_desc_t md;
 
+	/*
+	 * Generic drivers must not be registered if a framebuffer exists.
+	 * If a native driver was probed, the display hardware was already
+	 * taken and attempting to use the system framebuffer is dangerous.
+	 */
+	if (num_registered_fb > 0) {
+		dev_err(&dev->dev,
+			"efifb: a framebuffer is already registered\n");
+		return -EINVAL;
+	}
+
 	if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI || pci_dev_disabled)
 		return -ENODEV;
 
video/fbdev/simplefb.c +11 -0
index 62f0ded70681..b63074fd892e 100644
@@ -407,6 +407,17 @@ static int simplefb_probe(struct platform_device *pdev)
 	struct simplefb_par *par;
 	struct resource *mem;
 
+	/*
+	 * Generic drivers must not be registered if a framebuffer exists.
+	 * If a native driver was probed, the display hardware was already
+	 * taken and attempting to use the system framebuffer is dangerous.
+	 */
+	if (num_registered_fb > 0) {
+		dev_err(&pdev->dev,
+			"simplefb: a framebuffer is already registered\n");
+		return -EINVAL;
+	}
+
 	if (fb_get_options("simplefb", NULL))
 		return -ENODEV;
 
Signature · 1 line
2.33.1
#1 lore raw
Quoted 16 lines · Javier Martinez Canillas

The efifb and simplefb drivers just render to a pre-allocated frame buffer and rely on the display hardware being initialized before the kernel boots.

But if another driver already probed correctly and registered a fbdev, the generic drivers shouldn't be probed since an actual driver for the display hardware is already present.

This is more likely to occur after commit d391c5827107 ("drivers/firmware:
move x86 Generic System Framebuffers support") since the "efi-framebuffer" and "simple-framebuffer" platform devices are registered at a later time.

Fixes d391c5827107 ("drivers/firmware: move x86 Generic System Framebuffers support")
Quoted 6 lines

---

Changes in v2:
- Add a Link: tag with a reference to the bug report (Thorsten Leemhuis).
- Add a comment explaining why the probe fails earlier (Daniel Vetter).
- Add a Fixes: tag for stable to pick the fix (Daniel Vetter).

That does not mean that it will make it into the stable tree. Please read:
https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
for how to do this properly.

thanks,

greg k-h

#2 lore raw
Quoted 24 lines · Greg Kroah-Hartman
Quoted 16 lines · Javier Martinez Canillas

The efifb and simplefb drivers just render to a pre-allocated frame buffer and rely on the display hardware being initialized before the kernel boots.

But if another driver already probed correctly and registered a fbdev, the generic drivers shouldn't be probed since an actual driver for the display hardware is already present.

This is more likely to occur after commit d391c5827107 ("drivers/firmware:
move x86 Generic System Framebuffers support") since the "efi-framebuffer" and "simple-framebuffer" platform devices are registered at a later time.

Fixes d391c5827107 ("drivers/firmware: move x86 Generic System Framebuffers support")
Quoted 6 lines

---

Changes in v2:
- Add a Link: tag with a reference to the bug report (Thorsten Leemhuis).
- Add a comment explaining why the probe fails earlier (Daniel Vetter).
- Add a Fixes: tag for stable to pick the fix (Daniel Vetter).

Quoted 4 lines

That does not mean that it will make it into the stable tree. Please read:
https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
for how to do this properly.

Defacto your auto-picker is aggressive enough that just Fixes: is actually good enough to get it into stable :-)

But yeah explicit cc: stable can't hurt.
-Daniel

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#3 lore raw
In reply to Javier Martinez Canillas

Hi Javier,

Quoted 34 lines · Javier Martinez Canillas

The efifb and simplefb drivers just render to a pre-allocated frame buffer and rely on the display hardware being initialized before the kernel boots.

But if another driver already probed correctly and registered a fbdev, the generic drivers shouldn't be probed since an actual driver for the display hardware is already present.

This is more likely to occur after commit d391c5827107 ("drivers/firmware:
move x86 Generic System Framebuffers support") since the "efi-framebuffer" and "simple-framebuffer" platform devices are registered at a later time.

Fixes d391c5827107 ("drivers/firmware: move x86 Generic System Framebuffers support")

---

Changes in v2:
- Add a Link: tag with a reference to the bug report (Thorsten Leemhuis).
- Add a comment explaining why the probe fails earlier (Daniel Vetter).
- Add a Fixes: tag for stable to pick the fix (Daniel Vetter).
- Add Daniel Vetter's Reviewed-by: tag.
- Improve the commit message and mention the culprit commit

 drivers/video/fbdev/efifb.c    | 11 +++++++++++
 drivers/video/fbdev/simplefb.c | 11 +++++++++++
 2 files changed, 22 insertions(+)
video/fbdev/efifb.c +0 -0
index edca3703b964..ea42ba6445b2 100644

video/fbdev/efifb.c static int efifb_probe(struct platform_device *dev) · line 359

Quoted 9 lines
video/fbdev/efifb.c +6 -0
@@ -351,6 +351,17 @@ static int efifb_probe(struct platform_device *dev)
        char *option = NULL;
        efi_memory_desc_t md;
 
+       /*
+        * Generic drivers must not be registered if a framebuffer exists.
+        * If a native driver was probed, the display hardware was already
+        * taken and attempting to use the system framebuffer is dangerous.
+        */
+       if (num_registered_fb > 0) {

Who says this registered fbdev is driving the same hardware as efifb? This might be e.g. a small external display connected to i2c or spi.

Quoted 13 lines
video/fbdev/efifb.c +5 -0
+               dev_err(&dev->dev,
+                       "efifb: a framebuffer is already registered\n");
+               return -EINVAL;
+       }
+
        if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI || pci_dev_disabled)
                return -ENODEV;
 
video/fbdev/simplefb.c +0 -0
index 62f0ded70681..b63074fd892e 100644

video/fbdev/simplefb.c static int simplefb_probe(struct platform_device *pdev) · line 415

Quoted 9 lines
video/fbdev/simplefb.c +6 -0
@@ -407,6 +407,17 @@ static int simplefb_probe(struct platform_device *pdev)
        struct simplefb_par *par;
        struct resource *mem;
 
+       /*
+        * Generic drivers must not be registered if a framebuffer exists.
+        * If a native driver was probed, the display hardware was already
+        * taken and attempting to use the system framebuffer is dangerous.
+        */
+       if (num_registered_fb > 0) {

Likewise.

video/fbdev/simplefb.c static int simplefb_probe(struct platform_device *pdev) · line 422

Quoted 7 lines
video/fbdev/simplefb.c +5 -0
+               dev_err(&pdev->dev,
+                       "simplefb: a framebuffer is already registered\n");
+               return -EINVAL;
+       }
+
        if (fb_get_options("simplefb", NULL))
                return -ENODEV;

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#4 lore raw

Hello Geert,

On 11/15/21 17:20, Geert Uytterhoeven wrote:

[snip]

Quoted 4 lines
Quoted 4 lines
video/fbdev/efifb.c +0 -0
@@ -351,6 +351,17 @@ static int efifb_probe(struct platform_device *dev)
        char *option = NULL;
        efi_memory_desc_t md;
 
Quoted 15 lines

video/fbdev/efifb.c static int efifb_probe(struct platform_device *dev) · line 359

Quoted 6 lines
video/fbdev/efifb.c +6 -0
+       /*
+        * Generic drivers must not be registered if a framebuffer exists.
+        * If a native driver was probed, the display hardware was already
+        * taken and attempting to use the system framebuffer is dangerous.
+        */
+       if (num_registered_fb > 0) {

Who says this registered fbdev is driving the same hardware as efifb? This might be e.g. a small external display connected to i2c or spi.

Quoted 5 lines
video/fbdev/efifb.c +5 -0
+               dev_err(&dev->dev,
+                       "efifb: a framebuffer is already registered\n");
+               return -EINVAL;
+       }
+

That's true, although I wonder if the {efi,simple}fb drivers should even be probed in that case. As I see it, these are always a best effort that are only useful for earlycon or if there isn't another display driver supported.

Since there may be other conditions needed in order for these to work. For example, when using the u-boot EFI stub in most cases the unused clocks and power domains can't be gated or otherwise the firmware frame buffer could go away (e.g: will need to boot with "clk_ignore_unused" and "pd_ignore_unused").

Same for the simplefb driver, if the DT node is missing resources that are needed by the display controller to continue working (clocks, regulators, power domains), the firmware setup framebuffer will go away at some point.

So this is already a fragile solution and $SUBJECT doesn't make things worse IMO. Since not having something like this can lead to issues as reported by:

https://lore.kernel.org/all/20211110200253.rfudkt3edbd3nsyj@lahvuun/

We could probably do some smarter here by providing a function that checks if the registered fbdev drivers matches the aperture base. But I'm unsure if that's worth it. After all, fbdev drivers are likely to be disabled by most distros soon now that we have the simpledrm driver.

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Linux Engineering
Red Hat
#5 lore raw

Hi Javier,

Quoted 37 lines · Javier Martinez Canillas
Quoted 4 lines · Geert Uytterhoeven
Quoted 4 lines
video/fbdev/efifb.c +0 -0
@@ -351,6 +351,17 @@ static int efifb_probe(struct platform_device *dev)
        char *option = NULL;
        efi_memory_desc_t md;
 
Quoted 15 lines

video/fbdev/efifb.c static int efifb_probe(struct platform_device *dev) · line 359

Quoted 6 lines
video/fbdev/efifb.c +6 -0
+       /*
+        * Generic drivers must not be registered if a framebuffer exists.
+        * If a native driver was probed, the display hardware was already
+        * taken and attempting to use the system framebuffer is dangerous.
+        */
+       if (num_registered_fb > 0) {

Who says this registered fbdev is driving the same hardware as efifb? This might be e.g. a small external display connected to i2c or spi.

Quoted 5 lines
video/fbdev/efifb.c +5 -0
+               dev_err(&dev->dev,
+                       "efifb: a framebuffer is already registered\n");
+               return -EINVAL;
+       }
+

That's true, although I wonder if the {efi,simple}fb drivers should even be probed in that case. As I see it, these are always a best effort that are only useful for earlycon or if there isn't another display driver supported.

Since there may be other conditions needed in order for these to work. For example, when using the u-boot EFI stub in most cases the unused clocks and power domains can't be gated or otherwise the firmware frame buffer could go away (e.g: will need to boot with "clk_ignore_unused" and "pd_ignore_unused").

Same for the simplefb driver, if the DT node is missing resources that are needed by the display controller to continue working (clocks, regulators, power domains), the firmware setup framebuffer will go away at some point.

So this is already a fragile solution and $SUBJECT doesn't make things worse IMO. Since not having something like this can lead to issues as reported by:

Quoted 6 lines

https://lore.kernel.org/all/20211110200253.rfudkt3edbd3nsyj@lahvuun/

We could probably do some smarter here by providing a function that checks if the registered fbdev drivers matches the aperture base. But I'm unsure if that's worth it. After all, fbdev drivers are likely to be disabled by most distros soon now that we have the simpledrm driver.

Checking the aperture base is what was done in all other cases of preventing generic (fbdev) drivers from stepping on specific drivers' toes...

But as you're only impacting efifb and simplefb, thus not crippling generic fbdev support, I don't care that much.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#6 lore raw

Hello Geert,

On 11/16/21 10:43, Geert Uytterhoeven wrote:

[snip]

Quoted 6 lines
Quoted 6 lines

So this is already a fragile solution and $SUBJECT doesn't make things worse IMO. Since not having something like this can lead to issues as reported by:

https://lore.kernel.org/all/20211110200253.rfudkt3edbd3nsyj@lahvuun/

Quoted 9 lines
Quoted 4 lines

We could probably do some smarter here by providing a function that checks if the registered fbdev drivers matches the aperture base. But I'm unsure if that's worth it. After all, fbdev drivers are likely to be disabled by most distros soon now that we have the simpledrm driver.

Checking the aperture base is what was done in all other cases of preventing generic (fbdev) drivers from stepping on specific drivers' toes...

Ok, I can re-spin the patch checking if the aperture ranges overlap. There's an apertures_overlap() function in drivers/video/fbdev/core/fbmem.c that can be exported for fbdev drivers to use.

Another option is to just say that DRM drivers should be built as a module if the {efi,simple}fb driver are built-in.

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Linux Engineering
Red Hat
#7 lore raw
Quoted 17 lines · Javier Martinez Canillas

Hello Geert,

On 11/16/21 10:43, Geert Uytterhoeven wrote:

[snip]

Quoted 11 lines
Quoted 10 lines

So this is already a fragile solution and $SUBJECT doesn't make things worse IMO. Since not having something like this can lead to issues as reported by:

https://lore.kernel.org/all/20211110200253.rfudkt3edbd3nsyj@lahvuun/

We could probably do some smarter here by providing a function that checks if the registered fbdev drivers matches the aperture base. But I'm unsure if that's worth it. After all, fbdev drivers are likely to be disabled by most distros soon now that we have the simpledrm driver.

Quoted 9 lines
Quoted 4 lines

Checking the aperture base is what was done in all other cases of preventing generic (fbdev) drivers from stepping on specific drivers' toes...

Ok, I can re-spin the patch checking if the aperture ranges overlap. There's an apertures_overlap() function in drivers/video/fbdev/core/fbmem.c that can be exported for fbdev drivers to use.

So I tried the following patch [0]. But when testing on a VM, the efifb driver is probed even after the virtio_gpu driver has already been probed. Being a DRM driver, it doesn't use the fbdev infra and AFAIU doesn't reserve any apertures.

When the {efi,simple}fb drivers check if there's an aperture already reserved using the fb_aperture_registered() helper, this just returns false even when a driver for the same hardware was already registered. The kernel log says:

[    0.891512] checking generic (0 0) vs hw (c0000000 1d5000)

That is because when DRM_FBDEV_EMULATION=y, the virtio_gpu driver registers an fbdev but without any aperture set.

I discussed this with Thomas and even though $SUBJECT is just a workaround, it seems that is the best we can do as an heuristic to prevent the generic fbdev drivers to be probed after a native DRM driver.

[0]:
From d962c20bc9fd90c2525d79b69e632d99e8050fc5 Mon Sep 17 00:00:00 2001
From: Javier Martinez Canillas <[email protected]>
Date: Thu, 11 Nov 2021 00:55:06 +0100
Subject: [PATCH v4] fbdev: Prevent probing generic drivers if a FB is already
registered

The efifb and simplefb drivers just render to a pre-allocated frame buffer and rely on the display hardware being initialized before the kernel boots.

But if another driver already probed correctly and registered a fbdev, the generic drivers shouldn't be probed since an actual driver for the display hardware is already present.

This is more likely to occur after commit d391c5827107 ("drivers/firmware:
move x86 Generic System Framebuffers support") since the "efi-framebuffer" and "simple-framebuffer" platform devices are registered at a later time.

Fixes d391c5827107 ("drivers/firmware: move x86 Generic System Framebuffers support")
Cc <[email protected]> # 5.15.x

---

Changes in v4:
- Only fail to probe if a registered fbdev has overlapping aperture (Geert).

Changes in v3:
- Cc <[email protected]> since a Fixes: tag is not enough (gregkh).

Changes in v2:
- Add a Link: tag with a reference to the bug report (Thorsten Leemhuis).
- Add a comment explaining why the probe fails earlier (Daniel Vetter).
- Add a Fixes: tag for stable to pick the fix (Daniel Vetter).
- Add Daniel Vetter's Reviewed-by: tag.
- Improve the commit message and mention the culprit commit

 drivers/video/fbdev/core/fbmem.c | 16 ++++++++++++++++
 drivers/video/fbdev/efifb.c      | 11 +++++++++++
 drivers/video/fbdev/simplefb.c   | 11 +++++++++++
 include/linux/fb.h               |  1 +
 4 files changed, 39 insertions(+)
video/fbdev/core/fbmem.c +16 -0
index 826175ad88a2..9906b83132cb 100644
@@ -1546,6 +1546,22 @@ static bool fb_do_apertures_overlap(struct apertures_struct *gena,
 	return false;
 }
 
+bool fb_aperture_registered(struct apertures_struct *a)
+{
+	int i;
+
+	for_each_registered_fb(i) {
+		struct apertures_struct *gen_aper;
+
+		gen_aper = registered_fb[i]->apertures;
+		if (fb_do_apertures_overlap(gen_aper, a))
+			return true;
+	}
+
+	return false;
+}
+EXPORT_SYMBOL(fb_aperture_registered);
+
 static void do_unregister_framebuffer(struct fb_info *fb_info);
 
 #define VGA_FB_PHYS 0xA0000
video/fbdev/efifb.c +11 -0
index edca3703b964..1ad6698b2e05 100644
@@ -457,6 +457,17 @@ static int efifb_probe(struct platform_device *dev)
 	info->apertures->ranges[0].base = efifb_fix.smem_start;
 	info->apertures->ranges[0].size = size_remap;
 
+	/*
+	 * Generic drivers must not be registered if a framebuffer exists.
+	 * If a native driver was probed, the display hardware was already
+	 * taken and attempting to use the system framebuffer is dangerous.
+	 */
+	if (fb_aperture_registered(info->apertures)) {
+		dev_err(&dev->dev,
+			"efifb: a framebuffer is already registered\n");
+		return -EINVAL;
+	}
+
 	if (efi_enabled(EFI_MEMMAP) &&
 	    !efi_mem_desc_lookup(efifb_fix.smem_start, &md)) {
 		if ((efifb_fix.smem_start + efifb_fix.smem_len) >
video/fbdev/simplefb.c +11 -0
index 62f0ded70681..3ad0f538ca91 100644
@@ -456,6 +456,17 @@ static int simplefb_probe(struct platform_device *pdev)
 	info->apertures->ranges[0].base = info->fix.smem_start;
 	info->apertures->ranges[0].size = info->fix.smem_len;
 
+	/*
+	 * Generic drivers must not be registered if a framebuffer exists.
+	 * If a native driver was probed, the display hardware was already
+	 * taken and attempting to use the system framebuffer is dangerous.
+	 */
+	if (fb_aperture_registered(info->apertures)) {
+		dev_err(&pdev->dev,
+			"simplefb: a framebuffer is already registered\n");
+		return -EINVAL;
+	}
+
 	info->fbops = &simplefb_ops;
 	info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE;
 	info->screen_base = ioremap_wc(info->fix.smem_start,
linux/fb.h +1 -0
index 6f3db99ab990..f1fbdb39932b 100644
@@ -604,6 +604,7 @@ extern ssize_t fb_sys_write(struct fb_info *info, const char __user *buf,
 			    size_t count, loff_t *ppos);
 
 /* drivers/video/fbmem.c */
+extern bool fb_aperture_registered(struct apertures_struct *a);
 extern int register_framebuffer(struct fb_info *fb_info);
 extern void unregister_framebuffer(struct fb_info *fb_info);
 extern int remove_conflicting_pci_framebuffers(struct pci_dev *pdev,
Signature · 7 lines
2.33.1

Best regards,
-- 
Javier Martinez Canillas
Linux Engineering
Red Hat