Skip to contentSkip to conversation

mm: Update mark_victim tracepoints fields

PATCH Applied Carlos Galo 3 participants 7 messages Jan 11, 2024 – Feb 23, 2024

Subsystems (3)

v2 of 3 Applied 1/1 Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm: Update mark_victim tracepoints fields

The current implementation of the mark_victim tracepoint provides only the process ID (pid) of the victim process. This limitation poses challenges for userspace tools that need additional information about the OOM victim. The association between pid and the additional data may be lost after the kill, making it difficult for userspace to correlate the OOM event with the specific process.

In order to mitigate this limitation, add the following fields:

- UID
In Android each installed application has a unique UID. Including the uid assists in correlating OOM events with specific apps.

- Process Name (comm)
Enables identification of the affected process.

- OOM Score
Allows userspace to get additional insights of the relative kill priority of the OOM victim.

Cc Steven Rostedt <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Suren Baghdasaryan <[email protected]>

---
v2: Fixed build error. Added missing comma when printing __entry->uid.

 include/trace/events/oom.h | 19 +++++++++++++++----
 mm/oom_kill.c              |  6 +++++-
 2 files changed, 20 insertions(+), 5 deletions(-)
include/trace/events/oom.h +15 -4
index 26a11e4a2c36..fb8a5d1b8a0a 100644
@@ -72,19 +72,30 @@ TRACE_EVENT(reclaim_retry_zone,
 );
 
 TRACE_EVENT(mark_victim,
-	TP_PROTO(int pid),
+	TP_PROTO(struct task_struct *task, uid_t uid),
 
-	TP_ARGS(pid),
+	TP_ARGS(task, uid),
 
 	TP_STRUCT__entry(
 		__field(int, pid)
+		__field(uid_t, uid)
+		__string(comm, task->comm)
+		__field(short, oom_score_adj)
 	),
 
 	TP_fast_assign(
-		__entry->pid = pid;
+		__entry->pid = task->pid;
+		__entry->uid = uid;
+		__assign_str(comm, task->comm);
+		__entry->oom_score_adj = task->signal->oom_score_adj;
 	),
 
-	TP_printk("pid=%d", __entry->pid)
+	TP_printk("pid=%d uid=%u comm=%s oom_score_adj=%hd",
+		__entry->pid,
+		__entry->uid,
+		__get_str(comm),
+		__entry->oom_score_adj
+	)
 );
 
 TRACE_EVENT(wake_reaper,
mm/oom_kill.c +5 -1
index 9e6071fde34a..0698c00c5da6 100644
@@ -44,6 +44,7 @@
 #include <linux/kthread.h>
 #include <linux/init.h>
 #include <linux/mmu_notifier.h>
+#include <linux/cred.h>
 
 #include <asm/tlb.h>
 #include "internal.h"
@@ -753,6 +754,7 @@ static inline void queue_oom_reaper(struct task_struct *tsk)
  */
 static void mark_oom_victim(struct task_struct *tsk)
 {
+	const struct cred *cred;
 	struct mm_struct *mm = tsk->mm;
 
 	WARN_ON(oom_killer_disabled);
@@ -772,7 +774,9 @@ static void mark_oom_victim(struct task_struct *tsk)
 	 */
 	__thaw_task(tsk);
 	atomic_inc(&oom_victims);
-	trace_mark_victim(tsk->pid);
+	cred = get_task_cred(tsk);
+	trace_mark_victim(tsk, cred->uid.val);
+	put_cred(cred);
 }
 
 /**

base-commit: 0dd3ee31125508cd67f7e7172247f05b7fd1753a

Signature · 1 line
2.43.0.275.g3460e3d667-goog
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 13 lines · On Thu, 11 Jan 2024 21:05:30 +0000 Carlos Galo <[email protected]> wrote:

The current implementation of the mark_victim tracepoint provides only the process ID (pid) of the victim process. This limitation poses challenges for userspace tools that need additional information about the OOM victim. The association between pid and the additional data may be lost after the kill, making it difficult for userspace to correlate the OOM event with the specific process.

In order to mitigate this limitation, add the following fields:

- UID
In Android each installed application has a unique UID. Including the uid assists in correlating OOM events with specific apps.

Quoted 7 lines

- Process Name (comm)
Enables identification of the affected process.

- OOM Score
Allows userspace to get additional insights of the relative kill priority of the OOM victim.

From a tracing POV this looks fine to me, but it's up to the mm maintainers to decide to take it.

Quoted 71 lines
Cc Steven Rostedt <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Suren Baghdasaryan <[email protected]>

---
v2: Fixed build error. Added missing comma when printing __entry->uid.

 include/trace/events/oom.h | 19 +++++++++++++++----
 mm/oom_kill.c              |  6 +++++-
 2 files changed, 20 insertions(+), 5 deletions(-)
include/trace/events/oom.h +15 -4
index 26a11e4a2c36..fb8a5d1b8a0a 100644
@@ -72,19 +72,30 @@ TRACE_EVENT(reclaim_retry_zone,
 );
 
 TRACE_EVENT(mark_victim,
-	TP_PROTO(int pid),
+	TP_PROTO(struct task_struct *task, uid_t uid),
 
-	TP_ARGS(pid),
+	TP_ARGS(task, uid),
 
 	TP_STRUCT__entry(
 		__field(int, pid)
+		__field(uid_t, uid)
+		__string(comm, task->comm)
+		__field(short, oom_score_adj)
 	),
 
 	TP_fast_assign(
-		__entry->pid = pid;
+		__entry->pid = task->pid;
+		__entry->uid = uid;
+		__assign_str(comm, task->comm);
+		__entry->oom_score_adj = task->signal->oom_score_adj;
 	),
 
-	TP_printk("pid=%d", __entry->pid)
+	TP_printk("pid=%d uid=%u comm=%s oom_score_adj=%hd",
+		__entry->pid,
+		__entry->uid,
+		__get_str(comm),
+		__entry->oom_score_adj
+	)
 );
 
 TRACE_EVENT(wake_reaper,
mm/oom_kill.c +2 -0
index 9e6071fde34a..0698c00c5da6 100644
@@ -44,6 +44,7 @@
 #include <linux/kthread.h>
 #include <linux/init.h>
 #include <linux/mmu_notifier.h>
+#include <linux/cred.h>
 
 #include <asm/tlb.h>
 #include "internal.h"
@@ -753,6 +754,7 @@ static inline void queue_oom_reaper(struct task_struct *tsk)
  */
 static void mark_oom_victim(struct task_struct *tsk)
 {
+	const struct cred *cred;
 	struct mm_struct *mm = tsk->mm;
 
 	WARN_ON(oom_killer_disabled);

mm/oom_kill.c static void mark_oom_victim(struct task_struct *tsk) · line 777

Quoted 5 lines
mm/oom_kill.c +1 -1
@@ -772,7 +774,9 @@ static void mark_oom_victim(struct task_struct *tsk)
 	 */
 	__thaw_task(tsk);
 	atomic_inc(&oom_victims);
-	trace_mark_victim(tsk->pid);
+	cred = get_task_cred(tsk);

get_task_cred() isn't a trivial function and is used only for tracing. But this isn't a fast path so I guess that is fine.

For tracing:

-- Steve

Quoted 7 lines
mm/oom_kill.c +2 -0
+	trace_mark_victim(tsk, cred->uid.val);
+	put_cred(cred);
 }
 
 /**

base-commit: 0dd3ee31125508cd67f7e7172247f05b7fd1753a

maintainer
#2 lore raw
In reply to Carlos Galo

Hi,
sorry I have missed this before.

Quoted 6 lines · Carlos Galo

The current implementation of the mark_victim tracepoint provides only the process ID (pid) of the victim process. This limitation poses challenges for userspace tools that need additional information about the OOM victim. The association between pid and the additional data may be lost after the kill, making it difficult for userspace to correlate the OOM event with the specific process.

You are correct that post OOM all per-process information is lost. On the other hand we do dump all this information to the kernel log. Could you explain why that is not suitable for your purpose?

Quoted 6 lines

In order to mitigate this limitation, add the following fields:

- UID
In Android each installed application has a unique UID. Including the uid assists in correlating OOM events with specific apps.

Quoted 6 lines

- Process Name (comm)
Enables identification of the affected process.

- OOM Score
Allows userspace to get additional insights of the relative kill priority of the OOM victim.

What is the oom score useful for?

Is there any reason to provide a different information from the one
reported to the kernel log?
__oom_kill_process:
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);
Signature · 2 lines
Michal Hocko
SUSE Labs
#3 lore raw
Quoted 12 lines · Michal Hocko

Hi,
sorry I have missed this before.

Quoted 6 lines · Carlos Galo

The current implementation of the mark_victim tracepoint provides only the process ID (pid) of the victim process. This limitation poses challenges for userspace tools that need additional information about the OOM victim. The association between pid and the additional data may be lost after the kill, making it difficult for userspace to correlate the OOM event with the specific process.

Quoted 3 lines

You are correct that post OOM all per-process information is lost. On the other hand we do dump all this information to the kernel log. Could you explain why that is not suitable for your purpose?

Userspace tools often need real-time visibility into OOM situations for userspace intervention. Our use case involves utilizing BPF programs, along with BPF ring buffers, to provide OOM notification to userspace. Parsing kernel logs would be significant overhead as opposed to the event based BPF approach.

Quoted 6 lines
Quoted 6 lines

In order to mitigate this limitation, add the following fields:

- UID
In Android each installed application has a unique UID. Including the uid assists in correlating OOM events with specific apps.

Quoted 9 lines
Quoted 6 lines

- Process Name (comm)
Enables identification of the affected process.

- OOM Score
Allows userspace to get additional insights of the relative kill priority of the OOM victim.

What is the oom score useful for?

The OOM score provides us a measure of the victim's importance. On the android side, it allows us to identify if top or foreground apps are killed, which have user perceptible impact.

Quoted 11 lines

Is there any reason to provide a different information from the one
reported to the kernel log?
__oom_kill_process:
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);

We added these fields we need (UID, process name, and OOM score), but we're open to adding the others if you prefer that for consistency with the kernel log.

Thanks,
Carlos

Quoted 3 lines

--
Michal Hocko
SUSE Labs

maintainer
#4 lore raw
Quoted 17 lines · Carlos Galo
Quoted 12 lines · Michal Hocko

Hi,
sorry I have missed this before.

Quoted 6 lines · Carlos Galo

The current implementation of the mark_victim tracepoint provides only the process ID (pid) of the victim process. This limitation poses challenges for userspace tools that need additional information about the OOM victim. The association between pid and the additional data may be lost after the kill, making it difficult for userspace to correlate the OOM event with the specific process.

Quoted 3 lines

You are correct that post OOM all per-process information is lost. On the other hand we do dump all this information to the kernel log. Could you explain why that is not suitable for your purpose?

Quoted 5 lines

Userspace tools often need real-time visibility into OOM situations for userspace intervention. Our use case involves utilizing BPF programs, along with BPF ring buffers, to provide OOM notification to userspace. Parsing kernel logs would be significant overhead as opposed to the event based BPF approach.

Please put that into the changelog.

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines

In order to mitigate this limitation, add the following fields:

- UID
In Android each installed application has a unique UID. Including the uid assists in correlating OOM events with specific apps.

- Process Name (comm)
Enables identification of the affected process.

- OOM Score
Allows userspace to get additional insights of the relative kill priority of the OOM victim.

Quoted 5 lines
Quoted 2 lines

What is the oom score useful for?

The OOM score provides us a measure of the victim's importance. On the android side, it allows us to identify if top or foreground apps are killed, which have user perceptible impact.

But the value on its own (wihtout knowing scores of other tasks) doesn't really tell you anything, does it?

Quoted 12 lines
Quoted 11 lines

Is there any reason to provide a different information from the one
reported to the kernel log?
__oom_kill_process:
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);
Quoted 3 lines

We added these fields we need (UID, process name, and OOM score), but we're open to adding the others if you prefer that for consistency with the kernel log.

yes, I think the consistency would be better here. For one it reports numbers which can tell quite a lot about the killed victim. It is a superset of what you already asking for. With a notable exception of the oom_score which is really dubious without a wider context.

Signature · 2 lines
Michal Hocko
SUSE Labs
#5 lore raw
Quoted 19 lines · Michal Hocko
Quoted 17 lines · Carlos Galo
Quoted 15 lines · Michal Hocko

Hi,
sorry I have missed this before.

Quoted 6 lines · Carlos Galo

The current implementation of the mark_victim tracepoint provides only the process ID (pid) of the victim process. This limitation poses challenges for userspace tools that need additional information about the OOM victim. The association between pid and the additional data may be lost after the kill, making it difficult for userspace to correlate the OOM event with the specific process.

You are correct that post OOM all per-process information is lost. On the other hand we do dump all this information to the kernel log. Could you explain why that is not suitable for your purpose?

Quoted 7 lines
Quoted 5 lines

Userspace tools often need real-time visibility into OOM situations for userspace intervention. Our use case involves utilizing BPF programs, along with BPF ring buffers, to provide OOM notification to userspace. Parsing kernel logs would be significant overhead as opposed to the event based BPF approach.

Please put that into the changelog.

Will do.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines

In order to mitigate this limitation, add the following fields:

- UID
In Android each installed application has a unique UID. Including the uid assists in correlating OOM events with specific apps.

- Process Name (comm)
Enables identification of the affected process.

- OOM Score
Allows userspace to get additional insights of the relative kill priority of the OOM victim.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

What is the oom score useful for?

The OOM score provides us a measure of the victim's importance. On the android side, it allows us to identify if top or foreground apps are killed, which have user perceptible impact.

But the value on its own (wihtout knowing scores of other tasks) doesn't really tell you anything, does it?

Android uses the OOM adj_score ranges to categorize app state (foreground, background, ...). I'll resend a v3 with the commit text updated to include details about this.

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

Is there any reason to provide a different information from the one
reported to the kernel log?
__oom_kill_process:
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);
Quoted 8 lines
Quoted 3 lines

We added these fields we need (UID, process name, and OOM score), but we're open to adding the others if you prefer that for consistency with the kernel log.

yes, I think the consistency would be better here. For one it reports numbers which can tell quite a lot about the killed victim. It is a superset of what you already asking for. With a notable exception of the oom_score which is really dubious without a wider context.

Sounds good, I'll resend a v3 that includes these fields as well.

Thanks,
Carlos

Quoted 3 lines

--
Michal Hocko
SUSE Labs

#6 lore raw
Quoted 78 lines · Carlos Galo
Quoted 19 lines · Michal Hocko
Quoted 17 lines · Carlos Galo
Quoted 15 lines · Michal Hocko

Hi,
sorry I have missed this before.

Quoted 6 lines · Carlos Galo

The current implementation of the mark_victim tracepoint provides only the process ID (pid) of the victim process. This limitation poses challenges for userspace tools that need additional information about the OOM victim. The association between pid and the additional data may be lost after the kill, making it difficult for userspace to correlate the OOM event with the specific process.

You are correct that post OOM all per-process information is lost. On the other hand we do dump all this information to the kernel log. Could you explain why that is not suitable for your purpose?

Quoted 7 lines
Quoted 5 lines

Userspace tools often need real-time visibility into OOM situations for userspace intervention. Our use case involves utilizing BPF programs, along with BPF ring buffers, to provide OOM notification to userspace. Parsing kernel logs would be significant overhead as opposed to the event based BPF approach.

Please put that into the changelog.

Will do.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines

In order to mitigate this limitation, add the following fields:

- UID
In Android each installed application has a unique UID. Including the uid assists in correlating OOM events with specific apps.

- Process Name (comm)
Enables identification of the affected process.

- OOM Score
Allows userspace to get additional insights of the relative kill priority of the OOM victim.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

What is the oom score useful for?

The OOM score provides us a measure of the victim's importance. On the android side, it allows us to identify if top or foreground apps are killed, which have user perceptible impact.

But the value on its own (wihtout knowing scores of other tasks) doesn't really tell you anything, does it?

Android uses the OOM adj_score ranges to categorize app state (foreground, background, ...). I'll resend a v3 with the commit text updated to include details about this.

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

Is there any reason to provide a different information from the one
reported to the kernel log?
__oom_kill_process:
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);
Quoted 8 lines
Quoted 3 lines

We added these fields we need (UID, process name, and OOM score), but we're open to adding the others if you prefer that for consistency with the kernel log.

yes, I think the consistency would be better here. For one it reports numbers which can tell quite a lot about the killed victim. It is a superset of what you already asking for. With a notable exception of the oom_score which is really dubious without a wider context.

Quoted 5 lines

Sounds good, I'll resend a v3 that includes these fields as well.

Thanks,
Carlos

I posted V3 here:
https://lore.kernel.org/all/[email protected]/

Thanks,
Carlos

Quoted 3 lines
Quoted 3 lines

--
Michal Hocko
SUSE Labs