linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/oom_kill: Remove unnecessary integer promotion in format string
@ 2025-12-28 15:44 Dipendra Khadka
  2025-12-29  3:52 ` David Rientjes
  2025-12-29  8:27 ` Michal Hocko
  0 siblings, 2 replies; 7+ messages in thread
From: Dipendra Khadka @ 2025-12-28 15:44 UTC (permalink / raw)
  To: Michal Hocko, Andrew Morton
  Cc: David Rientjes, Shakeel Butt, linux-mm, linux-kernel, Dipendra Khadka

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



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] mm/oom_kill: Remove unnecessary integer promotion in format string
  2025-12-28 15:44 [PATCH] mm/oom_kill: Remove unnecessary integer promotion in format string Dipendra Khadka
@ 2025-12-29  3:52 ` David Rientjes
  2025-12-29  8:27 ` Michal Hocko
  1 sibling, 0 replies; 7+ messages in thread
From: David Rientjes @ 2025-12-29  3:52 UTC (permalink / raw)
  To: Dipendra Khadka
  Cc: Michal Hocko, Andrew Morton, Shakeel Butt, linux-mm, linux-kernel

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>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] mm/oom_kill: Remove unnecessary integer promotion in format string
  2025-12-28 15:44 [PATCH] mm/oom_kill: Remove unnecessary integer promotion in format string Dipendra Khadka
  2025-12-29  3:52 ` David Rientjes
@ 2025-12-29  8:27 ` Michal Hocko
  2025-12-30 20:33   ` Andrew Morton
  1 sibling, 1 reply; 7+ messages in thread
From: Michal Hocko @ 2025-12-29  8:27 UTC (permalink / raw)
  To: Dipendra Khadka
  Cc: Andrew Morton, David Rientjes, Shakeel Butt, linux-mm, linux-kernel

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] mm/oom_kill: Remove unnecessary integer promotion in format string
  2025-12-29  8:27 ` Michal Hocko
@ 2025-12-30 20:33   ` Andrew Morton
  2025-12-31 12:21     ` Dipendra Khadka
  0 siblings, 1 reply; 7+ messages in thread
From: Andrew Morton @ 2025-12-30 20:33 UTC (permalink / raw)
  To: Michal Hocko
  Cc: Dipendra Khadka, David Rientjes, Shakeel Butt, linux-mm, linux-kernel

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.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] mm/oom_kill: Remove unnecessary integer promotion in format string
  2025-12-30 20:33   ` Andrew Morton
@ 2025-12-31 12:21     ` Dipendra Khadka
  2026-01-01  1:45       ` Andrew Morton
  2026-01-05 12:22       ` Michal Hocko
  0 siblings, 2 replies; 7+ messages in thread
From: Dipendra Khadka @ 2025-12-31 12:21 UTC (permalink / raw)
  To: akpm; +Cc: kdipendra88, linux-kernel, linux-mm, mhocko, rientjes, shakeel.butt

>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



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] mm/oom_kill: Remove unnecessary integer promotion in format string
  2025-12-31 12:21     ` Dipendra Khadka
@ 2026-01-01  1:45       ` Andrew Morton
  2026-01-05 12:22       ` Michal Hocko
  1 sibling, 0 replies; 7+ messages in thread
From: Andrew Morton @ 2026-01-01  1:45 UTC (permalink / raw)
  To: Dipendra Khadka; +Cc: linux-kernel, linux-mm, mhocko, rientjes, shakeel.butt

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.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] mm/oom_kill: Remove unnecessary integer promotion in format string
  2025-12-31 12:21     ` Dipendra Khadka
  2026-01-01  1:45       ` Andrew Morton
@ 2026-01-05 12:22       ` Michal Hocko
  1 sibling, 0 replies; 7+ messages in thread
From: Michal Hocko @ 2026-01-05 12:22 UTC (permalink / raw)
  To: Dipendra Khadka; +Cc: akpm, linux-kernel, linux-mm, rientjes, shakeel.butt

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-01-05 12:23 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-12-28 15:44 [PATCH] mm/oom_kill: Remove unnecessary integer promotion in format string Dipendra Khadka
2025-12-29  3:52 ` David Rientjes
2025-12-29  8:27 ` Michal Hocko
2025-12-30 20:33   ` Andrew Morton
2025-12-31 12:21     ` Dipendra Khadka
2026-01-01  1:45       ` Andrew Morton
2026-01-05 12:22       ` Michal Hocko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox