• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(&current->sighand->siglock);
301 	if (current->jobctl & JOBCTL_STOP_DEQUEUED)
302 		set_special_state(TASK_STOPPED);
303 	spin_unlock_irq(&current->sighand->siglock);
304 
305 	schedule();
306 }
307 #ifdef __ia64__
308 # define ___ARCH_SI_IA64(_a1, _a2, _a3) , _a1, _a2, _a3
309 #else
310 # define ___ARCH_SI_IA64(_a1, _a2, _a3)
311 #endif
312 
313 int force_sig_fault_to_task(int sig, int code, void __user *addr
314 	___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr)
315 	, struct task_struct *t);
316 int force_sig_fault(int sig, int code, void __user *addr
317 	___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr));
318 int send_sig_fault(int sig, int code, void __user *addr
319 	___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr)
320 	, struct task_struct *t);
321 
322 int force_sig_mceerr(int code, void __user *, short);
323 int send_sig_mceerr(int code, void __user *, short, struct task_struct *);
324 
325 int force_sig_bnderr(void __user *addr, void __user *lower, void __user *upper);
326 int force_sig_pkuerr(void __user *addr, u32 pkey);
327 int force_sig_perf(void __user *addr, u32 type, u64 sig_data);
328 
329 int force_sig_ptrace_errno_trap(int errno, void __user *addr);
330 int force_sig_fault_trapno(int sig, int code, void __user *addr, int trapno);
331 int send_sig_fault_trapno(int sig, int code, void __user *addr, int trapno,
332 			struct task_struct *t);
333 int force_sig_seccomp(int syscall, int reason, bool force_coredump);
334 
335 extern int send_sig_info(int, struct kernel_siginfo *, struct task_struct *);
336 extern void force_sigsegv(int sig);
337 extern int force_sig_info(struct kernel_siginfo *);
338 extern int __kill_pgrp_info(int sig, struct kernel_siginfo *info, struct pid *pgrp);
339 extern int kill_pid_info(int sig, struct kernel_siginfo *info, struct pid *pid);
340 extern int kill_pid_usb_asyncio(int sig, int errno, sigval_t addr, struct pid *,
341 				const struct cred *);
342 extern int kill_pgrp(struct pid *pid, int sig, int priv);
343 extern int kill_pid(struct pid *pid, int sig, int priv);
344 extern __must_check bool do_notify_parent(struct task_struct *, int);
345 extern void __wake_up_parent(struct task_struct *p, struct task_struct *parent);
346 extern void force_sig(int);
347 extern void force_fatal_sig(int);
348 extern void force_exit_sig(int);
349 extern int send_sig(int, struct task_struct *, int);
350 extern int zap_other_threads(struct task_struct *p);
351 extern struct sigqueue *sigqueue_alloc(void);
352 extern void sigqueue_free(struct sigqueue *);
353 extern int send_sigqueue(struct sigqueue *, struct pid *, enum pid_type);
354 extern int do_sigaction(int, struct k_sigaction *, struct k_sigaction *);
355 
restart_syscall(void)356 static inline int restart_syscall(void)
357 {
358 	set_tsk_thread_flag(current, TIF_SIGPENDING);
359 	return -ERESTARTNOINTR;
360 }
361 
task_sigpending(struct task_struct * p)362 static inline int task_sigpending(struct task_struct *p)
363 {
364 	return unlikely(test_tsk_thread_flag(p,TIF_SIGPENDING));
365 }
366 
signal_pending(struct task_struct * p)367 static inline int signal_pending(struct task_struct *p)
368 {
369 	/*
370 	 * TIF_NOTIFY_SIGNAL isn't really a signal, but it requires the same
371 	 * behavior in terms of ensuring that we break out of wait loops
372 	 * so that notify signal callbacks can be processed.
373 	 */
374 	if (unlikely(test_tsk_thread_flag(p, TIF_NOTIFY_SIGNAL)))
375 		return 1;
376 	return task_sigpending(p);
377 }
378 
__fatal_signal_pending(struct task_struct * p)379 static inline int __fatal_signal_pending(struct task_struct *p)
380 {
381 	return unlikely(sigismember(&p->pending.signal, SIGKILL));
382 }
383 
fatal_signal_pending(struct task_struct * p)384 static inline int fatal_signal_pending(struct task_struct *p)
385 {
386 	return task_sigpending(p) && __fatal_signal_pending(p);
387 }
388 
signal_pending_state(unsigned int state,struct task_struct * p)389 static inline int signal_pending_state(unsigned int state, struct task_struct *p)
390 {
391 	if (!(state & (TASK_INTERRUPTIBLE | TASK_WAKEKILL)))
392 		return 0;
393 	if (!signal_pending(p))
394 		return 0;
395 
396 	return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
397 }
398 
399 /*
400  * This should only be used in fault handlers to decide whether we
401  * should stop the current fault routine to handle the signals
402  * instead, especially with the case where we've got interrupted with
403  * a VM_FAULT_RETRY.
404  */
fault_signal_pending(vm_fault_t fault_flags,struct pt_regs * regs)405 static inline bool fault_signal_pending(vm_fault_t fault_flags,
406 					struct pt_regs *regs)
407 {
408 	return unlikely((fault_flags & VM_FAULT_RETRY) &&
409 			(fatal_signal_pending(current) ||
410 			 (user_mode(regs) && signal_pending(current))));
411 }
412 
413 /*
414  * Reevaluate whether the task has signals pending delivery.
415  * Wake the task if so.
416  * This is required every time the blocked sigset_t changes.
417  * callers must hold sighand->siglock.
418  */
419 extern void recalc_sigpending_and_wake(struct task_struct *t);
420 extern void recalc_sigpending(void);
421 extern void calculate_sigpending(void);
422 
423 extern void signal_wake_up_state(struct task_struct *t, unsigned int state);
424 
signal_wake_up(struct task_struct * t,bool resume)425 static inline void signal_wake_up(struct task_struct *t, bool resume)
426 {
427 	signal_wake_up_state(t, resume ? TASK_WAKEKILL : 0);
428 }
ptrace_signal_wake_up(struct task_struct * t,bool resume)429 static inline void ptrace_signal_wake_up(struct task_struct *t, bool resume)
430 {
431 	signal_wake_up_state(t, resume ? __TASK_TRACED : 0);
432 }
433 
434 void task_join_group_stop(struct task_struct *task);
435 
436 #ifdef TIF_RESTORE_SIGMASK
437 /*
438  * Legacy restore_sigmask accessors.  These are inefficient on
439  * SMP architectures because they require atomic operations.
440  */
441 
442 /**
443  * set_restore_sigmask() - make sure saved_sigmask processing gets done
444  *
445  * This sets TIF_RESTORE_SIGMASK and ensures that the arch signal code
446  * will run before returning to user mode, to process the flag.  For
447  * all callers, TIF_SIGPENDING is already set or it's no harm to set
448  * it.  TIF_RESTORE_SIGMASK need not be in the set of bits that the
449  * arch code will notice on return to user mode, in case those bits
450  * are scarce.  We set TIF_SIGPENDING here to ensure that the arch
451  * signal code always gets run when TIF_RESTORE_SIGMASK is set.
452  */
set_restore_sigmask(void)453 static inline void set_restore_sigmask(void)
454 {
455 	set_thread_flag(TIF_RESTORE_SIGMASK);
456 }
457 
clear_tsk_restore_sigmask(struct task_struct * task)458 static inline void clear_tsk_restore_sigmask(struct task_struct *task)
459 {
460 	clear_tsk_thread_flag(task, TIF_RESTORE_SIGMASK);
461 }
462 
clear_restore_sigmask(void)463 static inline void clear_restore_sigmask(void)
464 {
465 	clear_thread_flag(TIF_RESTORE_SIGMASK);
466 }
test_tsk_restore_sigmask(struct task_struct * task)467 static inline bool test_tsk_restore_sigmask(struct task_struct *task)
468 {
469 	return test_tsk_thread_flag(task, TIF_RESTORE_SIGMASK);
470 }
test_restore_sigmask(void)471 static inline bool test_restore_sigmask(void)
472 {
473 	return test_thread_flag(TIF_RESTORE_SIGMASK);
474 }
test_and_clear_restore_sigmask(void)475 static inline bool test_and_clear_restore_sigmask(void)
476 {
477 	return test_and_clear_thread_flag(TIF_RESTORE_SIGMASK);
478 }
479 
480 #else	/* TIF_RESTORE_SIGMASK */
481 
482 /* Higher-quality implementation, used if TIF_RESTORE_SIGMASK doesn't exist. */
set_restore_sigmask(void)483 static inline void set_restore_sigmask(void)
484 {
485 	current->restore_sigmask = true;
486 }
clear_tsk_restore_sigmask(struct task_struct * task)487 static inline void clear_tsk_restore_sigmask(struct task_struct *task)
488 {
489 	task->restore_sigmask = false;
490 }
clear_restore_sigmask(void)491 static inline void clear_restore_sigmask(void)
492 {
493 	current->restore_sigmask = false;
494 }
test_restore_sigmask(void)495 static inline bool test_restore_sigmask(void)
496 {
497 	return current->restore_sigmask;
498 }
test_tsk_restore_sigmask(struct task_struct * task)499 static inline bool test_tsk_restore_sigmask(struct task_struct *task)
500 {
501 	return task->restore_sigmask;
502 }
test_and_clear_restore_sigmask(void)503 static inline bool test_and_clear_restore_sigmask(void)
504 {
505 	if (!current->restore_sigmask)
506 		return false;
507 	current->restore_sigmask = false;
508 	return true;
509 }
510 #endif
511 
restore_saved_sigmask(void)512 static inline void restore_saved_sigmask(void)
513 {
514 	if (test_and_clear_restore_sigmask())
515 		__set_current_blocked(&current->saved_sigmask);
516 }
517 
518 extern int set_user_sigmask(const sigset_t __user *umask, size_t sigsetsize);
519 
restore_saved_sigmask_unless(bool interrupted)520 static inline void restore_saved_sigmask_unless(bool interrupted)
521 {
522 	if (interrupted)
523 		WARN_ON(!signal_pending(current));
524 	else
525 		restore_saved_sigmask();
526 }
527 
sigmask_to_save(void)528 static inline sigset_t *sigmask_to_save(void)
529 {
530 	sigset_t *res = &current->blocked;
531 	if (unlikely(test_restore_sigmask()))
532 		res = &current->saved_sigmask;
533 	return res;
534 }
535 
kill_cad_pid(int sig,int priv)536 static inline int kill_cad_pid(int sig, int priv)
537 {
538 	return kill_pid(cad_pid, sig, priv);
539 }
540 
541 /* These can be the second arg to send_sig_info/send_group_sig_info.  */
542 #define SEND_SIG_NOINFO ((struct kernel_siginfo *) 0)
543 #define SEND_SIG_PRIV	((struct kernel_siginfo *) 1)
544 
__on_sig_stack(unsigned long sp)545 static inline int __on_sig_stack(unsigned long sp)
546 {
547 #ifdef CONFIG_STACK_GROWSUP
548 	return sp >= current->sas_ss_sp &&
549 		sp - current->sas_ss_sp < current->sas_ss_size;
550 #else
551 	return sp > current->sas_ss_sp &&
552 		sp - current->sas_ss_sp <= current->sas_ss_size;
553 #endif
554 }
555 
556 /*
557  * True if we are on the alternate signal stack.
558  */
on_sig_stack(unsigned long sp)559 static inline int on_sig_stack(unsigned long sp)
560 {
561 	/*
562 	 * If the signal stack is SS_AUTODISARM then, by construction, we
563 	 * can't be on the signal stack unless user code deliberately set
564 	 * SS_AUTODISARM when we were already on it.
565 	 *
566 	 * This improves reliability: if user state gets corrupted such that
567 	 * the stack pointer points very close to the end of the signal stack,
568 	 * then this check will enable the signal to be handled anyway.
569 	 */
570 	if (current->sas_ss_flags & SS_AUTODISARM)
571 		return 0;
572 
573 	return __on_sig_stack(sp);
574 }
575 
sas_ss_flags(unsigned long sp)576 static inline int sas_ss_flags(unsigned long sp)
577 {
578 	if (!current->sas_ss_size)
579 		return SS_DISABLE;
580 
581 	return on_sig_stack(sp) ? SS_ONSTACK : 0;
582 }
583 
sas_ss_reset(struct task_struct * p)584 static inline void sas_ss_reset(struct task_struct *p)
585 {
586 	p->sas_ss_sp = 0;
587 	p->sas_ss_size = 0;
588 	p->sas_ss_flags = SS_DISABLE;
589 }
590 
sigsp(unsigned long sp,struct ksignal * ksig)591 static inline unsigned long sigsp(unsigned long sp, struct ksignal *ksig)
592 {
593 	if (unlikely((ksig->ka.sa.sa_flags & SA_ONSTACK)) && ! sas_ss_flags(sp))
594 #ifdef CONFIG_STACK_GROWSUP
595 		return current->sas_ss_sp;
596 #else
597 		return current->sas_ss_sp + current->sas_ss_size;
598 #endif
599 	return sp;
600 }
601 
602 extern void __cleanup_sighand(struct sighand_struct *);
603 extern void flush_itimer_signals(void);
604 
605 #define tasklist_empty() \
606 	list_empty(&init_task.tasks)
607 
608 #define next_task(p) \
609 	list_entry_rcu((p)->tasks.next, struct task_struct, tasks)
610 
611 #define for_each_process(p) \
612 	for (p = &init_task ; (p = next_task(p)) != &init_task ; )
613 
614 extern bool current_is_single_threaded(void);
615 
616 /*
617  * Careful: do_each_thread/while_each_thread is a double loop so
618  *          'break' will not work as expected - use goto instead.
619  */
620 #define do_each_thread(g, t) \
621 	for (g = t = &init_task ; (g = t = next_task(g)) != &init_task ; ) do
622 
623 #define while_each_thread(g, t) \
624 	while ((t = next_thread(t)) != g)
625 
626 #define __for_each_thread(signal, t)	\
627 	list_for_each_entry_rcu(t, &(signal)->thread_head, thread_node)
628 
629 #define for_each_thread(p, t)		\
630 	__for_each_thread((p)->signal, t)
631 
632 /* Careful: this is a double loop, 'break' won't work as expected. */
633 #define for_each_process_thread(p, t)	\
634 	for_each_process(p) for_each_thread(p, t)
635 
636 typedef int (*proc_visitor)(struct task_struct *p, void *data);
637 void walk_process_tree(struct task_struct *top, proc_visitor, void *);
638 
639 static inline
task_pid_type(struct task_struct * task,enum pid_type type)640 struct pid *task_pid_type(struct task_struct *task, enum pid_type type)
641 {
642 	struct pid *pid;
643 	if (type == PIDTYPE_PID)
644 		pid = task_pid(task);
645 	else
646 		pid = task->signal->pids[type];
647 	return pid;
648 }
649 
task_tgid(struct task_struct * task)650 static inline struct pid *task_tgid(struct task_struct *task)
651 {
652 	return task->signal->pids[PIDTYPE_TGID];
653 }
654 
655 /*
656  * Without tasklist or RCU lock it is not safe to dereference
657  * the result of task_pgrp/task_session even if task == current,
658  * we can race with another thread doing sys_setsid/sys_setpgid.
659  */
task_pgrp(struct task_struct * task)660 static inline struct pid *task_pgrp(struct task_struct *task)
661 {
662 	return task->signal->pids[PIDTYPE_PGID];
663 }
664 
task_session(struct task_struct * task)665 static inline struct pid *task_session(struct task_struct *task)
666 {
667 	return task->signal->pids[PIDTYPE_SID];
668 }
669 
get_nr_threads(struct task_struct * task)670 static inline int get_nr_threads(struct task_struct *task)
671 {
672 	return task->signal->nr_threads;
673 }
674 
thread_group_leader(struct task_struct * p)675 static inline bool thread_group_leader(struct task_struct *p)
676 {
677 	return p->exit_signal >= 0;
678 }
679 
680 static inline
same_thread_group(struct task_struct * p1,struct task_struct * p2)681 bool same_thread_group(struct task_struct *p1, struct task_struct *p2)
682 {
683 	return p1->signal == p2->signal;
684 }
685 
next_thread(const struct task_struct * p)686 static inline struct task_struct *next_thread(const struct task_struct *p)
687 {
688 	return list_entry_rcu(p->thread_group.next,
689 			      struct task_struct, thread_group);
690 }
691 
thread_group_empty(struct task_struct * p)692 static inline int thread_group_empty(struct task_struct *p)
693 {
694 	return list_empty(&p->thread_group);
695 }
696 
697 #define delay_group_leader(p) \
698 		(thread_group_leader(p) && !thread_group_empty(p))
699 
700 extern bool thread_group_exited(struct pid *pid);
701 
702 extern struct sighand_struct *__lock_task_sighand(struct task_struct *task,
703 							unsigned long *flags);
704 
lock_task_sighand(struct task_struct * task,unsigned long * flags)705 static inline struct sighand_struct *lock_task_sighand(struct task_struct *task,
706 						       unsigned long *flags)
707 {
708 	struct sighand_struct *ret;
709 
710 	ret = __lock_task_sighand(task, flags);
711 	(void)__cond_lock(&task->sighand->siglock, ret);
712 	return ret;
713 }
714 
unlock_task_sighand(struct task_struct * task,unsigned long * flags)715 static inline void unlock_task_sighand(struct task_struct *task,
716 						unsigned long *flags)
717 {
718 	spin_unlock_irqrestore(&task->sighand->siglock, *flags);
719 }
720 
721 #ifdef CONFIG_LOCKDEP
722 extern void lockdep_assert_task_sighand_held(struct task_struct *task);
723 #else
lockdep_assert_task_sighand_held(struct task_struct * task)724 static inline void lockdep_assert_task_sighand_held(struct task_struct *task) { }
725 #endif
726 
task_rlimit(const struct task_struct * task,unsigned int limit)727 static inline unsigned long task_rlimit(const struct task_struct *task,
728 		unsigned int limit)
729 {
730 	return READ_ONCE(task->signal->rlim[limit].rlim_cur);
731 }
732 
task_rlimit_max(const struct task_struct * task,unsigned int limit)733 static inline unsigned long task_rlimit_max(const struct task_struct *task,
734 		unsigned int limit)
735 {
736 	return READ_ONCE(task->signal->rlim[limit].rlim_max);
737 }
738 
rlimit(unsigned int limit)739 static inline unsigned long rlimit(unsigned int limit)
740 {
741 	return task_rlimit(current, limit);
742 }
743 
rlimit_max(unsigned int limit)744 static inline unsigned long rlimit_max(unsigned int limit)
745 {
746 	return task_rlimit_max(current, limit);
747 }
748 
749 #endif /* _LINUX_SCHED_SIGNAL_H */
750