[PATCH 01/13] fs: add OOM badness callback to file_operatrations struct

From: Christian König
Date: Tue May 31 2022 - 06:00:32 EST


From: Andrey Grodzovsky <andrey.grodzovsky@xxxxxxx>

This allows file_operation implementations to specify an additional
badness for the OOM killer when they allocate memory on behalf of
userspace.

This badness is per file because file descriptor and therefor the
reference to the allocated memory can migrate between processes.

For easy debugging this also adds printing of the per file oom badness
to fdinfo inside procfs.

Signed-off-by: Andrey Grodzovsky <andrey.grodzovsky@xxxxxxx>
Signed-off-by: Christian König <christian.koenig@xxxxxxx>
---
fs/proc/fd.c | 4 ++++
include/linux/fs.h | 1 +
2 files changed, 5 insertions(+)

diff --git a/fs/proc/fd.c b/fs/proc/fd.c
index 172c86270b31..d1905c05cb3a 100644
--- a/fs/proc/fd.c
+++ b/fs/proc/fd.c
@@ -59,6 +59,10 @@ static int seq_show(struct seq_file *m, void *v)
real_mount(file->f_path.mnt)->mnt_id,
file_inode(file)->i_ino);

+ if (file->f_op->oom_badness)
+ seq_printf(m, "oom_badness:\t%lu\n",
+ file->f_op->oom_badness(file));
+
/* show_fd_locks() never deferences files so a stale value is safe */
show_fd_locks(m, file, files);
if (seq_has_overflowed(m))
diff --git a/include/linux/fs.h b/include/linux/fs.h
index bbde95387a23..d5222543aeb0 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1995,6 +1995,7 @@ struct file_operations {
struct file *file_out, loff_t pos_out,
loff_t len, unsigned int remap_flags);
int (*fadvise)(struct file *, loff_t, loff_t, int);
+ long (*oom_badness)(struct file *);
} __randomize_layout;

struct inode_operations {
--
2.25.1