Blame view

kernel/pid.c 11.2 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
  /*
   * Generic pidhash and scalable, time-bounded PID allocator
   *
6d49e352a   Nadia Yvette Chambers   propagate name ch...
4
5
   * (C) 2002-2003 Nadia Yvette Chambers, IBM
   * (C) 2004 Nadia Yvette Chambers, Oracle
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
   * (C) 2002-2004 Ingo Molnar, Red Hat
   *
   * pid-structures are backing objects for tasks sharing a given ID to chain
   * against. There is very little to them aside from hashing them and
   * parking tasks using given ID's on a list.
   *
   * The hash is always changed with the tasklist_lock write-acquired,
   * and the hash is only accessed with the tasklist_lock at least
   * read-acquired, so there's no additional SMP locking needed here.
   *
   * We have a list of bitmap pages, which bitmaps represent the PID space.
   * Allocating and freeing PIDs is completely lockless. The worst-case
   * allocation scenario when all but one out of 1 million PIDs possible are
   * allocated already: the scanning of 32 list entries and at most PAGE_SIZE
   * bytes. The typical fastpath is a single successful setbit. Freeing is O(1).
30e49c263   Pavel Emelyanov   pid namespaces: a...
21
22
23
24
25
26
   *
   * Pid namespaces:
   *    (C) 2007 Pavel Emelyanov <xemul@openvz.org>, OpenVZ, SWsoft Inc.
   *    (C) 2007 Sukadev Bhattiprolu <sukadev@us.ibm.com>, IBM
   *     Many thanks to Oleg Nesterov for comments and help
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
28
29
   */
  
  #include <linux/mm.h>
9984de1a5   Paul Gortmaker   kernel: Map most ...
30
  #include <linux/export.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
32
  #include <linux/slab.h>
  #include <linux/init.h>
82524746c   Franck Bui-Huu   rcu: split list.h...
33
  #include <linux/rculist.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
35
  #include <linux/bootmem.h>
  #include <linux/hash.h>
61a58c6c2   Sukadev Bhattiprolu   [PATCH] rename st...
36
  #include <linux/pid_namespace.h>
820e45db2   Sukadev Bhattiprolu   statically initia...
37
  #include <linux/init_task.h>
3eb07c8c8   Sukadev Bhattiprolu   pid namespaces: d...
38
  #include <linux/syscalls.h>
0bb80f240   David Howells   proc: Split the n...
39
  #include <linux/proc_ns.h>
0a01f2cc3   Eric W. Biederman   pidns: Make the p...
40
  #include <linux/proc_fs.h>
299300258   Ingo Molnar   sched/headers: Pr...
41
  #include <linux/sched/task.h>
95846ecf9   Gargi Sharma   pid: replace pid ...
42
  #include <linux/idr.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43

