Skip to contentSkip to conversation

lib/vsprintf: replace min_t/max_t with min/max

PATCH In review Thorsten Blum 4 participants 5 messages May 18 – Jun 3

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/vsprintf: replace min_t/max_t with min/max

Use the simpler min()/max() macros since the values are all compatible.

---
 lib/vsprintf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
lib/vsprintf.c +3 -3
index 9f359b31c8d1..57ae85680536 100644
@@ -1208,7 +1208,7 @@ char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
 	}
 
 	if (spec.field_width > 0)
-		len = min_t(int, spec.field_width, 64);
+		len = min(spec.field_width, 64);
 
 	for (i = 0; i < len; ++i) {
 		if (buf < end)
@@ -1233,7 +1233,7 @@ char *bitmap_string(char *buf, char *end, const unsigned long *bitmap,
 		    struct printf_spec spec, const char *fmt)
 {
 	const int CHUNKSZ = 32;
-	int nr_bits = max_t(int, spec.field_width, 0);
+	int nr_bits = max(spec.field_width, 0);
 	int i, chunksz;
 	bool first = true;
 
@@ -1276,7 +1276,7 @@ static noinline_for_stack
 char *bitmap_list_string(char *buf, char *end, const unsigned long *bitmap,
 			 struct printf_spec spec, const char *fmt)
 {
-	int nr_bits = max_t(int, spec.field_width, 0);
+	int nr_bits = max(spec.field_width, 0);
 	bool first = true;
 	int rbot, rtop;
 
maintainer
#1 lore raw

lib/vsprintf.c char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, · line 1211

Quoted 10 lines · Thorsten Blum

Use the simpler min()/max() macros since the values are all compatible.

lib/vsprintf.c +1 -1
@@ -1208,7 +1208,7 @@ char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
 	}
 
 	if (spec.field_width > 0)
-		len = min_t(int, spec.field_width, 64);
+		len = min(spec.field_width, 64);

Honestly, I do not see any big advantage in replacing the macros. In fact, the min()/max() macros are even more complex because they check compatibility of the compared types.

IMHO, this adds non-necessary churn into the git history without an obvious advantage.

Best Regards,
Petr

#2 lore raw
Quoted 2 lines · Petr Mladek
Quoted 1 line · Thorsten Blum

Use the simpler min()/max() macros since the values are all compatible.

...

Quoted 10 lines

lib/vsprintf.c char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, · line 1211

Quoted 3 lines
lib/vsprintf.c +1 -1
 	if (spec.field_width > 0)
-		len = min_t(int, spec.field_width, 64);
+		len = min(spec.field_width, 64);

Honestly, I do not see any big advantage in replacing the macros. In fact, the min()/max() macros are even more complex because they check compatibility of the compared types.

IMHO, this adds non-necessary churn into the git history without an obvious advantage.

The advantage I see is to give a good example to others to avoid min_t() whenever is possible. I am not insisting to include this patch, just share my 2 cents.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
In reply to Petr Mladek
Quoted 8 lines · On Thu, 21 May 2026 15:49:12 +0200 Petr Mladek <[email protected]> wrote:
Quoted 7 lines · Thorsten Blum

Use the simpler min()/max() macros since the values are all compatible.

lib/vsprintf.c +0 -0
@@ -1208,7 +1208,7 @@ char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
 	}
 
Quoted 7 lines

lib/vsprintf.c char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, · line 1211

Quoted 3 lines
lib/vsprintf.c +1 -1
 	if (spec.field_width > 0)
-		len = min_t(int, spec.field_width, 64);
+		len = min(spec.field_width, 64);  

Honestly, I do not see any big advantage in replacing the macros. In fact, the min()/max() macros are even more complex because they check compatibility of the compared types.

But min_t() is often worse that just letting the compiler promote a negative signed value to a very large unsigned one.

There are basically three common misuses:
1) Using the type you want the result to be.
2) Using the smaller type, including 'long' v 'u64' on 32bit.
3) Thinking min_t(type, a, b) is the best way to compare items of type 'type'.

All not helped by checkpatch suggesting that:

	min(foo, (type)bar)
is 'an opportunity for':
	min_t(type, foo, bar)
but the latter is:
	min((type)foo, (type)bar)

and you never need both casts.

There are also a few of the pointless/buggy:

	min_t(u8, foo, U8_MAX)
but you'd notice that the test:
	if ((u8)foo > U8_MAX)

doesn't do what was intended.

Ideally min_t() ought to be killed, but it is a big job. (The sort of thing Linus could because of his 'god' bit.)

-- David

Quoted 7 lines

IMHO, this adds non-necessary churn into the git history without an obvious advantage.

Best Regards,
Petr

maintainer
#4 lore raw
Quoted 36 lines · David Laight
Quoted 8 lines · On Thu, 21 May 2026 15:49:12 +0200 Petr Mladek <[email protected]> wrote:
Quoted 7 lines · Thorsten Blum

Use the simpler min()/max() macros since the values are all compatible.

lib/vsprintf.c +0 -0
@@ -1208,7 +1208,7 @@ char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
 	}
 
Quoted 7 lines

lib/vsprintf.c char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, · line 1211

Quoted 3 lines
lib/vsprintf.c +1 -1
 	if (spec.field_width > 0)
-		len = min_t(int, spec.field_width, 64);
+		len = min(spec.field_width, 64);  

Honestly, I do not see any big advantage in replacing the macros. In fact, the min()/max() macros are even more complex because they check compatibility of the compared types.

But min_t() is often worse that just letting the compiler promote a negative signed value to a very large unsigned one.

There are basically three common misuses:
1) Using the type you want the result to be.
2) Using the smaller type, including 'long' v 'u64' on 32bit.
3) Thinking min_t(type, a, b) is the best way to compare items of type 'type'.

All not helped by checkpatch suggesting that:

	min(foo, (type)bar)
is 'an opportunity for':
	min_t(type, foo, bar)
but the latter is:
	min((type)foo, (type)bar)

and you never need both casts.

Quoted 8 lines

There are also a few of the pointless/buggy:

	min_t(u8, foo, U8_MAX)
but you'd notice that the test:
	if ((u8)foo > U8_MAX)

doesn't do what was intended.

Ideally min_t() ought to be killed, but it is a big job. (The sort of thing Linus could because of his 'god' bit.)

Andy, David, thanks for feedback. Fair enough.
Thorsten, thanks for the patch.

I have just pushed the patch into printk/linux.git, branch for-7.2.

Best Regards,
Petr