Skip to contentSkip to conversation

Handle reboot in a child pid namespace

PATCH Superseded Daniel Lezcano 3 participants 5 messages Dec 3, 2011 – Dec 4, 2011 Namespaces:

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover Handle reboot in a child pid namespace

ChangeLog:

* V2
- added a lock for the pid namespace to prevent racy call
to the 'reboot' syscall
- Moved 'reboot' command assigned in zap_pid_ns_processes instead of wait_task_zombie
- added tasklist lock around force_sig
- added do_exit in pid_ns_reboot
- used task_active_pid_ns instead of declaring a new variable in sys_reboot
- moved code up before POWER_OFF changed to HALT in sys_reboot

Daniel Lezcano (1):
Add reboot_pid_ns to handle the reboot syscall

 include/linux/pid_namespace.h |    9 ++++++-
 kernel/pid_namespace.c        |   54 +++++++++++++++++++++++++++++++++++++++++
 kernel/sys.c                  |    3 ++
 3 files changed, 65 insertions(+), 1 deletions(-)
Signature · 1 line
1.7.5.4
#1 lore raw

PATCH v2 1/1 Add reboot_pid_ns to handle the reboot syscall

In the case of a child pid namespace, rebooting the system does not really makes sense. When the pid namespace is used in conjunction with the other namespaces in order to create a linux container, the reboot syscall leads to some problems.

A container can reboot the host. That can be fixed by dropping the sys_reboot capability but we are unable to correctly to poweroff/ halt/reboot a container and the container stays stuck at the shutdown time with the container's init process waiting indefinitively.

After several attempts, no solution from userspace was found to reliabily handle the shutdown from a container.

This patch propose to store the reboot value in the 16 upper bits of the exit code from the processes belonging to a pid namespace which has rebooted. When the reboot syscall is called and we are not in the initial pid namespace, we kill the pid namespace.

By this way the parent process of the child pid namespace to know if it rebooted or not and take the right decision.

---
 include/linux/pid_namespace.h |    9 ++++++-
 kernel/pid_namespace.c        |   54 +++++++++++++++++++++++++++++++++++++++++
 kernel/sys.c                  |    3 ++
 3 files changed, 65 insertions(+), 1 deletions(-)
include/linux/pid_namespace.h +8 -1
index e7cf666..f5f1f60 100644
@@ -32,6 +32,8 @@ struct pid_namespace {
 #endif
 	gid_t pid_gid;
 	int hide_pid;
+	int reboot;
+	spinlock_t reboot_lock;
 };
 
 extern struct pid_namespace init_pid_ns;
@@ -47,6 +49,7 @@ static inline struct pid_namespace *get_pid_ns(struct pid_namespace *ns)
 extern struct pid_namespace *copy_pid_ns(unsigned long flags, struct pid_namespace *ns);
 extern void free_pid_ns(struct kref *kref);
 extern void zap_pid_ns_processes(struct pid_namespace *pid_ns);
+extern int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd);
 
 static inline void put_pid_ns(struct pid_namespace *ns)
 {
@@ -74,11 +77,15 @@ static inline void put_pid_ns(struct pid_namespace *ns)
 {
 }
 
-
 static inline void zap_pid_ns_processes(struct pid_namespace *ns)
 {
 	BUG();
 }
+
+static inline int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
+{
+	BUG();
+}
 #endif /* CONFIG_PID_NS */
 
 extern struct pid_namespace *task_active_pid_ns(struct task_struct *tsk);
kernel/pid_namespace.c +54 -0

78 lines not on this page · raw message

kernel/sys.c +3 -0
index ddf8155..31acf63 100644
@@ -444,6 +444,9 @@ SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
 	                magic2 != LINUX_REBOOT_MAGIC2C))
 		return -EINVAL;
 
+	if (task_active_pid_ns(current) != &init_pid_ns)
+		return reboot_pid_ns(task_active_pid_ns(current), cmd);
+
 	/* Instead of trying to make the power_off code look like
 	 * halt when pm_power_off is not set do it the easy way.
 	 */
Signature · 1 line
1.7.5.4
#2 lore raw

Re: [PATCH 1/1][v2] Add reboot_pid_ns to handle the reboot syscall

