nsfs: add pid translation ioctls
Christian Brauner
maintainerPATCH 1/1 nsfs: add pid translation ioctls
Add ioctl()s to translate pids between pid namespaces.
LXCFS is a tiny fuse filesystem used to virtualize various aspects of procfs. LXCFS is run on the host. The files and directories it creates can be bind-mounted by e.g. a container at startup and mounted over the various procfs files the container wishes to have virtualized. When e.g. a read request for uptime is received, LXCFS will receive the pid of the reader. In order to virtualize the corresponding read, LXCFS needs to know the pid of the init process of the reader's pid namespace. In order to do this, LXCFS first needs to fork() two helper processes. The first helper process setns() to the readers pid namespace. The second helper process is needed to create a process that is a proper member of the pid namespace. The second helper process then creates a ucred message with ucred.pid set to 1 and sends it back to LXCFS. The kernel will translate the ucred.pid field to the corresponding pid number in LXCFS's pid namespace. This way LXCFS can learn the init pid number of the reader's pid namespace and can go on to virtualize. Since these two forks() are costly LXCFS maintains an init pid cache that caches a given pid for a fixed amount of time. The cache is pruned during new read requests. However, even with the cache the hit of the two forks() is singificant when a very large number of containers are running. With this simple patch we add an ns ioctl that let's a caller retrieve the init pid nr of a pid namespace through its pid namespace fd. This significantly improves performance with a very simple change.
Support translation of pids and tgids. Other concepts can be added but there are no obvious users for this right now.
To protect against races pidfds can be used to check whether the process is still valid. If needed, this can also be extended to work on pidfds directly.
---
--- fs/nsfs.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/nsfs.h | 8 ++++++++ 2 files changed, 55 insertions(+)
fs/nsfs.c +47 -0
@@ -8,9 +8,11 @@#include <linux/magic.h> #include <linux/ktime.h> #include <linux/seq_file.h> +#include <linux/pid_namespace.h> #include <linux/user_namespace.h> #include <linux/nsfs.h> #include <linux/uaccess.h> +#include <linux/cleanup.h> #include "internal.h"
@@ -123,9 +125,12 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) {struct user_namespace *user_ns; +struct pid_namespace *pid_ns; +struct task_struct *tsk;struct ns_common *ns =get_proc_ns (file_inode (filp));uid_t __user *argp;uid_t uid; +pid_t pid_nr;switch (ioctl) {case NS_GET_USERNS :
@@ -143,6 +148,48 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl, argp = (uid_t __user *) arg; uid =from_kuid_munged (current_user_ns (), user_ns->owner);return put_user (uid, argp); +case NS_GET_PID_FROM_PIDNS : + fallthrough; +case NS_GET_TGID_FROM_PIDNS : + fallthrough; +case NS_GET_PID_IN_PIDNS : + fallthrough; +case NS_GET_TGID_IN_PIDNS : +if (ns->ops->type !=CLONE_NEWPID ) +return -EINVAL ; + + pid_ns = container_of(ns, struct pid_namespace, ns); + +guard (rcu)(); +if (ioctl ==NS_GET_PID_IN_PIDNS || + ioctl ==NS_GET_TGID_IN_PIDNS ) + tsk =find_task_by_vpid (arg); +else + tsk =find_task_by_pid_ns (arg, pid_ns); +if (!tsk) +return -ESRCH ; + +switch (ioctl) { +case NS_GET_PID_FROM_PIDNS : + pid_nr =task_pid_vnr (tsk); +break ; +case NS_GET_TGID_FROM_PIDNS : + pid_nr =task_tgid_vnr (tsk); +break ; +case NS_GET_PID_IN_PIDNS : + pid_nr =task_pid_nr_ns (tsk, pid_ns); +break ; +case NS_GET_TGID_IN_PIDNS : + pid_nr =task_tgid_nr_ns (tsk, pid_ns); +break ; +default : + pid_nr =0 ; +break ; + } +if (!pid_nr) +return -ESRCH ; + +return pid_nr;default :return -ENOTTY ; }
include/uapi/linux/nsfs.h +8 -0
@@ -15,5 +15,13 @@#define NS_GET_NSTYPE _IO(NSIO, 0x3)/* Get owner UID (in the caller's user namespace) for a user namespace */ #define NS_GET_OWNER_UID _IO(NSIO, 0x4) +/* Translate pid from target pid namespace into the caller's pid namespace. */ +#define NS_GET_PID_FROM_PIDNS _IOR(NSIO, 0x5, int) +/* Return thread-group leader id of pid in the callers pid namespace. */ +#define NS_GET_TGID_FROM_PIDNS _IOR(NSIO, 0x7, int) +/* Translate pid from caller's pid namespace into a target pid namespace. */ +#define NS_GET_PID_IN_PIDNS _IOR(NSIO, 0x6, int) +/* Return thread-group leader id of pid in the target pid namespace. */ +#define NS_GET_TGID_IN_PIDNS _IOR(NSIO, 0x8, int)#endif /* __LINUX_NSFS_H */
---
base-commit: 1613e604df0cd359cf2a7fbd9be7a0bcfacfabd0
change-id: 20240619-work-ns_ioctl-447979cf0820
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Christian,
kernel test robot noticed the following build errors:
[auto build test ERROR on 1613e604df0cd359cf2a7fbd9be7a0bcfacfabd0]
url: https://github.com/intel-lab-lkp/linux/commits/Christian-Brauner/nsfs-add-pid-translation-ioctls/20240619-215147 base: 1613e604df0cd359cf2a7fbd9be7a0bcfacfabd0 patch link: https://lore.kernel.org/r/20240619-work-ns_ioctl-v1-1-7c0097e6bb6b%40kernel.org patch subject: [PATCH] nsfs: add pid translation ioctls config: i386-buildonly-randconfig-002-20240620 (https://download.01.org/0day-ci/archive/20240620/[email protected]/config) compiler: clang version 18.1.5 (https://github.com/llvm/llvm-project 617a15a9eac96088ae5e9134248d8236e34b91b1) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240620/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> fs/nsfs.c:193:2: error: cannot jump from switch statement to this case label
193 | default:
| ^
fs/nsfs.c:163:3: note: jump bypasses initialization of variable with __attribute__((cleanup))
163 | guard(rcu)();
| ^
include/linux/cleanup.h:164:15: note: expanded from macro 'guard'
164 | CLASS(_name, __UNIQUE_ID(guard))
| ^
include/linux/compiler.h:189:29: note: expanded from macro '__UNIQUE_ID'
189 | #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
| ^
include/linux/compiler_types.h:84:22: note: expanded from macro '__PASTE'
84 | #define __PASTE(a,b) ___PASTE(a,b)
| ^
include/linux/compiler_types.h:83:23: note: expanded from macro '___PASTE'
83 | #define ___PASTE(a,b) a##b
| ^
<scratch space>:95:1: note: expanded from here
95 | __UNIQUE_ID_guard305
| ^1 error generated.
vim +193 fs/nsfs.c
6786741dbf99e4 Andrey Vagin 2016-09-06 123
6786741dbf99e4 Andrey Vagin 2016-09-06 124 static long ns_ioctl(struct file *filp, unsigned int ioctl,
6786741dbf99e4 Andrey Vagin 2016-09-06 125 unsigned long arg)
6786741dbf99e4 Andrey Vagin 2016-09-06 126 {
d95fa3c76a66b6 Michael Kerrisk (man-pages 2017-01-25 127) struct user_namespace *user_ns;
db2704dabb53ec Christian Brauner 2024-06-19 128 struct pid_namespace *pid_ns;
db2704dabb53ec Christian Brauner 2024-06-19 129 struct task_struct *tsk;
6786741dbf99e4 Andrey Vagin 2016-09-06 130 struct ns_common *ns = get_proc_ns(file_inode(filp));
d95fa3c76a66b6 Michael Kerrisk (man-pages 2017-01-25 131) uid_t __user *argp;
d95fa3c76a66b6 Michael Kerrisk (man-pages 2017-01-25 132) uid_t uid;
db2704dabb53ec Christian Brauner 2024-06-19 133 pid_t pid_nr;
6786741dbf99e4 Andrey Vagin 2016-09-06 134
6786741dbf99e4 Andrey Vagin 2016-09-06 135 switch (ioctl) {
6786741dbf99e4 Andrey Vagin 2016-09-06 136 case NS_GET_USERNS:
6786741dbf99e4 Andrey Vagin 2016-09-06 137 return open_related_ns(ns, ns_get_owner);
a7306ed8d94af7 Andrey Vagin 2016-09-06 138 case NS_GET_PARENT:
a7306ed8d94af7 Andrey Vagin 2016-09-06 139 if (!ns->ops->get_parent)
a7306ed8d94af7 Andrey Vagin 2016-09-06 140 return -EINVAL;
a7306ed8d94af7 Andrey Vagin 2016-09-06 141 return open_related_ns(ns, ns->ops->get_parent);
e5ff5ce6e20ee2 Michael Kerrisk (man-pages 2017-01-25 142) case NS_GET_NSTYPE:
e5ff5ce6e20ee2 Michael Kerrisk (man-pages 2017-01-25 143) return ns->ops->type;
d95fa3c76a66b6 Michael Kerrisk (man-pages 2017-01-25 144) case NS_GET_OWNER_UID:
d95fa3c76a66b6 Michael Kerrisk (man-pages 2017-01-25 145) if (ns->ops->type != CLONE_NEWUSER)
d95fa3c76a66b6 Michael Kerrisk (man-pages 2017-01-25 146) return -EINVAL;
d95fa3c76a66b6 Michael Kerrisk (man-pages 2017-01-25 147) user_ns = container_of(ns, struct user_namespace, ns);
d95fa3c76a66b6 Michael Kerrisk (man-pages 2017-01-25 148) argp = (uid_t __user *) arg;
d95fa3c76a66b6 Michael Kerrisk (man-pages 2017-01-25 149) uid = from_kuid_munged(current_user_ns(), user_ns->owner);
d95fa3c76a66b6 Michael Kerrisk (man-pages 2017-01-25 150) return put_user(uid, argp);
db2704dabb53ec Christian Brauner 2024-06-19 151 case NS_GET_PID_FROM_PIDNS:
db2704dabb53ec Christian Brauner 2024-06-19 152 fallthrough;
db2704dabb53ec Christian Brauner 2024-06-19 153 case NS_GET_TGID_FROM_PIDNS:
db2704dabb53ec Christian Brauner 2024-06-19 154 fallthrough;
db2704dabb53ec Christian Brauner 2024-06-19 155 case NS_GET_PID_IN_PIDNS:
db2704dabb53ec Christian Brauner 2024-06-19 156 fallthrough;
db2704dabb53ec Christian Brauner 2024-06-19 157 case NS_GET_TGID_IN_PIDNS:
db2704dabb53ec Christian Brauner 2024-06-19 158 if (ns->ops->type != CLONE_NEWPID)
db2704dabb53ec Christian Brauner 2024-06-19 159 return -EINVAL;
db2704dabb53ec Christian Brauner 2024-06-19 160
db2704dabb53ec Christian Brauner 2024-06-19 161 pid_ns = container_of(ns, struct pid_namespace, ns);
db2704dabb53ec Christian Brauner 2024-06-19 162
db2704dabb53ec Christian Brauner 2024-06-19 163 guard(rcu)();
db2704dabb53ec Christian Brauner 2024-06-19 164 if (ioctl == NS_GET_PID_IN_PIDNS ||
db2704dabb53ec Christian Brauner 2024-06-19 165 ioctl == NS_GET_TGID_IN_PIDNS)
db2704dabb53ec Christian Brauner 2024-06-19 166 tsk = find_task_by_vpid(arg);
db2704dabb53ec Christian Brauner 2024-06-19 167 else
db2704dabb53ec Christian Brauner 2024-06-19 168 tsk = find_task_by_pid_ns(arg, pid_ns);
db2704dabb53ec Christian Brauner 2024-06-19 169 if (!tsk)
db2704dabb53ec Christian Brauner 2024-06-19 170 return -ESRCH;
db2704dabb53ec Christian Brauner 2024-06-19 171
db2704dabb53ec Christian Brauner 2024-06-19 172 switch (ioctl) {
db2704dabb53ec Christian Brauner 2024-06-19 173 case NS_GET_PID_FROM_PIDNS:
db2704dabb53ec Christian Brauner 2024-06-19 174 pid_nr = task_pid_vnr(tsk);
db2704dabb53ec Christian Brauner 2024-06-19 175 break;
db2704dabb53ec Christian Brauner 2024-06-19 176 case NS_GET_TGID_FROM_PIDNS:
db2704dabb53ec Christian Brauner 2024-06-19 177 pid_nr = task_tgid_vnr(tsk);
db2704dabb53ec Christian Brauner 2024-06-19 178 break;
db2704dabb53ec Christian Brauner 2024-06-19 179 case NS_GET_PID_IN_PIDNS:
db2704dabb53ec Christian Brauner 2024-06-19 180 pid_nr = task_pid_nr_ns(tsk, pid_ns);
db2704dabb53ec Christian Brauner 2024-06-19 181 break;
db2704dabb53ec Christian Brauner 2024-06-19 182 case NS_GET_TGID_IN_PIDNS:
db2704dabb53ec Christian Brauner 2024-06-19 183 pid_nr = task_tgid_nr_ns(tsk, pid_ns);
db2704dabb53ec Christian Brauner 2024-06-19 184 break;
db2704dabb53ec Christian Brauner 2024-06-19 185 default:
db2704dabb53ec Christian Brauner 2024-06-19 186 pid_nr = 0;
db2704dabb53ec Christian Brauner 2024-06-19 187 break;
db2704dabb53ec Christian Brauner 2024-06-19 188 }
db2704dabb53ec Christian Brauner 2024-06-19 189 if (!pid_nr)
db2704dabb53ec Christian Brauner 2024-06-19 190 return -ESRCH;
db2704dabb53ec Christian Brauner 2024-06-19 191
db2704dabb53ec Christian Brauner 2024-06-19 192 return pid_nr;
6786741dbf99e4 Andrey Vagin 2016-09-06 @193 default:
6786741dbf99e4 Andrey Vagin 2016-09-06 194 return -ENOTTY;
6786741dbf99e4 Andrey Vagin 2016-09-06 195 }
6786741dbf99e4 Andrey Vagin 2016-09-06 196 }
6786741dbf99e4 Andrey Vagin 2016-09-06 197 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Am Mi., 19. Juni 2024 um 15:50 Uhr schrieb Christian Brauner <[email protected]>:
Quoted 26 lines
Add ioctl()s to translate pids between pid namespaces.
LXCFS is a tiny fuse filesystem used to virtualize various aspects of procfs. LXCFS is run on the host. The files and directories it creates can be bind-mounted by e.g. a container at startup and mounted over the various procfs files the container wishes to have virtualized. When e.g. a read request for uptime is received, LXCFS will receive the pid of the reader. In order to virtualize the corresponding read, LXCFS needs to know the pid of the init process of the reader's pid namespace. In order to do this, LXCFS first needs to
fork()two helper processes. The first helper processsetns()to the readers pid namespace. The second helper process is needed to create a process that is a proper member of the pid namespace. The second helper process then creates a ucred message with ucred.pid set to 1 and sends it back to LXCFS. The kernel will translate the ucred.pid field to the corresponding pid number in LXCFS's pid namespace. This way LXCFS can learn the init pid number of the reader's pid namespace and can go on to virtualize. Since these twoforks()are costly LXCFS maintains an init pid cache that caches a given pid for a fixed amount of time. The cache is pruned during new read requests. However, even with the cache the hit of the twoforks()is singificant when a very large number of containers are running. With this simple patch we add an ns ioctl that let's a caller retrieve the init pid nr of a pid namespace through its pid namespace fd. This significantly improves performance with a very simple change.
Quoted 8 lines
Support translation of pids and tgids. Other concepts can be added but there are no obvious users for this right now.
To protect against races pidfds can be used to check whether the process is still valid. If needed, this can also be extended to work on pidfds directly.
Signed-off-by Christian Brauner <[email protected]>
Dear Christian,
This is an amazing idea! Thanks for implementing and posting this!
Quoted 107 lines
---
--- fs/nsfs.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/nsfs.h | 8 ++++++++ 2 files changed, 55 insertions(+)fs/nsfs.c +47
-0@@ -8,9 +8,11 @@#include <linux/magic.h> #include <linux/ktime.h> #include <linux/seq_file.h> +#include <linux/pid_namespace.h> #include <linux/user_namespace.h> #include <linux/nsfs.h> #include <linux/uaccess.h> +#include <linux/cleanup.h> #include "internal.h" @@ -123,9 +125,12 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) {struct user_namespace *user_ns; +struct pid_namespace *pid_ns; +struct task_struct *tsk;struct ns_common *ns =get_proc_ns (file_inode (filp));uid_t __user *argp;uid_t uid; +pid_t pid_nr;switch (ioctl) {case NS_GET_USERNS :@@ -143,6 +148,48 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl, argp = (uid_t __user *) arg; uid =from_kuid_munged (current_user_ns (), user_ns->owner);return put_user (uid, argp); +case NS_GET_PID_FROM_PIDNS : + fallthrough; +case NS_GET_TGID_FROM_PIDNS : + fallthrough; +case NS_GET_PID_IN_PIDNS : + fallthrough; +case NS_GET_TGID_IN_PIDNS : +if (ns->ops->type !=CLONE_NEWPID ) +return -EINVAL ; + + pid_ns = container_of(ns, struct pid_namespace, ns); + +guard (rcu)(); +if (ioctl ==NS_GET_PID_IN_PIDNS || + ioctl ==NS_GET_TGID_IN_PIDNS ) + tsk =find_task_by_vpid (arg); +else + tsk =find_task_by_pid_ns (arg, pid_ns); +if (!tsk) +return -ESRCH ; + +switch (ioctl) { +case NS_GET_PID_FROM_PIDNS : + pid_nr =task_pid_vnr (tsk); +break ; +case NS_GET_TGID_FROM_PIDNS : + pid_nr =task_tgid_vnr (tsk); +break ; +case NS_GET_PID_IN_PIDNS : + pid_nr =task_pid_nr_ns (tsk, pid_ns); +break ; +case NS_GET_TGID_IN_PIDNS : + pid_nr =task_tgid_nr_ns (tsk, pid_ns); +break ; +default : + pid_nr =0 ; +break ; + } +if (!pid_nr) +return -ESRCH ; + +return pid_nr;default :return -ENOTTY ; }include/uapi/linux/nsfs.h +8
-1@@ -15,5 +15,13 @@#define NS_GET_NSTYPE _IO(NSIO, 0x3)/* Get owner UID (in the caller's user namespace) for a user namespace */ #define NS_GET_OWNER_UID _IO(NSIO, 0x4) +/* Translate pid from target pid namespace into the caller's pid namespace. */ +#define NS_GET_PID_FROM_PIDNS _IOR(NSIO, 0x5, int) +/* Return thread-group leader id of pid in the callers pid namespace. */ +#define NS_GET_TGID_FROM_PIDNS _IOR(NSIO, 0x7, int) +/* Translate pid from caller's pid namespace into a target pid namespace. */ +#define NS_GET_PID_IN_PIDNS _IOR(NSIO, 0x6, int) +/* Return thread-group leader id of pid in the target pid namespace. */ +#define NS_GET_TGID_IN_PIDNS _IOR(NSIO, 0x8, int)#endif /* __LINUX_NSFS_H */ ---base-commit:
1613e604df0cd359cf2a7fbd9be7a0bcfacfabd0
change-id: 20240619-work-ns_ioctl-447979cf0820