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