Skip to contentSkip to conversation

lib/fonts: Avoid unncessary 64-bit math in font code

PATCH In review Helge Deller 4 participants 9 messages Jun 7 – Jun 9

Subsystems (3)
lore
Share & tools
lore

Helge Deller

author maintainer
#0 lore raw

PATCH 1/1 lib/fonts: Avoid unncessary 64-bit math in font code

The text display code used in the Risc PC kernel image decompression code uses arch/arm/boot/compressed/font.c, which includes lib/fonts/font_acorn_8x8.c, which further includes <linux/font.h>.

Since commit 97df8960240a ("lib/fonts: Provide helpers for calculating glyph pitch and size") <linux/font.h> contains inline functions that require __do_div64, which is not linked into the ARM kernel decompressor. This makes Risc PC zImages fail to build.

There is no need to use 64-bit division code here, so resolve this issue by using plain standard addition and shift maths.

Fixes 97df8960240a ("lib/fonts: Provide helpers for calculating glyph pitch and size")
---
 include/linux/font.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
include/linux/font.h +1 -2
index 6845f02d739a..67d32268989d 100644
@@ -11,7 +11,6 @@
 #ifndef _VIDEO_FONT_H
 #define _VIDEO_FONT_H
 
-#include <linux/math.h>
 #include <linux/types.h>
 
 struct console_font;
