diff mbox series

[3/5] fbdev: Introduce devm_register_framebuffer()

Message ID 20240827-efifb-sysfs-v1-3-c9cc3e052180@weissschuh.net (mailing list archive)
State Accepted, archived
Headers show
Series fbdev: devm_register_framebuffer() and some fixes for efifb | expand

Commit Message

Thomas Weißschuh Aug. 27, 2024, 3:25 p.m. UTC
Introduce a device-managed variant of register_framebuffer() which
automatically unregisters the framebuffer on device destruction.
This can simplify the error handling and resource management in drivers.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 drivers/video/fbdev/core/fbmem.c | 24 ++++++++++++++++++++++++
 include/linux/fb.h               |  1 +
 2 files changed, 25 insertions(+)

Comments

Thomas Weißschuh Aug. 30, 2024, 7:17 a.m. UTC | #1
Hi everybody,

On 2024-08-27 17:25:14+0000, Thomas Weißschuh wrote:
> Introduce a device-managed variant of register_framebuffer() which
> automatically unregisters the framebuffer on device destruction.
> This can simplify the error handling and resource management in drivers.

Bert reported that this series broke his framebuffer ([0], [1]).

[0] https://lore.kernel.org/lkml/20240829224124.2978-1-spasswolf@web.de/
[1] https://lore.kernel.org/lkml/20240829230438.3226-1-spasswolf@web.de/

> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
>  drivers/video/fbdev/core/fbmem.c | 24 ++++++++++++++++++++++++
>  include/linux/fb.h               |  1 +
>  2 files changed, 25 insertions(+)
> 
> diff --git a/drivers/video/fbdev/core/fbmem.c b/drivers/video/fbdev/core/fbmem.c
> index 4c4ad0a86a50..d17a2daa2483 100644
> --- a/drivers/video/fbdev/core/fbmem.c
> +++ b/drivers/video/fbdev/core/fbmem.c
> @@ -544,6 +544,30 @@ unregister_framebuffer(struct fb_info *fb_info)

[..]

> +/**
> + *	devm_register_framebuffer - resource-managed frame buffer device registration
> + *	@dev: device the framebuffer belongs to
> + *	@fb_info: frame buffer info structure
> + *
> + *	Registers a frame buffer device @fb_info to device @dev.
> + *
> + *	Returns negative errno on error, or zero for success.
> + *
> + */
> +int
> +devm_register_framebuffer(struct device *dev, struct fb_info *fb_info)
> +{
> +	return devm_add_action_or_reset(dev, devm_unregister_framebuffer, fb_info);
> +}
> +EXPORT_SYMBOL(devm_register_framebuffer);

This implementation is wrong, it never actually registers the
framebuffer. It should look like this:

int
devm_register_framebuffer(struct device *dev, struct fb_info *fb_info)
{
	int ret;

	ret = register_framebuffer(fb_info);
	if (ret)
		return ret;

	return devm_add_action_or_reset(dev, devm_unregister_framebuffer, fb_info);
}
EXPORT_SYMBOL(devm_register_framebuffer);

Bert, could you test this?
Helge, do you want me to resend the series, minus the original patch 1?
Bert Karwatzki Aug. 30, 2024, 8:25 a.m. UTC | #2
Am Freitag, dem 30.08.2024 um 09:17 +0200 schrieb Thomas Weißschuh:
> Hi everybody,
>
> On 2024-08-27 17:25:14+0000, Thomas Weißschuh wrote:
> > Introduce a device-managed variant of register_framebuffer() which
> > automatically unregisters the framebuffer on device destruction.
> > This can simplify the error handling and resource management in drivers.
>
> Bert reported that this series broke his framebuffer ([0], [1]).
>
> [0] https://lore.kernel.org/lkml/20240829224124.2978-1-spasswolf@web.de/
> [1] https://lore.kernel.org/lkml/20240829230438.3226-1-spasswolf@web.de/
>
> > Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> > ---
> >  drivers/video/fbdev/core/fbmem.c | 24 ++++++++++++++++++++++++
> >  include/linux/fb.h               |  1 +
> >  2 files changed, 25 insertions(+)
> >
> > diff --git a/drivers/video/fbdev/core/fbmem.c b/drivers/video/fbdev/core/fbmem.c
> > index 4c4ad0a86a50..d17a2daa2483 100644
> > --- a/drivers/video/fbdev/core/fbmem.c
> > +++ b/drivers/video/fbdev/core/fbmem.c
> > @@ -544,6 +544,30 @@ unregister_framebuffer(struct fb_info *fb_info)
>
> [..]
>
> > +/**
> > + *	devm_register_framebuffer - resource-managed frame buffer device registration
> > + *	@dev: device the framebuffer belongs to
> > + *	@fb_info: frame buffer info structure
> > + *
> > + *	Registers a frame buffer device @fb_info to device @dev.
> > + *
> > + *	Returns negative errno on error, or zero for success.
> > + *
> > + */
> > +int
> > +devm_register_framebuffer(struct device *dev, struct fb_info *fb_info)
> > +{
> > +	return devm_add_action_or_reset(dev, devm_unregister_framebuffer, fb_info);
> > +}
> > +EXPORT_SYMBOL(devm_register_framebuffer);
>
> This implementation is wrong, it never actually registers the
> framebuffer. It should look like this:
>
> int
> devm_register_framebuffer(struct device *dev, struct fb_info *fb_info)
> {
> 	int ret;
>
> 	ret = register_framebuffer(fb_info);
> 	if (ret)
> 		return ret;
>
> 	return devm_add_action_or_reset(dev, devm_unregister_framebuffer, fb_info);
> }
> EXPORT_SYMBOL(devm_register_framebuffer);
>
> Bert, could you test this?
> Helge, do you want me to resend the series, minus the original patch 1?

