1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_SCHED_SIGNAL_H
3 #define _LINUX_SCHED_SIGNAL_H
4
5 #include <linux/rculist.h>
6 #include <linux/signal.h>
7 #include <linux/sched.h>
8 #include <linux/sched/jobctl.h>
9 #include <linux/sched/task.h>
10 #include <linux/cred.h>
11 #include <linux/refcount.h>
12 #include <linux/posix-timers.h>
13 #include <linux/mm_types.h>
14 #include <asm/ptrace.h>
15 #include <linux/android_kabi.h>
16
17 /*
18 * Types defining task->signal and task->sighand and APIs using them:
19 */
20
21 struct sighand_struct {
22 spinlock_t siglock;
23 refcount_t count;
24 wait_queue_head_t signalfd_wqh;
25 struct k_sigaction action[_NSIG];
26 };
27
28 /*
29 * Per-process accounting stats:
30 */
31 struct pacct_struct {
32 int ac_flag;
33 long ac_exitcode;
34 unsigned long ac_mem;
35 u64 ac_utime, ac_stime;
36 unsigned long ac_minflt, ac_majflt;
37 };
38
39 struct cpu_itimer {
40 u64 expires;
41 u64 incr;
42 };
43
44 /*
45 * This is the atomic variant of task_cputime, which can be used for
46 * storing and updating task_cputime statistics without locking.
47 */
48 struct task_cputime_atomic {
49 atomic64_t utime;
50 atomic64_t stime;
51 atomic64_t sum_exec_runtime;
52 };
53
54 #define INIT_CPUTIME_ATOMIC \
55 (struct task_cputime_atomic) { \
56 .utime = ATOMIC64_INIT(0), \
57 .stime = ATOMIC64_INIT(0), \
58 .sum_exec_runtime = ATOMIC64_INIT(0), \
59 }
60 /**
61 * struct thread_group_cputimer - thread group interval timer counts
62 * @cputime_atomic: atomic thread group interval timers.
63 *
64 * This structure contains the version of task_cputime, above, that is
65 * used for thread group CPU timer calculations.
66 */
67 struct thread_group_cputimer {
68 struct task_cputime_atomic cputime_atomic;
69 };
70
71 struct multiprocess_signals {
72 sigset_t signal;
73 struct hlist_node node;
74 };
75
76 /*
77 * NOTE! "signal_struct" does not have its own
78 * locking, because a shared signal_struct always
79 * implies a shared sighand_struct, so locking
80 * sighand_struct is always a proper superset of
81 * the locking of signal_struct.
82 */
83 struct signal_struct {
84 refcount_t sigcnt;
85 atomic_t live;
86 int nr_threads;
87 struct list_head thread_head;
88
89 wait_queue_head_t wait_chldexit; /* for wait4() */
90
91 /* current thread group signal load-balancing target: */
92 struct task_struct *curr_target;
93
94 /* shared signal handling: */
95 struct sigpending shared_pending;
96
97 /* For collecting multiprocess signals during fork */
98 struct hlist_head multiprocess;
99
100 /* thread group exit support */
101 int group_exit_code;
102 /* overloaded:
103 * - notify group_exit_task when ->count is equal to notify_count
104 * - everyone except group_exit_task is stopped during signal delivery
105 * of fatal signals, group_exit_task processes the signal.
106 */
107 int notify_count;
108 struct task_struct *group_exit_task;
109
110 /* thread group stop support, overloads group_exit_code too */
111 int group_stop_count;
112 unsigned int flags; /* see SIGNAL_* flags below */
113
114 /*
115 * PR_SET_CHILD_SUBREAPER marks a process, like a service
116 * manager, to re-parent orphan (double-forking) child processes
117 * to this process instead of 'init'. The service manager is
118 * able to receive SIGCHLD signals and is able to investigate
119 * the process until it calls wait(). All children of this
120 * process will inherit a flag if they should look for a
121 * child_subreaper process at exit.
122 */
123 unsigned int is_child_subreaper:1;
124 unsigned int has_child_subreaper:1;
125
126 #ifdef CONFIG_POSIX_TIMERS
127
128 /* POSIX.1b Interval Timers */
129 int posix_timer_id;
130 struct list_head posix_timers;
131
132 /* ITIMER_REAL timer for the process */
133 struct hrtimer real_timer;
134 ktime_t it_real_incr;
135
136 /*
137 * ITIMER_PROF and ITIMER_VIRTUAL timers for the process, we use
138 * CPUCLOCK_PROF and CPUCLOCK_VIRT for indexing array as these
139 * values are defined to 0 and 1 respectively
140 */
141 struct cpu_itimer it[2];
142
143 /*
144 * Thread group totals for process CPU timers.
145 * See thread_group_cputimer(), et al, for details.
146 */
147 struct thread_group_cputimer cputimer;
148
149 #endif
150 /* Empty if CONFIG_POSIX_TIMERS=n */
151 struct posix_cputimers posix_cputimers;
152
153 /* PID/PID hash table linkage. */
154 struct pid *pids[PIDTYPE_MAX];
155
156 #ifdef CONFIG_NO_HZ_FULL
157 atomic_t tick_dep_mask;
158 #endif
159
160 struct pid *tty_old_pgrp;
161
162 /* boolean value for session group leader */
163 int leader;
164
165 struct tty_struct *tty; /* NULL if no tty */
166
167 #ifdef CONFIG_SCHED_AUTOGROUP
168 struct autogroup *autogroup;
169 #endif
170 /*
171 * Cumulative resource counters for dead threads in the group,
172 * and for reaped dead child processes forked by this group.
173 * Live threads maintain their own counters and add to these
174 * in __exit_signal, except for the group leader.
175 */
176 seqlock_t stats_lock;
177 u64 utime, stime, cutime, cstime;
178 u64 gtime;
179 u64 cgtime;
180 struct prev_cputime prev_cputime;
181 unsigned long nvcsw, nivcsw, cnvcsw, cnivcsw;
182 unsigned long min_flt, maj_flt, cmin_flt, cmaj_flt;
183 unsigned long inblock, oublock, cinblock, coublock;
184 unsigned long maxrss, cmaxrss;
185 struct task_io_accounting ioac;
186
187 /*
188 * Cumulative ns of schedule CPU time fo dead threads in the
189 * group, not including a zombie group leader, (This only differs
190 * from jiffies_to_ns(utime + stime) if sched_clock uses something
191 * other than jiffies.)
192 */
193 unsigned long long sum_sched_runtime;
194
195 /*
196 * We don't bother to synchronize most readers of this at all,
197 * because there is no reader checking a limit that actually needs
198 * to get both rlim_cur and rlim_max atomically, and either one
199 * alone is a single word that can safely be read normally.
200 * getrlimit/setrlimit use task_lock(current->group_leader) to
201 * protect this instead of the siglock, because they really
202 * have no need to disable irqs.
203 */
204 struct rlimit rlim[RLIM_NLIMITS];
205
206 #ifdef CONFIG_BSD_PROCESS_ACCT
207 struct pacct_struct pacct; /* per-process accounting information */
208 #endif
209 #ifdef CONFIG_TASKSTATS
210 struct taskstats *stats;
211 #endif
212 #ifdef CONFIG_AUDIT
213 unsigned audit_tty;
214 struct tty_audit_buf *tty_audit_buf;
215 #endif
216
217 /*
218 * Thread is the potential origin of an oom condition; kill first on
219 * oom
220 */
221 bool oom_flag_origin;
222 short oom_score_adj; /* OOM kill score adjustment */
223 short oom_score_adj_min; /* OOM kill score adjustment min value.
224 * Only settable by CAP_SYS_RESOURCE. */
225 struct mm_struct *oom_mm; /* recorded mm when the thread group got
226 * killed by the oom killer */
227
228 struct mutex cred_guard_mutex; /* guard against foreign influences on
229 * credential calculations
230 * (notably. ptrace)
231 * Deprecated do not use in new code.
232 * Use exec_update_lock instead.
233 */
234 struct rw_semaphore exec_update_lock; /* Held while task_struct is
235 * being updated during exec,
236 * and may have inconsistent
237 * permissions.
238 */
239
240 ANDROID_KABI_RESERVE(1);
241 ANDROID_KABI_RESERVE(2);
242 ANDROID_KABI_RESERVE(3);
243 ANDROID_KABI_RESERVE(4);
244 } __randomize_layout;
245
246 /*
247 * Bits in flags field of signal_struct.
248 */
249 #define SIGNAL_STOP_STOPPED 0x00000001 /* job control stop in effect */
250 #define SIGNAL_STOP_CONTINUED 0x00000002 /* SIGCONT since WCONTINUED reap */
251 #define SIGNAL_GROUP_EXIT 0x00000004 /* group exit in progress */
252 #define SIGNAL_GROUP_COREDUMP 0x00000008 /* coredump in progress */
253 /*
254 * Pending notifications to parent.
255 */
256 #define SIGNAL_CLD_STOPPED 0x00000010
257 #define SIGNAL_CLD_CONTINUED 0x00000020
258 #define SIGNAL_CLD_MASK (SIGNAL_CLD_STOPPED|SIGNAL_CLD_CONTINUED)
259
260 #define SIGNAL_UNKILLABLE 0x00000040 /* for init: ignore fatal signals */
261
262 #define SIGNAL_STOP_MASK (SIGNAL_CLD_MASK | SIGNAL_STOP_STOPPED | \
263 SIGNAL_STOP_CONTINUED)
264
signal_set_stop_flags(struct signal_struct * sig,unsigned int flags)265 static inline void signal_set_stop_flags(struct signal_struct *sig,
266 unsigned int flags)
267 {
268 WARN_ON(sig->flags & (SIGNAL_GROUP_EXIT|SIGNAL_GROUP_COREDUMP));
269 sig->flags = (sig->flags & ~SIGNAL_STOP_MASK) | flags;
270 }
271
272 /* If true, all threads except ->group_exit_task have pending SIGKILL */
signal_group_exit(const struct signal_struct * sig)273 static inline int signal_group_exit(const struct signal_struct *sig)
274 {
275 return (sig->flags & SIGNAL_GROUP_EXIT) ||
276 (sig->group_exit_task != NULL);
277 }
278
279 extern void flush_signals(struct task_struct *);
280 extern void ignore_signals(struct task_struct *);
281 extern void flush_signal_handlers(struct task_struct *, int force_default);
282 extern int dequeue_signal(struct task_struct *task,
283 sigset_t *mask, kernel_siginfo_t *info);
284
kernel_dequeue_signal(void)285 static inline int kernel_dequeue_signal(void)
286 {
287 struct task_struct *task = current;
288 kernel_siginfo_t __info;
289 int ret;
290
291 spin_lock_irq(&task->sighand->siglock);
292 ret = dequeue_signal(task, &task->blocked, &__info);
293 spin_unlock_irq(&task->sighand->siglock);
294
295 return ret;
296 }
297
kernel_signal_stop(void)298 static inline void kernel_signal_stop(void)
299 {
300 spin_lock_irq(¤t->sighand->siglock);
301 if (current->jobctl & JOBCTL_STOP_DEQUEUED)
302 set_special_state(TASK_STOPPED);
303 spin_unlock_irq(¤t->sighand->siglock);
304
305 schedule();
306 }
307 #ifdef __ARCH_SI_TRAPNO
308 # define ___ARCH_SI_TRAPNO(_a1) , _a1
309 #else
310 # define ___ARCH_SI_TRAPNO(_a1)
311 #endif
312 #ifdef __ia64__
313 # define ___ARCH_SI_IA64(_a1, _a2, _a3) , _a1, _a2, _a3
314 #else
315 # define ___ARCH_SI_IA64(_a1, _a2, _a3)
316 #endif
317
318 int force_sig_fault_to_task(int sig, int code, void __user *addr
319 ___ARCH_SI_TRAPNO(int trapno)
320 ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr)
321 , struct task_struct *t);
322 int force_sig_fault(int sig, int code, void __user *addr
323 ___ARCH_SI_TRAPNO(int trapno)
324 ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr));
325 int send_sig_fault(int sig, int code, void __user *addr
326 ___ARCH_SI_TRAPNO(int trapno)
327 ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr)
328 , struct task_struct *t);
329
330 int force_sig_mceerr(int code, void __user *, short);
331 int send_sig_mceerr(int code, void __user *, short, struct task_struct *);
332
333 int force_sig_bnderr(void __user *addr, void __user *lower, void __user *upper);
334 int force_sig_pkuerr(void __user *addr, u32 pkey);
335
336 int force_sig_ptrace_errno_trap(int errno, void __user *addr);
337
338 extern int send_sig_info(int, struct kernel_siginfo *, struct task_struct *);
339 extern void force_sigsegv(int sig);
340 extern int force_sig_info(struct kernel_siginfo *);
341 extern int __kill_pgrp_info(int sig, struct kernel_siginfo *info, struct pid *pgrp);
342 extern int kill_pid_info(int sig, struct kernel_siginfo *info, struct pid *pid);
343 extern int kill_pid_usb_asyncio(int sig, int errno, sigval_t addr, struct pid *,
344 const struct cred *);
345 extern int kill_pgrp(struct pid *pid, int sig, int priv);
346 extern int kill_pid(struct pid *pid, int sig, int priv);
347 extern __must_check bool do_notify_parent(struct task_struct *, int);
348 extern void __wake_up_parent(struct task_struct *p, struct task_struct *parent);
349 extern void force_sig(int);
350 extern int send_sig(int, struct task_struct *, int);
351 extern int zap_other_threads(struct task_struct *p);
352 extern struct sigqueue *sigqueue_alloc(void);
353 extern void sigqueue_free(struct sigqueue *);
354 extern int send_sigqueue(struct sigqueue *, struct pid *, enum pid_type);
355 extern int do_sigaction(int, struct k_sigaction *, struct k_sigaction *);
356
restart_syscall(void)357 static inline int restart_syscall(void)
358 {
359 set_tsk_thread_flag(current, TIF_SIGPENDING);
360 return -ERESTARTNOINTR;
361 }
362
task_sigpending(struct task_struct * p)363 static inline int task_sigpending(struct task_struct *p)
364 {
365 return unlikely(test_tsk_thread_flag(p,TIF_SIGPENDING));
366 }
367
signal_pending(struct task_struct * p)368 static inline int signal_pending(struct task_struct *p)
369 {
370 /*
371 * TIF_NOTIFY_SIGNAL isn't really a signal, but it requires the same
372 * behavior in terms of ensuring that we break out of wait loops
373 * so that notify signal callbacks can be processed.
374 */
375 if (unlikely(test_tsk_thread_flag(p, TIF_NOTIFY_SIGNAL)))
376 return 1;
377 return task_sigpending(p);
378 }
379
__fatal_signal_pending(struct task_struct * p)380 static inline int __fatal_signal_pending(struct task_struct *p)
381 {
382 return unlikely(sigismember(&p->pending.signal, SIGKILL));
383 }
384
fatal_signal_pending(struct task_struct * p)385 static inline int fatal_signal_pending(struct task_struct *p)
386 {
387 return task_sigpending(p) && __fatal_signal_pending(p);
388 }
389
signal_pending_state(long state,struct task_struct * p)390 static inline int signal_pending_state(long state, struct task_struct *p)
391 {
392 if (!(state & (TASK_INTERRUPTIBLE | TASK_WAKEKILL)))
393 return 0;
394 if (!signal_pending(p))
395 return 0;
396
397 return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
398 }
399
400 /*
401 * This should only be used in fault handlers to decide whether we
402 * should stop the current fault routine to handle the signals
403 * instead, especially with the case where we've got interrupted with
404 * a VM_FAULT_RETRY.
405 */
fault_signal_pending(vm_fault_t fault_flags,struct pt_regs * regs)406 static inline bool fault_signal_pending(vm_fault_t fault_flags,
407 struct pt_regs *regs)
408 {
409 return unlikely((fault_flags & VM_FAULT_RETRY) &&
410 (fatal_signal_pending(current) ||
411 (user_mode(regs) && signal_pending(current))));
412 }
413
414 /*
415 * Reevaluate whether the task has signals pending delivery.
416 * Wake the task if so.
417 * This is required every time the blocked sigset_t changes.
418 * callers must hold sighand->siglock.
419 */
420 extern void recalc_sigpending_and_wake(struct task_struct *t);
421 extern void recalc_sigpending(void);
422 extern void calculate_sigpending(void);
423
424 extern void signal_wake_up_state(struct task_struct *t, unsigned int state);
425
signal_wake_up(struct task_struct * t,bool resume)426 static inline void signal_wake_up(struct task_struct *t, bool resume)
427 {
428 signal_wake_up_state(t, resume ? TASK_WAKEKILL : 0);
429 }
ptrace_signal_wake_up(struct task_struct * t,bool resume)430 static inline void ptrace_signal_wake_up(struct task_struct *t, bool resume)
431 {
432 signal_wake_up_state(t, resume ? __TASK_TRACED : 0);
433 }
434
435 void task_join_group_stop(struct task_struct *task);
436
437 #ifdef TIF_RESTORE_SIGMASK
438 /*
439 * Legacy restore_sigmask accessors. These are inefficient on
440 * SMP architectures because they require atomic operations.
441 */
442
443 /**
444 * set_restore_sigmask() - make sure saved_sigmask processing gets done
445 *
446 * This sets TIF_RESTORE_SIGMASK and ensures that the arch signal code
447 * will run before returning to user mode, to process the flag. For
448 * all callers, TIF_SIGPENDING is already set or it's no harm to set
449 * it. TIF_RESTORE_SIGMASK need not be in the set of bits that the
450 * arch code will notice on return to user mode, in case those bits
451 * are scarce. We set TIF_SIGPENDING here to ensure that the arch
452 * signal code always gets run when TIF_RESTORE_SIGMASK is set.
453 */
set_restore_sigmask(void)454 static inline void set_restore_sigmask(void)
455 {
456 set_thread_flag(TIF_RESTORE_SIGMASK);
457 }
458
clear_tsk_restore_sigmask(struct task_struct * task)459 static inline void clear_tsk_restore_sigmask(struct task_struct *task)
460 {
461 clear_tsk_thread_flag(task, TIF_RESTORE_SIGMASK);
462 }
463
clear_restore_sigmask(void)464 static inline void clear_restore_sigmask(void)
465 {
466 clear_thread_flag(TIF_RESTORE_SIGMASK);
467 }
test_tsk_restore_sigmask(struct task_struct * task)468 static inline bool test_tsk_restore_sigmask(struct task_struct *task)
469 {
470 return test_tsk_thread_flag(task, TIF_RESTORE_SIGMASK);
471 }
test_restore_sigmask(void)472 static inline bool test_restore_sigmask(void)
473 {
474 return test_thread_flag(TIF_RESTORE_SIGMASK);
475 }
test_and_clear_restore_sigmask(void)476 static inline bool test_and_clear_restore_sigmask(void)
477 {
478 return test_and_clear_thread_flag(TIF_RESTORE_SIGMASK);
479 }
480
481 #else /* TIF_RESTORE_SIGMASK */
482
483 /* Higher-quality implementation, used if TIF_RESTORE_SIGMASK doesn't exist. */
set_restore_sigmask(void)484 static inline void set_restore_sigmask(void)
485 {
486 current->restore_sigmask = true;
487 }
clear_tsk_restore_sigmask(struct task_struct * task)488 static inline void clear_tsk_restore_sigmask(struct task_struct *task)
489 {
490 task->restore_sigmask = false;
491 }
clear_restore_sigmask(void)492 static inline void clear_restore_sigmask(void)
493 {
494 current->restore_sigmask = false;
495 }
test_restore_sigmask(void)496 static inline bool test_restore_sigmask(void)
497 {
498 return current->restore_sigmask;
499 }
test_tsk_restore_sigmask(struct task_struct * task)500 static inline bool test_tsk_restore_sigmask(struct task_struct *task)
501 {
502 return task->restore_sigmask;
503 }
test_and_clear_restore_sigmask(void)504 static inline bool test_and_clear_restore_sigmask(void)
505 {
506 if (!current->restore_sigmask)
507 return false;
508 current->restore_sigmask = false;
509 return true;
510 }
511 #endif
512
restore_saved_sigmask(void)513 static inline void restore_saved_sigmask(void)
514 {
515 if (test_and_clear_restore_sigmask())
516 __set_current_blocked(¤t->saved_sigmask);
517 }
518
519 extern int set_user_sigmask(const sigset_t __user *umask, size_t sigsetsize);
520
restore_saved_sigmask_unless(bool interrupted)521 static inline void restore_saved_sigmask_unless(bool interrupted)
522 {
523 if (interrupted)
524 WARN_ON(!signal_pending(current));
525 else
526 restore_saved_sigmask();
527 }
528
sigmask_to_save(void)529 static inline sigset_t *sigmask_to_save(void)
530 {
531 sigset_t *res = ¤t->blocked;
532 if (unlikely(test_restore_sigmask()))
533 res = ¤t->saved_sigmask;
534 return res;
535 }
536
kill_cad_pid(int sig,int priv)537 static inline int kill_cad_pid(int sig, int priv)
538 {
539 return kill_pid(cad_pid, sig, priv);
540 }
541
542 /* These can be the second arg to send_sig_info/send_group_sig_info. */
543 #define SEND_SIG_NOINFO ((struct kernel_siginfo *) 0)
544 #define SEND_SIG_PRIV ((struct kernel_siginfo *) 1)
545
__on_sig_stack(unsigned long sp)546 static inline int __on_sig_stack(unsigned long sp)
547 {
548 #ifdef CONFIG_STACK_GROWSUP
549 return sp >= current->sas_ss_sp &&
550 sp - current->sas_ss_sp < current->sas_ss_size;
551 #else
552 return sp > current->sas_ss_sp &&
553 sp - current->sas_ss_sp <= current->sas_ss_size;
554 #endif
555 }
556
557 /*
558 * True if we are on the alternate signal stack.
559 */
on_sig_stack(unsigned long sp)560 static inline int on_sig_stack(unsigned long sp)
561 {
562 /*
563 * If the signal stack is SS_AUTODISARM then, by construction, we
564 * can't be on the signal stack unless user code deliberately set
565 * SS_AUTODISARM when we were already on it.
566 *
567 * This improves reliability: if user state gets corrupted such that
568 * the stack pointer points very close to the end of the signal stack,
569 * then this check will enable the signal to be handled anyway.
570 */
571 if (current->sas_ss_flags & SS_AUTODISARM)
572 return 0;
573
574 return __on_sig_stack(sp);
575 }
576
sas_ss_flags(unsigned long sp)577 static inline int sas_ss_flags(unsigned long sp)
578 {
579 if (!current->sas_ss_size)
580 return SS_DISABLE;
581
582 return on_sig_stack(sp) ? SS_ONSTACK : 0;
583 }
584
sas_ss_reset(struct task_struct * p)585 static inline void sas_ss_reset(struct task_struct *p)
586 {
587 p->sas_ss_sp = 0;
588 p->sas_ss_size = 0;
589 p->sas_ss_flags = SS_DISABLE;
590 }
591
sigsp(unsigned long sp,struct ksignal * ksig)592 static inline unsigned long sigsp(unsigned long sp, struct ksignal *ksig)
593 {
594 if (unlikely((ksig->ka.sa.sa_flags & SA_ONSTACK)) && ! sas_ss_flags(sp))
595 #ifdef CONFIG_STACK_GROWSUP
596 return current->sas_ss_sp;
597 #else
598 return current->sas_ss_sp + current->sas_ss_size;
599 #endif
600 return sp;
601 }
602
603 extern void __cleanup_sighand(struct sighand_struct *);
604 extern void flush_itimer_signals(void);
605
606 #define tasklist_empty() \
607 list_empty(&init_task.tasks)
608
609 #define next_task(p) \
610 list_entry_rcu((p)->tasks.next, struct task_struct, tasks)
611
612 #define for_each_process(p) \
613 for (p = &init_task ; (p = next_task(p)) != &init_task ; )
614
615 extern bool current_is_single_threaded(void);
616
617 /*
618 * Careful: do_each_thread/while_each_thread is a double loop so
619 * 'break' will not work as expected - use goto instead.
620 */
621 #define do_each_thread(g, t) \
622 for (g = t = &init_task ; (g = t = next_task(g)) != &init_task ; ) do
623
624 #define while_each_thread(g, t) \
625 while ((t = next_thread(t)) != g)
626
627 #define __for_each_thread(signal, t) \
628 list_for_each_entry_rcu(t, &(signal)->thread_head, thread_node)
629
630 #define for_each_thread(p, t) \
631 __for_each_thread((p)->signal, t)
632
633 /* Careful: this is a double loop, 'break' won't work as expected. */
634 #define for_each_process_thread(p, t) \
635 for_each_process(p) for_each_thread(p, t)
636
637 typedef int (*proc_visitor)(struct task_struct *p, void *data);
638 void walk_process_tree(struct task_struct *top, proc_visitor, void *);
639
640 static inline
task_pid_type(struct task_struct * task,enum pid_type type)641 struct pid *task_pid_type(struct task_struct *task, enum pid_type type)
642 {
643 struct pid *pid;
644 if (type == PIDTYPE_PID)
645 pid = task_pid(task);
646 else
647 pid = task->signal->pids[type];
648 return pid;
649 }
650
task_tgid(struct task_struct * task)651 static inline struct pid *task_tgid(struct task_struct *task)
652 {
653 return task->signal->pids[PIDTYPE_TGID];
654 }
655
656 /*
657 * Without tasklist or RCU lock it is not safe to dereference
658 * the result of task_pgrp/task_session even if task == current,
659 * we can race with another thread doing sys_setsid/sys_setpgid.
660 */
task_pgrp(struct task_struct * task)661 static inline struct pid *task_pgrp(struct task_struct *task)
662 {
663 return task->signal->pids[PIDTYPE_PGID];
664 }
665
task_session(struct task_struct * task)666 static inline struct pid *task_session(struct task_struct *task)
667 {
668 return task->signal->pids[PIDTYPE_SID];
669 }
670
get_nr_threads(struct task_struct * task)671 static inline int get_nr_threads(struct task_struct *task)
672 {
673 return task->signal->nr_threads;
674 }
675
thread_group_leader(struct task_struct * p)676 static inline bool thread_group_leader(struct task_struct *p)
677 {
678 return p->exit_signal >= 0;
679 }
680
681 static inline
same_thread_group(struct task_struct * p1,struct task_struct * p2)682 bool same_thread_group(struct task_struct *p1, struct task_struct *p2)
683 {
684 return p1->signal == p2->signal;
685 }
686
next_thread(const struct task_struct * p)687 static inline struct task_struct *next_thread(const struct task_struct *p)
688 {
689 return list_entry_rcu(p->thread_group.next,
690 struct task_struct, thread_group);
691 }
692
thread_group_empty(struct task_struct * p)693 static inline int thread_group_empty(struct task_struct *p)
694 {
695 return list_empty(&p->thread_group);
696 }
697
698 #define delay_group_leader(p) \
699 (thread_group_leader(p) && !thread_group_empty(p))
700
701 extern bool thread_group_exited(struct pid *pid);
702
703 extern struct sighand_struct *__lock_task_sighand(struct task_struct *task,
704 unsigned long *flags);
705
lock_task_sighand(struct task_struct * task,unsigned long * flags)706 static inline struct sighand_struct *lock_task_sighand(struct task_struct *task,
707 unsigned long *flags)
708 {
709 struct sighand_struct *ret;
710
711 ret = __lock_task_sighand(task, flags);
712 (void)__cond_lock(&task->sighand->siglock, ret);
713 return ret;
714 }
715
unlock_task_sighand(struct task_struct * task,unsigned long * flags)716 static inline void unlock_task_sighand(struct task_struct *task,
717 unsigned long *flags)
718 {
719 spin_unlock_irqrestore(&task->sighand->siglock, *flags);
720 }
721
task_rlimit(const struct task_struct * task,unsigned int limit)722 static inline unsigned long task_rlimit(const struct task_struct *task,
723 unsigned int limit)
724 {
725 return READ_ONCE(task->signal->rlim[limit].rlim_cur);
726 }
727
task_rlimit_max(const struct task_struct * task,unsigned int limit)728 static inline unsigned long task_rlimit_max(const struct task_struct *task,
729 unsigned int limit)
730 {
731 return READ_ONCE(task->signal->rlim[limit].rlim_max);
732 }
733
rlimit(unsigned int limit)734 static inline unsigned long rlimit(unsigned int limit)
735 {
736 return task_rlimit(current, limit);
737 }
738
rlimit_max(unsigned int limit)739 static inline unsigned long rlimit_max(unsigned int limit)
740 {
741 return task_rlimit_max(current, limit);
742 }
743
744 #endif /* _LINUX_SCHED_SIGNAL_H */
745