Skip to content

Commit a71e4f1

Browse files
committed
pidfs: simplify PIDFD_GET_<type>_NAMESPACE ioctls
We have reworked namespaces sufficiently that all this special-casing shouldn't be needed anymore Link: https://patch.msgid.link/20251117-eidesstattlich-apotheke-36d2e644079f@brauner Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent cc7d6c6 commit a71e4f1

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed

fs/pidfs.c

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,6 @@ static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
454454
struct task_struct *task __free(put_task) = NULL;
455455
struct nsproxy *nsp __free(put_nsproxy) = NULL;
456456
struct ns_common *ns_common = NULL;
457-
struct pid_namespace *pid_ns;
458457

459458
if (!pidfs_ioctl_valid(cmd))
460459
return -ENOIOCTLCMD;
@@ -496,66 +495,64 @@ static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
496495
switch (cmd) {
497496
/* Namespaces that hang of nsproxy. */
498497
case PIDFD_GET_CGROUP_NAMESPACE:
499-
if (IS_ENABLED(CONFIG_CGROUPS)) {
500-
get_cgroup_ns(nsp->cgroup_ns);
501-
ns_common = to_ns_common(nsp->cgroup_ns);
502-
}
498+
if (!ns_ref_get(nsp->cgroup_ns))
499+
break;
500+
ns_common = to_ns_common(nsp->cgroup_ns);
503501
break;
504502
case PIDFD_GET_IPC_NAMESPACE:
505-
if (IS_ENABLED(CONFIG_IPC_NS)) {
506-
get_ipc_ns(nsp->ipc_ns);
507-
ns_common = to_ns_common(nsp->ipc_ns);
508-
}
503+
if (!ns_ref_get(nsp->ipc_ns))
504+
break;
505+
ns_common = to_ns_common(nsp->ipc_ns);
509506
break;
510507
case PIDFD_GET_MNT_NAMESPACE:
511-
get_mnt_ns(nsp->mnt_ns);
508+
if (!ns_ref_get(nsp->mnt_ns))
509+
break;
512510
ns_common = to_ns_common(nsp->mnt_ns);
513511
break;
514512
case PIDFD_GET_NET_NAMESPACE:
515-
if (IS_ENABLED(CONFIG_NET_NS)) {
516-
ns_common = to_ns_common(nsp->net_ns);
517-
get_net_ns(ns_common);
518-
}
513+
if (!ns_ref_get(nsp->net_ns))
514+
break;
515+
ns_common = to_ns_common(nsp->net_ns);
519516
break;
520517
case PIDFD_GET_PID_FOR_CHILDREN_NAMESPACE:
521-
if (IS_ENABLED(CONFIG_PID_NS)) {
522-
get_pid_ns(nsp->pid_ns_for_children);
523-
ns_common = to_ns_common(nsp->pid_ns_for_children);
524-
}
518+
if (!ns_ref_get(nsp->pid_ns_for_children))
519+
break;
520+
ns_common = to_ns_common(nsp->pid_ns_for_children);
525521
break;
526522
case PIDFD_GET_TIME_NAMESPACE:
527-
if (IS_ENABLED(CONFIG_TIME_NS)) {
528-
get_time_ns(nsp->time_ns);
529-
ns_common = to_ns_common(nsp->time_ns);
530-
}
523+
if (!ns_ref_get(nsp->time_ns))
524+
break;
525+
ns_common = to_ns_common(nsp->time_ns);
531526
break;
532527
case PIDFD_GET_TIME_FOR_CHILDREN_NAMESPACE:
533-
if (IS_ENABLED(CONFIG_TIME_NS)) {
534-
get_time_ns(nsp->time_ns_for_children);
535-
ns_common = to_ns_common(nsp->time_ns_for_children);
536-
}
528+
if (!ns_ref_get(nsp->time_ns_for_children))
529+
break;
530+
ns_common = to_ns_common(nsp->time_ns_for_children);
537531
break;
538532
case PIDFD_GET_UTS_NAMESPACE:
539-
if (IS_ENABLED(CONFIG_UTS_NS)) {
540-
get_uts_ns(nsp->uts_ns);
541-
ns_common = to_ns_common(nsp->uts_ns);
542-
}
533+
if (!ns_ref_get(nsp->uts_ns))
534+
break;
535+
ns_common = to_ns_common(nsp->uts_ns);
543536
break;
544537
/* Namespaces that don't hang of nsproxy. */
545538
case PIDFD_GET_USER_NAMESPACE:
546-
if (IS_ENABLED(CONFIG_USER_NS)) {
547-
rcu_read_lock();
548-
ns_common = to_ns_common(get_user_ns(task_cred_xxx(task, user_ns)));
549-
rcu_read_unlock();
539+
scoped_guard(rcu) {
540+
struct user_namespace *user_ns;
541+
542+
user_ns = task_cred_xxx(task, user_ns);
543+
if (!ns_ref_get(user_ns))
544+
break;
545+
ns_common = to_ns_common(user_ns);
550546
}
551547
break;
552548
case PIDFD_GET_PID_NAMESPACE:
553-
if (IS_ENABLED(CONFIG_PID_NS)) {
554-
rcu_read_lock();
549+
scoped_guard(rcu) {
550+
struct pid_namespace *pid_ns;
551+
555552
pid_ns = task_active_pid_ns(task);
556-
if (pid_ns)
557-
ns_common = to_ns_common(get_pid_ns(pid_ns));
558-
rcu_read_unlock();
553+
if (!ns_ref_get(pid_ns))
554+
break;
555+
ns_common = to_ns_common(pid_ns);
559556
}
560557
break;
561558
default:

0 commit comments

Comments
 (0)