Yes, this works for me. Thanks!

Bert Karwatzki
Helge Deller Aug. 30, 2024, 8:44 a.m. UTC | #3
On 8/30/24 10:25, Bert Karwatzki wrote:
> Am Freitag, dem 30.08.2024 um 09:17 +0200 schrieb Thomas Weißschuh:
>> Hi everybody,
>>
>> On 2024-08-27 17:25:14+0000, Thomas Weißschuh wrote:
>>> Introduce a device-managed variant of register_framebuffer() which
>>> automatically unregisters the framebuffer on device destruction.
>>> This can simplify the error handling and resource management in drivers.
>>
>> Bert reported that this series broke his framebuffer ([0], [1]).
>>
>> [0] https://lore.kernel.org/lkml/20240829224124.2978-1-spasswolf@web.de/
>> [1] https://lore.kernel.org/lkml/20240829230438.3226-1-spasswolf@web.de/
>>
>>> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
>>> ---
>>>   drivers/video/fbdev/core/fbmem.c | 24 ++++++++++++++++++++++++
>>>   include/linux/fb.h               |  1 +
>>>   2 files changed, 25 insertions(+)
>>>
>>> diff --git a/drivers/video/fbdev/core/fbmem.c b/drivers/video/fbdev/core/fbmem.c
>>> index 4c4ad0a86a50..d17a2daa2483 100644
>>> --- a/drivers/video/fbdev/core/fbmem.c
>>> +++ b/drivers/video/fbdev/core/fbmem.c
>>> @@ -544,6 +544,30 @@ unregister_framebuffer(struct fb_info *fb_info)
>>
>> [..]
>>
>>> +/**
>>> + *	devm_register_framebuffer - resource-managed frame buffer device registration
>>> + *	@dev: device the framebuffer belongs to
>>> + *	@fb_info: frame buffer info structure
>>> + *
>>> + *	Registers a frame buffer device @fb_info to device @dev.
>>> + *
>>> + *	Returns negative errno on error, or zero for success.
>>> + *
>>> + */
>>> +int
>>> +devm_register_framebuffer(struct device *dev, struct fb_info *fb_info)
>>> +{
>>> +	return devm_add_action_or_reset(dev, devm_unregister_framebuffer, fb_info);
>>> +}
>>> +EXPORT_SYMBOL(devm_register_framebuffer);
>>
>> This implementation is wrong, it never actually registers the
>> framebuffer. It should look like this:
>>
>> int
>> devm_register_framebuffer(struct device *dev, struct fb_info *fb_info)
>> {
>> 	int ret;
>>
>> 	ret = register_framebuffer(fb_info);
>> 	if (ret)
>> 		return ret;
>>
>> 	return devm_add_action_or_reset(dev, devm_unregister_framebuffer, fb_info);
>> }
>> EXPORT_SYMBOL(devm_register_framebuffer);
>>
>> Bert, could you test this?
>> Helge, do you want me to resend the series, minus the original patch 1?
>
> Yes, this works for me. Thanks!

Good.

Thomas, please just resend the fixed patch #3 (this one).
And maybe you want to document devm_unregister_framebuffer()
similiar to what you added for devm_register_framebuffer() ?

Helge
diff mbox series

Patch

diff --git a/drivers/video/fbdev/core/fbmem.c b/drivers/video/fbdev/core/fbmem.c
index 4c4ad0a86a50..d17a2daa2483 100644
--- a/drivers/video/fbdev/core/fbmem.c
+++ b/drivers/video/fbdev/core/fbmem.c
@@ -544,6 +544,30 @@  unregister_framebuffer(struct fb_info *fb_info)
 }
 EXPORT_SYMBOL(unregister_framebuffer);
 
+static void devm_unregister_framebuffer(void *data)
+{
+	struct fb_info *info = data;
+
+	unregister_framebuffer(info);
+}
+
+/**
+ *	devm_register_framebuffer - resource-managed frame buffer device registration
+ *	@dev: device the framebuffer belongs to
+ *	@fb_info: frame buffer info structure
+ *
+ *	Registers a frame buffer device @fb_info to device @dev.
+ *
+ *	Returns negative errno on error, or zero for success.
+ *
+ */
+int
+devm_register_framebuffer(struct device *dev, struct fb_info *fb_info)
+{
+	return devm_add_action_or_reset(dev, devm_unregister_framebuffer, fb_info);
+}
+EXPORT_SYMBOL(devm_register_framebuffer);
+
 /**
  *	fb_set_suspend - low level driver signals suspend
  *	@info: framebuffer affected
diff --git a/include/linux/fb.h b/include/linux/fb.h
index db7d97b10964..abf6643ebcaf 100644
--- a/include/linux/fb.h
+++ b/include/linux/fb.h
@@ -601,6 +601,7 @@  extern ssize_t fb_sys_write(struct fb_info *info, const char __user *buf,
 /* fbmem.c */
 extern int register_framebuffer(struct fb_info *fb_info);
 extern void unregister_framebuffer(struct fb_info *fb_info);
+extern int devm_register_framebuffer(struct device *dev, struct fb_info *fb_info);
 extern char* fb_get_buffer_offset(struct fb_info *info, struct fb_pixmap *buf, u32 size);
 extern void fb_pad_unaligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 idx,
 				u32 height, u32 shift_high, u32 shift_low, u32 mod);