Re: [PATCH] pid_namespace: Add stub for to_pid_ns() when CONFIG_PID_NS is disabled

From: Eric W. Biederman

Date: Mon Mar 02 2026 - 14:24:35 EST


Pengpeng Hou <pengpeng.hou@xxxxxxxxxxxxxxxx> writes:

> Currently, to_pid_ns() is only defined when CONFIG_PID_NS is enabled. This
> leads to compilation errors in subsystems that attempt to retrieve a PID
> namespace from an ns_common structure in a non-namespace-aware kernel
> configuration.
>
> This patch adds a static inline stub for to_pid_ns() in the #else block,
> returning the address of the global init_pid_ns. This ensures consistent
> API availability and fixes potential build failures.

What code in the kernel tree has this problem?

Most places in the kernel (notably drivers) it is an error (of the
abstraction violation kind) to be playing with pids or namespaces.

What code in the kernel tree needs this fix?

This change also removes whitespace between functions in the header,
and it is desirable to have that whitespace.

Eric


> Signed-off-by: Pengpeng Hou <pengpeng.hou@xxxxxxxxxxxxxxxx>
> ---
> include/linux/pid_namespace.h | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/pid_namespace.h b/include/linux/pid_namespace.h
> index b20baaa7e..ca6032db8 100644
> --- a/include/linux/pid_namespace.h
> +++ b/include/linux/pid_namespace.h
> @@ -103,7 +103,10 @@ static inline int pidns_memfd_noexec_scope(struct pid_namespace *ns)
> {
> return 0;
> }
> -
> +static inline struct pid_namespace *to_pid_ns(struct ns_common *ns)
> +{
> + return &init_pid_ns;
> +}
> static inline struct pid_namespace *copy_pid_ns(u64 flags,
> struct user_namespace *user_ns, struct pid_namespace *ns)
> {