e1e871aff   David Howells   Expand INIT_STRUC...
44
45
46
47
48
49
50
51
52
53
54
55
56
  struct pid init_struct_pid = {
  	.count 		= ATOMIC_INIT(1),
  	.tasks		= {
  		{ .first = NULL },
  		{ .first = NULL },
  		{ .first = NULL },
  	},
  	.level		= 0,
  	.numbers	= { {
  		.nr		= 0,
  		.ns		= &init_pid_ns,
  	}, }
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
57
58
  
  int pid_max = PID_MAX_DEFAULT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
60
61
62
63
  
  #define RESERVED_PIDS		300
  
  int pid_max_min = RESERVED_PIDS + 1;
  int pid_max_max = PID_MAX_LIMIT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
65
66
67
68
69
  /*
   * PID-map pages start out as NULL, they get allocated upon
   * first use and are never deallocated. This way a low pid_max
   * value does not cause lots of bitmaps to be allocated, but
   * the scheme scales to up to 4 million PIDs, runtime.
   */
61a58c6c2   Sukadev Bhattiprolu   [PATCH] rename st...
70
  struct pid_namespace init_pid_ns = {
1e24edca0   Peter Zijlstra   locking/atomic, k...
71
  	.kref = KREF_INIT(2),
f6bb2a2c0   Matthew Wilcox   xarray: add the x...
72
  	.idr = IDR_INIT(init_pid_ns.idr),
e8cfbc245   Gargi Sharma   pid: remove pidhash
73
  	.pid_allocated = PIDNS_ADDING,
faacbfd3a   Pavel Emelyanov   pid namespaces: a...
74
75
  	.level = 0,
  	.child_reaper = &init_task,
49f4d8b93   Eric W. Biederman   pidns: Capture th...
76
  	.user_ns = &init_user_ns,
435d5f4bb   Al Viro   common object emb...
77
  	.ns.inum = PROC_PID_INIT_INO,
33c429405   Al Viro   copy address of p...
78
79
80
  #ifdef CONFIG_PID_NS
  	.ns.ops = &pidns_operations,
  #endif
3fbc96486   Sukadev Bhattiprolu   [PATCH] Define st...
81
  };
198fe21b0   Pavel Emelyanov   pid namespaces: h...
82
  EXPORT_SYMBOL_GPL(init_pid_ns);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
83

92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
84
85
86
87
88
89
90
91
92
93
94
95
96
  /*
   * Note: disable interrupts while the pidmap_lock is held as an
   * interrupt might come in and do read_lock(&tasklist_lock).
   *
   * If we don't disable interrupts there is a nasty deadlock between
   * detach_pid()->free_pid() and another cpu that does
   * spin_lock(&pidmap_lock) followed by an interrupt routine that does
   * read_lock(&tasklist_lock);
   *
   * After we clean up the tasklist_lock and know there are no
   * irq handlers that take it we can leave the interrupts enabled.
   * For now it is easier to be safe than to prove it can't happen.
   */
3fbc96486   Sukadev Bhattiprolu   [PATCH] Define st...
97

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
  static  __cacheline_aligned_in_smp DEFINE_SPINLOCK(pidmap_lock);
7ad5b3a50   Harvey Harrison   kernel: remove fa...
99
  void put_pid(struct pid *pid)
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
100
  {
baf8f0f82   Pavel Emelianov   pid namespaces: d...
101
  	struct pid_namespace *ns;
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
102
103
  	if (!pid)
  		return;
baf8f0f82   Pavel Emelianov   pid namespaces: d...
104

8ef047aaa   Pavel Emelyanov   pid namespaces: m...
105
  	ns = pid->numbers[pid->level].ns;
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
106
  	if ((atomic_read(&pid->count) == 1) ||
8ef047aaa   Pavel Emelyanov   pid namespaces: m...
107
  	     atomic_dec_and_test(&pid->count)) {
baf8f0f82   Pavel Emelianov   pid namespaces: d...
108
  		kmem_cache_free(ns->pid_cachep, pid);
b461cc038   Pavel Emelyanov   pid namespaces: m...
109
  		put_pid_ns(ns);
8ef047aaa   Pavel Emelyanov   pid namespaces: m...
110
  	}
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
111
  }
bbf73147e   Eric W. Biederman   [PATCH] pid: expo...
112
  EXPORT_SYMBOL_GPL(put_pid);
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
113
114
115
116
117
118
  
  static void delayed_put_pid(struct rcu_head *rhp)
  {
  	struct pid *pid = container_of(rhp, struct pid, rcu);
  	put_pid(pid);
  }
7ad5b3a50   Harvey Harrison   kernel: remove fa...
119
  void free_pid(struct pid *pid)
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
120
121
  {
  	/* We can be called with write_lock_irq(&tasklist_lock) held */
8ef047aaa   Pavel Emelyanov   pid namespaces: m...
122
  	int i;
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
123
124
125
  	unsigned long flags;
  
  	spin_lock_irqsave(&pidmap_lock, flags);
0a01f2cc3   Eric W. Biederman   pidns: Make the p...
126
127
  	for (i = 0; i <= pid->level; i++) {
  		struct upid *upid = pid->numbers + i;
af4b8a83a   Eric W. Biederman   pidns: Wait in za...
128
  		struct pid_namespace *ns = upid->ns;
e8cfbc245   Gargi Sharma   pid: remove pidhash
129
  		switch (--ns->pid_allocated) {
a60648851   Eric W. Biederman   pidns: Fix hang i...
130
  		case 2:
af4b8a83a   Eric W. Biederman   pidns: Wait in za...
131
132
133
134
135
136
137
  		case 1:
  			/* When all that is left in the pid namespace
  			 * is the reaper wake up the reaper.  The reaper
  			 * may be sleeping in zap_pid_ns_processes().
  			 */
  			wake_up_process(ns->child_reaper);
  			break;
e8cfbc245   Gargi Sharma   pid: remove pidhash
138
  		case PIDNS_ADDING:
314a8ad0f   Oleg Nesterov   pidns: fix free_p...
139
140
  			/* Handle a fork failure of the first process */
  			WARN_ON(ns->child_reaper);
e8cfbc245   Gargi Sharma   pid: remove pidhash
141
  			ns->pid_allocated = 0;
314a8ad0f   Oleg Nesterov   pidns: fix free_p...
142
  			/* fall through */
af4b8a83a   Eric W. Biederman   pidns: Wait in za...
143
  		case 0:
af4b8a83a   Eric W. Biederman   pidns: Wait in za...
144
145
  			schedule_work(&ns->proc_work);
  			break;
5e1182deb   Eric W. Biederman   pidns: Don't allo...
146
  		}
95846ecf9   Gargi Sharma   pid: replace pid ...
147
148
  
  		idr_remove(&ns->idr, upid->nr);
0a01f2cc3   Eric W. Biederman   pidns: Make the p...
149
  	}
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
150
  	spin_unlock_irqrestore(&pidmap_lock, flags);
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
151
152
  	call_rcu(&pid->rcu, delayed_put_pid);
  }
8ef047aaa   Pavel Emelyanov   pid namespaces: m...
153
  struct pid *alloc_pid(struct pid_namespace *ns)
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
154
155
156
  {
  	struct pid *pid;
  	enum pid_type type;
8ef047aaa   Pavel Emelyanov   pid namespaces: m...
157
158
  	int i, nr;
  	struct pid_namespace *tmp;
198fe21b0   Pavel Emelyanov   pid namespaces: h...
159
  	struct upid *upid;
35f71bc0a   Michal Hocko   fork: report pid ...
160
  	int retval = -ENOMEM;
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
161

baf8f0f82   Pavel Emelianov   pid namespaces: d...
162
  	pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL);
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
163
  	if (!pid)
35f71bc0a   Michal Hocko   fork: report pid ...
164
  		return ERR_PTR(retval);
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
165

8ef047aaa   Pavel Emelyanov   pid namespaces: m...
166
  	tmp = ns;
0a01f2cc3   Eric W. Biederman   pidns: Make the p...
167
  	pid->level = ns->level;
95846ecf9   Gargi Sharma   pid: replace pid ...
168

8ef047aaa   Pavel Emelyanov   pid namespaces: m...
169
  	for (i = ns->level; i >= 0; i--) {
95846ecf9   Gargi Sharma   pid: replace pid ...
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
  		int pid_min = 1;
  
  		idr_preload(GFP_KERNEL);
  		spin_lock_irq(&pidmap_lock);
  
  		/*
  		 * init really needs pid 1, but after reaching the maximum
  		 * wrap back to RESERVED_PIDS
  		 */
  		if (idr_get_cursor(&tmp->idr) > RESERVED_PIDS)
  			pid_min = RESERVED_PIDS;
  
  		/*
  		 * Store a null pointer so find_pid_ns does not find
  		 * a partially initialized PID (see below).
  		 */
  		nr = idr_alloc_cyclic(&tmp->idr, NULL, pid_min,
  				      pid_max, GFP_ATOMIC);
  		spin_unlock_irq(&pidmap_lock);
  		idr_preload_end();
287980e49   Arnd Bergmann   remove lots of IS...
190
  		if (nr < 0) {
f83606f5e   KJ Tsanaktsidis   fork: report pid ...
191
  			retval = (nr == -ENOSPC) ? -EAGAIN : nr;
8ef047aaa   Pavel Emelyanov   pid namespaces: m...
192
  			goto out_free;
35f71bc0a   Michal Hocko   fork: report pid ...
193
  		}
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
194

8ef047aaa   Pavel Emelyanov   pid namespaces: m...
195
196
197
198
  		pid->numbers[i].nr = nr;
  		pid->numbers[i].ns = tmp;
  		tmp = tmp->parent;
  	}
0a01f2cc3   Eric W. Biederman   pidns: Make the p...
199
  	if (unlikely(is_child_reaper(pid))) {
c0ee55490   Eric W. Biederman   pid: Handle failu...
200
  		if (pid_ns_prepare_proc(ns))
0a01f2cc3   Eric W. Biederman   pidns: Make the p...
201
202
  			goto out_free;
  	}
b461cc038   Pavel Emelyanov   pid namespaces: m...
203
  	get_pid_ns(ns);
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
204
  	atomic_set(&pid->count, 1);
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
205
206
  	for (type = 0; type < PIDTYPE_MAX; ++type)
  		INIT_HLIST_HEAD(&pid->tasks[type]);
417e31524   André Goddard Rosa   pid: reduce code ...
207
  	upid = pid->numbers + ns->level;
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
208
  	spin_lock_irq(&pidmap_lock);
e8cfbc245   Gargi Sharma   pid: remove pidhash
209
  	if (!(ns->pid_allocated & PIDNS_ADDING))
5e1182deb   Eric W. Biederman   pidns: Don't allo...
210
  		goto out_unlock;
0a01f2cc3   Eric W. Biederman   pidns: Make the p...
211
  	for ( ; upid >= pid->numbers; --upid) {
95846ecf9   Gargi Sharma   pid: replace pid ...
212
213
  		/* Make the PID visible to find_pid_ns. */
  		idr_replace(&upid->ns->idr, pid, upid->nr);
e8cfbc245   Gargi Sharma   pid: remove pidhash
214
  		upid->ns->pid_allocated++;
0a01f2cc3   Eric W. Biederman   pidns: Make the p...
215
  	}
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
216
  	spin_unlock_irq(&pidmap_lock);
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
217
  	return pid;
5e1182deb   Eric W. Biederman   pidns: Don't allo...
218
  out_unlock:
6e6668845   Eric W. Biederman   kernel/pid.c: ree...
219
  	spin_unlock_irq(&pidmap_lock);
24c037ebf   Oleg Nesterov   exit: pidns: allo...
220
  	put_pid_ns(ns);
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
221
  out_free:
95846ecf9   Gargi Sharma   pid: replace pid ...
222
  	spin_lock_irq(&pidmap_lock);
a85449199   Matthew Wilcox   Fix failure path ...
223
224
225
226
  	while (++i <= ns->level) {
  		upid = pid->numbers + i;
  		idr_remove(&upid->ns->idr, upid->nr);
  	}
95846ecf9   Gargi Sharma   pid: replace pid ...
227

c0ee55490   Eric W. Biederman   pid: Handle failu...
228
229
230
  	/* On failure to allocate the first pid, reset the state */
  	if (ns->pid_allocated == PIDNS_ADDING)
  		idr_set_cursor(&ns->idr, 0);
95846ecf9   Gargi Sharma   pid: replace pid ...
231
  	spin_unlock_irq(&pidmap_lock);
8ef047aaa   Pavel Emelyanov   pid namespaces: m...
232

baf8f0f82   Pavel Emelianov   pid namespaces: d...
233
  	kmem_cache_free(ns->pid_cachep, pid);
35f71bc0a   Michal Hocko   fork: report pid ...
234
  	return ERR_PTR(retval);
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
235
  }
