Skip to contentSkip to conversation

sysfb: Fix memory-region management

PATCH In review Thomas Zimmermann 4 participants 17 messages Jan 24, 2022 – Jan 25, 2022

Subsystems (8)
lore
Share & tools
lore

Thomas Zimmermann

author maintainer
#0 lore raw

cover sysfb: Fix memory-region management

Request framebuffer memory in simpledrm and simplefb. Do a hot-unplug operation when removing fbdev firmware drivers.

After being unloaded by a hardware driver, simplefb leaves behind the firmware framebuffer's platform device. This prevents other drivers from acquiring the memory as reported at [1].

Patch 1 changes the removal code of remove_conflicting_framebuffers() to remove the underlying device and the rsp memory region.

Patches 2 to 4 update sysfb and its drivers. The sysfb code does no longer mark the framebuffer memory with IORESOURCE_BUSY. Instead, the device drivers acquire the memory when they probe the device.

Patch 5 adds a todo item to acquire memory regions in all DRM drivers.

Tested with simpledrm and simplefb.

[1] https://lore.kernel.org/dri-devel/[email protected]/

Javier Martinez Canillas (1):
drivers/firmware: Don't mark as busy the simple-framebuffer IO
resource

Thomas Zimmermann (4):
fbdev: Hot-unplug firmware fb devices on forced removal drm/simpledrm: Request memory region in driver fbdev/simplefb: Request memory region in driver
drm: Add TODO item for requesting memory regions

 Documentation/gpu/todo.rst        | 15 ++++++++
 drivers/firmware/sysfb_simplefb.c |  2 +-
 drivers/gpu/drm/tiny/simpledrm.c  | 20 ++++++++---
 drivers/video/fbdev/core/fbmem.c  | 29 +++++++++++++--
 drivers/video/fbdev/simplefb.c    | 59 ++++++++++++++++++++++---------
 include/linux/fb.h                |  1 +
 6 files changed, 100 insertions(+), 26 deletions(-)

base-commit: 0bb81b5d6db5f689b67f9d8b35323235c45e890f

Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH 1/5 fbdev: Hot-unplug firmware fb devices on forced removal

Hot-unplug all firmware-framebuffer devices as part of removing them via remove_conflicting_framebuffers() et al. Releases all memory regions to be acquired by native drivers.

Firmware, such as EFI, install a framebuffer while posting the computer. After removing the firmware-framebuffer device from fbdev, a native driver takes over the hardware and the firmware framebuffer becomes invalid.

Firmware-framebuffer drivers, specifically simplefb, don't release their device from Linux' device hierarchy. It still owns the firmware framebuffer and blocks the native drivers from loading. This has been observed in the vmwgfx driver. [1]

Initiating a device removal (i.e., hot unplug) as part of remove_conflicting_framebuffers() removes the underlying device and returns the memory range to the system.

[1] https://lore.kernel.org/dri-devel/[email protected]/

---
 drivers/video/fbdev/core/fbmem.c | 29 ++++++++++++++++++++++++++---
 include/linux/fb.h               |  1 +
 2 files changed, 27 insertions(+), 3 deletions(-)
drivers/video/fbdev/core/fbmem.c +26 -3
index 0fa7ede94fa6..f73f8415b8cb 100644
@@ -25,6 +25,7 @@
 #include <linux/init.h>
 #include <linux/linux_logo.h>
 #include <linux/proc_fs.h>
+#include <linux/platform_device.h>
 #include <linux/seq_file.h>
 #include <linux/console.h>
 #include <linux/kmod.h>
@@ -1557,18 +1558,36 @@ static void do_remove_conflicting_framebuffers(struct apertures_struct *a,
 	/* check all firmware fbs and kick off if the base addr overlaps */
 	for_each_registered_fb(i) {
 		struct apertures_struct *gen_aper;
+		struct device *dev;
 
 		if (!(registered_fb[i]->flags & FBINFO_MISC_FIRMWARE))
 			continue;
 
 		gen_aper = registered_fb[i]->apertures;
+		dev = registered_fb[i]->device;
 		if (fb_do_apertures_overlap(gen_aper, a) ||
 			(primary && gen_aper && gen_aper->count &&
 			 gen_aper->ranges[0].base == VGA_FB_PHYS)) {
 
 			printk(KERN_INFO "fb%d: switching to %s from %s\n",
 			       i, name, registered_fb[i]->fix.id);
-			do_unregister_framebuffer(registered_fb[i]);
+
+			/*
+			 * If we kick-out a firmware driver, we also want to remove
+			 * the underlying platform device, such as simple-framebuffer,
+			 * VESA, EFI, etc. A native driver will then be able to
+			 * allocate the memory range.
+			 *
+			 * If it's not a platform device, at least print a warning. A
+			 * fix would add code to remove the device from the system.
+			 */
+			if (dev_is_platform(dev)) {
+				registered_fb[i]->forced_out = true;
+				platform_device_unregister(to_platform_device(dev));
+			} else {
+				pr_warn("fb%d: cannot remove device\n", i);
+				do_unregister_framebuffer(registered_fb[i]);
+			}
 		}
 	}
 }
