@@ -1082,57 +1082,97 @@ static __always_inline int bpf_ppm_get_tty(struct task_struct *task)
10821082 return tty_nr ;
10831083}
10841084
1085- enum bpf_pid_type {
1086- BPF_PIDTYPE_PID ,
1087- BPF_PIDTYPE_PGID ,
1088- BPF_PIDTYPE_SID ,
1089- BPF_PIDTYPE_MAX ,
1090- BPF__PIDTYPE_TGID
1091- };
1085+ static __always_inline struct pid * bpf_task_pid (struct task_struct * task )
1086+ {
1087+ #if LINUX_VERSION_CODE < KERNEL_VERSION (4 , 19 , 0 )
1088+ return _READ (task -> pids [PIDTYPE_PID ].pid );
1089+ #else
1090+ return _READ (task -> thread_pid );
1091+ #endif
1092+ }
10921093
1093- static __always_inline pid_t bpf_task_pid_nr_ns (struct task_struct * task ,
1094- enum bpf_pid_type type )
1094+ static __always_inline struct pid_namespace * bpf_ns_of_pid (struct pid * pid )
10951095{
1096- pid_t nr = 0 ;
1097- struct pid * pid = _READ (task -> pids [PIDTYPE_PID ].pid );
1098- unsigned int pid_level = _READ (pid -> level );
1099- struct pid_namespace * ns = _READ (pid -> numbers [pid_level ].ns );
1100- unsigned int ns_level = _READ (ns -> level );
1096+ struct pid_namespace * ns = NULL ;
11011097
1102- if (type != BPF_PIDTYPE_PID ) {
1103- if (type == BPF__PIDTYPE_TGID )
1104- type = BPF_PIDTYPE_PID ;
1098+ if (pid )
1099+ ns = _READ (pid -> numbers [_READ (pid -> level )].ns );
1100+ return ns ;
1101+ }
11051102
1106- task = _READ (task -> group_leader );
1103+ static __always_inline struct pid_namespace * bpf_task_active_pid_ns (struct task_struct * tsk )
1104+ {
1105+ return bpf_ns_of_pid (bpf_task_pid (tsk ));
1106+ }
1107+
1108+ static __always_inline pid_t bpf_pid_nr_ns (struct pid * pid ,
1109+ struct pid_namespace * ns )
1110+ {
1111+ unsigned int ns_level ;
1112+ struct upid * upid ;
1113+ pid_t nr = 0 ;
1114+
1115+ ns_level = _READ (ns -> level );
1116+ if (pid && ns_level <= _READ (pid -> level )) {
1117+ upid = & pid -> numbers [ns_level ];
1118+ if (_READ (upid -> ns ) == ns )
1119+ nr = _READ (upid -> nr );
11071120 }
1121+ return nr ;
1122+ }
11081123
1109- pid = _READ (task -> pids [type ].pid );
1110- pid_level = _READ (pid -> level );
1124+ #if LINUX_VERSION_CODE >= KERNEL_VERSION (4 , 19 , 0 )
1125+ static __always_inline struct pid * * bpf_task_pid_ptr (struct task_struct * task ,
1126+ enum pid_type type )
1127+ {
1128+ return (type == PIDTYPE_PID ) ?
1129+ & task -> thread_pid :
1130+ & _READ (task -> signal )-> pids [type ];
1131+ }
1132+ #endif
11111133
1112- if (pid && ns_level <= pid_level ) {
1113- struct upid * upid = & pid -> numbers [ns_level ];
1114- struct pid_namespace * upid_ns = _READ (upid -> ns );
1134+ static __always_inline pid_t bpf_task_pid_nr_ns (struct task_struct * task ,
1135+ enum pid_type type ,
1136+ struct pid_namespace * ns )
1137+ {
1138+ pid_t nr = 0 ;
11151139
1116- if (upid_ns == ns )
1117- nr = _READ (upid -> nr );
1140+ if (!ns )
1141+ ns = bpf_task_active_pid_ns (task );
1142+
1143+ #if LINUX_VERSION_CODE < KERNEL_VERSION (4 , 19 , 0 )
1144+ if (type != PIDTYPE_PID ) {
1145+ if (type == __PIDTYPE_TGID )
1146+ type = PIDTYPE_PID ;
1147+
1148+ task = _READ (task -> group_leader );
11181149 }
11191150
1151+ nr = bpf_pid_nr_ns (_READ (task -> pids [type ].pid ), ns );
1152+ #else
1153+ nr = bpf_pid_nr_ns (_READ (* bpf_task_pid_ptr (task , type )), ns );
1154+ #endif
1155+
11201156 return nr ;
11211157}
11221158
11231159static __always_inline pid_t bpf_task_pid_vnr (struct task_struct * task )
11241160{
1125- return bpf_task_pid_nr_ns (task , BPF_PIDTYPE_PID );
1161+ return bpf_task_pid_nr_ns (task , PIDTYPE_PID , NULL );
11261162}
11271163
11281164static __always_inline pid_t bpf_task_tgid_vnr (struct task_struct * task )
11291165{
1130- return bpf_task_pid_nr_ns (task , BPF__PIDTYPE_TGID );
1166+ #if LINUX_VERSION_CODE < KERNEL_VERSION (4 , 19 , 0 )
1167+ return bpf_task_pid_nr_ns (task , __PIDTYPE_TGID , NULL );
1168+ #else
1169+ return bpf_task_pid_nr_ns (task , PIDTYPE_TGID , NULL );
1170+ #endif
11311171}
11321172
11331173static __always_inline pid_t bpf_task_pgrp_vnr (struct task_struct * task )
11341174{
1135- return bpf_task_pid_nr_ns (task , BPF_PIDTYPE_PGID );
1175+ return bpf_task_pid_nr_ns (task , PIDTYPE_PGID , NULL );
11361176}
11371177
11381178#define MAX_CGROUP_PATHS 6
0 commit comments