c876ad768   Eric W. Biederman   pidns: Stop pid a...
236
237
238
  void disable_pid_allocation(struct pid_namespace *ns)
  {
  	spin_lock_irq(&pidmap_lock);
e8cfbc245   Gargi Sharma   pid: remove pidhash
239
  	ns->pid_allocated &= ~PIDNS_ADDING;
c876ad768   Eric W. Biederman   pidns: Stop pid a...
240
241
  	spin_unlock_irq(&pidmap_lock);
  }
7ad5b3a50   Harvey Harrison   kernel: remove fa...
242
  struct pid *find_pid_ns(int nr, struct pid_namespace *ns)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
243
  {
e8cfbc245   Gargi Sharma   pid: remove pidhash
244
  	return idr_find(&ns->idr, nr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
245
  }
198fe21b0   Pavel Emelyanov   pid namespaces: h...
246
  EXPORT_SYMBOL_GPL(find_pid_ns);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
247

8990571eb   Pavel Emelyanov   Uninline find_pid...
248
249
  struct pid *find_vpid(int nr)
  {
17cf22c33   Eric W. Biederman   pidns: Use task_a...
250
  	return find_pid_ns(nr, task_active_pid_ns(current));
8990571eb   Pavel Emelyanov   Uninline find_pid...
251
252
  }
  EXPORT_SYMBOL_GPL(find_vpid);
2c4704756   Eric W. Biederman   pids: Move the pg...
253
254
255
256
  static struct pid **task_pid_ptr(struct task_struct *task, enum pid_type type)
  {
  	return (type == PIDTYPE_PID) ?
  		&task->thread_pid :
2c4704756   Eric W. Biederman   pids: Move the pg...
257
258
  		&task->signal->pids[type];
  }
e713d0dab   Sukadev Bhattiprolu   attach_pid() with...
259
260
261
  /*
   * attach_pid() must be called with the tasklist_lock write-held.
   */
819077398   Oleg Nesterov   kernel/fork.c:cop...
262
  void attach_pid(struct task_struct *task, enum pid_type type)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
263
  {
2c4704756   Eric W. Biederman   pids: Move the pg...
264
265
  	struct pid *pid = *task_pid_ptr(task, type);
  	hlist_add_head_rcu(&task->pid_links[type], &pid->tasks[type]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
266
  }
24336eaee   Oleg Nesterov   pids: introduce c...
267
268
  static void __change_pid(struct task_struct *task, enum pid_type type,
  			struct pid *new)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
269
  {
2c4704756   Eric W. Biederman   pids: Move the pg...
270
  	struct pid **pid_ptr = task_pid_ptr(task, type);
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
271
272
  	struct pid *pid;
  	int tmp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273

2c4704756   Eric W. Biederman   pids: Move the pg...
274
  	pid = *pid_ptr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
275

2c4704756   Eric W. Biederman   pids: Move the pg...
276
277
  	hlist_del_rcu(&task->pid_links[type]);
  	*pid_ptr = new;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
278

92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
279
280
281
  	for (tmp = PIDTYPE_MAX; --tmp >= 0; )
  		if (!hlist_empty(&pid->tasks[tmp]))
  			return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
282

92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
283
  	free_pid(pid);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
284
  }
24336eaee   Oleg Nesterov   pids: introduce c...
285
286
287
288
289
290
291
292
293
  void detach_pid(struct task_struct *task, enum pid_type type)
  {
  	__change_pid(task, type, NULL);
  }
  
  void change_pid(struct task_struct *task, enum pid_type type,
  		struct pid *pid)
  {
  	__change_pid(task, type, pid);
819077398   Oleg Nesterov   kernel/fork.c:cop...
294
  	attach_pid(task, type);
24336eaee   Oleg Nesterov   pids: introduce c...
295
  }
c18258c6f   Eric W. Biederman   [PATCH] pid: Impl...
296
  /* transfer_pid is an optimization of attach_pid(new), detach_pid(old) */
7ad5b3a50   Harvey Harrison   kernel: remove fa...
297
  void transfer_pid(struct task_struct *old, struct task_struct *new,
c18258c6f   Eric W. Biederman   [PATCH] pid: Impl...
298
299
  			   enum pid_type type)
  {
2c4704756   Eric W. Biederman   pids: Move the pg...
300
301
302
  	if (type == PIDTYPE_PID)
  		new->thread_pid = old->thread_pid;
  	hlist_replace_rcu(&old->pid_links[type], &new->pid_links[type]);
c18258c6f   Eric W. Biederman   [PATCH] pid: Impl...
303
  }
7ad5b3a50   Harvey Harrison   kernel: remove fa...
304
  struct task_struct *pid_task(struct pid *pid, enum pid_type type)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
305
  {
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
306
307
308
  	struct task_struct *result = NULL;
  	if (pid) {
  		struct hlist_node *first;
67bdbffd6   Arnd Bergmann   rculist: avoid __...
309
  		first = rcu_dereference_check(hlist_first_rcu(&pid->tasks[type]),
db1466b3e   Paul E. McKenney   rcu: Use wrapper ...
310
  					      lockdep_tasklist_lock_is_held());
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
311
  		if (first)
2c4704756   Eric W. Biederman   pids: Move the pg...
312
  			result = hlist_entry(first, struct task_struct, pid_links[(type)]);
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
313
314
315
  	}
  	return result;
  }
eccba0689   Pavel Emelyanov   gfs2: make gfs2_g...
316
  EXPORT_SYMBOL(pid_task);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
317

92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
318
  /*
9728e5d6e   Tetsuo Handa   kernel/pid.c: upd...
319
   * Must be called under rcu_read_lock().
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
320
   */
17f98dcf6   Christoph Hellwig   pids: clean up fi...
321
  struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns)
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
322
  {
f78f5b90c   Paul E. McKenney   rcu: Rename rcu_l...
323
324
  	RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
  			 "find_task_by_pid_ns() needs rcu_read_lock() protection");
17f98dcf6   Christoph Hellwig   pids: clean up fi...
325
  	return pid_task(find_pid_ns(nr, ns), PIDTYPE_PID);
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
326
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
327

228ebcbe6   Pavel Emelyanov   Uninline find_tas...
328
329
  struct task_struct *find_task_by_vpid(pid_t vnr)
  {
17cf22c33   Eric W. Biederman   pidns: Use task_a...
330
  	return find_task_by_pid_ns(vnr, task_active_pid_ns(current));
228ebcbe6   Pavel Emelyanov   Uninline find_tas...
331
  }
228ebcbe6   Pavel Emelyanov   Uninline find_tas...
332

2ee082608   Mike Rapoport   pids: introduce f...
333
334
335
336
337
338
339
340
341
342
343
344
  struct task_struct *find_get_task_by_vpid(pid_t nr)
  {
  	struct task_struct *task;
  
  	rcu_read_lock();
  	task = find_task_by_vpid(nr);
  	if (task)
  		get_task_struct(task);
  	rcu_read_unlock();
  
  	return task;
  }
1a657f78d   Oleg Nesterov   [PATCH] introduce...
345
346
347
348
  struct pid *get_task_pid(struct task_struct *task, enum pid_type type)
  {
  	struct pid *pid;
  	rcu_read_lock();
2c4704756   Eric W. Biederman   pids: Move the pg...
349
  	pid = get_pid(rcu_dereference(*task_pid_ptr(task, type)));
1a657f78d   Oleg Nesterov   [PATCH] introduce...
350
351
352
  	rcu_read_unlock();
  	return pid;
  }
77c100c83   Rik van Riel   export pid symbol...
353
  EXPORT_SYMBOL_GPL(get_task_pid);
1a657f78d   Oleg Nesterov   [PATCH] introduce...
354

7ad5b3a50   Harvey Harrison   kernel: remove fa...
355
  struct task_struct *get_pid_task(struct pid *pid, enum pid_type type)
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
356
357
358
359
360
361
362
363
  {
  	struct task_struct *result;
  	rcu_read_lock();
  	result = pid_task(pid, type);
  	if (result)
  		get_task_struct(result);
  	rcu_read_unlock();
  	return result;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
364
  }
77c100c83   Rik van Riel   export pid symbol...
365
  EXPORT_SYMBOL_GPL(get_pid_task);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
366

92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
367
  struct pid *find_get_pid(pid_t nr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
368
369
  {
  	struct pid *pid;
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
370
  	rcu_read_lock();
198fe21b0   Pavel Emelyanov   pid namespaces: h...
371
  	pid = get_pid(find_vpid(nr));
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
372
  	rcu_read_unlock();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
373

92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
374
  	return pid;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
375
  }
339caf2a2   David Sterba   proc: misplaced e...
376
  EXPORT_SYMBOL_GPL(find_get_pid);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
377

7af572947   Pavel Emelyanov   pid namespaces: h...
378
379
380
381
382
383
384
385
386
387
388
389
  pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns)
  {
  	struct upid *upid;
  	pid_t nr = 0;
  
  	if (pid && ns->level <= pid->level) {
  		upid = &pid->numbers[ns->level];
  		if (upid->ns == ns)
  			nr = upid->nr;
  	}
  	return nr;
  }
4f82f4573   Eric W. Biederman   net ip6 flowlabel...
390
  EXPORT_SYMBOL_GPL(pid_nr_ns);
7af572947   Pavel Emelyanov   pid namespaces: h...
391

44c4e1b25   Eric W. Biederman   pid: Extend/Fix p...
392
393
  pid_t pid_vnr(struct pid *pid)
  {
17cf22c33   Eric W. Biederman   pidns: Use task_a...
394
  	return pid_nr_ns(pid, task_active_pid_ns(current));
44c4e1b25   Eric W. Biederman   pid: Extend/Fix p...
395
396
  }
  EXPORT_SYMBOL_GPL(pid_vnr);
52ee2dfdd   Oleg Nesterov   pids: refactor vn...
397
398
  pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
  			struct pid_namespace *ns)