@@ -1898,9 +1917,13 @@ EXPORT_SYMBOL(register_framebuffer);
 void
 unregister_framebuffer(struct fb_info *fb_info)
 {
-	mutex_lock(&registration_lock);
+	bool forced_out = fb_info->forced_out;
+
+	if (!forced_out)
+		mutex_lock(&registration_lock);
 	do_unregister_framebuffer(fb_info);
-	mutex_unlock(&registration_lock);
+	if (!forced_out)
+		mutex_unlock(&registration_lock);
 }
 EXPORT_SYMBOL(unregister_framebuffer);
 
include/linux/fb.h +1 -0
index 3da95842b207..9a14f3f8a329 100644
@@ -502,6 +502,7 @@ struct fb_info {
 	} *apertures;
 
 	bool skip_vt_switch; /* no VT switch on suspend/resume required */
+	bool forced_out; /* set when being removed by another driver */
 };
 
 static inline struct apertures_struct *alloc_apertures(unsigned int max_num) {
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/5] fbdev: Hot-unplug firmware fb devices on forced removal

Hello Thomas,

Thanks for the patch.

Quoted 14 lines · Thomas Zimmermann

Hot-unplug all firmware-framebuffer devices as part of removing them via remove_conflicting_framebuffers() et al. Releases all memory regions to be acquired by native drivers.

Firmware, such as EFI, install a framebuffer while posting the computer. After removing the firmware-framebuffer device from fbdev, a native driver takes over the hardware and the firmware framebuffer becomes invalid.

Firmware-framebuffer drivers, specifically simplefb, don't release their device from Linux' device hierarchy. It still owns the firmware framebuffer and blocks the native drivers from loading. This has been observed in the vmwgfx driver. [1]

Quoted 6 lines

Initiating a device removal (i.e., hot unplug) as part of remove_conflicting_framebuffers() removes the underlying device and returns the memory range to the system.

[1] https://lore.kernel.org/dri-devel/[email protected]/

I would add a Reported-by tag here for Zack.

Quoted 38 lines
---
 drivers/video/fbdev/core/fbmem.c | 29 ++++++++++++++++++++++++++---
 include/linux/fb.h               |  1 +
 2 files changed, 27 insertions(+), 3 deletions(-)
drivers/video/fbdev/core/fbmem.c +4 -1
index 0fa7ede94fa6..f73f8415b8cb 100644
@@ -25,6 +25,7 @@
 #include <linux/init.h>
 #include <linux/linux_logo.h>
 #include <linux/proc_fs.h>
+#include <linux/platform_device.h>
 #include <linux/seq_file.h>
 #include <linux/console.h>
 #include <linux/kmod.h>
