@@ -88,20 +88,6 @@ struct pid_namespace init_pid_ns = {
88
88
};
89
89
EXPORT_SYMBOL_GPL (init_pid_ns );
90
90
91
- /*
92
- * Note: disable interrupts while the pidmap_lock is held as an
93
- * interrupt might come in and do read_lock(&tasklist_lock).
94
- *
95
- * If we don't disable interrupts there is a nasty deadlock between
96
- * detach_pid()->free_pid() and another cpu that does
97
- * spin_lock(&pidmap_lock) followed by an interrupt routine that does
98
- * read_lock(&tasklist_lock);
99
- *
100
- * After we clean up the tasklist_lock and know there are no
101
- * irq handlers that take it we can leave the interrupts enabled.
102
- * For now it is easier to be safe than to prove it can't happen.
103
- */
104
-
105
91
static __cacheline_aligned_in_smp DEFINE_SPINLOCK (pidmap_lock );
106
92
seqcount_spinlock_t pidmap_lock_seq = SEQCNT_SPINLOCK_ZERO (pidmap_lock_seq , & pidmap_lock );
107
93
@@ -128,11 +114,11 @@ static void delayed_put_pid(struct rcu_head *rhp)
128
114
129
115
void free_pid (struct pid * pid )
130
116
{
131
- /* We can be called with write_lock_irq(&tasklist_lock) held */
132
117
int i ;
133
- unsigned long flags ;
134
118
135
- spin_lock_irqsave (& pidmap_lock , flags );
119
+ lockdep_assert_not_held (& tasklist_lock );
120
+
121
+ spin_lock (& pidmap_lock );
136
122
for (i = 0 ; i <= pid -> level ; i ++ ) {
137
123
struct upid * upid = pid -> numbers + i ;
138
124
struct pid_namespace * ns = upid -> ns ;
@@ -155,11 +141,23 @@ void free_pid(struct pid *pid)
155
141
idr_remove (& ns -> idr , upid -> nr );
156
142
}
157
143
pidfs_remove_pid (pid );
158
- spin_unlock_irqrestore (& pidmap_lock , flags );
144
+ spin_unlock (& pidmap_lock );
159
145
160
146
call_rcu (& pid -> rcu , delayed_put_pid );
161
147
}
162
148
149
+ void free_pids (struct pid * * pids )
150
+ {
151
+ int tmp ;
152
+
153
+ /*
154
+ * This can batch pidmap_lock.
155
+ */
156
+ for (tmp = PIDTYPE_MAX ; -- tmp >= 0 ; )
157
+ if (pids [tmp ])
158
+ free_pid (pids [tmp ]);
159
+ }
160
+
163
161
struct pid * alloc_pid (struct pid_namespace * ns , pid_t * set_tid ,
164
162
size_t set_tid_size )
165
163
{
@@ -211,7 +209,7 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid,
211
209
}
212
210
213
211
idr_preload (GFP_KERNEL );
214
- spin_lock_irq (& pidmap_lock );
212
+ spin_lock (& pidmap_lock );
215
213
216
214
if (tid ) {
217
215
nr = idr_alloc (& tmp -> idr , NULL , tid ,
@@ -238,7 +236,7 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid,
238
236
nr = idr_alloc_cyclic (& tmp -> idr , NULL , pid_min ,
239
237
pid_max , GFP_ATOMIC );
240
238
}
241
- spin_unlock_irq (& pidmap_lock );
239
+ spin_unlock (& pidmap_lock );
242
240
idr_preload_end ();
243
241
244
242
if (nr < 0 ) {
@@ -272,7 +270,7 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid,
272
270
273
271
upid = pid -> numbers + ns -> level ;
274
272
idr_preload (GFP_KERNEL );
275
- spin_lock_irq (& pidmap_lock );
273
+ spin_lock (& pidmap_lock );
276
274
if (!(ns -> pid_allocated & PIDNS_ADDING ))
277
275
goto out_unlock ;
278
276
pidfs_add_pid (pid );
@@ -281,18 +279,18 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid,
281
279
idr_replace (& upid -> ns -> idr , pid , upid -> nr );
282
280
upid -> ns -> pid_allocated ++ ;
283
281
}
284
- spin_unlock_irq (& pidmap_lock );
282
+ spin_unlock (& pidmap_lock );
285
283
idr_preload_end ();
286
284
287
285
return pid ;
288
286
289
287
out_unlock :
290
- spin_unlock_irq (& pidmap_lock );
288
+ spin_unlock (& pidmap_lock );
291
289
idr_preload_end ();
292
290
put_pid_ns (ns );
293
291
294
292
out_free :
295
- spin_lock_irq (& pidmap_lock );
293
+ spin_lock (& pidmap_lock );
296
294
while (++ i <= ns -> level ) {
297
295
upid = pid -> numbers + i ;
298
296
idr_remove (& upid -> ns -> idr , upid -> nr );
@@ -302,17 +300,17 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid,
302
300
if (ns -> pid_allocated == PIDNS_ADDING )
303
301
idr_set_cursor (& ns -> idr , 0 );
304
302
305
- spin_unlock_irq (& pidmap_lock );
303
+ spin_unlock (& pidmap_lock );
306
304
307
305
kmem_cache_free (ns -> pid_cachep , pid );
308
306
return ERR_PTR (retval );
309
307
}
310
308
311
309
void disable_pid_allocation (struct pid_namespace * ns )
312
310
{
313
- spin_lock_irq (& pidmap_lock );
311
+ spin_lock (& pidmap_lock );
314
312
ns -> pid_allocated &= ~PIDNS_ADDING ;
315
- spin_unlock_irq (& pidmap_lock );
313
+ spin_unlock (& pidmap_lock );
316
314
}
317
315
318
316
struct pid * find_pid_ns (int nr , struct pid_namespace * ns )
@@ -339,17 +337,23 @@ static struct pid **task_pid_ptr(struct task_struct *task, enum pid_type type)
339
337
*/
340
338
void attach_pid (struct task_struct * task , enum pid_type type )
341
339
{
342
- struct pid * pid = * task_pid_ptr (task , type );
340
+ struct pid * pid ;
341
+
342
+ lockdep_assert_held_write (& tasklist_lock );
343
+
344
+ pid = * task_pid_ptr (task , type );
343
345
hlist_add_head_rcu (& task -> pid_links [type ], & pid -> tasks [type ]);
344
346
}
345
347
346
- static void __change_pid (struct task_struct * task , enum pid_type type ,
347
- struct pid * new )
348
+ static void __change_pid (struct pid * * pids , struct task_struct * task ,
349
+ enum pid_type type , struct pid * new )
348
350
{
349
- struct pid * * pid_ptr = task_pid_ptr (task , type );
350
- struct pid * pid ;
351
+ struct pid * * pid_ptr , * pid ;
351
352
int tmp ;
352
353
354
+ lockdep_assert_held_write (& tasklist_lock );
355
+
356
+ pid_ptr = task_pid_ptr (task , type );
353
357
pid = * pid_ptr ;
354
358
355
359
hlist_del_rcu (& task -> pid_links [type ]);
@@ -364,18 +368,19 @@ static void __change_pid(struct task_struct *task, enum pid_type type,
364
368
if (pid_has_task (pid , tmp ))
365
369
return ;
366
370
367
- free_pid (pid );
371
+ WARN_ON (pids [type ]);
372
+ pids [type ] = pid ;
368
373
}
369
374
370
- void detach_pid (struct task_struct * task , enum pid_type type )
375
+ void detach_pid (struct pid * * pids , struct task_struct * task , enum pid_type type )
371
376
{
372
- __change_pid (task , type , NULL );
377
+ __change_pid (pids , task , type , NULL );
373
378
}
374
379
375
- void change_pid (struct task_struct * task , enum pid_type type ,
380
+ void change_pid (struct pid * * pids , struct task_struct * task , enum pid_type type ,
376
381
struct pid * pid )
377
382
{
378
- __change_pid (task , type , pid );
383
+ __change_pid (pids , task , type , pid );
379
384
attach_pid (task , type );
380
385
}
381
386
@@ -386,6 +391,8 @@ void exchange_tids(struct task_struct *left, struct task_struct *right)
386
391
struct hlist_head * head1 = & pid1 -> tasks [PIDTYPE_PID ];
387
392
struct hlist_head * head2 = & pid2 -> tasks [PIDTYPE_PID ];
388
393
394
+ lockdep_assert_held_write (& tasklist_lock );
395
+
389
396
/* Swap the single entry tid lists */
390
397
hlists_swap_heads_rcu (head1 , head2 );
391
398
@@ -403,6 +410,7 @@ void transfer_pid(struct task_struct *old, struct task_struct *new,
403
410
enum pid_type type )
404
411
{
405
412
WARN_ON_ONCE (type == PIDTYPE_PID );
413
+ lockdep_assert_held_write (& tasklist_lock );
406
414
hlist_replace_rcu (& old -> pid_links [type ], & new -> pid_links [type ]);
407
415
}
408
416
0 commit comments