Commit f400e198b2ed26ce55b22a1412ded0896e7516ac

Authored by Sukadev Bhattiprolu
Committed by Linus Torvalds
1 parent 959ed340f4

[PATCH] pidspace: is_init()

This is an updated version of Eric Biederman's is_init() patch.
(http://lkml.org/lkml/2006/2/6/280).  It applies cleanly to 2.6.18-rc3 and
replaces a few more instances of ->pid == 1 with is_init().

Further, is_init() checks pid and thus removes dependency on Eric's other
patches for now.

Eric's original description:

	There are a lot of places in the kernel where we test for init
	because we give it special properties.  Most  significantly init
	must not die.  This results in code all over the kernel test
	->pid == 1.

	Introduce is_init to capture this case.

	With multiple pid spaces for all of the cases affected we are
	looking for only the first process on the system, not some other
	process that has pid == 1.

Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Signed-off-by: Sukadev Bhattiprolu <sukadev@us.ibm.com>
Cc: Dave Hansen <haveblue@us.ibm.com>
Cc: Serge Hallyn <serue@us.ibm.com>
Cc: Cedric Le Goater <clg@fr.ibm.com>
Cc: <lxc-devel@lists.sourceforge.net>
Acked-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>

Showing 29 changed files with 41 additions and 30 deletions Side-by-side Diff

arch/alpha/mm/fault.c
... ... @@ -193,7 +193,7 @@
193 193 /* We ran out of memory, or some other thing happened to us that
194 194 made us unable to handle the page fault gracefully. */
195 195 out_of_memory:
196   - if (current->pid == 1) {
  196 + if (is_init(current)) {
197 197 yield();
198 198 down_read(&mm->mmap_sem);
199 199 goto survive;
... ... @@ -198,7 +198,7 @@
198 198 return fault;
199 199 }
200 200  
201   - if (tsk->pid != 1)
  201 + if (!is_init(tsk))
202 202 goto out;
203 203  
204 204 /*
arch/arm26/mm/fault.c
... ... @@ -185,7 +185,7 @@
185 185 }
186 186  
187 187 fault = -3; /* out of memory */
188   - if (tsk->pid != 1)
  188 + if (!is_init(tsk))
189 189 goto out;
190 190  
191 191 /*
arch/i386/lib/usercopy.c
... ... @@ -739,7 +739,7 @@
739 739 retval = get_user_pages(current, current->mm,
740 740 (unsigned long )to, 1, 1, 0, &pg, NULL);
741 741  
742   - if (retval == -ENOMEM && current->pid == 1) {
  742 + if (retval == -ENOMEM && is_init(current)) {
743 743 up_read(&current->mm->mmap_sem);
744 744 blk_congestion_wait(WRITE, HZ/50);
745 745 goto survive;
arch/i386/mm/fault.c
... ... @@ -589,7 +589,7 @@
589 589 */
590 590 out_of_memory:
591 591 up_read(&mm->mmap_sem);
592   - if (tsk->pid == 1) {
  592 + if (is_init(tsk)) {
593 593 yield();
594 594 down_read(&mm->mmap_sem);
595 595 goto survive;
arch/ia64/mm/fault.c
... ... @@ -280,7 +280,7 @@
280 280  
281 281 out_of_memory:
282 282 up_read(&mm->mmap_sem);
283   - if (current->pid == 1) {
  283 + if (is_init(current)) {
284 284 yield();
285 285 down_read(&mm->mmap_sem);
286 286 goto survive;
arch/m32r/mm/fault.c
... ... @@ -299,7 +299,7 @@
299 299 */
300 300 out_of_memory:
301 301 up_read(&mm->mmap_sem);
302   - if (tsk->pid == 1) {
  302 + if (is_init(tsk)) {
303 303 yield();
304 304 down_read(&mm->mmap_sem);
305 305 goto survive;
arch/m68k/mm/fault.c
... ... @@ -181,7 +181,7 @@
181 181 */
182 182 out_of_memory:
183 183 up_read(&mm->mmap_sem);
184   - if (current->pid == 1) {
  184 + if (is_init(current)) {
185 185 yield();
186 186 down_read(&mm->mmap_sem);
187 187 goto survive;
arch/mips/mm/fault.c
... ... @@ -171,7 +171,7 @@
171 171 */
172 172 out_of_memory:
173 173 up_read(&mm->mmap_sem);
174   - if (tsk->pid == 1) {
  174 + if (is_init(tsk)) {
175 175 yield();
176 176 down_read(&mm->mmap_sem);
177 177 goto survive;
arch/powerpc/mm/fault.c
... ... @@ -386,7 +386,7 @@
386 386 */
387 387 out_of_memory:
388 388 up_read(&mm->mmap_sem);
389   - if (current->pid == 1) {
  389 + if (is_init(current)) {
390 390 yield();
391 391 down_read(&mm->mmap_sem);
392 392 goto survive;
arch/powerpc/platforms/pseries/ras.c
... ... @@ -337,7 +337,7 @@
337 337 err->disposition == RTAS_DISP_NOT_RECOVERED &&
338 338 err->target == RTAS_TARGET_MEMORY &&
339 339 err->type == RTAS_TYPE_ECC_UNCORR &&
340   - !(current->pid == 0 || current->pid == 1)) {
  340 + !(current->pid == 0 || is_init(current))) {
341 341 /* Kill off a user process with an ECC error */
342 342 printk(KERN_ERR "MCE: uncorrectable ecc error for pid %d\n",
343 343 current->pid);
arch/ppc/kernel/traps.c
... ... @@ -119,7 +119,7 @@
119 119 * generate the same exception over and over again and we get
120 120 * nowhere. Better to kill it and let the kernel panic.
121 121 */
122   - if (current->pid == 1) {
  122 + if (is_init(current)) {
123 123 __sighandler_t handler;
124 124  
125 125 spin_lock_irq(&current->sighand->siglock);
... ... @@ -291,7 +291,7 @@
291 291 */
292 292 out_of_memory:
293 293 up_read(&mm->mmap_sem);
294   - if (current->pid == 1) {
  294 + if (is_init(current)) {
295 295 yield();
296 296 down_read(&mm->mmap_sem);
297 297 goto survive;
arch/s390/mm/fault.c
... ... @@ -353,7 +353,7 @@
353 353 */
354 354 out_of_memory:
355 355 up_read(&mm->mmap_sem);
356   - if (tsk->pid == 1) {
  356 + if (is_init(tsk)) {
357 357 yield();
358 358 down_read(&mm->mmap_sem);
359 359 goto survive;
... ... @@ -149,7 +149,7 @@
149 149 */
150 150 out_of_memory:
151 151 up_read(&mm->mmap_sem);
152   - if (current->pid == 1) {
  152 + if (is_init(current)) {
153 153 yield();
154 154 down_read(&mm->mmap_sem);
155 155 goto survive;
arch/sh64/mm/fault.c
... ... @@ -277,7 +277,7 @@
277 277 show_regs(regs);
278 278 #endif
279 279 }
280   - if (tsk->pid == 1) {
  280 + if (is_init(tsk)) {
281 281 panic("INIT had user mode bad_area\n");
282 282 }
283 283 tsk->thread.address = address;
284 284  
... ... @@ -319,14 +319,14 @@
319 319 * us unable to handle the page fault gracefully.
320 320 */
321 321 out_of_memory:
322   - if (current->pid == 1) {
  322 + if (is_init(current)) {
323 323 panic("INIT out of memory\n");
324 324 yield();
325 325 goto survive;
326 326 }
327 327 printk("fault:Out of memory\n");
328 328 up_read(&mm->mmap_sem);
329   - if (current->pid == 1) {
  329 + if (is_init(current)) {
330 330 yield();
331 331 down_read(&mm->mmap_sem);
332 332 goto survive;
arch/um/kernel/trap.c
... ... @@ -120,7 +120,7 @@
120 120 * us unable to handle the page fault gracefully.
121 121 */
122 122 out_of_memory:
123   - if (current->pid == 1) {
  123 + if (is_init(current)) {
124 124 up_read(&mm->mmap_sem);
125 125 yield();
126 126 down_read(&mm->mmap_sem);
arch/x86_64/mm/fault.c
... ... @@ -244,7 +244,7 @@
244 244  
245 245 int unhandled_signal(struct task_struct *tsk, int sig)
246 246 {
247   - if (tsk->pid == 1)
  247 + if (is_init(tsk))
248 248 return 1;
249 249 if (tsk->ptrace & PT_PTRACED)
250 250 return 0;
... ... @@ -580,7 +580,7 @@
580 580 */
581 581 out_of_memory:
582 582 up_read(&mm->mmap_sem);
583   - if (current->pid == 1) {
  583 + if (is_init(current)) {
584 584 yield();
585 585 goto again;
586 586 }
arch/xtensa/mm/fault.c
... ... @@ -144,7 +144,7 @@
144 144 */
145 145 out_of_memory:
146 146 up_read(&mm->mmap_sem);
147   - if (current->pid == 1) {
  147 + if (is_init(current)) {
148 148 yield();
149 149 down_read(&mm->mmap_sem);
150 150 goto survive;
drivers/char/sysrq.c
... ... @@ -208,7 +208,7 @@
208 208 struct task_struct *p;
209 209  
210 210 for_each_process(p) {
211   - if (p->mm && p->pid != 1)
  211 + if (p->mm && !is_init(p))
212 212 /* Not swapper, init nor kernel thread */
213 213 force_sig(sig, p);
214 214 }
include/linux/sched.h
... ... @@ -1033,6 +1033,16 @@
1033 1033 return p->pids[PIDTYPE_PID].pid != NULL;
1034 1034 }
1035 1035  
  1036 +/**
  1037 + * is_init - check if a task structure is the first user space
  1038 + * task the kernel created.
  1039 + * @p: Task structure to be checked.
  1040 + */
  1041 +static inline int is_init(struct task_struct *tsk)
  1042 +{
  1043 + return tsk->pid == 1;
  1044 +}
  1045 +
1036 1046 extern void free_task(struct task_struct *tsk);
1037 1047 #define get_task_struct(tsk) do { atomic_inc(&(tsk)->usage); } while(0)
1038 1048  
... ... @@ -133,7 +133,7 @@
133 133 int found = 0;
134 134  
135 135 do_each_thread(g, target) {
136   - if (target == current || target->pid == 1)
  136 + if (target == current || is_init(target))
137 137 continue;
138 138 found = 1;
139 139 if (security_capset_check(target, effective, inheritable,
... ... @@ -240,7 +240,7 @@
240 240 * A cpuset can only be deleted if both its 'count' of using tasks
241 241 * is zero, and its list of 'children' cpusets is empty. Since all
242 242 * tasks in the system use _some_ cpuset, and since there is always at
243   - * least one task in the system (init, pid == 1), therefore, top_cpuset
  243 + * least one task in the system (init), therefore, top_cpuset
244 244 * always has either children cpusets and/or using tasks. So we don't
245 245 * need a special hack to ensure that top_cpuset cannot be deleted.
246 246 *
... ... @@ -219,7 +219,7 @@
219 219 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
220 220 if (p == ignored_task
221 221 || p->exit_state
222   - || p->real_parent->pid == 1)
  222 + || is_init(p->real_parent))
223 223 continue;
224 224 if (process_group(p->real_parent) != pgrp
225 225 && p->real_parent->signal->session == p->signal->session) {
... ... @@ -40,7 +40,7 @@
40 40  
41 41 int kexec_should_crash(struct task_struct *p)
42 42 {
43   - if (in_interrupt() || !p->pid || p->pid == 1 || panic_on_oops)
  43 + if (in_interrupt() || !p->pid || is_init(p) || panic_on_oops)
44 44 return 1;
45 45 return 0;
46 46 }
... ... @@ -440,6 +440,7 @@
440 440 child = find_task_by_pid(pid);
441 441 if (child)
442 442 get_task_struct(child);
  443 +
443 444 read_unlock(&tasklist_lock);
444 445 if (!child)
445 446 return ERR_PTR(-ESRCH);
... ... @@ -1915,7 +1915,7 @@
1915 1915 return -EPERM;
1916 1916 }
1917 1917  
1918   - op = (current->pid == 1) ? OP_SET : OP_AND;
  1918 + op = is_init(current) ? OP_SET : OP_AND;
1919 1919 return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
1920 1920 do_proc_dointvec_bset_conv,&op);
1921 1921 }
... ... @@ -255,7 +255,7 @@
255 255 */
256 256 static void __oom_kill_task(struct task_struct *p, const char *message)
257 257 {
258   - if (p->pid == 1) {
  258 + if (is_init(p)) {
259 259 WARN_ON(1);
260 260 printk(KERN_WARNING "tried to kill init!\n");
261 261 return;
security/commoncap.c
... ... @@ -169,7 +169,7 @@
169 169 /* For init, we want to retain the capabilities set
170 170 * in the init_task struct. Thus we skip the usual
171 171 * capability rules */
172   - if (current->pid != 1) {
  172 + if (!is_init(current)) {
173 173 current->cap_permitted = new_permitted;
174 174 current->cap_effective =
175 175 cap_intersect (new_permitted, bprm->cap_effective);