@@ -1557,18 +1558,36 @@ static void do_remove_conflicting_framebuffers(struct apertures_struct *a,
 	/* check all firmware fbs and kick off if the base addr overlaps */
 	for_each_registered_fb(i) {
 		struct apertures_struct *gen_aper;
+		struct device *dev;
 
 		if (!(registered_fb[i]->flags & FBINFO_MISC_FIRMWARE))
 			continue;
 
 		gen_aper = registered_fb[i]->apertures;
+		dev = registered_fb[i]->device;
 		if (fb_do_apertures_overlap(gen_aper, a) ||
 			(primary && gen_aper && gen_aper->count &&
 			 gen_aper->ranges[0].base == VGA_FB_PHYS)) {
 
 			printk(KERN_INFO "fb%d: switching to %s from %s\n",
 			       i, name, registered_fb[i]->fix.id);
-			do_unregister_framebuffer(registered_fb[i]);
+

drivers/video/fbdev/core/fbmem.c static void do_remove_conflicting_framebuffers(struct apertures_struct *a, · line 1584

Quoted 10 lines
drivers/video/fbdev/core/fbmem.c +10 -0
+			/*
+			 * If we kick-out a firmware driver, we also want to remove
+			 * the underlying platform device, such as simple-framebuffer,
+			 * VESA, EFI, etc. A native driver will then be able to
+			 * allocate the memory range.
+			 *
+			 * If it's not a platform device, at least print a warning. A
+			 * fix would add code to remove the device from the system.
+			 */
+			if (dev_is_platform(dev)) {

In do_register_framebuffer() creating the fb%d is not a fatal error. It would be safer to do if (!IS_ERR_OR_NULL(dev) && dev_is_platform(dev)) instead here.

https://elixir.bootlin.com/linux/latest/source/drivers/video/fbdev/core/fbmem.c#L1605

Quoted 13 lines
drivers/video/fbdev/core/fbmem.c +6 -0
+				registered_fb[i]->forced_out = true;
+				platform_device_unregister(to_platform_device(dev));
+			} else {
+				pr_warn("fb%d: cannot remove device\n", i);
+				do_unregister_framebuffer(registered_fb[i]);
+			}
 		}
 	}
 }
@@ -1898,9 +1917,13 @@ EXPORT_SYMBOL(register_framebuffer);
 void
 unregister_framebuffer(struct fb_info *fb_info)
 {

drivers/video/fbdev/core/fbmem.c EXPORT_SYMBOL(register_framebuffer); · line 1927

Quoted 10 lines
drivers/video/fbdev/core/fbmem.c +6 -2
-	mutex_lock(&registration_lock);
+	bool forced_out = fb_info->forced_out;
+
+	if (!forced_out)
+		mutex_lock(&registration_lock);
 	do_unregister_framebuffer(fb_info);
-	mutex_unlock(&registration_lock);
+	if (!forced_out)
+		mutex_unlock(&registration_lock);
 }

I'm not sure to follow the logic here. The forced_out bool is set when the platform device is unregistered in do_remove_conflicting_framebuffers(), but shouldn't the struct platform_driver .remove callback be executed even in this case ?

That is, the platform_device_unregister() will trigger the call to the .remove callback that in turn will call unregister_framebuffer().

Shouldn't we always hold the mutex when calling do_unregister_framebuffer() ?

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Linux Engineering
Red Hat
maintainer Reviewed-by
#3 lore raw

Re: [PATCH 1/5] fbdev: Hot-unplug firmware fb devices on forced removal

On 1/24/22 14:52, Javier Martinez Canillas wrote:

[snip]

Quoted 20 lines
Quoted 4 lines
drivers/video/fbdev/core/fbmem.c +0 -0
@@ -1898,9 +1917,13 @@ EXPORT_SYMBOL(register_framebuffer);
 void
 unregister_framebuffer(struct fb_info *fb_info)
 {

drivers/video/fbdev/core/fbmem.c EXPORT_SYMBOL(register_framebuffer); · line 1927

Quoted 10 lines
drivers/video/fbdev/core/fbmem.c +6 -2
-	mutex_lock(&registration_lock);
+	bool forced_out = fb_info->forced_out;
+
+	if (!forced_out)
+		mutex_lock(&registration_lock);
 	do_unregister_framebuffer(fb_info);
-	mutex_unlock(&registration_lock);
+	if (!forced_out)
+		mutex_unlock(&registration_lock);
 }

I'm not sure to follow the logic here. The forced_out bool is set when the platform device is unregistered in do_remove_conflicting_framebuffers(), but shouldn't the struct platform_driver .remove callback be executed even in this case ?

Quoted 5 lines

That is, the platform_device_unregister() will trigger the call to the .remove callback that in turn will call unregister_framebuffer().

Shouldn't we always hold the mutex when calling do_unregister_framebuffer() ?

Scratch that, I got it now. That's exactly the reason why you skip the mutext_lock(). After adding the check for dev, feel free to add:

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Linux Engineering
Red Hat
author maintainer
#4 lore raw

Re: [PATCH 1/5] fbdev: Hot-unplug firmware fb devices on forced removal

In reply to Javier Martinez Canillas

Hi

Am 24.01.22 um 14:52 schrieb Javier Martinez Canillas:

Quoted 19 lines

Hello Thomas,

Thanks for the patch.

Quoted 14 lines · Thomas Zimmermann

Hot-unplug all firmware-framebuffer devices as part of removing them via remove_conflicting_framebuffers() et al. Releases all memory regions to be acquired by native drivers.

Firmware, such as EFI, install a framebuffer while posting the computer. After removing the firmware-framebuffer device from fbdev, a native driver takes over the hardware and the firmware framebuffer becomes invalid.

Firmware-framebuffer drivers, specifically simplefb, don't release their device from Linux' device hierarchy. It still owns the firmware framebuffer and blocks the native drivers from loading. This has been observed in the vmwgfx driver. [1]

Quoted 8 lines
Quoted 6 lines

Initiating a device removal (i.e., hot unplug) as part of remove_conflicting_framebuffers() removes the underlying device and returns the memory range to the system.

[1] https://lore.kernel.org/dri-devel/[email protected]/

I would add a Reported-by tag here for Zack.

Indeed, I simply forgot about it.

Quoted 53 lines
Quoted 39 lines
---
  drivers/video/fbdev/core/fbmem.c | 29 ++++++++++++++++++++++++++---
  include/linux/fb.h               |  1 +
  2 files changed, 27 insertions(+), 3 deletions(-)
drivers/video/fbdev/core/fbmem.c +4 -1
index 0fa7ede94fa6..f73f8415b8cb 100644
@@ -25,6 +25,7 @@
  #include <linux/init.h>
  #include <linux/linux_logo.h>
  #include <linux/proc_fs.h>
+#include <linux/platform_device.h>
  #include <linux/seq_file.h>
  #include <linux/console.h>
  #include <linux/kmod.h>
@@ -1557,18 +1558,36 @@ static void do_remove_conflicting_framebuffers(struct apertures_struct *a,
  	/* check all firmware fbs and kick off if the base addr overlaps */
  	for_each_registered_fb(i) {
  		struct apertures_struct *gen_aper;
+		struct device *dev;
 
  		if (!(registered_fb[i]->flags & FBINFO_MISC_FIRMWARE))
  			continue;
 
  		gen_aper = registered_fb[i]->apertures;
+		dev = registered_fb[i]->device;
  		if (fb_do_apertures_overlap(gen_aper, a) ||
  			(primary && gen_aper && gen_aper->count &&
  			 gen_aper->ranges[0].base == VGA_FB_PHYS)) {
 
  			printk(KERN_INFO "fb%d: switching to %s from %s\n",
  			       i, name, registered_fb[i]->fix.id);
-			do_unregister_framebuffer(registered_fb[i]);
+

drivers/video/fbdev/core/fbmem.c static void do_remove_conflicting_framebuffers(struct apertures_struct *a, · line 1584

Quoted 10 lines
drivers/video/fbdev/core/fbmem.c +10 -0
+			/*
+			 * If we kick-out a firmware driver, we also want to remove
+			 * the underlying platform device, such as simple-framebuffer,
+			 * VESA, EFI, etc. A native driver will then be able to
+			 * allocate the memory range.
+			 *
+			 * If it's not a platform device, at least print a warning. A
+			 * fix would add code to remove the device from the system.
+			 */
+			if (dev_is_platform(dev)) {

In do_register_framebuffer() creating the fb%d is not a fatal error. It would be safer to do if (!IS_ERR_OR_NULL(dev) && dev_is_platform(dev)) instead here.

Quoted 1 line

https://elixir.bootlin.com/linux/latest/source/drivers/video/fbdev/core/fbmem.c#L1605

'dev' here refers to 'fb_info->device', which is the underlying device created by the sysfb code. fb_info->dev is something different.

Quoted 30 lines
Quoted 14 lines
drivers/video/fbdev/core/fbmem.c +6 -0
+				registered_fb[i]->forced_out = true;
+				platform_device_unregister(to_platform_device(dev));
+			} else {
+				pr_warn("fb%d: cannot remove device\n", i);
+				do_unregister_framebuffer(registered_fb[i]);
+			}
  		}
  	}
  }
@@ -1898,9 +1917,13 @@ EXPORT_SYMBOL(register_framebuffer);
  void
  unregister_framebuffer(struct fb_info *fb_info)
  {

drivers/video/fbdev/core/fbmem.c EXPORT_SYMBOL(register_framebuffer); · line 1927

Quoted 10 lines
drivers/video/fbdev/core/fbmem.c +6 -2
-	mutex_lock(&registration_lock);
+	bool forced_out = fb_info->forced_out;
+
+	if (!forced_out)
+		mutex_lock(&registration_lock);
  	do_unregister_framebuffer(fb_info);
-	mutex_unlock(&registration_lock);
+	if (!forced_out)
+		mutex_unlock(&registration_lock);
  }

I'm not sure to follow the logic here. The forced_out bool is set when the platform device is unregistered in do_remove_conflicting_framebuffers(), but shouldn't the struct platform_driver .remove callback be executed even in this case ?

Quoted 4 lines

That is, the platform_device_unregister() will trigger the call to the .remove callback that in turn will call unregister_framebuffer().

Shouldn't we always hold the mutex when calling do_unregister_framebuffer() ?

Doing the hot-unplug will end up in unregister_framebuffer(), but we already hold the lock from the do_remove_conflicting_framebuffer() code.

Best regards
Thomas

Quoted 2 lines

Best regards,

Signature · 6 lines
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
#5 lore raw

Re: [PATCH 1/5] fbdev: Hot-unplug firmware fb devices on forced removal

On 1/24/22 15:19, Thomas Zimmermann wrote:

[snip]

Quoted 10 lines
Quoted 6 lines

drivers/video/fbdev/core/fbmem.c static void do_remove_conflicting_framebuffers(struct apertures_struct *a, · line 1584

Quoted 1 line
drivers/video/fbdev/core/fbmem.c +1 -0
+			if (dev_is_platform(dev)) {

In do_register_framebuffer() creating the fb%d is not a fatal error. It would be safer to do if (!IS_ERR_OR_NULL(dev) && dev_is_platform(dev)) instead here.

https://elixir.bootlin.com/linux/latest/source/drivers/video/fbdev/core/fbmem.c#L1605

'dev' here refers to 'fb_info->device', which is the underlying device created by the sysfb code. fb_info->dev is something different.

oh, indeed. I conflated the two.

Maybe the local variable could be renamed to 'device' just to avoid confusion ?

[snip]

Quoted 5 lines
Quoted 5 lines

I'm not sure to follow the logic here. The forced_out bool is set when the platform device is unregistered in do_remove_conflicting_framebuffers(), but shouldn't the struct platform_driver .remove callback be executed even in this case ?

Quoted 8 lines
Quoted 4 lines

That is, the platform_device_unregister() will trigger the call to the .remove callback that in turn will call unregister_framebuffer().

Shouldn't we always hold the mutex when calling do_unregister_framebuffer() ?

Doing the hot-unplug will end up in unregister_framebuffer(), but we already hold the lock from the do_remove_conflicting_framebuffer() code.

Yes, I realized that just after sending the first email. Sorry for the noise.

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Linux Engineering
Red Hat
Reviewed-by on v1 1/5
#6 lore raw

Re: [PATCH 1/5] fbdev: Hot-unplug firmware fb devices on forced removal

In reply to Thomas Zimmermann
Quoted 18 lines · Thomas Zimmermann

Hot-unplug all firmware-framebuffer devices as part of removing them via remove_conflicting_framebuffers() et al. Releases all memory regions to be acquired by native drivers.

Firmware, such as EFI, install a framebuffer while posting the computer. After removing the firmware-framebuffer device from fbdev, a native driver takes over the hardware and the firmware framebuffer becomes invalid.

Firmware-framebuffer drivers, specifically simplefb, don't release their device from Linux' device hierarchy. It still owns the firmware framebuffer and blocks the native drivers from loading. This has been observed in the vmwgfx driver. [1]

Initiating a device removal (i.e., hot unplug) as part of remove_conflicting_framebuffers() removes the underlying device and returns the memory range to the system.

Quoted 5 lines

[1]
https://lore.kernel.org/dri-devel/[email protected]/

Looks great, thanks!

author maintainer
#7 lore raw

PATCH 2/5 drivers/firmware: Don't mark as busy the simple-framebuffer IO resource

In reply to Thomas Zimmermann

From: Javier Martinez Canillas <[email protected]>

The sysfb_create_simplefb() function requests a IO memory resource for the simple-framebuffer platform device, but it also marks it as busy which can lead to drivers requesting the same memory resource to fail.

Let's drop the IORESOURCE_BUSY flag and let drivers to request it as busy instead.

---
 drivers/firmware/sysfb_simplefb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/sysfb_simplefb.c +1 -1
index 303a491e520d..76c4abc42a30 100644
@@ -99,7 +99,7 @@ __init int sysfb_create_simplefb(const struct screen_info *si,
 
 	/* setup IORESOURCE_MEM as framebuffer memory */
 	memset(&res, 0, sizeof(res));
-	res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
+	res.flags = IORESOURCE_MEM;
 	res.name = simplefb_resname;
 	res.start = base;
 	res.end = res.start + length - 1;
Signature · 1 line
2.34.1
Reviewed-by on v1 2/5
#8 lore raw

Re: [PATCH 2/5] drivers/firmware: Don't mark as busy the simple-framebuffer IO resource

Quoted 8 lines · Thomas Zimmermann

From: Javier Martinez Canillas <[email protected]>

The sysfb_create_simplefb() function requests a IO memory resource for the
simple-framebuffer platform device, but it also marks it as busy which can
lead to drivers requesting the same memory resource to fail.

Quoted 5 lines

Let's drop the IORESOURCE_BUSY flag and let drivers to request it as busy
instead.

author maintainer
#9 lore raw

PATCH 3/5 drm/simpledrm: Request memory region in driver

In reply to Thomas Zimmermann

Requesting the framebuffer memory in simpledrm marks the memory range as busy. This used to be done by the firmware sysfb code, but the driver is the correct place.

---
 drivers/gpu/drm/tiny/simpledrm.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
drivers/gpu/drm/tiny/simpledrm.c +15 -5
index 04146da2d1d8..f72b71511a65 100644
@@ -526,21 +526,31 @@ static int simpledrm_device_init_mm(struct simpledrm_device *sdev)
 {
 	struct drm_device *dev = &sdev->dev;
 	struct platform_device *pdev = sdev->pdev;
-	struct resource *mem;
+	struct resource *res, *mem;
 	void __iomem *screen_base;
 	int ret;
 
-	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-	if (!mem)
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!res)
 		return -EINVAL;
 
-	ret = devm_aperture_acquire_from_firmware(dev, mem->start, resource_size(mem));
+	ret = devm_aperture_acquire_from_firmware(dev, res->start, resource_size(res));
 	if (ret) {
 		drm_err(dev, "could not acquire memory range %pr: error %d\n",
-			mem, ret);
+			res, ret);
 		return ret;
 	}
 
+	mem = devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
+				      sdev->dev.driver->name);
+	if (!mem) {
+		/*
+		 * We cannot make this fatal. Sometimes this comes from magic
+		 * spaces our resource handlers simply don't know about
+		 */
+		drm_warn(dev, "could not acquire memory region %pr\n", res);
+	}
+
 	screen_base = devm_ioremap_wc(&pdev->dev, mem->start,
 				      resource_size(mem));
 	if (!screen_base)
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 3/5
#10 lore raw

Re: [PATCH 3/5] drm/simpledrm: Request memory region in driver

Quoted 6 lines · Thomas Zimmermann

Requesting the framebuffer memory in simpledrm marks the memory range as busy. This used to be done by the firmware sysfb code, but the driver is the correct place.

---

Looks good to me.

Best regards,

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

Re: [PATCH 3/5] drm/simpledrm: Request memory region in driver

In reply to Thomas Zimmermann
Quoted 39 lines · Thomas Zimmermann

Requesting the framebuffer memory in simpledrm marks the memory range as busy. This used to be done by the firmware sysfb code, but the driver is the correct place.

---
  drivers/gpu/drm/tiny/simpledrm.c | 20 +++++++++++++++-----
  1 file changed, 15 insertions(+), 5 deletions(-)
drivers/gpu/drm/tiny/simpledrm.c +7 -5
index 04146da2d1d8..f72b71511a65 100644
@@ -526,21 +526,31 @@ static int simpledrm_device_init_mm(struct simpledrm_device *sdev)
  {
  	struct drm_device *dev = &sdev->dev;
  	struct platform_device *pdev = sdev->pdev;
-	struct resource *mem;
+	struct resource *res, *mem;
  	void __iomem *screen_base;
  	int ret;
 
-	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-	if (!mem)
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!res)
  		return -EINVAL;
 
-	ret = devm_aperture_acquire_from_firmware(dev, mem->start, resource_size(mem));
+	ret = devm_aperture_acquire_from_firmware(dev, res->start, resource_size(res));
  	if (ret) {
  		drm_err(dev, "could not acquire memory range %pr: error %d\n",
-			mem, ret);
+			res, ret);
  		return ret;
  	}
 
+	mem = devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
+				      sdev->dev.driver->name);

drivers/gpu/drm/tiny/simpledrm.c static int simpledrm_device_init_mm(struct simpledrm_device *sdev) · line 555

Quoted 10 lines
drivers/gpu/drm/tiny/simpledrm.c +8 -0
+	if (!mem) {
+		/*
+		 * We cannot make this fatal. Sometimes this comes from magic
+		 * spaces our resource handlers simply don't know about
+		 */
+		drm_warn(dev, "could not acquire memory region %pr\n", res);
+	}
+
  	screen_base = devm_ioremap_wc(&pdev->dev, mem->start,
  				      resource_size(mem));

if mem is NULL, accessing mem->start will segfault after the warning. I think you renamed "mem" to "res" so probably it should be renamed here too ?

Quoted 1 line
drivers/gpu/drm/tiny/simpledrm.c +0 -0
  	if (!screen_base)
author maintainer
#12 lore raw

Re: [PATCH 3/5] drm/simpledrm: Request memory region in driver

Hi

Am 24.01.22 um 15:23 schrieb Jocelyn Falempe:

Quoted 46 lines · Thomas Zimmermann
Quoted 45 lines

Requesting the framebuffer memory in simpledrm marks the memory range as busy. This used to be done by the firmware sysfb code, but the driver is the correct place.

---
  drivers/gpu/drm/tiny/simpledrm.c | 20 +++++++++++++++-----

  1 file changed, 15 insertions(+), 5 deletions(-)
drivers/gpu/drm/tiny/simpledrm.c +12 -5
index 04146da2d1d8..f72b71511a65 100644
@@ -526,21 +526,31 @@ static int simpledrm_device_init_mm(struct 
 simpledrm_device *sdev)
   {
       struct drm_device *dev = &sdev->dev;
       struct platform_device *pdev = sdev->pdev;
-    struct resource *mem;
+    struct resource *res, *mem;
       void __iomem *screen_base;
       int ret;
-    mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-    if (!mem)
+    res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+    if (!res)
           return -EINVAL;
-    ret = devm_aperture_acquire_from_firmware(dev, mem->start, 
 resource_size(mem));
+    ret = devm_aperture_acquire_from_firmware(dev, res->start, 
 resource_size(res));
       if (ret) {
           drm_err(dev, "could not acquire memory range %pr: error %d\n",
-            mem, ret);
+            res, ret);
           return ret;
       }
+    mem = devm_request_mem_region(&pdev->dev, res->start, 
 resource_size(res),
+                      sdev->dev.driver->name);
+    if (!mem) {
+        /*
+         * We cannot make this fatal. Sometimes this comes from magic
+         * spaces our resource handlers simply don't know about
+         */
Quoted 8 lines

drivers/gpu/drm/tiny/simpledrm.c static int simpledrm_device_init_mm(struct simpledrm_device *sdev) · line 555

Quoted 5 lines
drivers/gpu/drm/tiny/simpledrm.c +3 -0
+        drm_warn(dev, "could not acquire memory region %pr\n", res);
+    }
+
       screen_base = devm_ioremap_wc(&pdev->dev, mem->start,
                         resource_size(mem));

if mem is NULL, accessing mem->start will segfault after the warning. I think you renamed "mem" to "res" so probably it should be renamed here too ?

Thanks for reviewing. Will be fixed in the next version. That code used to fail and i changed it to a warning after sync'ing with the simplefb driver. :/

Best regards
Thomas

Quoted 3 lines
Quoted 2 lines

      if (!screen_base)

Signature · 6 lines
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
author maintainer
#13 lore raw

PATCH 4/5 fbdev/simplefb: Request memory region in driver

In reply to Thomas Zimmermann

Requesting the framebuffer memory in simpledrm marks the memory range as busy. This used to be done by the firmware sysfb code, but the driver is the correct place.

---
 drivers/video/fbdev/simplefb.c | 59 ++++++++++++++++++++++++----------
 1 file changed, 42 insertions(+), 17 deletions(-)
drivers/video/fbdev/simplefb.c +42 -17
index 57541887188b..84452028ecc9 100644
@@ -66,16 +66,36 @@ static int simplefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
 	return 0;
 }
 
