[PATCH] mm/oom_kill: Remove unnecessary integer promotion in format string

Dipendra Khadka posted 1 patch 1 month, 1 week ago
mm/oom_kill.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH] mm/oom_kill: Remove unnecessary integer promotion in format string
Posted by Dipendra Khadka 1 month, 1 week ago
The 'h' length modifier in '%hd' is unnecessary as short integers are
promoted to int in variadic functions. Use '%d' instead.

Signed-off-by: Dipendra Khadka <kdipendra88@gmail.com>
---
 mm/oom_kill.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index 5eb11fbba704..94066316e3ec 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -458,7 +458,7 @@ static void dump_oom_victim(struct oom_control *oc, struct task_struct *victim)
 
 static void dump_header(struct oom_control *oc)
 {
-	pr_warn("%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, oom_score_adj=%hd\n",
+	pr_warn("%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, oom_score_adj=%d\n",
 		current->comm, oc->gfp_mask, &oc->gfp_mask, oc->order,
 			current->signal->oom_score_adj);
 	if (!IS_ENABLED(CONFIG_COMPACTION) && oc->order)
@@ -958,7 +958,7 @@ static void __oom_kill_process(struct task_struct *victim, const char *message)
 	 */
 	do_send_sig_info(SIGKILL, SEND_SIG_PRIV, victim, PIDTYPE_TGID);
 	mark_oom_victim(victim);
-	pr_err("%s: Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB, UID:%u pgtables:%lukB oom_score_adj:%hd\n",
+	pr_err("%s: Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB, UID:%u pgtables:%lukB oom_score_adj:%d\n",
 		message, task_pid_nr(victim), victim->comm, K(mm->total_vm),
 		K(get_mm_counter(mm, MM_ANONPAGES)),
 		K(get_mm_counter(mm, MM_FILEPAGES)),
-- 
2.43.0
Re: [PATCH] mm/oom_kill: Remove unnecessary integer promotion in format string
Posted by Michal Hocko 1 month, 1 week ago
On Sun 28-12-25 15:44:55, Dipendra Khadka wrote:
> The 'h' length modifier in '%hd' is unnecessary as short integers are
> promoted to int in variadic functions. Use '%d' instead.
> 
> Signed-off-by: Dipendra Khadka <kdipendra88@gmail.com>

Why should we change the currently correct code in the first place?
What is an added value?

-- 
Michal Hocko
SUSE Labs
Re: [PATCH] mm/oom_kill: Remove unnecessary integer promotion in format string
Posted by Andrew Morton 1 month, 1 week ago
On Mon, 29 Dec 2025 09:27:18 +0100 Michal Hocko <mhocko@suse.com> wrote:

> On Sun 28-12-25 15:44:55, Dipendra Khadka wrote:
> > The 'h' length modifier in '%hd' is unnecessary as short integers are
> > promoted to int in variadic functions. Use '%d' instead.
> > 
> > Signed-off-by: Dipendra Khadka <kdipendra88@gmail.com>
> 
> Why should we change the currently correct code in the first place?
> What is an added value?

It reduces vmlinux by 2 bytes ;)

It's a teeny issue but I do like the present code - be very explicit
and careful about the types we're dealing with, don't rely on unobvious
C rules.
Re: [PATCH] mm/oom_kill: Remove unnecessary integer promotion in format string
Posted by Dipendra Khadka 1 month, 1 week ago
>It's a teeny issue but I do like the present code - be very explicit
>and careful about the types we're dealing with, don't rely on unobvious
>C rules.

Actually, the scenario here is that while printing a short, it is 
**promoted** to int due to C's variadic argument promotion rules.

Yes, we must use %hd when the **semantic type** is short. However, 
due to integer promotion, the value is already passed as a 4-byte int 
on the stack. The %hd format specifier then casts it back to short 
for display.

Since the value is already promoted to int internally, using %d is 
simpler and avoids the unnecessary cast. The checkpatch warning 
confirms this:

```
WARNING: Integer promotion: Using 'h' in '%hd' is unnecessary
#461: FILE: mm/oom_kill.c:461:
+	pr_warn("%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, oom_score_adj=%hd\n",
+		current->comm, oc->gfp_mask, &oc->gfp_mask, oc->order,
+			current->signal->oom_score_adj);


WARNING: Integer promotion: Using 'h' in '%hd' is unnecessary
#961: FILE: mm/oom_kill.c:961:
+	pr_err("%s: Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB, UID:%u pgtables:%lukB oom_score_adj:%hd\n",
+		message, task_pid_nr(victim), victim->comm, K(mm->total_vm),
+		K(get_mm_counter(mm, MM_ANONPAGES)),
+		K(get_mm_counter(mm, MM_FILEPAGES)),
+		K(get_mm_counter(mm, MM_SHMEMPAGES)),
+		from_kuid(&init_user_ns, task_uid(victim)),
+		mm_pgtables_bytes(mm) >> 10, victim->signal->oom_score_adj);
```

Checkpatch flags the 'h' modifier as unnecessary for this reason, 
and many other subsystems have moved to using %d for promoted types. 
Hence, I think this patch aligns with kernel coding practices.

Best Rgards,
Dipendra Khadka
Re: [PATCH] mm/oom_kill: Remove unnecessary integer promotion in format string
Posted by Michal Hocko 1 month ago
On Wed 31-12-25 12:21:17, Dipendra Khadka wrote:
[...]
> Checkpatch flags the 'h' modifier as unnecessary for this reason, 
> and many other subsystems have moved to using %d for promoted types. 
> Hence, I think this patch aligns with kernel coding practices.

And here, this is the actual argument to push change like that. On its
own this is not really worth it. But if there is a general push to
remove %hd then sure change like this makes us closer to do that and
quite honestly I am not a great fan of %hd. I will not miss it.

Please update the changelog and then feel free to add
Acked-by: Michal Hocko <mhocko@suse.com>

Thanks!
-- 
Michal Hocko
SUSE Labs
Re: [PATCH] mm/oom_kill: Remove unnecessary integer promotion in format string
Posted by Andrew Morton 1 month, 1 week ago
On Wed, 31 Dec 2025 12:21:17 +0000 Dipendra Khadka <kdipendra88@gmail.com> wrote:

> Since the value is already promoted to int internally, using %d is 
> simpler and avoids the unnecessary cast. The checkpatch warning 
> confirms this:
> 
> ```
> WARNING: Integer promotion: Using 'h' in '%hd' is unnecessary
> #461: FILE: mm/oom_kill.c:461:
> +	pr_warn("%s invoked oom-killer: gfp_mask=%#x(%pGg), order=%d, oom_score_adj=%hd\n",
> +		current->comm, oc->gfp_mask, &oc->gfp_mask, oc->order,
> +			current->signal->oom_score_adj);
> 
> 
> WARNING: Integer promotion: Using 'h' in '%hd' is unnecessary
> #961: FILE: mm/oom_kill.c:961:
> +	pr_err("%s: Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB, UID:%u pgtables:%lukB oom_score_adj:%hd\n",
> +		message, task_pid_nr(victim), victim->comm, K(mm->total_vm),
> +		K(get_mm_counter(mm, MM_ANONPAGES)),
> +		K(get_mm_counter(mm, MM_FILEPAGES)),
> +		K(get_mm_counter(mm, MM_SHMEMPAGES)),
> +		from_kuid(&init_user_ns, task_uid(victim)),
> +		mm_pgtables_bytes(mm) >> 10, victim->signal->oom_score_adj);
> ```
> 
> Checkpatch flags the 'h' modifier as unnecessary for this reason, 
> and many other subsystems have moved to using %d for promoted types. 
> Hence, I think this patch aligns with kernel coding practices.

hm, OK, this code is pretty lonely.

hp2:/usr/src/linux-6.19-rc3> grep -r "%hd" . | wc -l
50

Consistency is good.
Re: [PATCH] mm/oom_kill: Remove unnecessary integer promotion in format string
Posted by David Rientjes 1 month, 1 week ago
On Sun, 28 Dec 2025, Dipendra Khadka wrote:

> The 'h' length modifier in '%hd' is unnecessary as short integers are
> promoted to int in variadic functions. Use '%d' instead.
> 
> Signed-off-by: Dipendra Khadka <kdipendra88@gmail.com>

I don't have a strong opinion given this will result in the same output.

Acked-by: David Rientjes <rientjes@google.com>