Skip to content

Commit 52ee2df

Browse files
oleg-nesterovtorvalds
authored andcommitted
pids: refactor vnr/nr_ns helpers to make them safe
Inho, the safety rules for vnr/nr_ns helpers are horrible and buggy. task_pid_nr_ns(task) needs rcu/tasklist depending on task == current. As for "special" pids, vnr/nr_ns helpers always need rcu. However, if task != current, they are unsafe even under rcu lock, we can't trust task->group_leader without the special checks. And almost every helper has a callsite which needs a fix. Also, it is a bit annoying that the implementations of, say, task_pgrp_vnr() and task_pgrp_nr_ns() are not "symmetrical". This patch introduces the new helper, __task_pid_nr_ns(), which is always safe to use, and turns all other helpers into the trivial wrappers. After this I'll send another patch which converts task_tgid_xxx() as well, they're are a bit special. Signed-off-by: Oleg Nesterov <oleg@redhat.com> Cc: Louis Rilling <Louis.Rilling@kerlabs.com> Cc: "Eric W. Biederman" <ebiederm@xmission.com> Cc: Pavel Emelyanov <xemul@openvz.org> Cc: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com> Cc: Roland McGrath <roland@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 2ae448e commit 52ee2df

2 files changed

Lines changed: 36 additions & 22 deletions

File tree

‎include/linux/sched.h‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,17 +1519,23 @@ struct pid_namespace;
15191519
*
15201520
* see also pid_nr() etc in include/linux/pid.h
15211521
*/
1522+
pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
1523+
struct pid_namespace *ns);
15221524

15231525
static inline pid_t task_pid_nr(struct task_struct *tsk)
15241526
{
15251527
return tsk->pid;
15261528
}
15271529

1528-
pid_t task_pid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns);
1530+
static inline pid_t task_pid_nr_ns(struct task_struct *tsk,
1531+
struct pid_namespace *ns)
1532+
{
1533+
return __task_pid_nr_ns(tsk, PIDTYPE_PID, ns);
1534+
}
15291535

15301536
static inline pid_t task_pid_vnr(struct task_struct *tsk)
15311537
{
1532-
return pid_vnr(task_pid(tsk));
1538+
return __task_pid_nr_ns(tsk, PIDTYPE_PID, NULL);
15331539
}
15341540

15351541

@@ -1551,11 +1557,15 @@ static inline pid_t task_pgrp_nr(struct task_struct *tsk)
15511557
return tsk->signal->__pgrp;
15521558
}
15531559

1554-
pid_t task_pgrp_nr_ns(struct task_struct *tsk, struct pid_namespace *ns);
1560+
static inline pid_t task_pgrp_nr_ns(struct task_struct *tsk,
1561+
struct pid_namespace *ns)
1562+
{
1563+
return __task_pid_nr_ns(tsk, PIDTYPE_PGID, ns);
1564+
}
15551565

15561566
static inline pid_t task_pgrp_vnr(struct task_struct *tsk)
15571567
{
1558-
return pid_vnr(task_pgrp(tsk));
1568+
return __task_pid_nr_ns(tsk, PIDTYPE_PGID, NULL);
15591569
}
15601570

15611571

@@ -1564,14 +1574,17 @@ static inline pid_t task_session_nr(struct task_struct *tsk)
15641574
return tsk->signal->__session;
15651575
}
15661576

1567-
pid_t task_session_nr_ns(struct task_struct *tsk, struct pid_namespace *ns);
1577+
static inline pid_t task_session_nr_ns(struct task_struct *tsk,
1578+
struct pid_namespace *ns)
1579+
{
1580+
return __task_pid_nr_ns(tsk, PIDTYPE_SID, ns);
1581+
}
15681582

15691583
static inline pid_t task_session_vnr(struct task_struct *tsk)
15701584
{
1571-
return pid_vnr(task_session(tsk));
1585+
return __task_pid_nr_ns(tsk, PIDTYPE_SID, NULL);
15721586
}
15731587

1574-
15751588
/**
15761589
* pid_alive - check that a task structure is not stale
15771590
* @p: Task structure to be checked.

‎kernel/pid.c‎

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -452,30 +452,31 @@ pid_t pid_vnr(struct pid *pid)
452452
}
453453
EXPORT_SYMBOL_GPL(pid_vnr);
454454

455-
pid_t task_pid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
455+
pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
456+
struct pid_namespace *ns)
456457
{
457-
return pid_nr_ns(task_pid(tsk), ns);
458+
pid_t nr = 0;
459+
460+
rcu_read_lock();
461+
if (!ns)
462+
ns = current->nsproxy->pid_ns;
463+
if (likely(pid_alive(task))) {
464+
if (type != PIDTYPE_PID)
465+
task = task->group_leader;
466+
nr = pid_nr_ns(task->pids[type].pid, ns);
467+
}
468+
rcu_read_unlock();
469+
470+
return nr;
458471
}
459-
EXPORT_SYMBOL(task_pid_nr_ns);
472+
EXPORT_SYMBOL(__task_pid_nr_ns);
460473

461474
pid_t task_tgid_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
462475
{
463476
return pid_nr_ns(task_tgid(tsk), ns);
464477
}
465478
EXPORT_SYMBOL(task_tgid_nr_ns);
466479

467-
pid_t task_pgrp_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
468-
{
469-
return pid_nr_ns(task_pgrp(tsk), ns);
470-
}
471-
EXPORT_SYMBOL(task_pgrp_nr_ns);
472-
473-
pid_t task_session_nr_ns(struct task_struct *tsk, struct pid_namespace *ns)
474-
{
475-
return pid_nr_ns(task_session(tsk), ns);
476-
}
477-
EXPORT_SYMBOL(task_session_nr_ns);
478-
479480
struct pid_namespace *task_active_pid_ns(struct task_struct *tsk)
480481
{
481482
return ns_of_pid(task_pid(tsk));

0 commit comments

Comments
 (0)