-struct simplefb_par;
+struct simplefb_par {
+	u32 palette[PSEUDO_PALETTE_SIZE];
+#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
+	bool clks_enabled;
+	unsigned int clk_count;
+	struct clk **clks;
+#endif
+#if defined CONFIG_OF && defined CONFIG_REGULATOR
+	bool regulators_enabled;
+	u32 regulator_count;
+	struct regulator **regulators;
+#endif
+	bool release_mem_region;
+};
+
 static void simplefb_clocks_destroy(struct simplefb_par *par);
 static void simplefb_regulators_destroy(struct simplefb_par *par);
 
 static void simplefb_destroy(struct fb_info *info)
 {
+	struct simplefb_par *par = info->par;
+
 	simplefb_regulators_destroy(info->par);
 	simplefb_clocks_destroy(info->par);
 	if (info->screen_base)
 		iounmap(info->screen_base);
+
+	if (par->release_mem_region)
+		release_mem_region(info->apertures->ranges[0].base,
+				   info->apertures->ranges[0].size);
 }
 
 static const struct fb_ops simplefb_ops = {
@@ -169,20 +189,6 @@ static int simplefb_parse_pd(struct platform_device *pdev,
 	return 0;
 }
 
-struct simplefb_par {
-	u32 palette[PSEUDO_PALETTE_SIZE];
-#if defined CONFIG_OF && defined CONFIG_COMMON_CLK
-	bool clks_enabled;
-	unsigned int clk_count;
-	struct clk **clks;
-#endif
-#if defined CONFIG_OF && defined CONFIG_REGULATOR
-	bool regulators_enabled;
-	u32 regulator_count;
-	struct regulator **regulators;
-#endif
-};
-
 #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
 /*
  * Clock handling code.
@@ -401,6 +407,7 @@ static void simplefb_regulators_destroy(struct simplefb_par *par) { }
 
 static int simplefb_probe(struct platform_device *pdev)
 {
+	bool request_mem_succeeded = false;
 	int ret;
 	struct simplefb_params params;
 	struct fb_info *info;
@@ -436,9 +443,22 @@ static int simplefb_probe(struct platform_device *pdev)
 		return -EINVAL;
 	}
 
+	if (request_mem_region(mem->start, resource_size(mem), "simplefb")) {
+		request_mem_succeeded = true;
+	} else {
+		/*
+		 * We cannot make this fatal. Sometimes this comes from magic
+		 * spaces our resource handlers simply don't know about
+		 */
+		dev_warn(&pdev->dev, "simplefb: cannot reserve video memory at %pR\n",
+			 mem);
+	}
+
 	info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