include/linux/pid_namespace.h struct pid_namespace { · line 37

Quoted 8 lines · Daniel Lezcano
include/linux/pid_namespace.h +2 -0
@@ -32,6 +32,8 @@ struct pid_namespace {
 #endif
 	gid_t pid_gid;
 	int hide_pid;
+	int reboot;
+	spinlock_t reboot_lock;
 };

Well. I was thinking about the serialization too, but this ->reboot_lock asks for v3 imho ;)

First of all, do we really care? force_sig(SIGKILL, child_reaper) can't race with itself, it does nothing if init is already killed.

So why it is important to protect pid_ns->reboot? Yes, it is possible to change it again if two callers do sys_reboot() "at the same time". But in this case we can't predict which caller wins anyway, so why should we worry?

IOW. Say, we have the 2 tasks doing HALT and RESTART in parallel. It is possible that HALT sets ->reboot and kills init first, then RESTART changes ->reboot and the second force_sig() does nothing. In this case we can simply pretend that RESTART wins and the dying init kills HALT before it calls sys_reboot().

In any case. Even if you want to serialize, instead of adding the new lock reboot_pid_ns() can simply do:

	if (cmpxchg(&pid_ns->reboot, 0, reboot) != 0)
		return -EBUSY;

this looks much simpler to me.

Oleg.

#4 lore raw

Re: [PATCH 1/1][v2] Add reboot_pid_ns to handle the reboot syscall

In reply to Daniel Lezcano

In article <[email protected]> you write:

Quoted 4 lines

This patch propose to store the reboot value in the 16 upper bits of the exit code from the processes belonging to a pid namespace which has rebooted. When the reboot syscall is called and we are not in the initial pid namespace, we kill the pid namespace.

Should there be a way to find out if the reboot systemcall is containerized or not (other than comparing kernel versions once this is mainlined) ?

For example, a tool like lxc-start might want to disable CAP_SYS_REBOOT if the reboot system call is not containerized.

Perhaps a LINUX_REBOOT_CMD_ISCONTAINER (or ISVIRTUAL, whatever, bikeshed away) should be added that simply returns 0 if sys_reboot is containerized. It would return -EINVAL as it does today if it's not.

Thoughts ?

Mike.

#3 lore raw

Re: [PATCH 1/1][v2] Add reboot_pid_ns to handle the reboot syscall

In reply to Oleg Nesterov
Quoted 19 lines · Oleg Nesterov

include/linux/pid_namespace.h struct pid_namespace { · line 37

Quoted 7 lines · Daniel Lezcano
include/linux/pid_namespace.h +2 -0
@@ -32,6 +32,8 @@ struct pid_namespace {
 #endif
 	gid_t pid_gid;
 	int hide_pid;
+	int reboot;
+	spinlock_t reboot_lock;
 };

Well. I was thinking about the serialization too, but this ->reboot_lock asks for v3 imho ;)

First of all, do we really care? force_sig(SIGKILL, child_reaper) can't race with itself, it does nothing if init is already killed.

So why it is important to protect pid_ns->reboot? Yes, it is possible to change it again if two callers do sys_reboot() "at the same time". But in this case we can't predict which caller wins anyway, so why should we worry?

Quoted 5 lines

IOW. Say, we have the 2 tasks doing HALT and RESTART in parallel. It is possible that HALT sets ->reboot and kills init first, then RESTART changes ->reboot and the second force_sig() does nothing. In this case we can simply pretend that RESTART wins and the dying init kills HALT before it calls sys_reboot().

In the case of racy access, your argument makes sense but it is also to prevent multiple calls to 'reboot'. In the init_pid_ns, when a shutdown is on the way, the lock will prevent another task to invoke a machine restart. But anyway, we can get ride of this lock and the serialization, it is a nit we can fix later if that makes sense with the couple of lines you specified below.

Quoted 7 lines

In any case. Even if you want to serialize, instead of adding the new lock reboot_pid_ns() can simply do:

	if (cmpxchg(&pid_ns->reboot, 0, reboot) != 0)
		return -EBUSY;

this looks much simpler to me.

Yes, definitively :)

Thanks
-- Daniel