proc: Move OOM-related procfs code to fs/proc/oom.c
Subsystems (3)
PATCH 1/1 proc: Move OOM-related procfs code to fs/proc/oom.c
From: Ye Liu <[email protected]>
fs/proc/base.c is a large file (~3700 lines) handling all per-process procfs entries. The OOM score, oom_adj, and oom_score_adj interface code is self-contained and has no dependencies on other parts of base.c, so move it into a dedicated fs/proc/oom.c following the same pattern as fs/proc/fd.c and fs/proc/task_mmu.c.
This makes both files easier to read and keeps OOM-related procfs entries together in one place.
--- MAINTAINERS | 1 + fs/proc/Makefile | 1 + fs/proc/base.c | 232 ------------------------------------------------- fs/proc/internal.h | 8 ++ fs/proc/oom.c | 249 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 259 insertions(+), 232 deletions(-)
MAINTAINERS +1 -0
@@ -17676,6 +17676,7 @@ R: David Rientjes <[email protected]> R: Shakeel Butt <[email protected]> L: [email protected] S: Maintained +F: fs/proc/oom.c F: include/linux/oom.h F: include/trace/events/oom.h F: include/uapi/linux/oom.h
fs/proc/Makefile +1 -0
@@ -21,6 +21,7 @@ proc-y += interrupts.o endif proc-y += loadavg.o proc-y += meminfo.o +proc-y += oom.o proc-y += stat.o proc-y += uptime.o proc-y += util.o
fs/proc/base.c +0 -232
256 lines not on this page · raw message
fs/proc/internal.h +8 -0
@@ -238,6 +238,14 @@ static inline int folio_average_page_mapcount(struct folio *folio)/* ... but return at least 1. */ return max_t (int, avg + entire_mapcount,1 ); } +/* +* oom.c +*/ +int proc_oom_score (struct seq_file *m,struct pid_namespace *ns, +struct pid *pid,struct task_struct *task); +extern const struct file_operations proc_oom_adj_operations; +extern const struct file_operations proc_oom_score_adj_operations; +/* * array.c */
fs/proc/oom.c new file +249 -0
249 lines not on this page · raw message
---
base-commit: 1fffefb00cc0b0da1f2ae24e3d33364530ea6515
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git master
change-id: 20261014-oom_procfs-f74a922f73f1
Best regards,
Signature · 1 line
Ye Liu <[email protected]>
Quoted 10 lines · Ye Liu
From: Ye Liu <[email protected]>
fs/proc/base.cis a large file (~3700 lines) handling all per-process procfs entries. The OOM score,oom_adj, andoom_score_adjinterface code is self-contained and has no dependencies on other parts ofbase.c, so move it into a dedicatedfs/proc/oom.cfollowing the same pattern asfs/proc/fd.candfs/proc/task_mmu.c.This makes both files easier to read and keeps OOM-related procfs entries together in one place.
Are there plans to separate other functionality or is this one off thing?
In general I am not against of moving stuff to more specialized files. An obvious downside is a review as it is quite easy to overlook a subtle change when moving so much code. Something worth undertaking if there is a bigger goal. Not so sure if this is just a one of and fs/proc/base.c still keeps other random stuff in.
Quoted 580 lines
Signed-off-by Ye Liu <[email protected]>--- MAINTAINERS | 1 + fs/proc/Makefile | 1 + fs/proc/base.c | 232 ------------------------------------------------- fs/proc/internal.h | 8 ++ fs/proc/oom.c | 249 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 259 insertions(+), 232 deletions(-)MAINTAINERS +1
-0@@ -17676,6 +17676,7 @@ R: David Rientjes <[email protected]> R: Shakeel Butt <[email protected]> L: [email protected] S: Maintained +F: fs/proc/oom.c F: include/linux/oom.h F: include/trace/events/oom.h F: include/uapi/linux/oom.hfs/proc/Makefile +1
-0@@ -21,6 +21,7 @@ proc-y += interrupts.o endif proc-y += loadavg.o proc-y += meminfo.o +proc-y += oom.o proc-y += stat.o proc-y += uptime.o proc-y += util.ofs/proc/base.c +0
-232@@ -80,7 +80,6 @@#include <linux/audit.h> #include <linux/poll.h> #include <linux/nsproxy.h> -#include <linux/oom.h> #include <linux/elf.h> #include <linux/pid_namespace.h> #include <linux/user_namespace.h> @@ -99,7 +98,6 @@#include <linux/cn_proc.h> #include <linux/ksm.h> #include <uapi/linux/lsm.h> -#include <trace/events/oom.h> #include "internal.h" #include "fd.h" @@ -580,28 +578,6 @@ static const struct file_operations proc_lstats_operations = {#endif -static int proc_oom_score (struct seq_file *m,struct pid_namespace *ns, -struct pid *pid,struct task_struct *task) -{ -unsigned long totalpages =totalram_pages () + total_swap_pages; -unsigned long points =0 ; -long badness; - - badness =oom_badness (task, totalpages); -/* -* Special case OOM_SCORE_ADJ_MIN for all others scale the -* badness value into [0, 2000] range which we have been -* exporting for a long time so userspace might depend on it. -*/ -if (badness !=LONG_MIN ) - points = (OOM_SCORE_ADJ_MAX + - badness *OOM_SCORE_ADJ_MAX / (long )totalpages) *2 /3 ; - -seq_printf (m,"%lu\n" , points); - -return 0 ; -} -struct limit_names {const char *name;const char *unit;@@ -1115,214 +1091,6 @@ static const struct file_operations proc_auxv_operations = { .release = mem_release, }; -static ssize_t oom_adj_read (struct file *file,char __user *buf,size_t count, -loff_t *ppos) -{ -struct task_struct *task =get_proc_task (file_inode (file)); -char buffer[PROC_NUMBUF ]; -int oom_adj =OOM_ADJUST_MIN ; -size_t len; - -if (!task) -return -ESRCH ; -if (task->signal->oom_score_adj ==OOM_SCORE_ADJ_MAX ) - oom_adj =OOM_ADJUST_MAX ; -else - oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE ) / -OOM_SCORE_ADJ_MAX ; -put_task_struct (task); -if (oom_adj >OOM_ADJUST_MAX ) - oom_adj =OOM_ADJUST_MAX ; - len =snprintf (buffer,sizeof (buffer),"%d\n" , oom_adj); -return simple_read_from_buffer (buf, count, ppos, buffer, len); -} - -static int __set_oom_adj (struct file *file,int oom_adj,bool legacy) -{ -struct mm_struct *mm =NULL ; -struct task_struct *task; -int err =0 ; - - task =get_proc_task (file_inode (file)); -if (!task) -return -ESRCH ; - -mutex_lock (&oom_adj_mutex); -if (legacy) { -if (oom_adj < task->signal->oom_score_adj && - !capable (CAP_SYS_RESOURCE )) { - err = -EACCES ; - goto err_unlock; - } -/* -* /proc/pid/oom_adj is provided for legacy purposes, ask users to use -* /proc/pid/oom_score_adj instead. -*/ -pr_warn_once ("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n" , - current->comm,task_pid_nr (current),task_pid_nr (task), -task_pid_nr (task)); - }else { -if ((short )oom_adj < task->signal->oom_score_adj_min && - !capable (CAP_SYS_RESOURCE )) { - err = -EACCES ; - goto err_unlock; - } - } - -/* -* Make sure we will check other processes sharing the mm if this is -* not vfrok which wants its own oom_score_adj. -* pin the mm so it doesn't go away and get reused after task_unlock -*/ -if (!task->vfork_done) { -struct task_struct *p =find_lock_task_mm (task); - -if (p) { -if (mm_flags_test (MMF_MULTIPROCESS , p->mm)) { - mm = p->mm; -mmgrab (mm); - } -task_unlock (p); - } - } - - task->signal->oom_score_adj = oom_adj; -if (!legacy &&has_capability_noaudit (current,CAP_SYS_RESOURCE )) - task->signal->oom_score_adj_min = (short )oom_adj; -trace_oom_score_adj_update (task); - -if (mm) { -struct task_struct *p; - -rcu_read_lock (); -for_each_process (p) { -if (same_thread_group (task, p)) -continue ; - -/* do not touch kernel threads or the global init */ -if (p->flags &PF_KTHREAD ||is_global_init (p)) -continue ; - -task_lock (p); -if (!p->vfork_done &&process_shares_mm (p, mm)) { - p->signal->oom_score_adj = oom_adj; -if (!legacy &&has_capability_noaudit (current,CAP_SYS_RESOURCE )) - p->signal->oom_score_adj_min = (short )oom_adj; - } -task_unlock (p); - } -rcu_read_unlock (); -mmdrop (mm); - } -err_unlock: -mutex_unlock (&oom_adj_mutex); -put_task_struct (task); -return err; -} - -/* -* /proc/pid/oom_adj exists solely for backwards compatibility with previous -* kernels. The effective policy is defined by oom_score_adj, which has a -* different scale: oom_adj grew exponentially and oom_score_adj grows linearly. -* Values written to oom_adj are simply mapped linearly to oom_score_adj. -* Processes that become oom disabled via oom_adj will still be oom disabled -* with this implementation. -* -* oom_adj cannot be removed since existing userspace binaries use it. -*/ -static ssize_t oom_adj_write (struct file *file,const char __user *buf, -size_t count,loff_t *ppos) -{ -char buffer[PROC_NUMBUF ] = {}; -int oom_adj; -int err; - -if (count >sizeof (buffer) -1 ) - count =sizeof (buffer) -1 ; -if (copy_from_user (buffer, buf, count)) { - err = -EFAULT ; - goto out; - } - - err =kstrtoint (strstrip (buffer),0 , &oom_adj); -if (err) - goto out; -if ((oom_adj <OOM_ADJUST_MIN || oom_adj >OOM_ADJUST_MAX ) && - oom_adj !=OOM_DISABLE ) { - err = -EINVAL ; - goto out; - } - -/* -* Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum -* value is always attainable. -*/ -if (oom_adj ==OOM_ADJUST_MAX ) - oom_adj =OOM_SCORE_ADJ_MAX ; -else - oom_adj = (oom_adj *OOM_SCORE_ADJ_MAX ) / -OOM_DISABLE ; - - err =__set_oom_adj (file, oom_adj, true); -out: -return err <0 ? err : count; -} - -static const struct file_operations proc_oom_adj_operations = { - .read = oom_adj_read, - .write = oom_adj_write, - .llseek = generic_file_llseek, -}; - -static ssize_t oom_score_adj_read (struct file *file,char __user *buf, -size_t count,loff_t *ppos) -{ -struct task_struct *task =get_proc_task (file_inode (file)); -char buffer[PROC_NUMBUF ]; -short oom_score_adj =OOM_SCORE_ADJ_MIN ; -size_t len; - -if (!task) -return -ESRCH ; - oom_score_adj = task->signal->oom_score_adj; -put_task_struct (task); - len =snprintf (buffer,sizeof (buffer),"%hd\n" , oom_score_adj); -return simple_read_from_buffer (buf, count, ppos, buffer, len); -} - -static ssize_t oom_score_adj_write (struct file *file,const char __user *buf, -size_t count,loff_t *ppos) -{ -char buffer[PROC_NUMBUF ] = {}; -int oom_score_adj; -int err; - -if (count >sizeof (buffer) -1 ) - count =sizeof (buffer) -1 ; -if (copy_from_user (buffer, buf, count)) { - err = -EFAULT ; - goto out; - } - - err =kstrtoint (strstrip (buffer),0 , &oom_score_adj); -if (err) - goto out; -if (oom_score_adj <OOM_SCORE_ADJ_MIN || - oom_score_adj >OOM_SCORE_ADJ_MAX ) { - err = -EINVAL ; - goto out; - } - - err =__set_oom_adj (file, oom_score_adj, false); -out: -return err <0 ? err : count; -} - -static const struct file_operations proc_oom_score_adj_operations = { - .read = oom_score_adj_read, - .write = oom_score_adj_write, - .llseek = default_llseek, -}; -#ifdef CONFIG_AUDIT #define TMPBUFLEN 11 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,fs/proc/internal.h +8
-0@@ -238,6 +238,14 @@ static inline int folio_average_page_mapcount(struct folio *folio)/* ... but return at least 1. */ return max_t (int, avg + entire_mapcount,1 ); } +/* +* oom.c +*/ +int proc_oom_score (struct seq_file *m,struct pid_namespace *ns, +struct pid *pid,struct task_struct *task); +extern const struct file_operations proc_oom_adj_operations; +extern const struct file_operations proc_oom_score_adj_operations; +/* * array.c */ fs/proc/oom.c new file +249
-1@@ -0,0 +1,249 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* OOM-related procfs entries +* +* This file handles the OOM score, oom_adj, and oom_score_adj interfaces +* in /proc/<pid>/. +*/ + +#include <linux/proc_fs.h> +#include <linux/oom.h> +#include <linux/seq_file.h> +#include <linux/capability.h> +#include <linux/mm.h> +#include <linux/swap.h> +#include <linux/sched/signal.h> +#include <linux/pid_namespace.h> +#include <trace/events/oom.h> + +#include "internal.h" + +int proc_oom_score (struct seq_file *m,struct pid_namespace *ns, +struct pid *pid,struct task_struct *task) +{ +unsigned long totalpages =totalram_pages () + total_swap_pages; +unsigned long points =0 ; +long badness; + + badness =oom_badness (task, totalpages); +/* +* Special case OOM_SCORE_ADJ_MIN for all others scale the +* badness value into [0, 2000] range which we have been +* exporting for a long time so userspace might depend on it. +*/ +if (badness !=LONG_MIN ) + points = (OOM_SCORE_ADJ_MAX + + badness *OOM_SCORE_ADJ_MAX / (long )totalpages) *2 /3 ; + +seq_printf (m,"%lu\n" , points); + +return 0 ; +} + +static ssize_t oom_adj_read (struct file *file,char __user *buf,size_t count, +loff_t *ppos) +{ +struct task_struct *task =get_proc_task (file_inode (file)); +char buffer[PROC_NUMBUF ]; +int oom_adj =OOM_ADJUST_MIN ; +size_t len; + +if (!task) +return -ESRCH ; +if (task->signal->oom_score_adj ==OOM_SCORE_ADJ_MAX ) + oom_adj =OOM_ADJUST_MAX ; +else + oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE ) / +OOM_SCORE_ADJ_MAX ; +put_task_struct (task); +if (oom_adj >OOM_ADJUST_MAX ) + oom_adj =OOM_ADJUST_MAX ; + len =snprintf (buffer,sizeof (buffer),"%d\n" , oom_adj); +return simple_read_from_buffer (buf, count, ppos, buffer, len); +} + +static int __set_oom_adj (struct file *file,int oom_adj,bool legacy) +{ +struct mm_struct *mm =NULL ; +struct task_struct *task; +int err =0 ; + + task =get_proc_task (file_inode (file)); +if (!task) +return -ESRCH ; + +mutex_lock (&oom_adj_mutex); +if (legacy) { +if (oom_adj < task->signal->oom_score_adj && + !capable (CAP_SYS_RESOURCE )) { + err = -EACCES ; + goto err_unlock; + } +/* +* /proc/pid/oom_adj is provided for legacy purposes, ask users to use +* /proc/pid/oom_score_adj instead. +*/ +pr_warn_once ("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n" , + current->comm,task_pid_nr (current),task_pid_nr (task), +task_pid_nr (task)); + }else { +if ((short )oom_adj < task->signal->oom_score_adj_min && + !capable (CAP_SYS_RESOURCE )) { + err = -EACCES ; + goto err_unlock; + } + } + +/* +* Make sure we will check other processes sharing the mm if this is +* not vfrok which wants its own oom_score_adj. +* pin the mm so it doesn't go away and get reused after task_unlock +*/ +if (!task->vfork_done) { +struct task_struct *p =find_lock_task_mm (task); + +if (p) { +if (mm_flags_test (MMF_MULTIPROCESS , p->mm)) { + mm = p->mm; +mmgrab (mm); + } +task_unlock (p); + } + } + + task->signal->oom_score_adj = oom_adj; +if (!legacy &&has_capability_noaudit (current,CAP_SYS_RESOURCE )) + task->signal->oom_score_adj_min = (short )oom_adj; +trace_oom_score_adj_update (task); + +if (mm) { +struct task_struct *p; + +rcu_read_lock (); +for_each_process (p) { +if (same_thread_group (task, p)) +continue ; + +/* do not touch kernel threads or the global init */ +if (p->flags &PF_KTHREAD ||is_global_init (p)) +continue ; + +task_lock (p); +if (!p->vfork_done &&process_shares_mm (p, mm)) { + p->signal->oom_score_adj = oom_adj; +if (!legacy &&has_capability_noaudit (current,CAP_SYS_RESOURCE )) + p->signal->oom_score_adj_min = (short )oom_adj; + } +task_unlock (p); + } +rcu_read_unlock (); +mmdrop (mm); + } +err_unlock: +mutex_unlock (&oom_adj_mutex); +put_task_struct (task); +return err; +} + +/* +* /proc/pid/oom_adj exists solely for backwards compatibility with previous +* kernels. The effective policy is defined by oom_score_adj, which has a +* different scale: oom_adj grew exponentially and oom_score_adj grows linearly. +* Values written to oom_adj are simply mapped linearly to oom_score_adj. +* Processes that become oom disabled via oom_adj will still be oom disabled +* with this implementation. +* +* oom_adj cannot be removed since existing userspace binaries use it. +*/ +static ssize_t oom_adj_write (struct file *file,const char __user *buf, +size_t count,loff_t *ppos) +{ +char buffer[PROC_NUMBUF ] = {}; +int oom_adj; +int err; + +if (count >sizeof (buffer) -1 ) + count =sizeof (buffer) -1 ; +if (copy_from_user (buffer, buf, count)) { + err = -EFAULT ; + goto out; + } + + err =kstrtoint (strstrip (buffer),0 , &oom_adj); +if (err) + goto out; +if ((oom_adj <OOM_ADJUST_MIN || oom_adj >OOM_ADJUST_MAX ) && + oom_adj !=OOM_DISABLE ) { + err = -EINVAL ; + goto out; + } + +/* +* Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum +* value is always attainable. +*/ +if (oom_adj ==OOM_ADJUST_MAX ) + oom_adj =OOM_SCORE_ADJ_MAX ; +else + oom_adj = (oom_adj *OOM_SCORE_ADJ_MAX ) / -OOM_DISABLE ; + + err =__set_oom_adj (file, oom_adj, true); +out: +return err <0 ? err : count; +} + +const struct file_operations proc_oom_adj_operations = { + .read = oom_adj_read, + .write = oom_adj_write, + .llseek = generic_file_llseek, +}; + +static ssize_t oom_score_adj_read (struct file *file,char __user *buf, +size_t count,loff_t *ppos) +{ +struct task_struct *task =get_proc_task (file_inode (file)); +char buffer[PROC_NUMBUF ]; +short oom_score_adj =OOM_SCORE_ADJ_MIN ; +size_t len; + +if (!task) +return -ESRCH ; + oom_score_adj = task->signal->oom_score_adj; +put_task_struct (task); + len =snprintf (buffer,sizeof (buffer),"%hd\n" , oom_score_adj); +return simple_read_from_buffer (buf, count, ppos, buffer, len); +} + +static ssize_t oom_score_adj_write (struct file *file,const char __user *buf, +size_t count,loff_t *ppos) +{ +char buffer[PROC_NUMBUF ] = {}; +int oom_score_adj; +int err; + +if (count >sizeof (buffer) -1 ) + count =sizeof (buffer) -1 ; +if (copy_from_user (buffer, buf, count)) { + err = -EFAULT ; + goto out; + } + + err =kstrtoint (strstrip (buffer),0 , &oom_score_adj); +if (err) + goto out; +if (oom_score_adj <OOM_SCORE_ADJ_MIN || + oom_score_adj >OOM_SCORE_ADJ_MAX ) { + err = -EINVAL ; + goto out; + } + + err =__set_oom_adj (file, oom_score_adj, false); +out: +return err <0 ? err : count; +} + +const struct file_operations proc_oom_score_adj_operations = { + .read = oom_score_adj_read, + .write = oom_score_adj_write, + .llseek = default_llseek, +}; ---base-commit:
1fffefb00cc0b0da1f2ae24e3d33364530ea6515
base-tree:git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.gitmaster
change-id: 20261014-oom_procfs-f74a922f73f1Best regards,
Signature · 1 line
Ye Liu <[email protected]>
Signature · 2 lines
Michal Hocko SUSE Labs
Quoted 12 lines · Michal Hocko
Quoted 10 lines · Ye Liu
From: Ye Liu <[email protected]>
fs/proc/base.cis a large file (~3700 lines) handling all per-process procfs entries. The OOM score,oom_adj, andoom_score_adjinterface code is self-contained and has no dependencies on other parts ofbase.c, so move it into a dedicatedfs/proc/oom.cfollowing the same pattern asfs/proc/fd.candfs/proc/task_mmu.c.This makes both files easier to read and keeps OOM-related procfs entries together in one place.
Quoted 8 lines
Are there plans to separate other functionality or is this one off thing?
In general I am not against of moving stuff to more specialized files. An obvious downside is a review as it is quite easy to overlook a subtle change when moving so much code. Something worth undertaking if there is a bigger goal. Not so sure if this is just a one of andfs/proc/base.cstill keeps other random stuff in.
Hi Michal,
This is a one-off move specifically for the OOM-related procfs code. I don't have a broader plan to split up fs/proc/base.c at this time.
I understand the concern about subtle changes during code moves. To address that, the patch is a pure relocation with no functional modifications — all function bodies are moved verbatim. The only non-mechanical changes are:
- Three symbols (proc_oom_score, proc_oom_adj_operations, proc_oom_score_adj_operations) are made non-static and declared in fs/proc/internal.h, since base.c still references them via the proc_dir_entry table.
- The oom_adj_mutex static local is moved along with its users.
I have tested the patch and observed no functional issues.
Quoted 581 lines
Quoted 580 lines
Signed-off-by Ye Liu <[email protected]>--- MAINTAINERS | 1 + fs/proc/Makefile | 1 + fs/proc/base.c | 232 ------------------------------------------------- fs/proc/internal.h | 8 ++ fs/proc/oom.c | 249 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 259 insertions(+), 232 deletions(-)MAINTAINERS +1
-0@@ -17676,6 +17676,7 @@ R: David Rientjes <[email protected]> R: Shakeel Butt <[email protected]> L: [email protected] S: Maintained +F: fs/proc/oom.c F: include/linux/oom.h F: include/trace/events/oom.h F: include/uapi/linux/oom.hfs/proc/Makefile +1
-0@@ -21,6 +21,7 @@ proc-y += interrupts.o endif proc-y += loadavg.o proc-y += meminfo.o +proc-y += oom.o proc-y += stat.o proc-y += uptime.o proc-y += util.ofs/proc/base.c +0
-232@@ -80,7 +80,6 @@#include <linux/audit.h> #include <linux/poll.h> #include <linux/nsproxy.h> -#include <linux/oom.h> #include <linux/elf.h> #include <linux/pid_namespace.h> #include <linux/user_namespace.h> @@ -99,7 +98,6 @@#include <linux/cn_proc.h> #include <linux/ksm.h> #include <uapi/linux/lsm.h> -#include <trace/events/oom.h> #include "internal.h" #include "fd.h" @@ -580,28 +578,6 @@ static const struct file_operations proc_lstats_operations = {#endif -static int proc_oom_score (struct seq_file *m,struct pid_namespace *ns, -struct pid *pid,struct task_struct *task) -{ -unsigned long totalpages =totalram_pages () + total_swap_pages; -unsigned long points =0 ; -long badness; - - badness =oom_badness (task, totalpages); -/* -* Special case OOM_SCORE_ADJ_MIN for all others scale the -* badness value into [0, 2000] range which we have been -* exporting for a long time so userspace might depend on it. -*/ -if (badness !=LONG_MIN ) - points = (OOM_SCORE_ADJ_MAX + - badness *OOM_SCORE_ADJ_MAX / (long )totalpages) *2 /3 ; - -seq_printf (m,"%lu\n" , points); - -return 0 ; -} -struct limit_names {const char *name;const char *unit;@@ -1115,214 +1091,6 @@ static const struct file_operations proc_auxv_operations = { .release = mem_release, }; -static ssize_t oom_adj_read (struct file *file,char __user *buf,size_t count, -loff_t *ppos) -{ -struct task_struct *task =get_proc_task (file_inode (file)); -char buffer[PROC_NUMBUF ]; -int oom_adj =OOM_ADJUST_MIN ; -size_t len; - -if (!task) -return -ESRCH ; -if (task->signal->oom_score_adj ==OOM_SCORE_ADJ_MAX ) - oom_adj =OOM_ADJUST_MAX ; -else - oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE ) / -OOM_SCORE_ADJ_MAX ; -put_task_struct (task); -if (oom_adj >OOM_ADJUST_MAX ) - oom_adj =OOM_ADJUST_MAX ; - len =snprintf (buffer,sizeof (buffer),"%d\n" , oom_adj); -return simple_read_from_buffer (buf, count, ppos, buffer, len); -} - -static int __set_oom_adj (struct file *file,int oom_adj,bool legacy) -{ -struct mm_struct *mm =NULL ; -struct task_struct *task; -int err =0 ; - - task =get_proc_task (file_inode (file)); -if (!task) -return -ESRCH ; - -mutex_lock (&oom_adj_mutex); -if (legacy) { -if (oom_adj < task->signal->oom_score_adj && - !capable (CAP_SYS_RESOURCE )) { - err = -EACCES ; - goto err_unlock; - } -/* -* /proc/pid/oom_adj is provided for legacy purposes, ask users to use -* /proc/pid/oom_score_adj instead. -*/ -pr_warn_once ("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n" , - current->comm,task_pid_nr (current),task_pid_nr (task), -task_pid_nr (task)); - }else { -if ((short )oom_adj < task->signal->oom_score_adj_min && - !capable (CAP_SYS_RESOURCE )) { - err = -EACCES ; - goto err_unlock; - } - } - -/* -* Make sure we will check other processes sharing the mm if this is -* not vfrok which wants its own oom_score_adj. -* pin the mm so it doesn't go away and get reused after task_unlock -*/ -if (!task->vfork_done) { -struct task_struct *p =find_lock_task_mm (task); - -if (p) { -if (mm_flags_test (MMF_MULTIPROCESS , p->mm)) { - mm = p->mm; -mmgrab (mm); - } -task_unlock (p); - } - } - - task->signal->oom_score_adj = oom_adj; -if (!legacy &&has_capability_noaudit (current,CAP_SYS_RESOURCE )) - task->signal->oom_score_adj_min = (short )oom_adj; -trace_oom_score_adj_update (task); - -if (mm) { -struct task_struct *p; - -rcu_read_lock (); -for_each_process (p) { -if (same_thread_group (task, p)) -continue ; - -/* do not touch kernel threads or the global init */ -if (p->flags &PF_KTHREAD ||is_global_init (p)) -continue ; - -task_lock (p); -if (!p->vfork_done &&process_shares_mm (p, mm)) { - p->signal->oom_score_adj = oom_adj; -if (!legacy &&has_capability_noaudit (current,CAP_SYS_RESOURCE )) - p->signal->oom_score_adj_min = (short )oom_adj; - } -task_unlock (p); - } -rcu_read_unlock (); -mmdrop (mm); - } -err_unlock: -mutex_unlock (&oom_adj_mutex); -put_task_struct (task); -return err; -} - -/* -* /proc/pid/oom_adj exists solely for backwards compatibility with previous -* kernels. The effective policy is defined by oom_score_adj, which has a -* different scale: oom_adj grew exponentially and oom_score_adj grows linearly. -* Values written to oom_adj are simply mapped linearly to oom_score_adj. -* Processes that become oom disabled via oom_adj will still be oom disabled -* with this implementation. -* -* oom_adj cannot be removed since existing userspace binaries use it. -*/ -static ssize_t oom_adj_write (struct file *file,const char __user *buf, -size_t count,loff_t *ppos) -{ -char buffer[PROC_NUMBUF ] = {}; -int oom_adj; -int err; - -if (count >sizeof (buffer) -1 ) - count =sizeof (buffer) -1 ; -if (copy_from_user (buffer, buf, count)) { - err = -EFAULT ; - goto out; - } - - err =kstrtoint (strstrip (buffer),0 , &oom_adj); -if (err) - goto out; -if ((oom_adj <OOM_ADJUST_MIN || oom_adj >OOM_ADJUST_MAX ) && - oom_adj !=OOM_DISABLE ) { - err = -EINVAL ; - goto out; - } - -/* -* Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum -* value is always attainable. -*/ -if (oom_adj ==OOM_ADJUST_MAX ) - oom_adj =OOM_SCORE_ADJ_MAX ; -else - oom_adj = (oom_adj *OOM_SCORE_ADJ_MAX ) / -OOM_DISABLE ; - - err =__set_oom_adj (file, oom_adj, true); -out: -return err <0 ? err : count; -} - -static const struct file_operations proc_oom_adj_operations = { - .read = oom_adj_read, - .write = oom_adj_write, - .llseek = generic_file_llseek, -}; - -static ssize_t oom_score_adj_read (struct file *file,char __user *buf, -size_t count,loff_t *ppos) -{ -struct task_struct *task =get_proc_task (file_inode (file)); -char buffer[PROC_NUMBUF ]; -short oom_score_adj =OOM_SCORE_ADJ_MIN ; -size_t len; - -if (!task) -return -ESRCH ; - oom_score_adj = task->signal->oom_score_adj; -put_task_struct (task); - len =snprintf (buffer,sizeof (buffer),"%hd\n" , oom_score_adj); -return simple_read_from_buffer (buf, count, ppos, buffer, len); -} - -static ssize_t oom_score_adj_write (struct file *file,const char __user *buf, -size_t count,loff_t *ppos) -{ -char buffer[PROC_NUMBUF ] = {}; -int oom_score_adj; -int err; - -if (count >sizeof (buffer) -1 ) - count =sizeof (buffer) -1 ; -if (copy_from_user (buffer, buf, count)) { - err = -EFAULT ; - goto out; - } - - err =kstrtoint (strstrip (buffer),0 , &oom_score_adj); -if (err) - goto out; -if (oom_score_adj <OOM_SCORE_ADJ_MIN || - oom_score_adj >OOM_SCORE_ADJ_MAX ) { - err = -EINVAL ; - goto out; - } - - err =__set_oom_adj (file, oom_score_adj, false); -out: -return err <0 ? err : count; -} - -static const struct file_operations proc_oom_score_adj_operations = { - .read = oom_score_adj_read, - .write = oom_score_adj_write, - .llseek = default_llseek, -}; -#ifdef CONFIG_AUDIT #define TMPBUFLEN 11 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,fs/proc/internal.h +8
-0@@ -238,6 +238,14 @@ static inline int folio_average_page_mapcount(struct folio *folio)/* ... but return at least 1. */ return max_t (int, avg + entire_mapcount,1 ); } +/* +* oom.c +*/ +int proc_oom_score (struct seq_file *m,struct pid_namespace *ns, +struct pid *pid,struct task_struct *task); +extern const struct file_operations proc_oom_adj_operations; +extern const struct file_operations proc_oom_score_adj_operations; +/* * array.c */ fs/proc/oom.c new file +249
-1@@ -0,0 +1,249 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* OOM-related procfs entries +* +* This file handles the OOM score, oom_adj, and oom_score_adj interfaces +* in /proc/<pid>/. +*/ + +#include <linux/proc_fs.h> +#include <linux/oom.h> +#include <linux/seq_file.h> +#include <linux/capability.h> +#include <linux/mm.h> +#include <linux/swap.h> +#include <linux/sched/signal.h> +#include <linux/pid_namespace.h> +#include <trace/events/oom.h> + +#include "internal.h" + +int proc_oom_score (struct seq_file *m,struct pid_namespace *ns, +struct pid *pid,struct task_struct *task) +{ +unsigned long totalpages =totalram_pages () + total_swap_pages; +unsigned long points =0 ; +long badness; + + badness =oom_badness (task, totalpages); +/* +* Special case OOM_SCORE_ADJ_MIN for all others scale the +* badness value into [0, 2000] range which we have been +* exporting for a long time so userspace might depend on it. +*/ +if (badness !=LONG_MIN ) + points = (OOM_SCORE_ADJ_MAX + + badness *OOM_SCORE_ADJ_MAX / (long )totalpages) *2 /3 ; + +seq_printf (m,"%lu\n" , points); + +return 0 ; +} + +static ssize_t oom_adj_read (struct file *file,char __user *buf,size_t count, +loff_t *ppos) +{ +struct task_struct *task =get_proc_task (file_inode (file)); +char buffer[PROC_NUMBUF ]; +int oom_adj =OOM_ADJUST_MIN ; +size_t len; + +if (!task) +return -ESRCH ; +if (task->signal->oom_score_adj ==OOM_SCORE_ADJ_MAX ) + oom_adj =OOM_ADJUST_MAX ; +else + oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE ) / +OOM_SCORE_ADJ_MAX ; +put_task_struct (task); +if (oom_adj >OOM_ADJUST_MAX ) + oom_adj =OOM_ADJUST_MAX ; + len =snprintf (buffer,sizeof (buffer),"%d\n" , oom_adj); +return simple_read_from_buffer (buf, count, ppos, buffer, len); +} + +static int __set_oom_adj (struct file *file,int oom_adj,bool legacy) +{ +struct mm_struct *mm =NULL ; +struct task_struct *task; +int err =0 ; + + task =get_proc_task (file_inode (file)); +if (!task) +return -ESRCH ; + +mutex_lock (&oom_adj_mutex); +if (legacy) { +if (oom_adj < task->signal->oom_score_adj && + !capable (CAP_SYS_RESOURCE )) { + err = -EACCES ; + goto err_unlock; + } +/* +* /proc/pid/oom_adj is provided for legacy purposes, ask users to use +* /proc/pid/oom_score_adj instead. +*/ +pr_warn_once ("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n" , + current->comm,task_pid_nr (current),task_pid_nr (task), +task_pid_nr (task)); + }else { +if ((short )oom_adj < task->signal->oom_score_adj_min && + !capable (CAP_SYS_RESOURCE )) { + err = -EACCES ; + goto err_unlock; + } + } + +/* +* Make sure we will check other processes sharing the mm if this is +* not vfrok which wants its own oom_score_adj. +* pin the mm so it doesn't go away and get reused after task_unlock +*/ +if (!task->vfork_done) { +struct task_struct *p =find_lock_task_mm (task); + +if (p) { +if (mm_flags_test (MMF_MULTIPROCESS , p->mm)) { + mm = p->mm; +mmgrab (mm); + } +task_unlock (p); + } + } + + task->signal->oom_score_adj = oom_adj; +if (!legacy &&has_capability_noaudit (current,CAP_SYS_RESOURCE )) + task->signal->oom_score_adj_min = (short )oom_adj; +trace_oom_score_adj_update (task); + +if (mm) { +struct task_struct *p; + +rcu_read_lock (); +for_each_process (p) { +if (same_thread_group (task, p)) +continue ; + +/* do not touch kernel threads or the global init */ +if (p->flags &PF_KTHREAD ||is_global_init (p)) +continue ; + +task_lock (p); +if (!p->vfork_done &&process_shares_mm (p, mm)) { + p->signal->oom_score_adj = oom_adj; +if (!legacy &&has_capability_noaudit (current,CAP_SYS_RESOURCE )) + p->signal->oom_score_adj_min = (short )oom_adj; + } +task_unlock (p); + } +rcu_read_unlock (); +mmdrop (mm); + } +err_unlock: +mutex_unlock (&oom_adj_mutex); +put_task_struct (task); +return err; +} + +/* +* /proc/pid/oom_adj exists solely for backwards compatibility with previous +* kernels. The effective policy is defined by oom_score_adj, which has a +* different scale: oom_adj grew exponentially and oom_score_adj grows linearly. +* Values written to oom_adj are simply mapped linearly to oom_score_adj. +* Processes that become oom disabled via oom_adj will still be oom disabled +* with this implementation. +* +* oom_adj cannot be removed since existing userspace binaries use it. +*/ +static ssize_t oom_adj_write (struct file *file,const char __user *buf, +size_t count,loff_t *ppos) +{ +char buffer[PROC_NUMBUF ] = {}; +int oom_adj; +int err; + +if (count >sizeof (buffer) -1 ) + count =sizeof (buffer) -1 ; +if (copy_from_user (buffer, buf, count)) { + err = -EFAULT ; + goto out; + } + + err =kstrtoint (strstrip (buffer),0 , &oom_adj); +if (err) + goto out; +if ((oom_adj <OOM_ADJUST_MIN || oom_adj >OOM_ADJUST_MAX ) && + oom_adj !=OOM_DISABLE ) { + err = -EINVAL ; + goto out; + } + +/* +* Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum +* value is always attainable. +*/ +if (oom_adj ==OOM_ADJUST_MAX ) + oom_adj =OOM_SCORE_ADJ_MAX ; +else + oom_adj = (oom_adj *OOM_SCORE_ADJ_MAX ) / -OOM_DISABLE ; + + err =__set_oom_adj (file, oom_adj, true); +out: +return err <0 ? err : count; +} + +const struct file_operations proc_oom_adj_operations = { + .read = oom_adj_read, + .write = oom_adj_write, + .llseek = generic_file_llseek, +}; + +static ssize_t oom_score_adj_read (struct file *file,char __user *buf, +size_t count,loff_t *ppos) +{ +struct task_struct *task =get_proc_task (file_inode (file)); +char buffer[PROC_NUMBUF ]; +short oom_score_adj =OOM_SCORE_ADJ_MIN ; +size_t len; + +if (!task) +return -ESRCH ; + oom_score_adj = task->signal->oom_score_adj; +put_task_struct (task); + len =snprintf (buffer,sizeof (buffer),"%hd\n" , oom_score_adj); +return simple_read_from_buffer (buf, count, ppos, buffer, len); +} + +static ssize_t oom_score_adj_write (struct file *file,const char __user *buf, +size_t count,loff_t *ppos) +{ +char buffer[PROC_NUMBUF ] = {}; +int oom_score_adj; +int err; + +if (count >sizeof (buffer) -1 ) + count =sizeof (buffer) -1 ; +if (copy_from_user (buffer, buf, count)) { + err = -EFAULT ; + goto out; + } + + err =kstrtoint (strstrip (buffer),0 , &oom_score_adj); +if (err) + goto out; +if (oom_score_adj <OOM_SCORE_ADJ_MIN || + oom_score_adj >OOM_SCORE_ADJ_MAX ) { + err = -EINVAL ; + goto out; + } + + err =__set_oom_adj (file, oom_score_adj, false); +out: +return err <0 ? err : count; +} + +const struct file_operations proc_oom_score_adj_operations = { + .read = oom_score_adj_read, + .write = oom_score_adj_write, + .llseek = default_llseek, +}; ---base-commit:
1fffefb00cc0b0da1f2ae24e3d33364530ea6515
base-tree:git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.gitmaster
change-id: 20261014-oom_procfs-f74a922f73f1Best regards,
Signature · 1 line
Ye Liu <[email protected]>
Signature · 2 lines
Thanks, Ye Liu
Quoted 24 lines · Ye Liu
Quoted 12 lines · Michal Hocko
Quoted 10 lines · Ye Liu
From: Ye Liu <[email protected]>
fs/proc/base.cis a large file (~3700 lines) handling all per-process procfs entries. The OOM score,oom_adj, andoom_score_adjinterface code is self-contained and has no dependencies on other parts ofbase.c, so move it into a dedicatedfs/proc/oom.cfollowing the same pattern asfs/proc/fd.candfs/proc/task_mmu.c.This makes both files easier to read and keeps OOM-related procfs entries together in one place.
Quoted 8 lines
Are there plans to separate other functionality or is this one off thing?
In general I am not against of moving stuff to more specialized files. An obvious downside is a review as it is quite easy to overlook a subtle change when moving so much code. Something worth undertaking if there is a bigger goal. Not so sure if this is just a one of andfs/proc/base.cstill keeps other random stuff in.
Quoted 8 lines
Hi Michal,
This is a one-off move specifically for the OOM-related procfs code. I don't have a broader plan to split up
fs/proc/base.cat this time.I understand the concern about subtle changes during code moves. To address that, the patch is a pure relocation with no functional modifications — all function bodies are moved verbatim.
But you do understand that I effectivelly need to redo the patch to confirm this is the case, right?
This is something I am not particularly thrilled to do for a single isolated patch without a larger plan. taks_mmu.c will remain a huge pile of code after this change. So what's the point for all that effort?
Signature · 2 lines
Michal Hocko SUSE Labs
Quoted 34 lines · Michal Hocko
Quoted 24 lines · Ye Liu
Quoted 12 lines · Michal Hocko
Quoted 10 lines · Ye Liu
From: Ye Liu <[email protected]>
fs/proc/base.cis a large file (~3700 lines) handling all per-process procfs entries. The OOM score,oom_adj, andoom_score_adjinterface code is self-contained and has no dependencies on other parts ofbase.c, so move it into a dedicatedfs/proc/oom.cfollowing the same pattern asfs/proc/fd.candfs/proc/task_mmu.c.This makes both files easier to read and keeps OOM-related procfs entries together in one place.
Quoted 8 lines
Are there plans to separate other functionality or is this one off thing?
In general I am not against of moving stuff to more specialized files. An obvious downside is a review as it is quite easy to overlook a subtle change when moving so much code. Something worth undertaking if there is a bigger goal. Not so sure if this is just a one of andfs/proc/base.cstill keeps other random stuff in.Quoted 8 lines
Hi Michal,
This is a one-off move specifically for the OOM-related procfs code. I don't have a broader plan to split up
fs/proc/base.cat this time.I understand the concern about subtle changes during code moves. To address that, the patch is a pure relocation with no functional modifications — all function bodies are moved verbatim.
Quoted 7 lines
But you do understand that I effectivelly need to redo the patch to confirm this is the case, right?
This is something I am not particularly thrilled to do for a single isolated patch without a larger plan.
taks_mmu.cwill remain a huge pile of code after this change. So what's the point for all that effort?
My familiarity with fs/proc/base.c is currently limited to the OOM-related code. Meaningfully splitting other parts of base.c would require time to study those modules first, which I don't have available in the near term. I may revisit a broader cleanup when I have the bandwidth to do it properly.
For now, I'd like to withdraw this patch rather than leave it hanging.
Appreciate the review.
Signature · 2 lines
Thanks, Ye Liu