-	if (!info)
-		return -ENOMEM;
+	if (!info) {
+		ret = -ENOMEM;
+		goto error_release_mem_region;
+	}
 	platform_set_drvdata(pdev, info);
 
 	par = info->par;
@@ -495,6 +515,8 @@ static int simplefb_probe(struct platform_device *pdev)
 			     info->var.xres, info->var.yres,
 			     info->var.bits_per_pixel, info->fix.line_length);
 
+	par->release_mem_region = request_mem_succeeded;
+
 	ret = register_framebuffer(info);
 	if (ret < 0) {
 		dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
@@ -513,6 +535,9 @@ static int simplefb_probe(struct platform_device *pdev)
 	iounmap(info->screen_base);
 error_fb_release:
 	framebuffer_release(info);
+error_release_mem_region:
+	if (request_mem_succeeded)
+		release_mem_region(mem->start, resource_size(mem));
 	return ret;
 }
 
Signature · 1 line
2.34.1
#14 lore raw

Re: [PATCH 4/5] fbdev/simplefb: Request memory region in driver

Quoted 21 lines · Thomas Zimmermann

Requesting the framebuffer memory in simpledrm marks the memory range as busy. This used to be done by the firmware sysfb code, but the driver is the correct place.

---
 drivers/video/fbdev/simplefb.c | 59 ++++++++++++++++++++++++----------
 1 file changed, 42 insertions(+), 17 deletions(-)