@@ -35,7 +34,7 @@ struct console_font;
  */
 static inline unsigned int font_glyph_pitch(unsigned int width)
 {
-	return DIV_ROUND_UP(width, 8);
+	return (width + 7) >> 3;
 }
 
 /**
Signature · 1 line
2.54.0
#1 lore raw
Quoted 11 lines · Helge Deller

The text display code used in the Risc PC kernel image decompression code uses arch/arm/boot/compressed/font.c, which includes lib/fonts/font_acorn_8x8.c, which further includes <linux/font.h>.

Since commit 97df8960240a ("lib/fonts: Provide helpers for calculating glyph pitch and size") <linux/font.h> contains inline functions that require __do_div64, which is not linked into the ARM kernel decompressor. This makes Risc PC zImages fail to build.

There is no need to use 64-bit division code here, so resolve this issue by using plain standard addition and shift maths.

Hi, Helge,

I can confirm that this patch fixes the issue. Thank you for your thorough investigation.

Ethan

#2 lore raw
In reply to Helge Deller

Hi

Am 07.06.26 um 23:02 schrieb Helge Deller:

Quoted 11 lines

The text display code used in the Risc PC kernel image decompression code uses arch/arm/boot/compressed/font.c, which includes lib/fonts/font_acorn_8x8.c, which further includes <linux/font.h>.

Since commit 97df8960240a ("lib/fonts: Provide helpers for calculating glyph pitch and size") <linux/font.h> contains inline functions that require __do_div64, which is not linked into the ARM kernel decompressor. This makes Risc PC zImages fail to build.

There is no need to use 64-bit division code here, so resolve this issue by using plain standard addition and shift maths.

Why is there a 64-bit division at all?

Quoted 21 lines
Fixes 97df8960240a ("lib/fonts: Provide helpers for calculating glyph pitch and size")
---
  include/linux/font.h | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
include/linux/font.h +0 -1
index 6845f02d739a..67d32268989d 100644
@@ -11,7 +11,6 @@
  #ifndef _VIDEO_FONT_H
  #define _VIDEO_FONT_H
 
-#include <linux/math.h>
  #include <linux/types.h>
 
  struct console_font;

include/linux/font.h struct console_font; · line 37

Quoted 5 lines
include/linux/font.h +1 -1
@@ -35,7 +34,7 @@ struct console_font;
   */
  static inline unsigned int font_glyph_pitch(unsigned int width)
  {
-	return DIV_ROUND_UP(width, 8);
+	return (width + 7) >> 3;

Ok by me, if that's what's necessary. But can we try to keep a documented macro for the division to make the code explain itself? Does it work with DIV_ROUND_UP_POW2() ?

Best regards
Thomas

Quoted 3 lines
include/linux/font.h +0 -0
  }
 
  /**
Signature · 6 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
GF: Jochen Jaser, Andrew McDonald, Werner Knoblich, (HRB 36809, AG Nürnberg)
author maintainer
#3 lore raw
Quoted 12 lines · Thomas Zimmermann

Hi

Am 07.06.26 um 23:02 schrieb Helge Deller:

Quoted 9 lines

The text display code used in the Risc PC kernel image decompression code uses arch/arm/boot/compressed/font.c, which includes lib/fonts/font_acorn_8x8.c, which further includes <linux/font.h>.

Since commit 97df8960240a ("lib/fonts: Provide helpers for calculating glyph pitch and size") <linux/font.h> contains inline functions that require __do_div64, which is not linked into the ARM kernel decompressor. This makes Risc PC zImages fail to build.

Quoted 4 lines
Quoted 2 lines

There is no need to use 64-bit division code here, so resolve this issue by using plain standard addition and shift maths.

Why is there a 64-bit division at all?

Not sure. Might be platform specific.
Maybe, because you add two integers and divide by an integer, that the compiler then chooses to use 64-bit integer division by 32-bit integer.

Quoted 18 lines
Quoted 18 lines
Fixes 97df8960240a ("lib/fonts: Provide helpers for calculating glyph pitch and size")

---
  include/linux/font.h | 3 +--

  1 file changed, 1 insertion(+), 2 deletions(-)
include/linux/font.h +0 -1
index 6845f02d739a..67d32268989d 100644
@@ -11,7 +11,6 @@
   #ifndef _VIDEO_FONT_H
   #define _VIDEO_FONT_H
-#include <linux/math.h>
   #include <linux/types.h>
   struct console_font;
@@ -35,7 +34,7 @@ struct console_font;
Quoted 8 lines

include/linux/font.h struct console_font; · line 37

Quoted 5 lines
include/linux/font.h +1 -1
    */
   static inline unsigned int font_glyph_pitch(unsigned int width)
   {
-    return DIV_ROUND_UP(width, 8);
+    return (width + 7) >> 3;

Ok by me, if that's what's necessary.
But can we try to keep a documented macro for the division to make the code explain itself?

I'd expect everyone who messes with this kind of low-level graphics and bitmaps to understand this math addition and bit shift, and as such I think it should be self-explained.

Quoted 1 line

Does it work with DIV_ROUND_UP_POW2() ?

IMHO that's even worse than DIV_ROUND_UP().

Heleg

#4 lore raw

Hi, Helge and Thomas,

Quoted 7 lines · Helge Deller
Quoted 1 line · Thomas Zimmermann

Why is there a 64-bit division at all?

Not sure. Might be platform specific.
Maybe, because you add two integers and divide by an integer, that the compiler then chooses to use 64-bit integer division by 32-bit integer.

Actually, I think the real issue is that arch/arm/boot/compressed/Makefile defines "static" to nothing when compiling its copy of lib/fonts/font_acorn_8x8.c (via font.c), so that the font array is available outside of the object file. I assume that this causes various unused static inline functions in the headers it includes (such as <linux/math.h>) to be included in the object file because they become normal inline functions.

Ethan

#5 lore raw

* Ethan Nelson-Moore <[email protected]>:

Quoted 11 lines

Hi, Helge and Thomas,

Quoted 7 lines · Helge Deller
Quoted 1 line · Thomas Zimmermann

Why is there a 64-bit division at all?

Not sure. Might be platform specific.
Maybe, because you add two integers and divide by an integer, that the compiler then chooses to use 64-bit integer division by 32-bit integer.

Quoted 7 lines

Actually, I think the real issue is that arch/arm/boot/compressed/Makefile defines "static" to nothing when compiling its copy of lib/fonts/font_acorn_8x8.c (via font.c), so that the font array is available outside of the object file. I assume that this causes various unused static inline functions in the headers it includes (such as <linux/math.h>) to be included in the object file because they become normal inline functions.

Does this patch fix the issue then?

arch/arm/boot/compressed/Makefile +1 -1
index a159120d1e42..e3f550d62857 100644
@@ -157,4 +157,4 @@ $(obj)/piggy_data: $(obj)/../Image FORCE
 
 $(obj)/piggy.o: $(obj)/piggy_data
 
-CFLAGS_font.o := -Dstatic=
+CFLAGS_font.o := -DBOOTLOADER
lib/fonts/font_acorn_8x8.c +4 -0
index 36c51016769d..3327aa6d161d 100644
@@ -5,7 +5,11 @@
 
 #define FONTDATAMAX 2048
 
+#ifndef BOOTLOADER
 static const struct font_data acorndata_8x8 = {
+#else
+const struct font_data acorndata_8x8 = {
+#endif
 { 0, 0, FONTDATAMAX, 0 }, {
 /* 00 */  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ^@ */
 /* 01 */  0x7e, 0x81, 0xa5, 0x81, 0xbd, 0x99, 0x81, 0x7e, /* ^A */
#6 lore raw

Hi, Helge,

Quoted 1 line · Helge Deller

Does this patch fix the issue then?

Yes, it does. I remember you suggested this solution a while ago. Thank you.

Ethan

maintainer Reviewed-by
#7 lore raw
In reply to Helge Deller

Hi,

thanks for the fix.

Am 08.06.26 um 22:26 schrieb Helge Deller:

Quoted 34 lines

* Ethan Nelson-Moore <[email protected]>:

Quoted 8 lines

Hi, Helge and Thomas,

Quoted 5 lines · Helge Deller
Quoted 1 line · Thomas Zimmermann

Why is there a 64-bit division at all?

Not sure. Might be platform specific.
Maybe, because you add two integers and divide by an integer, that the compiler then chooses to use 64-bit integer division by 32-bit integer.

Quoted 7 lines

Actually, I think the real issue is that arch/arm/boot/compressed/Makefile defines "static" to nothing when compiling its copy of lib/fonts/font_acorn_8x8.c (via font.c), so that the font array is available outside of the object file. I assume that this causes various unused static inline functions in the headers it includes (such as <linux/math.h>) to be included in the object file because they become normal inline functions.

Does this patch fix the issue then?

arch/arm/boot/compressed/Makefile +1 -1
index a159120d1e42..e3f550d62857 100644
@@ -157,4 +157,4 @@ $(obj)/piggy_data: $(obj)/../Image FORCE
 
  $(obj)/piggy.o: $(obj)/piggy_data
 
-CFLAGS_font.o := -Dstatic=
+CFLAGS_font.o := -DBOOTLOADER
lib/fonts/font_acorn_8x8.c +0 -0
index 36c51016769d..3327aa6d161d 100644
@@ -5,7 +5,11 @@
 

lib/fonts/font_acorn_8x8.c line 15

Quoted 10 lines
lib/fonts/font_acorn_8x8.c +4 -0
  #define FONTDATAMAX 2048
 
+#ifndef BOOTLOADER
  static const struct font_data acorndata_8x8 = {
+#else
+const struct font_data acorndata_8x8 = {
+#endif
  { 0, 0, FONTDATAMAX, 0 }, {
  /* 00 */  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ^@ */
  /* 01 */  0x7e, 0x81, 0xa5, 0x81, 0xbd, 0x99, 0x81, 0x7e, /* ^A */

I do like this better than the other patch.

Best regards
Thomas

Signature · 6 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
GF: Jochen Jaser, Andrew McDonald, Werner Knoblich, (HRB 36809, AG Nürnberg)
author maintainer
#8 lore raw
Quoted 39 lines · Thomas Zimmermann

Hi,

thanks for the fix.

Am 08.06.26 um 22:26 schrieb Helge Deller:

Quoted 34 lines

* Ethan Nelson-Moore <[email protected]>:

Quoted 8 lines

Hi, Helge and Thomas,

Quoted 5 lines · Helge Deller
Quoted 1 line · Thomas Zimmermann

Why is there a 64-bit division at all?

Not sure. Might be platform specific.
Maybe, because you add two integers and divide by an integer, that the compiler then chooses to use 64-bit integer division by 32-bit integer.

Quoted 7 lines

Actually, I think the real issue is that arch/arm/boot/compressed/Makefile defines "static" to nothing when compiling its copy of lib/fonts/font_acorn_8x8.c (via font.c), so that the font array is available outside of the object file. I assume that this causes various unused static inline functions in the headers it includes (such as <linux/math.h>) to be included in the object file because they become normal inline functions.

Does this patch fix the issue then?

arch/arm/boot/compressed/Makefile +0 -0
index a159120d1e42..e3f550d62857 100644
@@ -157,4 +157,4 @@ $(obj)/piggy_data: $(obj)/../Image FORCE

  $(obj)/piggy.o: $(obj)/piggy_data

arch/arm/boot/compressed/Makefile +1 -1
-CFLAGS_font.o := -Dstatic=
+CFLAGS_font.o := -DBOOTLOADER
lib/fonts/font_acorn_8x8.c +1 -0
index 36c51016769d..3327aa6d161d 100644
@@ -5,7 +5,11 @@
   #define FONTDATAMAX 2048
+#ifndef BOOTLOADER
   static const struct font_data acorndata_8x8 = {
Quoted 8 lines

lib/fonts/font_acorn_8x8.c line 15

Quoted 6 lines
lib/fonts/font_acorn_8x8.c +3 -0
+#else
+const struct font_data acorndata_8x8 = {
+#endif
   { 0, 0, FONTDATAMAX, 0 }, {
   /* 00 */  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ^@ */
   /* 01 */  0x7e, 0x81, 0xa5, 0x81, 0xbd, 0x99, 0x81, 0x7e, /* ^A */

Thanks for review!

Quoted 1 line

I do like this better than the other patch.

Yes, I'll drop the other patch and re-send this one properly.

Helge