[PATCH net v3 1/2] pid: Introduce helper task_is_in_init_pid_ns()

From: Leo Yan
Date: Wed Jan 26 2022 - 00:05:02 EST


Currently the kernel uses open code in multiple places to check if a
task is in the root PID namespace with the kind of format:

if (task_active_pid_ns(current) == &init_pid_ns)
do_something();

This patch creates a new helper function, task_is_in_init_pid_ns(), it
returns true if a passed task is in the root PID namespace, otherwise
returns false. So it will be used to replace open codes.

Suggested-by: Suzuki K Poulose <suzuki.poulose@xxxxxxx>
Signed-off-by: Leo Yan <leo.yan@xxxxxxxxxx>
Reviewed-by: Leon Romanovsky <leonro@xxxxxxxxxx>
Acked-by: Suzuki K Poulose <suzuki.poulose@xxxxxxx>
Acked-by: Balbir Singh <bsingharora@xxxxxxxxx>
---
include/linux/pid_namespace.h | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/include/linux/pid_namespace.h b/include/linux/pid_namespace.h
index 7c7e627503d2..07481bb87d4e 100644
--- a/include/linux/pid_namespace.h
+++ b/include/linux/pid_namespace.h
@@ -86,4 +86,9 @@ extern struct pid_namespace *task_active_pid_ns(struct task_struct *tsk);
void pidhash_init(void);
void pid_idr_init(void);

+static inline bool task_is_in_init_pid_ns(struct task_struct *tsk)
+{
+ return task_active_pid_ns(tsk) == &init_pid_ns;
+}
+
#endif /* _LINUX_PID_NS_H */
--
2.25.1