drivers/video/fbdev/simplefb.c +3 -1
index 57541887188b..84452028ecc9 100644
@@ -66,16 +66,36 @@ static int simplefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
 	return 0;
 }
 
-struct simplefb_par;
+struct simplefb_par {
+	u32 palette[PSEUDO_PALETTE_SIZE];
+#if defined CONFIG_OF && defined CONFIG_COMMON_CLK

drivers/video/fbdev/simplefb.c static int simplefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue, · line 81

Quoted 10 lines
drivers/video/fbdev/simplefb.c +10 -0
+	bool clks_enabled;
+	unsigned int clk_count;
+	struct clk **clks;
+#endif
+#if defined CONFIG_OF && defined CONFIG_REGULATOR
+	bool regulators_enabled;
+	u32 regulator_count;
+	struct regulator **regulators;
+#endif
+	bool release_mem_region;

Maybe instead you could have:

	struct resource *mem; 

that would be more consistent with the fields for the other resources like clocks, regulators, etc.

Quoted 7 lines
drivers/video/fbdev/simplefb.c +2 -0
+};
+
 static void simplefb_clocks_destroy(struct simplefb_par *par);
 static void simplefb_regulators_destroy(struct simplefb_par *par);
 
 static void simplefb_destroy(struct fb_info *info)
 {

drivers/video/fbdev/simplefb.c static int simplefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue, · line 98

Quoted 10 lines
drivers/video/fbdev/simplefb.c +6 -0
+	struct simplefb_par *par = info->par;
+
 	simplefb_regulators_destroy(info->par);
 	simplefb_clocks_destroy(info->par);
 	if (info->screen_base)
 		iounmap(info->screen_base);
+
+	if (par->release_mem_region)
+		release_mem_region(info->apertures->ranges[0].base,
+				   info->apertures->ranges[0].size);

and here you could instead use the pointer to the resource:

	if (par->mem)
		release_mem_region(par->mem->start, resource_size(par->mem));

[snip]

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

Quoted 12 lines

static int simplefb_probe(struct platform_device *pdev) {

drivers/video/fbdev/simplefb.c +3 -0
+	bool request_mem_succeeded = false;
 	int ret;
 	struct simplefb_params params;
 	struct fb_info *info;
@@ -436,9 +443,22 @@ static int simplefb_probe(struct platform_device *pdev)
 		return -EINVAL;
 	}
 
+	if (request_mem_region(mem->start, resource_size(mem), "simplefb")) {
+		request_mem_succeeded = true;

and if you do the request_mem_region() after the struct fb_info allocation then this could just be:

		par->mem = mem;

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Linux Engineering
Red Hat
author maintainer
#15 lore raw

PATCH 5/5 drm: Add TODO item for requesting memory regions

In reply to Thomas Zimmermann

Add a TODO item about requesting memory regions for each driver. The current DRM drivers don't do this consistently.

---
 Documentation/gpu/todo.rst | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
Documentation/gpu/todo.rst +15 -0
index da138dd39883..1b2372ef4131 100644
@@ -467,6 +467,21 @@ Contact: Thomas Zimmermann <[email protected]>
 
 Level: Intermediate
 
+Request memory regions in all drivers
+-------------------------------------
+
+Go through all drivers and add code to request the memory regions that the
+driver uses. This requires adding calls to request_mem_region(),
+pci_request_region() or similar functions. Use helpers for managed cleanup
+where possible.
+
+Drivers are pretty bad at doing this and there used to be conflicts among
+DRM and fbdev drivers. Still, it's the correct thing to do.
+
+Contact: Thomas Zimmermann <[email protected]>
+
+Level: Starter
+
 
 Core refactorings
 =================
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 5/5
#16 lore raw

Re: [PATCH 5/5] drm: Add TODO item for requesting memory regions

Quoted 5 lines · Thomas Zimmermann

Add a TODO item about requesting memory regions for each driver. The current DRM drivers don't do this consistently.

---

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Linux Engineering
Red Hat