fbcon: Clean up glyph handling on redraws
Subsystems (3)
Thomas Zimmermann
maintainerv2 cover fbcon: Clean up glyph handling on redraws
Fbcon's drawing functions open-code much of the glyph handling. Use font helpers instead. Makes fbcon more consistent and easier to understand.
Patch 1 updates fbcon to use unsigned char for glyph buffers. This is consistent with the font code and font_data_t.
Patches 2 and 3 update font rotation to return rotated glyphs as font_data_t. A rotated font can now use the same interfaces as the unrotated source font. Fbcon uses this to ref count the rotated font's buffer.
Patches 4 to 6 update various parts of fbcon to use font helpers instead of open-coding access to font_data_t. Having the rotated font as font_data_t makes this convenient.
Tested with fbcon on DRM's bochs driver (qemu).
v2:
- resolve merge conflicts in fbcon
- fix typos in commit descriptions
Thomas Zimmermann (6):
fbcon: Use unsigned char for glyph data
lib/fonts: Provide internal helpers for allocating font_data_t lib/fonts, fbcon: Use font_data_t for rotated fonts
fbcon: Refactor size calculation for cursor buffers
fbcon: Look up glyphs using the font interface
fbcon: Handle characters with no glyph defined in a consistent way
drivers/video/fbdev/core/bitblit.c | 82 +++++++++++++++---------- drivers/video/fbdev/core/fbcon.c | 3 +- drivers/video/fbdev/core/fbcon.h | 5 +- drivers/video/fbdev/core/fbcon_ccw.c | 56 +++++++++++------ drivers/video/fbdev/core/fbcon_cw.c | 57 ++++++++++------- drivers/video/fbdev/core/fbcon_rotate.c | 19 +++--- drivers/video/fbdev/core/fbcon_ud.c | 77 +++++++++++++++-------- include/linux/font.h | 5 +- lib/fonts/font.h | 12 ++++ lib/fonts/font_rotate.c | 32 ++++------ lib/fonts/fonts.c | 63 ++++++++++++------- 11 files changed, 254 insertions(+), 157 deletions(-)
base-commit: 7d502136b31169f6b5575f7f89c3da60cdf7891d
Signature · 1 line
2.56.0
Thomas Zimmermann
maintainerPATCH v2 1/6 fbcon: Use unsigned char for glyph data
Represent glyph data as unsigned char, as done by font_data_t. Convert all related code. Also declare source buffers with glyph data as const because internal fonts reside in read-only memory. Avoid writing them.
We still use char for glyph data in struct fb_image. The type is part of the kernel uAPI and therefore cannot be changed. Fall back to type casting in this case.
Destination buffers for font drawing are still u8. This is framebuffer memory provided by graphics hardware.
--- drivers/video/fbdev/core/bitblit.c | 25 ++++++++++++++----------- drivers/video/fbdev/core/fbcon.h | 4 ++-- drivers/video/fbdev/core/fbcon_ccw.c | 22 ++++++++++++---------- drivers/video/fbdev/core/fbcon_cw.c | 22 ++++++++++++---------- drivers/video/fbdev/core/fbcon_ud.c | 28 +++++++++++++++------------- 5 files changed, 55 insertions(+), 46 deletions(-)
drivers/video/fbdev/core/bitblit.c +14 -11
@@ -23,12 +23,13 @@/* * Accelerated handlers. */ -static void update_attr (u8 *dst,const u8 *src,int attribute,struct vc_data *vc) +static void update_attr (unsigned char *dst,const unsigned char *src,int attribute, +struct vc_data *vc) {int i, offset = (vc->vc_font.height <10 ) ?1 :2 ;int width =DIV_ROUND_UP (vc->vc_font.width,8 );unsigned int cellsize = vc->vc_font.height * width; -u8 c; +unsigned char c; offset = cellsize - (offset * width);for (i =0 ; i < cellsize; i++) {
@@ -76,12 +77,13 @@ static void bit_clear(struct vc_data *vc, struct fb_info *info, int sy,static inline void bit_putcs_aligned (struct vc_data *vc,struct fb_info *info,const u16 *s,u32 attr,u32 cnt,u32 d_pitch,u32 s_pitch,u32 cellsize, -struct fb_image *image,u8 *buf,u8 *dst) +struct fb_image *image, +unsigned char *buf,u8 *dst) {u16 charmask = vc->vc_hi_font_mask ?0x1ff :0xff ;unsigned int charcnt = vc->vc_font.charcount;u32 idx = vc->vc_font.width >>3 ; -const u8 *src; +const unsigned char *src;while (cnt--) {u16 ch =scr_readw (s++) & charmask;
@@ -112,15 +114,15 @@ static inline void bit_putcs_unaligned(struct vc_data *vc, struct fb_info *info, const u16 *s, u32 attr, u32 cnt, u32 d_pitch, u32 s_pitch, u32 cellsize, - struct fb_image *image, u8 *buf, - u8 *dst) + struct fb_image *image, + unsigned char *buf, u8 *dst) { u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff;unsigned int charcnt = vc->vc_font.charcount;u32 shift_low =0 , mod = vc->vc_font.width %8 ;u32 shift_high =8 ;u32 idx = vc->vc_font.width >>3 ; -const u8 *src; +const unsigned char *src;while (cnt--) {u16 ch =scr_readw (s++) & charmask;
@@ -161,7 +163,8 @@ static void bit_putcs(struct vc_data *vc, struct fb_info *info,u32 buf_align = info->pixmap.buf_align -1 ;u32 mod = vc->vc_font.width %8 , cnt, pitch, size;u32 attribute =get_attribute (info,scr_readw (s)); -u8 *dst, *buf =NULL ; +unsigned char *buf =NULL ; +u8 *dst; image.fg_color = fg; image.bg_color = bg;
@@ -269,7 +272,7 @@ static void bit_cursor(struct vc_data *vc, struct fb_info *info, bool enable,int y =real_y (par->p, vc->state.y);int attribute, use_sw = vc->vc_cursor_type &CUR_SW ;int err =1 ; -const u8 *src; +const unsigned char *src; cursor.set =0 ;
@@ -287,12 +290,12 @@ static void bit_cursor(struct vc_data *vc, struct fb_info *info, bool enable,if (par->cursor_state.image.data != (const char *)src || par->cursor_reset) { - par->cursor_state.image.data = src; + par->cursor_state.image.data = (const char *)src; cursor.set |=FB_CUR_SETIMAGE ; }if (attribute) { -u8 *dst; +unsigned char *dst; dst =kmalloc_array (w, vc->vc_font.height,GFP_ATOMIC );if (!dst)
drivers/video/fbdev/core/fbcon.h +2 -2
@@ -75,7 +75,7 @@ struct fbcon_bitops { #ifdef CONFIG_FRAMEBUFFER_CONSOLE_ROTATION struct fbcon_rotated { font_data_t *fontdata; /* source font */ - u8 *buf; /* rotated glyphs */ + unsigned char *buf; /* rotated glyphs */ size_t bufsize; int buf_rotate; /* rotation of buf */ unsigned int buf_width; /* geometry of source font */
@@ -98,7 +98,7 @@ struct fbcon_par {int graphics;bool initialized;int rotate; -char *cursor_data; +unsigned char *cursor_data;#ifdef CONFIG_FRAMEBUFFER_CONSOLE_ROTATION struct fbcon_rotated rotated;#endif
drivers/video/fbdev/core/fbcon_ccw.c +12 -10
@@ -23,13 +23,13 @@* Rotation 270 degrees */ -static void ccw_update_attr (u8 *dst,u8 *src,int attribute, -struct vc_data *vc) +static void ccw_update_attr (unsigned char *dst,const unsigned char *src,int attribute, +struct vc_data *vc) {int i, j, offset = (vc->vc_font.height <10 ) ?1 :2 ;int width =font_glyph_pitch (vc->vc_font.height);int mod = vc->vc_font.height %8 ; -u8 c, msk = ~(0xff << offset), msk1 =0 ; +unsigned char c, msk = ~(0xff << offset), msk1 =0 ;if (mod) msk <<= (8 - mod);
@@ -98,12 +98,13 @@ static void ccw_clear(struct vc_data *vc, struct fb_info *info, int sy,static inline void ccw_putcs_aligned (struct vc_data *vc,struct fb_info *info,const u16 *s,u32 attr,u32 cnt,u32 d_pitch,u32 s_pitch,u32 cellsize, -struct fb_image *image,u8 *buf,u8 *dst) +struct fb_image *image, +unsigned char *buf,u8 *dst) {struct fbcon_par *par = info->fbcon_par;u16 charmask = vc->vc_hi_font_mask ?0x1ff :0xff ;u32 idx =font_glyph_pitch (vc->vc_font.height); -u8 *src; +const unsigned char *src;while (cnt--) { src = par->rotated.buf + (scr_readw (s--) & charmask) * cellsize;
@@ -139,8 +140,9 @@ static void ccw_putcs(struct vc_data *vc, struct fb_info *info,u32 buf_align = info->pixmap.buf_align -1 ;u32 cnt, pitch, size;u32 attribute =get_attribute (info,scr_readw (s)); -u8 *dst, *buf =NULL ; +unsigned char *buf =NULL ;u32 vyres =GETVYRES (par->p, info); +u8 *dst;if (!par->rotated.buf)return ;
@@ -229,7 +231,7 @@ static void ccw_cursor(struct vc_data *vc, struct fb_info *info, bool enable,int y =real_y (par->p, vc->state.y);int attribute, use_sw = vc->vc_cursor_type &CUR_SW ;int err =1 , dx, dy; -char *src; +const unsigned char *src;u32 vyres =GETVYRES (par->p, info);if (!par->rotated.buf)
@@ -241,14 +243,14 @@ static void ccw_cursor(struct vc_data *vc, struct fb_info *info, bool enable, attribute =get_attribute (info, c); src = par->rotated.buf + ((c & charmask) * (w * vc->vc_font.width)); -if (par->cursor_state.image.data != src || +if (par->cursor_state.image.data != (const char *)src || par->cursor_reset) { - par->cursor_state.image.data = src; + par->cursor_state.image.data = (const char *)src; cursor.set |=FB_CUR_SETIMAGE ; }if (attribute) { -u8 *dst; +unsigned char *dst; dst =kmalloc_array (w, vc->vc_font.width,GFP_ATOMIC );if (!dst)
drivers/video/fbdev/core/fbcon_cw.c +12 -10
@@ -23,12 +23,12 @@* Rotation 90 degrees */ -static void cw_update_attr (u8 *dst,u8 *src,int attribute, -struct vc_data *vc) +static void cw_update_attr (unsigned char *dst,const unsigned char *src,int attribute, +struct vc_data *vc) {int i, j, offset = (vc->vc_font.height <10 ) ?1 :2 ;int width =font_glyph_pitch (vc->vc_font.height); -u8 c, msk = ~(0xff >> offset); +unsigned char c, msk = ~(0xff >> offset);for (i =0 ; i < vc->vc_font.width; i++) {for (j =0 ; j < width; j++) {
@@ -83,12 +83,13 @@ static void cw_clear(struct vc_data *vc, struct fb_info *info, int sy,static inline void cw_putcs_aligned (struct vc_data *vc,struct fb_info *info,const u16 *s,u32 attr,u32 cnt,u32 d_pitch,u32 s_pitch,u32 cellsize, -struct fb_image *image,u8 *buf,u8 *dst) +struct fb_image *image, +unsigned char *buf,u8 *dst) {struct fbcon_par *par = info->fbcon_par;u16 charmask = vc->vc_hi_font_mask ?0x1ff :0xff ;u32 idx =font_glyph_pitch (vc->vc_font.height); -u8 *src; +const unsigned char *src;while (cnt--) { src = par->rotated.buf + (scr_readw (s++) & charmask) * cellsize;
@@ -124,8 +125,9 @@ static void cw_putcs(struct vc_data *vc, struct fb_info *info,u32 buf_align = info->pixmap.buf_align -1 ;u32 cnt, pitch, size;u32 attribute =get_attribute (info,scr_readw (s)); -u8 *dst, *buf =NULL ; +unsigned char *buf =NULL ;u32 vxres =GETVXRES (par->p, info); +u8 *dst;if (!par->rotated.buf)return ;
@@ -212,7 +214,7 @@ static void cw_cursor(struct vc_data *vc, struct fb_info *info, bool enable,int y =real_y (par->p, vc->state.y);int attribute, use_sw = vc->vc_cursor_type &CUR_SW ;int err =1 , dx, dy; -char *src; +const unsigned char *src;u32 vxres =GETVXRES (par->p, info);if (!par->rotated.buf)
@@ -224,14 +226,14 @@ static void cw_cursor(struct vc_data *vc, struct fb_info *info, bool enable, attribute =get_attribute (info, c); src = par->rotated.buf + ((c & charmask) * (w * vc->vc_font.width)); -if (par->cursor_state.image.data != src || +if (par->cursor_state.image.data != (const char *)src || par->cursor_reset) { - par->cursor_state.image.data = src; + par->cursor_state.image.data = (const char *)src; cursor.set |=FB_CUR_SETIMAGE ; }if (attribute) { -u8 *dst; +unsigned char *dst; dst =kmalloc_array (w, vc->vc_font.width,GFP_ATOMIC );if (!dst)
drivers/video/fbdev/core/fbcon_ud.c +15 -13
@@ -22,13 +22,13 @@* Rotation 180 degrees */ -static void ud_update_attr (u8 *dst,u8 *src,int attribute, -struct vc_data *vc) +static void ud_update_attr (unsigned char *dst,const unsigned char *src,int attribute, +struct vc_data *vc) {int i, offset = (vc->vc_font.height <10 ) ?1 :2 ;int width =font_glyph_pitch (vc->vc_font.width);unsigned int cellsize = vc->vc_font.height * width; -u8 c; +unsigned char c; offset = offset * width;
@@ -84,12 +84,13 @@ static void ud_clear(struct vc_data *vc, struct fb_info *info, int sy,static inline void ud_putcs_aligned (struct vc_data *vc,struct fb_info *info,const u16 *s,u32 attr,u32 cnt,u32 d_pitch,u32 s_pitch,u32 cellsize, -struct fb_image *image,u8 *buf,u8 *dst) +struct fb_image *image, +unsigned char *buf,u8 *dst) {struct fbcon_par *par = info->fbcon_par;u16 charmask = vc->vc_hi_font_mask ?0x1ff :0xff ;u32 idx = vc->vc_font.width >>3 ; -u8 *src; +const unsigned char *src;while (cnt--) { src = par->rotated.buf + (scr_readw (s--) & charmask) * cellsize;
@@ -116,15 +117,15 @@ static inline void ud_putcs_unaligned(struct vc_data *vc, struct fb_info *info, const u16 *s, u32 attr, u32 cnt, u32 d_pitch, u32 s_pitch, u32 cellsize, - struct fb_image *image, u8 *buf, - u8 *dst) + struct fb_image *image, + unsigned char *buf, u8 *dst) { struct fbcon_par *par = info->fbcon_par;u16 charmask = vc->vc_hi_font_mask ?0x1ff :0xff ;u32 shift_low =0 , mod = vc->vc_font.width %8 ;u32 shift_high =8 ;u32 idx = vc->vc_font.width >>3 ; -u8 *src; +const unsigned char *src;while (cnt--) { src = par->rotated.buf + (scr_readw (s--) & charmask) * cellsize;
@@ -160,9 +161,10 @@ static void ud_putcs(struct vc_data *vc, struct fb_info *info,u32 buf_align = info->pixmap.buf_align -1 ;u32 mod = vc->vc_font.width %8 , cnt, pitch, size;u32 attribute =get_attribute (info,scr_readw (s)); -u8 *dst, *buf =NULL ; +unsigned char *buf =NULL ;u32 vyres =GETVYRES (par->p, info);u32 vxres =GETVXRES (par->p, info); +u8 *dst;if (!par->rotated.buf)return ;
@@ -258,7 +260,7 @@ static void ud_cursor(struct vc_data *vc, struct fb_info *info, bool enable,int y =real_y (par->p, vc->state.y);int attribute, use_sw = vc->vc_cursor_type &CUR_SW ;int err =1 , dx, dy; -char *src; +const unsigned char *src;u32 vyres =GETVYRES (par->p, info);u32 vxres =GETVXRES (par->p, info);
@@ -271,14 +273,14 @@ static void ud_cursor(struct vc_data *vc, struct fb_info *info, bool enable, attribute =get_attribute (info, c); src = par->rotated.buf + ((c & charmask) * (w * vc->vc_font.height)); -if (par->cursor_state.image.data != src || +if (par->cursor_state.image.data != (const char *)src || par->cursor_reset) { - par->cursor_state.image.data = src; + par->cursor_state.image.data = (const char *)src; cursor.set |=FB_CUR_SETIMAGE ; }if (attribute) { -u8 *dst; +unsigned char *dst; dst =kmalloc_array (w, vc->vc_font.height,GFP_ATOMIC );if (!dst)
Signature · 1 line
2.56.0
Re: [PATCH v2 1/6] fbcon: Use unsigned char for glyph data
Quoted 11 lines · Thomas Zimmermann
Represent glyph data as unsigned char, as done by
font_data_t. Convert all related code. Also declare source buffers with glyph data as const because internal fonts reside in read-only memory. Avoid writing them.We still use char for glyph data in struct
fb_image. The type is part of the kernel uAPI and therefore cannot be changed. Fall back to type casting in this case.Destination buffers for font drawing are still u8. This is framebuffer memory provided by graphics hardware.
Quoted 8 lines
Signed-off-by Thomas Zimmermann <[email protected]>--- drivers/video/fbdev/core/bitblit.c | 25 ++++++++++++++----------- drivers/video/fbdev/core/fbcon.h | 4 ++-- drivers/video/fbdev/core/fbcon_ccw.c | 22 ++++++++++++---------- drivers/video/fbdev/core/fbcon_cw.c | 22 ++++++++++++---------- drivers/video/fbdev/core/fbcon_ud.c | 28 +++++++++++++++------------- 5 files changed, 55 insertions(+), 46 deletions(-)
For the whole series:
I've added the series to my (upcoming) for-next git tree.
Thanks!
Helge
Thomas Zimmermann
maintainerRe: [PATCH v2 1/6] fbcon: Use unsigned char for glyph data
Hi
Am 21.11.26 um 18:37 schrieb Helge Deller:
Quoted 22 lines · Thomas Zimmermann
Quoted 11 lines
Represent glyph data as unsigned char, as done by
font_data_t. Convert all related code. Also declare source buffers with glyph data as const because internal fonts reside in read-only memory. Avoid writing them.We still use char for glyph data in struct
fb_image. The type is part of the kernel uAPI and therefore cannot be changed. Fall back to type casting in this case.Destination buffers for font drawing are still u8. This is framebuffer memory provided by graphics hardware.
Quoted 8 lines
Signed-off-by Thomas Zimmermann <[email protected]>---
drivers/video/fbdev/core/bitblit.c | 25 ++++++++++++++----------- drivers/video/fbdev/core/fbcon.h | 4 ++--drivers/video/fbdev/core/fbcon_ccw.c| 22 ++++++++++++---------- drivers/video/fbdev/core/fbcon_cw.c | 22 ++++++++++++---------- drivers/video/fbdev/core/fbcon_ud.c | 28 +++++++++++++++-------------5 files changed, 55 insertions(+), 46 deletions(-)
Quoted 4 lines
For the whole series:
Reviewed-by Helge Deller <[email protected]>I've added the series to my (upcoming) for-next git tree.
Thanks. The review bot found a few issues, so there might be an update coming.
Best regards
Thomas
Quoted 3 lines
Thanks!
Helge
Signature · 6 lines
-- Thomas Zimmermann Graphics Driver Developer SUSE Software Solutions Germany GmbH Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri, (HRB 36809, AG Nürnberg)
Thomas Zimmermann
maintainerPATCH v2 2/6 lib/fonts: Provide internal helpers for allocating font_data_t
In reply to Thomas ZimmermannFactor out the allocation code for font_data_t from font import in font_data_import(). Retrieve a writable glyph buffer from the new helper __font_data_wrbuf().
Having the allocation code in separate functions makes it available for font rotation and maybe other font utilities. The _wrbuf() helper returns a writable glyph buffer. Font data is only readable in general.
--- lib/fonts/font.h | 12 +++++++++ lib/fonts/fonts.c | 63 ++++++++++++++++++++++++++++++----------------- 2 files changed, 52 insertions(+), 23 deletions(-)
lib/fonts/font.h +12 -0
@@ -4,6 +4,7 @@#define _LIB_FONTS_FONT_H#include <linux/font.h> +#include <linux/types.h> /* * Font data
@@ -16,6 +17,17 @@ struct font_data {unsigned char data[]; } __packed; +/* +* font_data_t and helpers +*/ + +static inline unsigned char *__font_data_wrbuf (font_data_t *fd) +{ +return (unsigned char *)fd; +} + +font_data_t *__font_data_alloc (unsigned int size,gfp_t flags); +/* * Built-in fonts */
lib/fonts/fonts.c +40 -23
82 lines not on this page · raw message
Signature · 1 line
2.56.0
Thomas Zimmermann
maintainerPATCH v2 3/6 lib/fonts, fbcon: Use font_data_t for rotated fonts
In reply to Thomas ZimmermannReturn rotated font data as font_data_t instead of unsigned char*. Release the data with font_data_put() in fbcon.
It is still possible to use preallocated font buffers for the rotated font data. The font data tracks the buffer size and reference count to do so; font_data_rotate() invokes __font_data_alloc() to allocate a new font-data buffer if necessary.
Returning rotated font data as font_data_t further allows for using regular font-data helpers on it. Fbcon now manages buffer life time with the font data's reference counting. Removes the plain kfree().
v2:
- fix typo in commit description
--- drivers/video/fbdev/core/fbcon.c | 3 ++- drivers/video/fbdev/core/fbcon.h | 3 +-- drivers/video/fbdev/core/fbcon_rotate.c | 19 ++++++++------- include/linux/font.h | 5 ++-- lib/fonts/font_rotate.c | 32 ++++++++++--------------- 5 files changed, 28 insertions(+), 34 deletions(-)
drivers/video/fbdev/core/fbcon.c +2 -1
@@ -770,7 +770,8 @@ static void fbcon_release(struct fb_info *info)kfree (par->cursor_data);kfree (par->cursor_src);#ifdef CONFIG_FRAMEBUFFER_CONSOLE_ROTATION -kfree (par->rotated.buf); +if (par->rotated.buf) +font_data_put (par->rotated.buf);#endif kfree (info->fbcon_par); info->fbcon_par =NULL ;
drivers/video/fbdev/core/fbcon.h +1 -2
@@ -75,8 +75,7 @@ struct fbcon_bitops { #ifdef CONFIG_FRAMEBUFFER_CONSOLE_ROTATION struct fbcon_rotated { font_data_t *fontdata; /* source font */ - unsigned char *buf; /* rotated glyphs */ - size_t bufsize; + font_data_t *buf; /* rotated font */ int buf_rotate; /* rotation of buf */ unsigned int buf_width; /* geometry of source font */ unsigned int buf_height;
drivers/video/fbdev/core/fbcon_rotate.c +10 -9
@@ -18,7 +18,7 @@int fbcon_rotate_font (struct fb_info *info,struct vc_data *vc) {struct fbcon_par *par = info->fbcon_par; -unsigned char *buf; +font_data_t *buf;int ret; if (par->p->fontdata == par->rotated.fontdata &&
@@ -40,21 +40,22 @@ int fbcon_rotate_font(struct fb_info *info, struct vc_data *vc) buf =font_data_rotate (par->rotated.fontdata, vc->vc_font.width, vc->vc_font.height, vc->vc_font.charcount, - par->rotated.buf_rotate, par->rotated.buf, - &par->rotated.bufsize); + par->rotated.buf_rotate, par->rotated.buf);if (IS_ERR (buf)) { ret =PTR_ERR (buf); - goto err_kfree; + goto err_font_data_put; } +if (par->rotated.buf) +font_data_put (par->rotated.buf); par->rotated.buf = buf;return 0 ; -err_kfree: -kfree (par->rotated.buf); - par->rotated.buf =NULL ;/* clear here to avoid output */ - par->rotated.bufsize =0 ; - +err_font_data_put: +if (par->rotated.buf) { +font_data_put (par->rotated.buf); + par->rotated.buf =NULL ;/* clear here to avoid output */ + }return ret; }
include/linux/font.h +2 -3
@@ -120,9 +120,8 @@ void font_glyph_rotate_180(const unsigned char *glyph, unsigned int width, unsig unsigned char *out);void font_glyph_rotate_270 (const unsigned char *glyph,unsigned int width,unsigned int height,unsigned char *out); -unsigned char *font_data_rotate (font_data_t *fd,unsigned int width,unsigned int height, -unsigned int charcount,unsigned int steps, -unsigned char *buf,size_t *bufsize); +font_data_t *font_data_rotate (font_data_t *fd,unsigned int width,unsigned int height, +unsigned int charcount,unsigned int steps,font_data_t *buf);/* * Font description
lib/fonts/font_rotate.c +13 -19
@@ -181,8 +181,7 @@ EXPORT_SYMBOL_GPL(font_glyph_rotate_270);* @height: The number of scanlines in the glyph * @charcount: The number of glyphs in the font * @steps: Number of rotation steps of 90° -* @buf: Preallocated output buffer; can be NULL -* @bufsize: The size of @buf in bytes; can be NULL +* @buf: Preallocated font data; can be NULL * * The parameters @width and @height refer to the visible number of pixels * and scanlines in a single glyph. The number of glyphs is given in @charcount.
@@ -194,16 +193,14 @@ EXPORT_SYMBOL_GPL(font_glyph_rotate_270);* as returned by font_data_buf(). Callers are responsible to free the returned * buffer with kfree(). Font rotation typically happens when displays get * re-oriented. To avoid unnecessary re-allocation of the memory buffer, the -* caller can pass in an earlier result buffer in @buf for reuse. The old and -* new buffer sizes are given and retrieved by the caller in @bufsize. The -* allocation semantics are compatible with krealloc(). +* caller can pass in an earlier result in @buf for reuse. The returned font +* data should be released with font_data_put(). * * Returns: -* A buffer with rotated glyphs on success, or an error pointer otherwise +* Font data with rotated glyphs on success, or an error pointer otherwise */ -unsigned char *font_data_rotate (font_data_t *fd,unsigned int width,unsigned int height, -unsigned int charcount,unsigned int steps, -unsigned char *buf,size_t *bufsize) +font_data_t *font_data_rotate (font_data_t *fd,unsigned int width,unsigned int height, +unsigned int charcount,unsigned int steps,font_data_t *buf) {const unsigned char *src =font_data_buf (fd);unsigned int s_cellsize =font_glyph_size (width, height);
@@ -227,19 +224,16 @@ unsigned char *font_data_rotate(font_data_t *fd, unsigned int width, unsigned inif (check_mul_overflow (charcount, d_cellsize, &size))return ERR_PTR (-EINVAL ); -if (!buf || !bufsize || size > *bufsize) { - dst =kmalloc_array (charcount, d_cellsize,GFP_KERNEL ); -if (!dst) -return ERR_PTR (-ENOMEM ); - -kfree (buf); - buf = dst; -if (bufsize) - *bufsize = size; +if (!buf || size >font_data_size (buf)) { + buf =__font_data_alloc (size,GFP_KERNEL ); +if (IS_ERR (buf)) +return buf; }else { - dst = buf; +font_data_get (buf); } + dst =__font_data_wrbuf (buf); +switch (steps) {case 0 :memcpy (dst, src, size);
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v2 3/6] lib/fonts, fbcon: Use font_data_t for rotated fonts
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] fbcon: invalid kfree() of font_data_t pointer in fbcon_do_set_font() - [Low] lib/fonts…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] fbcon: invalid kfree() of font_data_t pointer in fbcon_do_set_font()
- [Low] lib/fonts: contradictory kernel-doc comment for font_data_rotate()
--
commit b5f0d07cd7c39fad0c72c3148ca9444f977625ca
Author: Thomas Zimmermann <[email protected]>
lib/fonts, fbcon: Use font_data_t for rotated fonts
This commit converts font_data_rotate() and struct fbcon_rotated.buf to use font_data_t instead of unsigned char *, releasing rotated font data in fbcon with font_data_put().
Quoted 5 lines
drivers/video/fbdev/core/fbcon.h +0
-0
drivers/video/fbdev/core/fbcon.h struct fbcon_bitops { · line 81
Quoted 9 lines
drivers/video/fbdev/core/fbcon.h +1
-2@@ -75,8 +75,7 @@ struct fbcon_bitops { #ifdef CONFIG_FRAMEBUFFER_CONSOLE_ROTATION struct fbcon_rotated { font_data_t *fontdata; /* source font */ - unsigned char *buf; /* rotated glyphs */ - size_t bufsize; + font_data_t *buf; /* rotated font */ int buf_rotate; /* rotation of buf */ unsigned int buf_width; /* geometry of source font */ unsigned int buf_height;
[Severity: High]
Does fbcon_do_set_font() still pass struct fbcon_rotated.buf directly to kfree() when changing the font on a visible rotated console?
drivers/video/fbdev/core/fbcon.c:fbcon_do_set_font() {
...
#ifdef CONFIG_FRAMEBUFFER_CONSOLE_ROTATION
if (rotated_saved)
kfree(saved_rotated.buf);
#endif
...
err_out:
#ifdef CONFIG_FRAMEBUFFER_CONSOLE_ROTATION
if (rotated_saved) {
kfree(par->rotated.buf);
par->rotated = saved_rotated;
}
#endif
...
}Because __font_data_alloc() returns &font_data->data[0] (offset 16 bytes past the start of struct font_data), calling kfree() on saved_rotated.buf or par->rotated.buf frees an interior pointer and corrupts the slab. Both paths need to check for a non-NULL buffer and release it with font_data_put() instead.
[ ... ]
Quoted 15 lines
lib/fonts/font_rotate.c +1
-2@@ -181,8 +181,7 @@ EXPORT_SYMBOL_GPL(font_glyph_rotate_270);* @height: The number of scanlines in the glyph * @charcount: The number of glyphs in the font * @steps: Number of rotation steps of 90° -* @buf: Preallocated output buffer; can be NULL -* @bufsize: The size of @buf in bytes; can be NULL +* @buf: Preallocated font data; can be NULL * * The parameters @width and @height refer to the visible number of pixels * and scanlines in a single glyph. The number of glyphs is given in @charcount.
lib/fonts/font_rotate.c EXPORT_SYMBOL_GPL(font_glyph_rotate_270); · line 197
Quoted 8 lines
lib/fonts/font_rotate.c +2
-3@@ -194,16 +193,14 @@ EXPORT_SYMBOL_GPL(font_glyph_rotate_270);* as returned by font_data_buf(). Callers are responsible to free the returned * buffer with kfree(). Font rotation typically happens when displays get * re-oriented. To avoid unnecessary re-allocation of the memory buffer, the -* caller can pass in an earlier result buffer in @buf for reuse. The old and -* new buffer sizes are given and retrieved by the caller in @bufsize. The -* allocation semantics are compatible with krealloc(). +* caller can pass in an earlier result in @buf for reuse. The returned font +* data should be released with font_data_put().
[Severity: Low]
This isn't a bug, but the earlier sentences in this kernel-doc comment still say that the returned buffer is not compatible with font_data_t and must be freed with kfree(), contradicting the new sentence at the end.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Thomas Zimmermann
maintainerPATCH v2 4/6 fbcon: Refactor size calculation for cursor buffers
In reply to Thomas ZimmermannReplace open-coded size calculation for cursor glyph buffers with font helpers. Use it for glyph lookup and buffer allocation.
Treat glyph width as bits per scanline instead of bytes. This is consistent with font helpers.
Note that with screen rotation enabled, width and height swap places for CW and CCW implementations.
--- drivers/video/fbdev/core/bitblit.c | 9 ++++++--- drivers/video/fbdev/core/fbcon_ccw.c | 10 ++++++---- drivers/video/fbdev/core/fbcon_cw.c | 10 ++++++---- drivers/video/fbdev/core/fbcon_ud.c | 10 ++++++---- 4 files changed, 24 insertions(+), 15 deletions(-)
drivers/video/fbdev/core/bitblit.c +6 -3
@@ -268,7 +268,10 @@ static void bit_cursor(struct vc_data *vc, struct fb_info *info, bool enable,struct fb_cursor cursor;struct fbcon_par *par = info->fbcon_par;unsigned short charmask = vc->vc_hi_font_mask ?0x1ff :0xff ; -int w =DIV_ROUND_UP (vc->vc_font.width,8 ), c; +int width = vc->vc_font.width; +int vpitch = vc->vc_font.height; +int cellsize =font_glyph_size (width, vpitch); +int c;int y =real_y (par->p, vc->state.y);int attribute, use_sw = vc->vc_cursor_type &CUR_SW ;int err =1 ;
@@ -286,7 +289,7 @@ static void bit_cursor(struct vc_data *vc, struct fb_info *info, bool enable,/* Clamp to font size, same as bit_putcs_aligned() */ if (c >= vc->vc_font.charcount) c =0 ; - src = vc->vc_font.data + (c * (w * vc->vc_font.height)); + src = vc->vc_font.data + (c * cellsize);if (par->cursor_state.image.data != (const char *)src || par->cursor_reset) {
@@ -297,7 +300,7 @@ static void bit_cursor(struct vc_data *vc, struct fb_info *info, bool enable,if (attribute) {unsigned char *dst; - dst =kmalloc_array (w, vc->vc_font.height,GFP_ATOMIC ); + dst =kmalloc (cellsize,GFP_ATOMIC );if (!dst)return ;kfree (par->cursor_data);
drivers/video/fbdev/core/fbcon_ccw.c +6 -4
@@ -226,7 +226,9 @@ static void ccw_cursor(struct vc_data *vc, struct fb_info *info, bool enable,struct fb_cursor cursor;struct fbcon_par *par = info->fbcon_par;unsigned short charmask = vc->vc_hi_font_mask ?0x1ff :0xff ; -int w =font_glyph_pitch (vc->vc_font.height); +int width = vc->vc_font.height; +int vpitch = vc->vc_font.width; +int cellsize =font_glyph_size (width, vpitch);int c;int y =real_y (par->p, vc->state.y);int attribute, use_sw = vc->vc_cursor_type &CUR_SW ;
@@ -241,7 +243,7 @@ static void ccw_cursor(struct vc_data *vc, struct fb_info *info, bool enable, c =scr_readw ((u16 *) vc->vc_pos); attribute =get_attribute (info, c); - src = par->rotated.buf + ((c & charmask) * (w * vc->vc_font.width)); + src = par->rotated.buf + ((c & charmask) * cellsize);if (par->cursor_state.image.data != (const char *)src || par->cursor_reset) {
@@ -252,7 +254,7 @@ static void ccw_cursor(struct vc_data *vc, struct fb_info *info, bool enable,if (attribute) {unsigned char *dst; - dst =kmalloc_array (w, vc->vc_font.width,GFP_ATOMIC ); + dst =kmalloc (cellsize,GFP_ATOMIC );if (!dst)return ;kfree (par->cursor_data);
@@ -304,7 +306,7 @@ static void ccw_cursor(struct vc_data *vc, struct fb_info *info, bool enable,if (!tmp)return ; - mask =kmalloc_array (vc->vc_font.width, w,GFP_ATOMIC ); + mask =kmalloc (cellsize,GFP_ATOMIC );if (!mask) {kfree (tmp);return ;
drivers/video/fbdev/core/fbcon_cw.c +6 -4
@@ -209,7 +209,9 @@ static void cw_cursor(struct vc_data *vc, struct fb_info *info, bool enable,struct fb_cursor cursor;struct fbcon_par *par = info->fbcon_par;unsigned short charmask = vc->vc_hi_font_mask ?0x1ff :0xff ; -int w =font_glyph_pitch (vc->vc_font.height); +int width = vc->vc_font.height; +int vpitch = vc->vc_font.width; +int cellsize =font_glyph_size (width, vpitch);int c;int y =real_y (par->p, vc->state.y);int attribute, use_sw = vc->vc_cursor_type &CUR_SW ;
@@ -224,7 +226,7 @@ static void cw_cursor(struct vc_data *vc, struct fb_info *info, bool enable, c =scr_readw ((u16 *) vc->vc_pos); attribute =get_attribute (info, c); - src = par->rotated.buf + ((c & charmask) * (w * vc->vc_font.width)); + src = par->rotated.buf + ((c & charmask) * (cellsize));if (par->cursor_state.image.data != (const char *)src || par->cursor_reset) {
@@ -235,7 +237,7 @@ static void cw_cursor(struct vc_data *vc, struct fb_info *info, bool enable,if (attribute) {unsigned char *dst; - dst =kmalloc_array (w, vc->vc_font.width,GFP_ATOMIC ); + dst =kmalloc (cellsize,GFP_ATOMIC );if (!dst)return ;kfree (par->cursor_data);
@@ -287,7 +289,7 @@ static void cw_cursor(struct vc_data *vc, struct fb_info *info, bool enable,if (!tmp)return ; - mask =kmalloc_array (vc->vc_font.width, w,GFP_ATOMIC ); + mask =kmalloc (cellsize,GFP_ATOMIC );if (!mask) {kfree (tmp);return ;
drivers/video/fbdev/core/fbcon_ud.c +6 -4
@@ -255,7 +255,9 @@ static void ud_cursor(struct vc_data *vc, struct fb_info *info, bool enable,struct fb_cursor cursor;struct fbcon_par *par = info->fbcon_par;unsigned short charmask = vc->vc_hi_font_mask ?0x1ff :0xff ; -int w =font_glyph_pitch (vc->vc_font.width); +int width = vc->vc_font.width; +int vpitch = vc->vc_font.height; +int cellsize =font_glyph_size (width, vpitch);int c;int y =real_y (par->p, vc->state.y);int attribute, use_sw = vc->vc_cursor_type &CUR_SW ;
@@ -271,7 +273,7 @@ static void ud_cursor(struct vc_data *vc, struct fb_info *info, bool enable, c =scr_readw ((u16 *) vc->vc_pos); attribute =get_attribute (info, c); - src = par->rotated.buf + ((c & charmask) * (w * vc->vc_font.height)); + src = par->rotated.buf + ((c & charmask) * cellsize);if (par->cursor_state.image.data != (const char *)src || par->cursor_reset) {
@@ -282,7 +284,7 @@ static void ud_cursor(struct vc_data *vc, struct fb_info *info, bool enable,if (attribute) {unsigned char *dst; - dst =kmalloc_array (w, vc->vc_font.height,GFP_ATOMIC ); + dst =kmalloc (cellsize,GFP_ATOMIC );if (!dst)return ;kfree (par->cursor_data);
@@ -334,7 +336,7 @@ static void ud_cursor(struct vc_data *vc, struct fb_info *info, bool enable,if (!tmp)return ; - mask =kmalloc_array (vc->vc_font.height, w,GFP_ATOMIC ); + mask =kmalloc (cellsize,GFP_ATOMIC );if (!mask) {kfree (tmp);return ;
Signature · 1 line
2.56.0
Thomas Zimmermann
maintainerPATCH v2 5/6 fbcon: Look up glyphs using the font interface
In reply to Thomas ZimmermannReplace the open-coded glyph lookup with font_data_glyph_buf() in implementations of putcs and cursor bitops. As rotated glyphs are now stored as font_data_t, the regular lookup function for glyphs works.
Also replace the open-coded size calculations with font-data helpers.
Note that with screen rotation enabled, width and height swap places for CW and CCW implementations.
--- drivers/video/fbdev/core/bitblit.c | 23 ++++++++++++++--------- drivers/video/fbdev/core/fbcon_ccw.c | 14 ++++++++------ drivers/video/fbdev/core/fbcon_cw.c | 14 ++++++++------ drivers/video/fbdev/core/fbcon_ud.c | 23 ++++++++++++++--------- 4 files changed, 44 insertions(+), 30 deletions(-)
drivers/video/fbdev/core/bitblit.c +14 -9
@@ -76,13 +76,15 @@ static void bit_clear(struct vc_data *vc, struct fb_info *info, int sy,static inline void bit_putcs_aligned (struct vc_data *vc,struct fb_info *info,const u16 *s,u32 attr,u32 cnt, -u32 d_pitch,u32 s_pitch,u32 cellsize, +u32 d_pitch,u32 s_width,u32 s_vpitch,struct fb_image *image,unsigned char *buf,u8 *dst) {u16 charmask = vc->vc_hi_font_mask ?0x1ff :0xff ;unsigned int charcnt = vc->vc_font.charcount;u32 idx = vc->vc_font.width >>3 ; +struct fbcon_par *par = info->fbcon_par; +u32 s_pitch =font_glyph_pitch (s_width);const unsigned char *src;while (cnt--) {
@@ -90,7 +92,7 @@ static inline void bit_putcs_aligned(struct vc_data *vc, struct fb_info *info,if (ch >= charcnt) ch =0 ; - src = vc->vc_font.data + (unsigned int )ch * cellsize; + src =font_data_glyph_buf (par->p->fontdata, s_width, s_vpitch, ch);if (attr) {update_attr (buf, src, attr, vc);
@@ -113,7 +115,7 @@ static inline void bit_putcs_aligned(struct vc_data *vc, struct fb_info *info,static inline void bit_putcs_unaligned (struct vc_data *vc,struct fb_info *info,const u16 *s,u32 attr,u32 cnt,u32 d_pitch, -u32 s_pitch,u32 cellsize, +u32 s_width,u32 s_vpitch,struct fb_image *image,unsigned char *buf,u8 *dst) {
@@ -122,6 +124,8 @@ static inline void bit_putcs_unaligned(struct vc_data *vc,u32 shift_low =0 , mod = vc->vc_font.width %8 ;u32 shift_high =8 ;u32 idx = vc->vc_font.width >>3 ; +struct fbcon_par *par = info->fbcon_par; +u32 s_pitch =font_glyph_pitch (s_width);const unsigned char *src;while (cnt--) {
@@ -129,7 +133,7 @@ static inline void bit_putcs_unaligned(struct vc_data *vc,if (ch >= charcnt) ch =0 ; - src = vc->vc_font.data + (unsigned int )ch * cellsize; + src =font_data_glyph_buf (par->p->fontdata, s_width, s_vpitch, ch);if (attr) {update_attr (buf, src, attr, vc);
@@ -156,8 +160,9 @@ static void bit_putcs(struct vc_data *vc, struct fb_info *info,struct fbcon_par *par = info->fbcon_par;struct fb_image image;u32 vyres =GETVYRES (par->p, info); -u32 width =DIV_ROUND_UP (vc->vc_font.width,8 ); -u32 cellsize = width * vc->vc_font.height; +u32 width = vc->vc_font.width; +u32 vpitch = vc->vc_font.height; +u32 cellsize =font_glyph_size (width, vpitch);u32 maxcnt = info->pixmap.size/cellsize;u32 scan_align = info->pixmap.scan_align -1 ;u32 buf_align = info->pixmap.buf_align -1 ;
@@ -212,10 +217,10 @@ static void bit_putcs(struct vc_data *vc, struct fb_info *info,if (!mod)bit_putcs_aligned (vc, info, s, attribute, cnt, pitch, - width, cellsize, &image, buf, dst); + width, vpitch, &image, buf, dst);else bit_putcs_unaligned (vc, info, s, attribute, cnt, - pitch, width, cellsize, &image, + pitch, width, vpitch, &image, buf, dst); image.dx += cnt * vc->vc_font.width;
@@ -289,7 +294,7 @@ static void bit_cursor(struct vc_data *vc, struct fb_info *info, bool enable,/* Clamp to font size, same as bit_putcs_aligned() */ if (c >= vc->vc_font.charcount) c =0 ; - src = vc->vc_font.data + (c * cellsize); + src =font_data_glyph_buf (par->p->fontdata, width, vpitch, c);if (par->cursor_state.image.data != (const char *)src || par->cursor_reset) {
drivers/video/fbdev/core/fbcon_ccw.c +8 -6
@@ -97,7 +97,7 @@ static void ccw_clear(struct vc_data *vc, struct fb_info *info, int sy,static inline void ccw_putcs_aligned (struct vc_data *vc,struct fb_info *info,const u16 *s,u32 attr,u32 cnt, -u32 d_pitch,u32 s_pitch,u32 cellsize, +u32 d_pitch,u32 s_width,u32 s_vpitch,struct fb_image *image,unsigned char *buf,u8 *dst) {
@@ -107,7 +107,8 @@ static inline void ccw_putcs_aligned(struct vc_data *vc, struct fb_info *info,const unsigned char *src;while (cnt--) { - src = par->rotated.buf + (scr_readw (s--) & charmask) * cellsize; + src =font_data_glyph_buf (par->rotated.buf, s_width, s_vpitch, +scr_readw (s--) & charmask);if (attr) {ccw_update_attr (buf, src, attr, vc);
@@ -133,8 +134,9 @@ static void ccw_putcs(struct vc_data *vc, struct fb_info *info, {struct fb_image image;struct fbcon_par *par = info->fbcon_par; -u32 width =font_glyph_pitch (vc->vc_font.height); -u32 cellsize = width * vc->vc_font.width; +u32 width = vc->vc_font.height; +u32 vpitch = vc->vc_font.width; +u32 cellsize =font_glyph_size (width, vpitch);u32 maxcnt = info->pixmap.size/cellsize;u32 scan_align = info->pixmap.scan_align -1 ;u32 buf_align = info->pixmap.buf_align -1 ;
@@ -176,7 +178,7 @@ static void ccw_putcs(struct vc_data *vc, struct fb_info *info, dst =fb_get_buffer_offset (info, &info->pixmap, size); image.data = dst;ccw_putcs_aligned (vc, info, s, attribute, cnt, pitch, - width, cellsize, &image, buf, dst); + width, vpitch, &image, buf, dst); image.dy += image.height; count -= cnt; s -= cnt;
@@ -243,7 +245,7 @@ static void ccw_cursor(struct vc_data *vc, struct fb_info *info, bool enable, c =scr_readw ((u16 *) vc->vc_pos); attribute =get_attribute (info, c); - src = par->rotated.buf + ((c & charmask) * cellsize); + src =font_data_glyph_buf (par->rotated.buf, width, vpitch, c & charmask);if (par->cursor_state.image.data != (const char *)src || par->cursor_reset) {
drivers/video/fbdev/core/fbcon_cw.c +8 -6
@@ -82,7 +82,7 @@ static void cw_clear(struct vc_data *vc, struct fb_info *info, int sy,static inline void cw_putcs_aligned (struct vc_data *vc,struct fb_info *info,const u16 *s,u32 attr,u32 cnt, -u32 d_pitch,u32 s_pitch,u32 cellsize, +u32 d_pitch,u32 s_width,u32 s_vpitch,struct fb_image *image,unsigned char *buf,u8 *dst) {
@@ -92,7 +92,8 @@ static inline void cw_putcs_aligned(struct vc_data *vc, struct fb_info *info,const unsigned char *src;while (cnt--) { - src = par->rotated.buf + (scr_readw (s++) & charmask) * cellsize; + src =font_data_glyph_buf (par->rotated.buf, s_width, s_vpitch, +scr_readw (s++) & charmask);if (attr) {cw_update_attr (buf, src, attr, vc);
@@ -118,8 +119,9 @@ static void cw_putcs(struct vc_data *vc, struct fb_info *info, {struct fb_image image;struct fbcon_par *par = info->fbcon_par; -u32 width =font_glyph_pitch (vc->vc_font.height); -u32 cellsize = width * vc->vc_font.width; +u32 width = vc->vc_font.height; +u32 vpitch = vc->vc_font.width; +u32 cellsize =font_glyph_size (width, vpitch);u32 maxcnt = info->pixmap.size/cellsize;u32 scan_align = info->pixmap.scan_align -1 ;u32 buf_align = info->pixmap.buf_align -1 ;
@@ -159,7 +161,7 @@ static void cw_putcs(struct vc_data *vc, struct fb_info *info, dst =fb_get_buffer_offset (info, &info->pixmap, size); image.data = dst;cw_putcs_aligned (vc, info, s, attribute, cnt, pitch, - width, cellsize, &image, buf, dst); + width, vpitch, &image, buf, dst); image.dy += image.height; count -= cnt; s += cnt;
@@ -226,7 +228,7 @@ static void cw_cursor(struct vc_data *vc, struct fb_info *info, bool enable, c =scr_readw ((u16 *) vc->vc_pos); attribute =get_attribute (info, c); - src = par->rotated.buf + ((c & charmask) * (cellsize)); + src =font_data_glyph_buf (par->rotated.buf, width, vpitch, c & charmask);if (par->cursor_state.image.data != (const char *)src || par->cursor_reset) {
drivers/video/fbdev/core/fbcon_ud.c +14 -9
@@ -83,17 +83,19 @@ static void ud_clear(struct vc_data *vc, struct fb_info *info, int sy,static inline void ud_putcs_aligned (struct vc_data *vc,struct fb_info *info,const u16 *s,u32 attr,u32 cnt, -u32 d_pitch,u32 s_pitch,u32 cellsize, +u32 d_pitch,u32 s_width,u32 s_vpitch,struct fb_image *image,unsigned char *buf,u8 *dst) {struct fbcon_par *par = info->fbcon_par;u16 charmask = vc->vc_hi_font_mask ?0x1ff :0xff ;u32 idx = vc->vc_font.width >>3 ; +u32 s_pitch =font_glyph_pitch (s_width);const unsigned char *src;while (cnt--) { - src = par->rotated.buf + (scr_readw (s--) & charmask) * cellsize; + src =font_data_glyph_buf (par->rotated.buf, s_width, s_vpitch, +scr_readw (s--) & charmask);if (attr) {ud_update_attr (buf, src, attr, vc);
@@ -116,7 +118,7 @@ static inline void ud_putcs_aligned(struct vc_data *vc, struct fb_info *info,static inline void ud_putcs_unaligned (struct vc_data *vc,struct fb_info *info,const u16 *s,u32 attr,u32 cnt,u32 d_pitch, -u32 s_pitch,u32 cellsize, +u32 s_width,u32 s_vpitch,struct fb_image *image,unsigned char *buf,u8 *dst) {
@@ -125,10 +127,12 @@ static inline void ud_putcs_unaligned(struct vc_data *vc,u32 shift_low =0 , mod = vc->vc_font.width %8 ;u32 shift_high =8 ;u32 idx = vc->vc_font.width >>3 ; +u32 s_pitch =font_glyph_pitch (s_width);const unsigned char *src;while (cnt--) { - src = par->rotated.buf + (scr_readw (s--) & charmask) * cellsize; + src =font_data_glyph_buf (par->rotated.buf, s_width, s_vpitch, +scr_readw (s--) & charmask);if (attr) {ud_update_attr (buf, src, attr, vc);
@@ -154,8 +158,9 @@ static void ud_putcs(struct vc_data *vc, struct fb_info *info, {struct fb_image image;struct fbcon_par *par = info->fbcon_par; -u32 width =font_glyph_pitch (vc->vc_font.width); -u32 cellsize = width * vc->vc_font.height; +u32 width = vc->vc_font.width; +u32 vpitch = vc->vc_font.height; +u32 cellsize =font_glyph_size (width, vpitch);u32 maxcnt = info->pixmap.size/cellsize;u32 scan_align = info->pixmap.scan_align -1 ;u32 buf_align = info->pixmap.buf_align -1 ;
@@ -200,10 +205,10 @@ static void ud_putcs(struct vc_data *vc, struct fb_info *info,if (!mod)ud_putcs_aligned (vc, info, s, attribute, cnt, pitch, - width, cellsize, &image, buf, dst); + width, vpitch, &image, buf, dst);else ud_putcs_unaligned (vc, info, s, attribute, cnt, pitch, - width, cellsize, &image, + width, vpitch, &image, buf, dst); image.dx += image.width;
@@ -273,7 +278,7 @@ static void ud_cursor(struct vc_data *vc, struct fb_info *info, bool enable, c =scr_readw ((u16 *) vc->vc_pos); attribute =get_attribute (info, c); - src = par->rotated.buf + ((c & charmask) * cellsize); + src =font_data_glyph_buf (par->rotated.buf, width, vpitch, c & charmask);if (par->cursor_state.image.data != (const char *)src || par->cursor_reset) {
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v2 5/6] fbcon: Look up glyphs using the font interface
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] fbcon: mismatched par->p->fontdata lookup and NULL deref in bitblit --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] fbcon: mismatched par->p->fontdata lookup and NULL deref in bitblit
--
commit 715441bc0ed4d52171baff9d0046bf7f7969ee89
Author: Thomas Zimmermann <[email protected]>
fbcon: Look up glyphs using the font interface
This commit replaces open-coded glyph lookups and cell-size calculations in the fbcon bitblit and rotation implementations with font_data_glyph_buf() and font_glyph_size().
Quoted 57 lines
drivers/video/fbdev/core/bitblit.c +8
-4@@ -76,13 +76,15 @@ static void bit_clear(struct vc_data *vc, struct fb_info *info, int sy,static inline void bit_putcs_aligned (struct vc_data *vc,struct fb_info *info,const u16 *s,u32 attr,u32 cnt, -u32 d_pitch,u32 s_pitch,u32 cellsize, +u32 d_pitch,u32 s_width,u32 s_vpitch,struct fb_image *image,unsigned char *buf,u8 *dst) {u16 charmask = vc->vc_hi_font_mask ?0x1ff :0xff ;unsigned int charcnt = vc->vc_font.charcount;u32 idx = vc->vc_font.width >>3 ; +struct fbcon_par *par = info->fbcon_par; +u32 s_pitch =font_glyph_pitch (s_width);const unsigned char *src;while (cnt--) {@@ -90,7 +92,7 @@ static inline void bit_putcs_aligned(struct vc_data *vc, struct fb_info *info,if (ch >= charcnt) ch =0 ; - src = vc->vc_font.data + (unsigned int )ch * cellsize; + src =font_data_glyph_buf (par->p->fontdata, s_width, s_vpitch, ch);if (attr) {update_attr (buf, src, attr, vc);@@ -113,7 +115,7 @@ static inline void bit_putcs_aligned(struct vc_data *vc, struct fb_info *info,static inline void bit_putcs_unaligned (struct vc_data *vc,struct fb_info *info,const u16 *s,u32 attr,u32 cnt,u32 d_pitch, -u32 s_pitch,u32 cellsize, +u32 s_width,u32 s_vpitch,struct fb_image *image,unsigned char *buf,u8 *dst) {@@ -122,6 +124,8 @@ static inline void bit_putcs_unaligned(struct vc_data *vc,u32 shift_low =0 , mod = vc->vc_font.width %8 ;u32 shift_high =8 ;u32 idx = vc->vc_font.width >>3 ; +struct fbcon_par *par = info->fbcon_par; +u32 s_pitch =font_glyph_pitch (s_width);const unsigned char *src;while (cnt--) {@@ -129,7 +133,7 @@ static inline void bit_putcs_unaligned(struct vc_data *vc,if (ch >= charcnt) ch =0 ; - src = vc->vc_font.data + (unsigned int )ch * cellsize; + src =font_data_glyph_buf (par->p->fontdata, s_width, s_vpitch, ch);if (attr) {update_attr (buf, src, attr, vc);
[ ... ]
drivers/video/fbdev/core/bitblit.c static void bit_cursor(struct vc_data *vc, struct fb_info *info, bool enable, · line 300
Quoted 9 lines
drivers/video/fbdev/core/bitblit.c +1
-1@@ -289,7 +294,7 @@ static void bit_cursor(struct vc_data *vc, struct fb_info *info, bool enable,/* Clamp to font size, same as bit_putcs_aligned() */ if (c >= vc->vc_font.charcount) c =0 ; - src = vc->vc_font.data + (c * cellsize); + src =font_data_glyph_buf (par->p->fontdata, width, vpitch, c);if (par->cursor_state.image.data != (const char *)src || par->cursor_reset) {
[Severity: High]
Can looking up glyphs in par->p->fontdata instead of vc->vc_font.data cause a NULL pointer dereference in bit_putcs_aligned(), bit_putcs_unaligned(), and bit_cursor()?
par is shared across all virtual consoles mapped to info, so par->p does not always point to &fb_display[vc->vc_num]. For example, con_font_set() calls hide_cursor(vc) -> fbcon_cursor(vc, false) -> bit_cursor() on a background console while par->p still points to &fb_display[fg_console], and set_con2fb_map() sets par->p = &fb_display[unit] for a background console before con2fb_init_display() calls update_screen() on fg_console.
When par->p->fontdata belongs to a console with a smaller font buffer than vc (such as an 8x16 font on par->p vs a 16x32 font on vc), any character index c >= font_data_num_glyphs(par->p->fontdata, width, vpitch) still passes the c >= vc->vc_font.charcount clamp, causing font_data_glyph_buf() to return NULL. That unchecked NULL src is then dereferenced in soft_cursor(), update_attr(), or the fb_pad_*_buffer() helpers; and if the console referenced by par->p was deallocated via VT_DISALLOCATE so that par->p->fontdata is NULL, font_data_glyph_buf() itself dereferences NULL in FNTSIZE().
The follow-up commit "fbcon: Handle characters with no glyph defined in a consistent way" adds if (!src) checks after font_data_glyph_buf(), but does not fix the par->p->fontdata mismatch in bitblit.c and falls back to dereferencing par->rotated.buf (which is NULL in bitblit) when !src.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Thomas Zimmermann
maintainerPATCH v2 6/6 fbcon: Handle characters with no glyph defined in a consistent way
In reply to Thomas ZimmermannCheck the result of font_data_glyph_buf() for NULL when drawing the screen. Fall back to a default value of '\0' for undefined glpyhs. Skip the character if no fallback exists.
Error checking for fonts has been inconsistent or absent from fbcon. Most of the drawing code relies on earlier validation in the font-import code. Some code implements the glyph test by itself. At least all of fbcon should be consistent in how it handles undefined glyphs.
Calls to font_data_glyph_buf() returning NULL for undefined glyphs make it easy to handle this special case consistently across fbcon.
--- drivers/video/fbdev/core/bitblit.c | 27 +++++++++++++++++---------- drivers/video/fbdev/core/fbcon_ccw.c | 12 +++++++++++- drivers/video/fbdev/core/fbcon_cw.c | 13 +++++++++++-- drivers/video/fbdev/core/fbcon_ud.c | 18 +++++++++++++++++- 4 files changed, 56 insertions(+), 14 deletions(-)
drivers/video/fbdev/core/bitblit.c +17 -10
@@ -81,7 +81,6 @@ static inline void bit_putcs_aligned(struct vc_data *vc, struct fb_info *info, unsigned char *buf, u8 *dst) { u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff; -unsigned int charcnt = vc->vc_font.charcount;u32 idx = vc->vc_font.width >>3 ;struct fbcon_par *par = info->fbcon_par;u32 s_pitch =font_glyph_pitch (s_width);
@@ -90,9 +89,12 @@ static inline void bit_putcs_aligned(struct vc_data *vc, struct fb_info *info,while (cnt--) {u16 ch =scr_readw (s++) & charmask; -if (ch >= charcnt) - ch =0 ; src =font_data_glyph_buf (par->p->fontdata, s_width, s_vpitch, ch); +if (!src) { + src =font_data_glyph_buf (par->rotated.buf, s_width, s_vpitch,'\0' ); +if (!src) + goto next_char; + }if (attr) {update_attr (buf, src, attr, vc);
@@ -105,7 +107,7 @@ static inline void bit_putcs_aligned(struct vc_data *vc, struct fb_info *info,else fb_pad_aligned_buffer (dst, d_pitch, src, idx, image->height); - +next_char: dst += s_pitch; }
@@ -120,7 +122,6 @@ static inline void bit_putcs_unaligned(struct vc_data *vc, unsigned char *buf, u8 *dst) { u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff; -unsigned int charcnt = vc->vc_font.charcount;u32 shift_low =0 , mod = vc->vc_font.width %8 ;u32 shift_high =8 ;u32 idx = vc->vc_font.width >>3 ;
@@ -131,9 +132,12 @@ static inline void bit_putcs_unaligned(struct vc_data *vc,while (cnt--) {u16 ch =scr_readw (s++) & charmask; -if (ch >= charcnt) - ch =0 ; src =font_data_glyph_buf (par->p->fontdata, s_width, s_vpitch, ch); +if (!src) { + src =font_data_glyph_buf (par->rotated.buf, s_width, s_vpitch,'\0' ); +if (!src) + goto next_char; + }if (attr) {update_attr (buf, src, attr, vc);
@@ -143,6 +147,7 @@ static inline void bit_putcs_unaligned(struct vc_data *vc,fb_pad_unaligned_buffer (dst, d_pitch, src, idx, image->height, shift_high, shift_low, mod); +next_char: shift_low += mod; dst += (shift_low >=8 ) ? s_pitch : s_pitch -1 ; shift_low &=7 ;
@@ -291,10 +296,12 @@ static void bit_cursor(struct vc_data *vc, struct fb_info *info, bool enable, attribute =get_attribute (info, c); c &= charmask; -/* Clamp to font size, same as bit_putcs_aligned() */ -if (c >= vc->vc_font.charcount) - c =0 ; src =font_data_glyph_buf (par->p->fontdata, width, vpitch, c); +if (!src) { + src =font_data_glyph_buf (par->p->fontdata, width, vpitch,'\0' ); +if (!src) +return ; + }if (par->cursor_state.image.data != (const char *)src || par->cursor_reset) {
drivers/video/fbdev/core/fbcon_ccw.c +11 -1
@@ -109,6 +109,11 @@ static inline void ccw_putcs_aligned(struct vc_data *vc, struct fb_info *info,while (cnt--) { src =font_data_glyph_buf (par->rotated.buf, s_width, s_vpitch,scr_readw (s--) & charmask); +if (!src) { + src =font_data_glyph_buf (par->rotated.buf, s_width, s_vpitch,'\0' ); +if (!src) + goto next_char; + }if (attr) {ccw_update_attr (buf, src, attr, vc);
@@ -121,7 +126,7 @@ static inline void ccw_putcs_aligned(struct vc_data *vc, struct fb_info *info,else fb_pad_aligned_buffer (dst, d_pitch, src, idx, vc->vc_font.width); - +next_char: dst += d_pitch * vc->vc_font.width; }
@@ -246,6 +251,11 @@ static void ccw_cursor(struct vc_data *vc, struct fb_info *info, bool enable, c =scr_readw ((u16 *) vc->vc_pos); attribute =get_attribute (info, c); src =font_data_glyph_buf (par->rotated.buf, width, vpitch, c & charmask); +if (!src) { + src =font_data_glyph_buf (par->rotated.buf, width, vpitch, c & charmask); +if (!src) +return ; + }if (par->cursor_state.image.data != (const char *)src || par->cursor_reset) {
drivers/video/fbdev/core/fbcon_cw.c +11 -2
@@ -94,7 +94,11 @@ static inline void cw_putcs_aligned(struct vc_data *vc, struct fb_info *info,while (cnt--) { src =font_data_glyph_buf (par->rotated.buf, s_width, s_vpitch,scr_readw (s++) & charmask); - +if (!src) { + src =font_data_glyph_buf (par->rotated.buf, s_width, s_vpitch,'\0' ); +if (!src) + goto next_char; + }if (attr) {cw_update_attr (buf, src, attr, vc); src = buf;
@@ -106,7 +110,7 @@ static inline void cw_putcs_aligned(struct vc_data *vc, struct fb_info *info,else fb_pad_aligned_buffer (dst, d_pitch, src, idx, vc->vc_font.width); - +next_char: dst += d_pitch * vc->vc_font.width; }
@@ -229,6 +233,11 @@ static void cw_cursor(struct vc_data *vc, struct fb_info *info, bool enable, c =scr_readw ((u16 *) vc->vc_pos); attribute =get_attribute (info, c); src =font_data_glyph_buf (par->rotated.buf, width, vpitch, c & charmask); +if (!src) { + src =font_data_glyph_buf (par->rotated.buf, width, vpitch,'\0' ); +if (!src) +return ; + }if (par->cursor_state.image.data != (const char *)src || par->cursor_reset) {
drivers/video/fbdev/core/fbcon_ud.c +17 -1
@@ -96,6 +96,11 @@ static inline void ud_putcs_aligned(struct vc_data *vc, struct fb_info *info,while (cnt--) { src =font_data_glyph_buf (par->rotated.buf, s_width, s_vpitch,scr_readw (s--) & charmask); +if (!src) { + src =font_data_glyph_buf (par->rotated.buf, s_width, s_vpitch,'\0' ); +if (!src) + goto next_char; + }if (attr) {ud_update_attr (buf, src, attr, vc);
@@ -108,7 +113,7 @@ static inline void ud_putcs_aligned(struct vc_data *vc, struct fb_info *info,else fb_pad_aligned_buffer (dst, d_pitch, src, idx, image->height); - +next_char: dst += s_pitch; }
@@ -133,6 +138,11 @@ static inline void ud_putcs_unaligned(struct vc_data *vc,while (cnt--) { src =font_data_glyph_buf (par->rotated.buf, s_width, s_vpitch,scr_readw (s--) & charmask); +if (!src) { + src =font_data_glyph_buf (par->rotated.buf, s_width, s_vpitch,'\0' ); +if (!src) + goto next_char; + }if (attr) {ud_update_attr (buf, src, attr, vc);
@@ -142,6 +152,7 @@ static inline void ud_putcs_unaligned(struct vc_data *vc,fb_pad_unaligned_buffer (dst, d_pitch, src, idx, image->height, shift_high, shift_low, mod); +next_char: shift_low += mod; dst += (shift_low >=8 ) ? s_pitch : s_pitch -1 ; shift_low &=7 ;
@@ -279,6 +290,11 @@ static void ud_cursor(struct vc_data *vc, struct fb_info *info, bool enable, c =scr_readw ((u16 *) vc->vc_pos); attribute =get_attribute (info, c); src =font_data_glyph_buf (par->rotated.buf, width, vpitch, c & charmask); +if (!src) { + src =font_data_glyph_buf (par->rotated.buf, width, vpitch,'\0' ); +if (!src) +return ; + }if (par->cursor_state.image.data != (const char *)src || par->cursor_reset) {
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v2 6/6] fbcon: Handle characters with no glyph defined in a consistent way
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] fbcon: wrong font buffer in bit_putcs_aligned() and bit_putcs_unaligned() - [Medium] fbcon: fallback…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] fbcon: wrong font buffer in bit_putcs_aligned() and bit_putcs_unaligned()
- [Medium] fbcon: fallback lookup in ccw_cursor() repeats c & charmask instead of NUL
--
commit 87130bf587571fed7c1353d5789cd5ab5b6c495b
Author: Thomas Zimmermann <[email protected]>
fbcon: Handle characters with no glyph defined in a consistent way
This commit checks the return value of font_data_glyph_buf() for NULL across fbcon drawing functions, falling back to the '\0' glyph or skipping the character when undefined.
Quoted 46 lines
drivers/video/fbdev/core/bitblit.c +6
-5@@ -81,7 +81,6 @@ static inline void bit_putcs_aligned(struct vc_data *vc, struct fb_info *info, unsigned char *buf, u8 *dst) { u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff; -unsigned int charcnt = vc->vc_font.charcount;u32 idx = vc->vc_font.width >>3 ;struct fbcon_par *par = info->fbcon_par;u32 s_pitch =font_glyph_pitch (s_width);@@ -90,9 +89,12 @@ static inline void bit_putcs_aligned(struct vc_data *vc, struct fb_info *info,while (cnt--) {u16 ch =scr_readw (s++) & charmask; -if (ch >= charcnt) - ch =0 ; src =font_data_glyph_buf (par->p->fontdata, s_width, s_vpitch, ch); +if (!src) { + src =font_data_glyph_buf (par->rotated.buf, s_width, s_vpitch,'\0' ); +if (!src) + goto next_char; + }if (attr) {update_attr (buf, src, attr, vc);@@ -105,7 +107,7 @@ static inline void bit_putcs_aligned(struct vc_data *vc, struct fb_info *info,else fb_pad_aligned_buffer (dst, d_pitch, src, idx, image->height); - +next_char: dst += s_pitch; }@@ -120,7 +122,6 @@ static inline void bit_putcs_unaligned(struct vc_data *vc, unsigned char *buf, u8 *dst) { u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff; -unsigned int charcnt = vc->vc_font.charcount;u32 shift_low =0 , mod = vc->vc_font.width %8 ;u32 shift_high =8 ;u32 idx = vc->vc_font.width >>3 ;@@ -131,9 +132,12 @@ static inline void bit_putcs_unaligned(struct vc_data *vc,while (cnt--) {
drivers/video/fbdev/core/bitblit.c static inline void bit_putcs_unaligned(struct vc_data *vc, · line 140
Quoted 10 lines
drivers/video/fbdev/core/bitblit.c +5
-2u16 ch =scr_readw (s++) & charmask; -if (ch >= charcnt) - ch =0 ; src =font_data_glyph_buf (par->p->fontdata, s_width, s_vpitch, ch); +if (!src) { + src =font_data_glyph_buf (par->rotated.buf, s_width, s_vpitch,'\0' ); +if (!src) + goto next_char; + }
[Severity: High]
Should the fallback lookups in bit_putcs_aligned() and bit_putcs_unaligned() pass par->p->fontdata instead of par->rotated.buf?
par->rotated is only defined when CONFIG_FRAMEBUFFER_CONSOLE_ROTATION is enabled, so builds without it fail to compile. When rotation support is compiled in, par->rotated.buf is NULL on an unrotated console, causing font_data_glyph_buf() -> font_data_num_glyphs() -> font_data_size() to dereference a NULL pointer whenever an out-of-range character index is drawn.
[ ... ]
Quoted 4 lines
drivers/video/fbdev/core/fbcon_ccw.c +0
-0
[ ... ]
drivers/video/fbdev/core/fbcon_ccw.c static void ccw_cursor(struct vc_data *vc, struct fb_info *info, bool enable, · line 258
Quoted 9 lines
drivers/video/fbdev/core/fbcon_ccw.c +5
-0@@ -246,6 +251,11 @@ static void ccw_cursor(struct vc_data *vc, struct fb_info *info, bool enable, c =scr_readw ((u16 *) vc->vc_pos); attribute =get_attribute (info, c); src =font_data_glyph_buf (par->rotated.buf, width, vpitch, c & charmask); +if (!src) { + src =font_data_glyph_buf (par->rotated.buf, width, vpitch, c & charmask); +if (!src) +return ; + }
[Severity: Medium]
Should the fallback call in ccw_cursor() look up '\0' instead of c & charmask again? Repeating c & charmask always returns NULL when the first lookup fails, so the cursor is skipped instead of falling back to the '\0' glyph.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6