2f2a3a46f   Pavel Emelyanov   Uninline the task...
399
  {
52ee2dfdd   Oleg Nesterov   pids: refactor vn...
400
401
402
403
  	pid_t nr = 0;
  
  	rcu_read_lock();
  	if (!ns)
17cf22c33   Eric W. Biederman   pidns: Use task_a...
404
  		ns = task_active_pid_ns(current);
2c4704756   Eric W. Biederman   pids: Move the pg...
405
406
  	if (likely(pid_alive(task)))
  		nr = pid_nr_ns(rcu_dereference(*task_pid_ptr(task, type)), ns);
52ee2dfdd   Oleg Nesterov   pids: refactor vn...
407
408
409
  	rcu_read_unlock();
  
  	return nr;
2f2a3a46f   Pavel Emelyanov   Uninline the task...
410
  }
52ee2dfdd   Oleg Nesterov   pids: refactor vn...
411
  EXPORT_SYMBOL(__task_pid_nr_ns);
2f2a3a46f   Pavel Emelyanov   Uninline the task...
412

61bce0f13   Eric W. Biederman   pid: generalize t...
413
414
415
416
417
  struct pid_namespace *task_active_pid_ns(struct task_struct *tsk)
  {
  	return ns_of_pid(task_pid(tsk));
  }
  EXPORT_SYMBOL_GPL(task_active_pid_ns);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
