Skip to content

Commit 74198dc

Browse files
mjguzikbrauner
authored andcommitted
pid: sprinkle tasklist_lock asserts
They cost nothing on production kernels and document the requirement of holding the tasklist_lock lock in respective routines. Reviewed-by: Oleg Nesterov <[email protected]> Signed-off-by: Mateusz Guzik <[email protected]> Link: https://lore.kernel.org/r/[email protected] Acked-by: "Liam R. Howlett" <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 6731cd9 commit 74198dc

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

kernel/pid.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,17 +339,23 @@ static struct pid **task_pid_ptr(struct task_struct *task, enum pid_type type)
339339
*/
340340
void attach_pid(struct task_struct *task, enum pid_type type)
341341
{
342-
struct pid *pid = *task_pid_ptr(task, type);
342+
struct pid *pid;
343+
344+
lockdep_assert_held_write(&tasklist_lock);
345+
346+
pid = *task_pid_ptr(task, type);
343347
hlist_add_head_rcu(&task->pid_links[type], &pid->tasks[type]);
344348
}
345349

346350
static void __change_pid(struct task_struct *task, enum pid_type type,
347351
struct pid *new)
348352
{
349-
struct pid **pid_ptr = task_pid_ptr(task, type);
350-
struct pid *pid;
353+
struct pid **pid_ptr, *pid;
351354
int tmp;
352355

356+
lockdep_assert_held_write(&tasklist_lock);
357+
358+
pid_ptr = task_pid_ptr(task, type);
353359
pid = *pid_ptr;
354360

355361
hlist_del_rcu(&task->pid_links[type]);
@@ -386,6 +392,8 @@ void exchange_tids(struct task_struct *left, struct task_struct *right)
386392
struct hlist_head *head1 = &pid1->tasks[PIDTYPE_PID];
387393
struct hlist_head *head2 = &pid2->tasks[PIDTYPE_PID];
388394

395+
lockdep_assert_held_write(&tasklist_lock);
396+
389397
/* Swap the single entry tid lists */
390398
hlists_swap_heads_rcu(head1, head2);
391399

@@ -403,6 +411,7 @@ void transfer_pid(struct task_struct *old, struct task_struct *new,
403411
enum pid_type type)
404412
{
405413
WARN_ON_ONCE(type == PIDTYPE_PID);
414+
lockdep_assert_held_write(&tasklist_lock);
406415
hlist_replace_rcu(&old->pid_links[type], &new->pid_links[type]);
407416
}
408417

0 commit comments

Comments
 (0)