git: b7df7b987e8f - main - linprocfs: Add /proc/self/oom_score_adj.

From: Dmitry Chagin <dchagin_at_FreeBSD.org>
Date: Thu, 31 Mar 2022 18:04:54 UTC
The branch main has been updated by dchagin:

URL: https://cgit.FreeBSD.org/src/commit/?id=b7df7b987e8fac05006aa5f132c424e2b2bcf156

commit b7df7b987e8fac05006aa5f132c424e2b2bcf156
Author:     Dmitry Chagin <dchagin@FreeBSD.org>
AuthorDate: 2022-03-31 18:04:44 +0000
Commit:     Dmitry Chagin <dchagin@FreeBSD.org>
CommitDate: 2022-03-31 18:04:44 +0000

    linprocfs: Add /proc/self/oom_score_adj.
    
    To avoid annoyng messages from LTP test suites add the simple
    implementation of /proc/self/oom_score_adj which is do nothing.
    
    Reviewed by:            emaste
    Differential revision:  https://reviews.freebsd.org/D34710
    MFC after:              2 weeks
---
 sys/compat/linprocfs/linprocfs.c | 30 ++++++++++++++++++++++++++++++
 sys/compat/linux/linux_emul.c    |  1 +
 sys/compat/linux/linux_emul.h    |  1 +
 sys/compat/linux/linux_misc.h    |  4 ++++
 4 files changed, 36 insertions(+)

diff --git a/sys/compat/linprocfs/linprocfs.c b/sys/compat/linprocfs/linprocfs.c
index 6a1a680f622a..8a9525b4375d 100644
--- a/sys/compat/linprocfs/linprocfs.c
+++ b/sys/compat/linprocfs/linprocfs.c
@@ -80,6 +80,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/vmmeter.h>
 #include <sys/vnode.h>
 #include <sys/bus.h>
+#include <sys/uio.h>
 
 #include <net/if.h>
 #include <net/if_var.h>
@@ -104,6 +105,7 @@ __FBSDID("$FreeBSD$");
 #endif /* __i386__ || __amd64__ */
 
 #include <compat/linux/linux.h>
+#include <compat/linux/linux_emul.h>
 #include <compat/linux/linux_mib.h>
 #include <compat/linux/linux_misc.h>
 #include <compat/linux/linux_util.h>
@@ -1932,6 +1934,32 @@ linprocfs_doauxv(PFS_FILL_ARGS)
 	return (error);
 }
 
+/*
+ * Filler function for proc/self/oom_score_adj
+ */
+static int
+linprocfs_do_oom_score_adj(PFS_FILL_ARGS)
+{
+	struct linux_pemuldata *pem;
+	long oom;
+
+	pem = pem_find(p);
+	if (pem == NULL || uio == NULL)
+		return (EOPNOTSUPP);
+	if (uio->uio_rw == UIO_READ) {
+		sbuf_printf(sb, "%d\n", pem->oom_score_adj);
+	} else {
+		sbuf_trim(sb);
+		sbuf_finish(sb);
+		oom = strtol(sbuf_data(sb), NULL, 10);
+		if (oom < LINUX_OOM_SCORE_ADJ_MIN ||
+		    oom > LINUX_OOM_SCORE_ADJ_MAX)
+			return (EINVAL);
+		pem->oom_score_adj = oom;
+	}
+	return (0);
+}
+
 /*
  * Constructor
  */
@@ -2018,6 +2046,8 @@ linprocfs_init(PFS_INIT_ARGS)
 	    NULL, &procfs_candebug, NULL, PFS_RD|PFS_RAWRD);
 	pfs_create_file(dir, "limits", &linprocfs_doproclimits,
 	    NULL, NULL, NULL, PFS_RD);
+	pfs_create_file(dir, "oom_score_adj", &linprocfs_do_oom_score_adj,
+	    procfs_attr_rw, &procfs_candebug, NULL, PFS_RDWR);
 
 	/* /proc/<pid>/task/... */
 	dir = pfs_create_dir(dir, "task", linprocfs_dotaskattr, NULL, NULL, 0);
diff --git a/sys/compat/linux/linux_emul.c b/sys/compat/linux/linux_emul.c
index eb132d97db4a..12e078546ac6 100644
--- a/sys/compat/linux/linux_emul.c
+++ b/sys/compat/linux/linux_emul.c
@@ -187,6 +187,7 @@ linux_proc_init(struct thread *td, struct thread *newtd, bool init_thread)
 		pem = pem_find(p);
 		KASSERT(pem != NULL, ("proc_exit: proc emuldata not found.\n"));
 		pem->persona = 0;
+		pem->oom_score_adj = 0;
 	}
 
 }
diff --git a/sys/compat/linux/linux_emul.h b/sys/compat/linux/linux_emul.h
index 70646cc93847..4e35da64606f 100644
--- a/sys/compat/linux/linux_emul.h
+++ b/sys/compat/linux/linux_emul.h
@@ -75,6 +75,7 @@ struct linux_pemuldata {
 	struct sx	pem_sx;		/* lock for this struct */
 	uint32_t	persona;	/* process execution domain */
 	uint32_t	ptrace_flags;	/* used by ptrace(2) */
+	uint32_t	oom_score_adj;	/* /proc/self/oom_score_adj */
 };
 
 #define	LINUX_PEM_XLOCK(p)	sx_xlock(&(p)->pem_sx)
diff --git a/sys/compat/linux/linux_misc.h b/sys/compat/linux/linux_misc.h
index a2dd5eb9f82b..0f134fc62e72 100644
--- a/sys/compat/linux/linux_misc.h
+++ b/sys/compat/linux/linux_misc.h
@@ -157,6 +157,10 @@ extern int stclohz;
 /* Linux seccomp flags */
 #define	LINUX_SECCOMP_GET_ACTION_AVAIL	2
 
+/* Linux /proc/self/oom_score_adj */
+#define	LINUX_OOM_SCORE_ADJ_MIN	-1000
+#define	LINUX_OOM_SCORE_ADJ_MAX	1000
+
 #if defined(__aarch64__) || (defined(__amd64__) && !defined(COMPAT_LINUX32))
 int linux_ptrace_status(struct thread *td, int pid, int status);
 #endif