418
  /*
025dfdafe   Frederik Schwarzer   trivial: fix then...
419
   * Used by proc to find the first pid that is greater than or equal to nr.
0804ef4b0   Eric W. Biederman   [PATCH] proc: rea...
420
   *
e49859e71   Pavel Emelyanov   pidns: remove now...
421
   * If there is a pid at nr this function is exactly the same as find_pid_ns.
0804ef4b0   Eric W. Biederman   [PATCH] proc: rea...
422
   */
198fe21b0   Pavel Emelyanov   pid namespaces: h...
423
  struct pid *find_ge_pid(int nr, struct pid_namespace *ns)
0804ef4b0   Eric W. Biederman   [PATCH] proc: rea...
424
  {
95846ecf9   Gargi Sharma   pid: replace pid ...
425
  	return idr_get_next(&ns->idr, &nr);
0804ef4b0   Eric W. Biederman   [PATCH] proc: rea...
426
  }
95846ecf9   Gargi Sharma   pid: replace pid ...
427
  void __init pid_idr_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
428
  {
840d6fe74   Zhen Lei   pid: Fix spelling...
429
  	/* Verify no one has done anything silly: */
e8cfbc245   Gargi Sharma   pid: remove pidhash
430
  	BUILD_BUG_ON(PID_MAX_LIMIT >= PIDNS_ADDING);
c876ad768   Eric W. Biederman   pidns: Stop pid a...
431

72680a191   Hedi Berriche   pids: increase pi...
432
433
434
435
436
437
438
  	/* bump default and minimum pid_max based on number of cpus */
  	pid_max = min(pid_max_max, max_t(int, pid_max,
  				PIDS_PER_CPU_DEFAULT * num_possible_cpus()));
  	pid_max_min = max_t(int, pid_max_min,
  				PIDS_PER_CPU_MIN * num_possible_cpus());
  	pr_info("pid_max: default: %u minimum: %u
  ", pid_max, pid_max_min);
95846ecf9   Gargi Sharma   pid: replace pid ...
439
  	idr_init(&init_pid_ns.idr);
92476d7fc   Eric W. Biederman   [PATCH] pidhash: ...
440

74bd59bb3   Pavel Emelyanov   namespaces: clean...
441
  	init_pid_ns.pid_cachep = KMEM_CACHE(pid,
5d097056c   Vladimir Davydov   kmemcg: account c...
442
  			SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
443
  }